DEADSOFTWARE

Code improvements
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameInput.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Input;
4 import com.badlogic.gdx.utils.TimeUtils;
5 import ru.deadsoftware.cavecraft.CaveGame;
6 import ru.deadsoftware.cavecraft.GameScreen;
7 import ru.deadsoftware.cavecraft.game.mobs.Pig;
8 import ru.deadsoftware.cavecraft.misc.AppState;
9 import ru.deadsoftware.cavecraft.misc.Assets;
11 public class GameInput {
13 private GameProc gp;
15 public GameInput(GameProc gp) {
16 this.gp = gp;
17 }
19 private boolean checkSwim() {
20 return GameItems.isFluid(gp.world.getForeMap(gp.player.getMapX(), gp.player.getMapY()));
21 }
23 private void wasdPressed(int keycode) {
24 if (gp.ctrlMode == 0 || !CaveGame.TOUCH) {
25 switch (keycode) {
26 case Input.Keys.A:
27 gp.player.mov.x = -GamePhysics.PL_SPEED;
28 gp.player.setDir(0);
29 if (CaveGame.TOUCH && checkSwim()) gp.swim = true;
30 break;
31 case Input.Keys.D:
32 gp.player.mov.x = GamePhysics.PL_SPEED;
33 gp.player.setDir(1);
34 if (CaveGame.TOUCH && checkSwim()) gp.swim = true;
35 break;
36 }
37 } else {
38 switch (keycode) {
39 case Input.Keys.A:
40 gp.curX--;
41 break;
42 case Input.Keys.D:
43 gp.curX++;
44 break;
45 case Input.Keys.W:
46 gp.curY--;
47 break;
48 case Input.Keys.S:
49 gp.curY++;
50 break;
51 }
52 gp.blockDmg = 0;
53 }
54 }
56 public void keyDown(int keycode) {
57 gp.isKeyDown = true;
58 gp.keyDownCode = keycode;
59 if (keycode == Input.Keys.W || keycode == Input.Keys.A ||
60 keycode == Input.Keys.S || keycode == Input.Keys.D) {
61 wasdPressed(keycode);
62 } else switch (keycode) {
63 case Input.Keys.ALT_LEFT:
64 if (CaveGame.TOUCH) {
65 gp.ctrlMode++;
66 if (gp.ctrlMode > 1) gp.ctrlMode = 0;
67 }
68 break;
70 case Input.Keys.SPACE:
71 if (checkSwim()) {
72 gp.swim = true;
73 } else if (gp.player.canJump) {
74 gp.player.mov.add(0, -7);
75 } else if (!gp.player.flyMode && gp.player.gameMode == 1) {
76 gp.player.flyMode = true;
77 gp.player.mov.y = 0;
78 } else if (gp.player.flyMode) {
79 gp.player.mov.y = -GamePhysics.PL_SPEED;
80 }
81 break;
83 case Input.Keys.CONTROL_LEFT:
84 gp.player.mov.y = GamePhysics.PL_SPEED;
85 break;
87 case Input.Keys.E:
88 if (CaveGame.STATE == AppState.GAME_PLAY) switch (gp.player.gameMode) {
89 case 0:
90 //TODO survival inv
91 break;
92 case 1:
93 CaveGame.STATE = AppState.GAME_CREATIVE_INV;
94 break;
95 }
96 else CaveGame.STATE = AppState.GAME_PLAY;
97 break;
99 case Input.Keys.G:
100 gp.mobs.add(new Pig(gp.curX * 16, gp.curY * 16));
101 break;
103 case Input.Keys.ESCAPE:
104 case Input.Keys.BACK:
105 CaveGame.STATE = AppState.GOTO_MENU;
106 break;
108 case Input.Keys.F1:
109 GameScreen.SHOW_DEBUG = !GameScreen.SHOW_DEBUG;
110 break;
114 public void keyUp(int keycode) {
115 switch (keycode) {
116 case Input.Keys.A:
117 case Input.Keys.D:
118 gp.player.mov.x = 0;
119 if (CaveGame.TOUCH && gp.swim) gp.swim = false;
120 break;
122 case Input.Keys.SPACE:
123 case Input.Keys.CONTROL_LEFT:
124 if (gp.player.flyMode) gp.player.mov.y = 0;
125 if (gp.swim) gp.swim = false;
126 break;
130 public void mouseMoved(int screenX, int screenY) {
133 public void touchDown(int screenX, int screenY, int button) {
134 gp.touchDownTime = TimeUtils.millis();
135 gp.isTouchDown = true;
136 gp.touchDownBtn = button;
137 gp.touchDownX = screenX;
138 gp.touchDownY = screenY;
141 public void touchUp(int screenX, int screenY, int button) {
142 if (CaveGame.TOUCH && gp.isKeyDown) {
143 keyUp(gp.keyDownCode);
144 gp.isKeyDown = false;
146 if (gp.isTouchDown) {
147 if (CaveGame.STATE == AppState.GAME_CREATIVE_INV &&
148 screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 &&
149 screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 &&
150 screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 &&
151 screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2) {
152 int ix = (int) (screenX - (gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18;
153 int iy = (int) (screenY - (gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18;
154 int item = gp.creativeScroll * 8 + (ix + iy * 8);
155 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) item = -1;
156 if (item >= 0 && item < GameItems.getItemsSize()) {
157 for (int i = 8; i > 0; i--) {
158 gp.player.inv[i] = gp.player.inv[i - 1];
160 gp.player.inv[0] = item;
162 } else if (CaveGame.STATE == AppState.GAME_CREATIVE_INV) {
163 CaveGame.STATE = AppState.GAME_PLAY;
164 } else if (screenY < Assets.invBar.getRegionHeight() &&
165 screenX > gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 &&
166 screenX < gp.renderer.getWidth() / 2 + Assets.invBar.getRegionWidth() / 2) {
167 gp.slot = (int) ((screenX - (gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20);
168 } else if (button == Input.Buttons.RIGHT) {
169 gp.useItem(gp.curX, gp.curY,
170 gp.player.inv[gp.slot], false);
171 } else if (button == Input.Buttons.LEFT) {
172 gp.blockDmg = 0;
175 gp.isTouchDown = false;
178 public void touchDragged(int screenX, int screenY) {
179 if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && Math.abs(screenY - gp.touchDownY) > 16) {
180 if (screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 &&
181 screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 &&
182 screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 &&
183 screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2) {
184 gp.creativeScroll -= (screenY - gp.touchDownY) / 16;
185 gp.touchDownX = screenX;
186 gp.touchDownY = screenY;
187 if (gp.creativeScroll < 0) gp.creativeScroll = 0;
188 if (gp.creativeScroll > gp.maxCreativeScroll)
189 gp.creativeScroll = gp.maxCreativeScroll;
194 public void scrolled(int amount) {
195 switch (CaveGame.STATE) {
196 case GAME_PLAY:
197 gp.slot += amount;
198 if (gp.slot < 0) gp.slot = 8;
199 if (gp.slot > 8) gp.slot = 0;
200 break;
201 case GAME_CREATIVE_INV:
202 gp.creativeScroll += amount;
203 if (gp.creativeScroll < 0) gp.creativeScroll = 0;
204 if (gp.creativeScroll > gp.maxCreativeScroll)
205 gp.creativeScroll = gp.maxCreativeScroll;
206 break;