DEADSOFTWARE

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