DEADSOFTWARE

Move some methods
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInput.java
1 package ru.deadsoftware.cavedroid.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.Input;
5 import com.badlogic.gdx.graphics.g2d.TextureRegion;
6 import com.badlogic.gdx.utils.TimeUtils;
7 import com.google.common.collect.Range;
8 import ru.deadsoftware.cavedroid.CaveGame;
9 import ru.deadsoftware.cavedroid.GameScreen;
10 import ru.deadsoftware.cavedroid.game.mobs.Mob;
11 import ru.deadsoftware.cavedroid.game.mobs.Pig;
12 import ru.deadsoftware.cavedroid.misc.Assets;
13 import ru.deadsoftware.cavedroid.misc.ControlMode;
14 import ru.deadsoftware.cavedroid.misc.states.AppState;
15 import ru.deadsoftware.cavedroid.misc.states.GameState;
17 import static ru.deadsoftware.cavedroid.GameScreen.GP;
18 import static ru.deadsoftware.cavedroid.game.GameItems.*;
20 public class GameInput {
22 private boolean keyDown, touchedDown;
24 private int keyDownCode, touchDownBtn;
25 private float touchDownX, touchDownY;
26 private long touchDownTime;
28 private int curX, curY;
29 private int creativeScroll;
30 private int blockDamage;
32 private boolean checkSwim() {
33 return GameItems.isFluid(GP.world.getForeMap(GP.player.getMapX(), GP.player.getLowerMapY()));
34 }
36 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
37 private boolean insideCreativeInv(float screenX, float screenY) {
38 TextureRegion creative = Assets.textureRegions.get("creative");
39 return (screenX > GP.renderer.getWidth() / 2 - creative.getRegionWidth() / 2 &&
40 screenX < GP.renderer.getWidth() / 2 + creative.getRegionWidth() / 2 &&
41 screenY > GP.renderer.getHeight() / 2 - creative.getRegionHeight() / 2 &&
42 screenY < GP.renderer.getHeight() / 2 + creative.getRegionHeight() / 2);
43 }
45 private void wasdPressed(int keycode) {
46 if (GP.controlMode == ControlMode.WALK || !CaveGame.TOUCH) {
47 switch (keycode) {
48 case Input.Keys.A:
49 GP.player.mov.x = -GamePhysics.PL_SPEED;
50 GP.player.setDir(0);
51 if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true;
52 break;
53 case Input.Keys.D:
54 GP.player.mov.x = GamePhysics.PL_SPEED;
55 GP.player.setDir(1);
56 if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true;
57 break;
58 }
59 } else {
60 switch (keycode) {
61 case Input.Keys.A:
62 curX--;
63 break;
64 case Input.Keys.D:
65 curX++;
66 break;
67 case Input.Keys.W:
68 curY--;
69 break;
70 case Input.Keys.S:
71 curY++;
72 break;
73 }
74 blockDamage = 0;
75 }
76 }
78 private boolean isNotAutoselectable(int x, int y) {
79 return (!GP.world.hasForeAt(x, y) || !GP.world.getForeMapBlock(x, y).hasCollision());
80 }
82 private void checkCursorBounds() {
83 if (curY < 0) {
84 curY = 0;
85 } else if (curY >= GP.world.getHeight()) {
86 curY = GP.world.getHeight() - 1;
87 }
89 if (GP.controlMode == ControlMode.CURSOR) {
90 if (curX * 16 + 8 < GP.player.pos.x + GP.player.getWidth() / 2) {
91 GP.player.setDir(Mob.LEFT);
92 } else {
93 GP.player.setDir(Mob.RIGHT);
94 }
95 }
96 }
98 private void moveCursor() {
99 int pastX = curX;
100 int pastY = curY;
102 if (GP.controlMode == ControlMode.WALK && CaveGame.TOUCH) {
103 curX = GP.player.getMapX() + (GP.player.looksLeft() ? -1 : 1);
104 curY = GP.player.getUpperMapY();
105 for (int i = 0; i < 2 && isNotAutoselectable(curX, curY); i++) {
106 curY++;
108 if (isNotAutoselectable(curX, curY)) {
109 curX += GP.player.looksLeft() ? 1 : -1;
111 } else if (!CaveGame.TOUCH) {
112 curX = (int) (Gdx.input.getX() * (GP.renderer.getWidth() / GameScreen.getWidth()) + GP.renderer.getCamX()) / 16;
113 curY = (int) (Gdx.input.getY() * (GP.renderer.getHeight() / GameScreen.getHeight()) + GP.renderer.getCamY()) / 16;
114 if (curX < 0) curX--;
117 if (pastX != curX || pastY != curY) {
118 blockDamage = 0;
121 checkCursorBounds();
124 private void useItem(int x, int y, int id, boolean bg) {
125 String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id);
126 if (id > 0) {
127 if (getItem(id).isBlock()) {
128 if (!bg) {
129 GP.world.placeToForeground(x, y, getBlockIdByItemId(id));
130 } else {
131 GP.world.placeToBackground(x, y, getBlockIdByItemId(id));
133 } else {
134 switch (key) {
135 case "bucket_water":
136 GP.world.placeToForeground(x, y, getBlockId("water"));
137 GP.player.inventory[GP.player.slot] = getItemId("bucket_empty");
138 break;
139 case "bucket_lava":
140 GP.world.placeToForeground(x, y, getBlockId("lava"));
141 GP.player.inventory[GP.player.slot] = getItemId("bucket_empty");
142 break;
148 private void pressLMB() {
149 if ((GP.world.hasForeAt(curX, curY) && GP.world.getForeMapBlock(curX, curY).getHp() >= 0) ||
150 (!GP.world.hasForeAt(curX, curY) && GP.world.hasBackAt(curX, curY) &&
151 GP.world.getBackMapBlock(curX, curY).getHp() >= 0)) {
152 if (GP.player.gameMode == 0) {
153 blockDamage++;
154 if (GP.world.hasForeAt(curX, curY)) {
155 if (blockDamage >= GP.world.getForeMapBlock(curX, curY).getHp()) {
156 GP.world.destroyForeMap(curX, curY);
157 blockDamage = 0;
159 } else if (GP.world.hasBackAt(curX, curY)) {
160 if (blockDamage >= GP.world.getBackMapBlock(curX, curY).getHp()) {
161 GP.world.destroyBackMap(curX, curY);
162 blockDamage = 0;
165 } else {
166 if (GP.world.hasForeAt(curX, curY)) {
167 GP.world.placeToForeground(curX, curY, 0);
168 } else if (GP.world.hasBackAt(curX, curY)) {
169 GP.world.placeToBackground(curX, curY, 0);
171 touchedDown = false;
176 private boolean insideHotbar(float x, float y) {
177 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
178 return y < hotbar.getRegionHeight() &&
179 Range.open(GP.renderer.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2,
180 GP.renderer.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x);
183 private void holdMB() {
184 if (touchDownBtn == Input.Buttons.RIGHT) {
185 useItem(curX, curY, GP.player.inventory[GP.player.slot], true);
186 touchedDown = false;
187 } else {
188 if (insideHotbar(touchDownX, touchDownY)) {
189 CaveGame.GAME_STATE = GameState.CREATIVE_INV;
190 touchedDown = false;
195 public void keyDown(int keycode) {
196 keyDown = true;
197 keyDownCode = keycode;
198 if (keycode == Input.Keys.W || keycode == Input.Keys.A ||
199 keycode == Input.Keys.S || keycode == Input.Keys.D) {
200 wasdPressed(keycode);
201 } else switch (keycode) {
202 case Input.Keys.ALT_LEFT:
203 if (CaveGame.TOUCH) {
204 GP.controlMode = GP.controlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
206 break;
208 case Input.Keys.SPACE:
209 if (checkSwim()) {
210 GP.player.swim = true;
211 } else if (GP.player.canJump) {
212 GP.player.mov.add(0, -7);
213 } else if (!GP.player.flyMode && GP.player.gameMode == 1) {
214 GP.player.flyMode = true;
215 GP.player.mov.y = 0;
216 } else if (GP.player.flyMode) {
217 GP.player.mov.y = -GamePhysics.PL_SPEED;
219 break;
221 case Input.Keys.CONTROL_LEFT:
222 GP.player.mov.y = GamePhysics.PL_SPEED;
223 break;
225 case Input.Keys.E:
226 if (CaveGame.GAME_STATE == GameState.PLAY) {
227 switch (GP.player.gameMode) {
228 case 0:
229 //TODO survival inv
230 break;
231 case 1:
232 CaveGame.GAME_STATE = GameState.CREATIVE_INV;
233 break;
235 } else {
236 CaveGame.GAME_STATE = GameState.PLAY;
238 break;
240 case Input.Keys.G:
241 GP.mobs.add(new Pig(curX * 16, curY * 16));
242 break;
244 case Input.Keys.Q:
245 GP.world.placeToForeground(curX, curY, 8);
246 break;
248 case Input.Keys.ESCAPE:
249 case Input.Keys.BACK:
250 CaveGame.APP_STATE = AppState.SAVE;
251 CaveGame.GAME_STATE = GameState.PAUSE;
252 break;
254 case Input.Keys.F1:
255 GameScreen.SHOW_DEBUG = !GameScreen.SHOW_DEBUG;
256 break;
258 case Input.Keys.M:
259 GameScreen.SHOW_MAP = !GameScreen.SHOW_MAP;
260 break;
264 public void keyUp(int keycode) {
265 switch (keycode) {
266 case Input.Keys.A:
267 case Input.Keys.D:
268 GP.player.mov.x = 0;
269 if (CaveGame.TOUCH && GP.player.swim) GP.player.swim = false;
270 break;
272 case Input.Keys.SPACE:
273 case Input.Keys.CONTROL_LEFT:
274 if (GP.player.flyMode) GP.player.mov.y = 0;
275 if (GP.player.swim) GP.player.swim = false;
276 break;
280 public void touchDown(float touchX, float touchY, int button) {
281 touchDownTime = TimeUtils.millis();
282 touchedDown = true;
283 touchDownBtn = button;
284 touchDownX = touchX;
285 touchDownY = touchY;
288 public void touchUp(float screenX, float screenY, int button) {
289 if (CaveGame.TOUCH && keyDown) {
290 keyUp(keyDownCode);
291 keyDown = false;
293 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
294 TextureRegion creative = Assets.textureRegions.get("creative");
295 if (touchedDown) {
296 if (CaveGame.GAME_STATE == GameState.CREATIVE_INV && insideCreativeInv(screenX, screenY)) {
297 int ix = (int) (screenX - (GP.renderer.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18;
298 int iy = (int) (screenY - (GP.renderer.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18;
299 int item = creativeScroll * 8 + (ix + iy * 8);
300 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) item = -1;
301 if (item >= 0 && item < GameItems.getItemsSize()) {
302 System.arraycopy(GP.player.inventory, 0, GP.player.inventory, 1, 8);
303 GP.player.inventory[0] = item;
305 } else if (CaveGame.GAME_STATE == GameState.CREATIVE_INV) {
306 CaveGame.GAME_STATE = GameState.PLAY;
307 } else if (screenY < hotbar.getRegionHeight() &&
308 screenX > GP.renderer.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 &&
309 screenX < GP.renderer.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) {
310 GP.player.slot = (int) ((screenX - (GP.renderer.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20);
311 } else if (button == Input.Buttons.RIGHT) {
312 useItem(curX, curY,
313 GP.player.inventory[GP.player.slot], false);
314 } else if (button == Input.Buttons.LEFT) {
315 blockDamage = 0;
318 touchedDown = false;
321 public void touchDragged(float screenX, float screenY) {
322 if (CaveGame.GAME_STATE == GameState.CREATIVE_INV && Math.abs(screenY - touchDownY) > 16) {
323 if (insideCreativeInv(screenX, screenY)) {
324 creativeScroll -= (screenY - touchDownY) / 16;
325 touchDownX = screenX;
326 touchDownY = screenY;
327 if (creativeScroll < 0) creativeScroll = 0;
328 if (creativeScroll > GameProc.MAX_CREATIVE_SCROLL)
329 creativeScroll = GameProc.MAX_CREATIVE_SCROLL;
334 public void scrolled(int amount) {
335 switch (CaveGame.GAME_STATE) {
336 case PLAY:
337 GP.player.slot += amount;
338 if (GP.player.slot < 0) GP.player.slot = 8;
339 if (GP.player.slot > 8) GP.player.slot = 0;
340 break;
341 case CREATIVE_INV:
342 creativeScroll += amount;
343 if (creativeScroll < 0) creativeScroll = 0;
344 if (creativeScroll > GameProc.MAX_CREATIVE_SCROLL)
345 creativeScroll = GameProc.MAX_CREATIVE_SCROLL;
346 break;
350 public int getKeyDownCode() {
351 return keyDownCode;
354 public boolean isKeyDown() {
355 return keyDown;
358 int getBlockDamage() {
359 return blockDamage;
362 int getCurX() {
363 return curX;
366 int getCurY() {
367 return curY;
370 int getCreativeScroll() {
371 return creativeScroll;
374 void update() {
375 moveCursor();
376 if (touchedDown && touchDownBtn == Input.Buttons.LEFT) {
377 pressLMB();
379 if (touchedDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
380 holdMB();