DEADSOFTWARE

Add world saving
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameProc.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.Input;
5 import com.badlogic.gdx.utils.Array;
6 import com.badlogic.gdx.utils.TimeUtils;
7 import ru.deadsoftware.cavecraft.*;
8 import ru.deadsoftware.cavecraft.game.mobs.Mob;
9 import ru.deadsoftware.cavecraft.game.mobs.Pig;
10 import ru.deadsoftware.cavecraft.game.objects.Player;
12 public class GameProc {
14 public static double RUN_TIME = 0;
16 public Player player;
18 public Array<Mob> mobs;
20 public GameWorld world;
21 public GameRenderer renderer;
22 public GamePhysics physics;
24 public int cursorX, cursorY;
25 public int invSlot;
26 public int ctrlMode;
28 public boolean isTouchDown, isKeyDown;
29 public int touchDownX, touchDownY, keyDownCode;
30 public int touchDownButton;
31 public long touchDownTime;
33 public GameProc() {
34 world = new GameWorld();
35 if (WorldSaver.exists()) {
36 world.load();
37 } else {
38 world.generate(1024, 256);
39 }
40 if (CaveGame.TOUCH) {
41 renderer = new GameRenderer(this,320,
42 320*((float)GameScreen.getHeight()/GameScreen.getWidth()));
43 } else {
44 renderer = new GameRenderer(this,480,
45 480*((float)GameScreen.getHeight()/GameScreen.getWidth()));
46 }
47 physics = new GamePhysics(this);
48 player = new Player(world.getSpawnPoint(0));
49 mobs = new Array<Mob>();
50 for (int i=0; i<world.getWidth(); i+=64) {
51 mobs.add(new Pig(i*16, (int)world.getSpawnPoint(i).y, world));
52 }
53 if (!CaveGame.TOUCH) ctrlMode = 1;
54 }
56 public void resetRenderer() {
57 if (CaveGame.TOUCH) {
58 renderer = new GameRenderer(this,320,
59 320*((float)GameScreen.getHeight()/GameScreen.getWidth()));
60 } else {
61 renderer = new GameRenderer(this,480,
62 480*((float)GameScreen.getHeight()/GameScreen.getWidth()));
63 }
64 }
66 private boolean isAutoselectable(int x, int y) {
67 return (world.getForeMap(x,y)>0 &&
68 Items.BLOCKS.getValueAt(world.getForeMap(x,y)).collision);
69 }
71 private void moveCursor() {
72 if (ctrlMode == 0 && CaveGame.TOUCH) {
73 cursorX = (int) (player.position.x + player.texWidth / 2) / 16;
74 if (player.dir == 0) cursorX--;
75 else cursorX++;
76 cursorY = (int) (player.position.y + player.texWidth) / 16;
77 if (!isAutoselectable(cursorX, cursorY)) {
78 cursorY++;
79 }
80 if (!isAutoselectable(cursorX, cursorY)) {
81 cursorY++;
82 }
83 if (!isAutoselectable(cursorX, cursorY)) {
84 if (player.dir == 0) cursorX++;
85 else cursorX--;
86 }
87 } else if (!CaveGame.TOUCH){
88 cursorX = (int)(Gdx.input.getX()*
89 (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)/16;
90 cursorY = (int)(Gdx.input.getY()*
91 (renderer.camera.viewportHeight/GameScreen.getHeight())+renderer.camera.position.y)/16;
92 if ((Gdx.input.getX()*
93 (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)<0)
94 cursorX--;
95 }
96 }
98 private void checkCursorBounds() {
99 if (cursorY < 0) cursorY = 0;
100 if (cursorY >= world.getHeight()) cursorY = world.getHeight()-1;
101 if (ctrlMode==1) {
102 if (cursorX*16+8<player.position.x+player.texWidth/2)
103 player.dir=0;
104 if (cursorX*16+8>player.position.x+player.texWidth/2)
105 player.dir=1;
109 public void update(float delta) {
110 RUN_TIME += delta;
112 physics.update(delta);
113 moveCursor();
114 checkCursorBounds();
116 if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
117 if (touchDownButton== Input.Buttons.RIGHT) {
118 world.placeToBackground(cursorX, cursorY,
119 player.inventory[invSlot]);
120 } else if (touchDownButton==Input.Buttons.LEFT &&
121 touchDownY< Assets.invBar.getRegionHeight() &&
122 touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 &&
123 touchDownX<renderer.camera.viewportWidth/2+Assets.invBar.getRegionWidth()/2) {
124 CaveGame.STATE = GameState.GAME_CREATIVE_INV;
126 isTouchDown = false;