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
, dragging
;
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()));
36 private void goUpwards() {
38 GP
.player
.swim
= true;
39 } else if (GP
.player
.canJump()) {
40 GP
.player
.getMove().add(0, -7);
41 } else if (!GP
.player
.isFlyMode() && GP
.player
.gameMode
== 1) {
42 GP
.player
.setFlyMode(true);
43 GP
.player
.getMove().y
= 0;
44 } else if (GP
.player
.isFlyMode()) {
45 GP
.player
.getMove().y
= -GamePhysics
.PL_SPEED
;
49 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
50 private boolean insideCreativeInv(float screenX
, float screenY
) {
51 TextureRegion creative
= Assets
.textureRegions
.get("creative");
52 return (screenX
> GP
.renderer
.getWidth() / 2 - creative
.getRegionWidth() / 2 &&
53 screenX
< GP
.renderer
.getWidth() / 2 + creative
.getRegionWidth() / 2 &&
54 screenY
> GP
.renderer
.getHeight() / 2 - creative
.getRegionHeight() / 2 &&
55 screenY
< GP
.renderer
.getHeight() / 2 + creative
.getRegionHeight() / 2);
58 private void wasdPressed(int keycode
) {
59 if (GP
.controlMode
== ControlMode
.WALK
|| !CaveGame
.TOUCH
) {
62 GP
.player
.getMove().x
= -GamePhysics
.PL_SPEED
;
63 GP
.player
.setDir(Mob
.Direction
.LEFT
);
64 if (CaveGame
.TOUCH
&& checkSwim()) {
65 GP
.player
.swim
= true;
69 GP
.player
.getMove().x
= GamePhysics
.PL_SPEED
;
70 GP
.player
.setDir(Mob
.Direction
.RIGHT
);
71 if (CaveGame
.TOUCH
&& checkSwim()) {
72 GP
.player
.swim
= true;
76 case Input
.Keys
.SPACE
:
80 case Input
.Keys
.CONTROL_LEFT
:
81 GP
.player
.getMove().y
= GamePhysics
.PL_SPEED
;
103 private boolean isNotAutoselectable(int x
, int y
) {
104 return (!GP
.world
.hasForeAt(x
, y
) || !GP
.world
.getForeMapBlock(x
, y
).hasCollision());
107 private void checkCursorBounds() {
110 } else if (curY
>= GP
.world
.getHeight()) {
111 curY
= GP
.world
.getHeight() - 1;
114 if (GP
.controlMode
== ControlMode
.CURSOR
) {
115 if (curX
* 16 + 8 < GP
.player
.getX() + GP
.player
.getWidth() / 2) {
116 GP
.player
.setDir(Mob
.Direction
.LEFT
);
118 GP
.player
.setDir(Mob
.Direction
.RIGHT
);
123 private void moveCursor() {
127 if (GP
.controlMode
== ControlMode
.WALK
&& CaveGame
.TOUCH
) {
128 curX
= GP
.player
.getMapX() + (GP
.player
.looksLeft() ?
-1 : 1);
129 curY
= GP
.player
.getUpperMapY();
130 for (int i
= 0; i
< 2 && isNotAutoselectable(curX
, curY
); i
++) {
133 if (isNotAutoselectable(curX
, curY
)) {
134 curX
+= GP
.player
.looksLeft() ?
1 : -1;
136 } else if (!CaveGame
.TOUCH
) {
137 curX
= (int) (Gdx
.input
.getX() * (GP
.renderer
.getWidth() /
138 GameScreen
.getWidth()) + GP
.renderer
.getCamX()) / 16;
140 curY
= (int) (Gdx
.input
.getY() * (GP
.renderer
.getHeight() /
141 GameScreen
.getHeight()) + GP
.renderer
.getCamY()) / 16;
147 if (pastX
!= curX
|| pastY
!= curY
) {
154 private void useItem(int x
, int y
, int id
, boolean bg
) {
155 String key
= getItem(id
).isBlock() ?
getBlockKey(id
) : getItemKey(id
);
157 if (getItem(id
).isBlock()) {
159 GP
.world
.placeToForeground(x
, y
, getBlockIdByItemId(id
));
161 GP
.world
.placeToBackground(x
, y
, getBlockIdByItemId(id
));
166 GP
.world
.placeToForeground(x
, y
, getBlockId("water"));
167 GP
.player
.inventory
[GP
.player
.slot
] = getItemId("bucket_empty");
170 GP
.world
.placeToForeground(x
, y
, getBlockId("lava"));
171 GP
.player
.inventory
[GP
.player
.slot
] = getItemId("bucket_empty");
178 private void pressLMB() {
179 if (CaveGame
.GAME_STATE
== GameState
.PLAY
&&
180 ((GP
.world
.hasForeAt(curX
, curY
) && GP
.world
.getForeMapBlock(curX
, curY
).getHp() >= 0) ||
181 (!GP
.world
.hasForeAt(curX
, curY
) && GP
.world
.hasBackAt(curX
, curY
) &&
182 GP
.world
.getBackMapBlock(curX
, curY
).getHp() >= 0))) {
183 if (GP
.player
.gameMode
== 0) {
185 if (GP
.world
.hasForeAt(curX
, curY
)) {
186 if (blockDamage
>= GP
.world
.getForeMapBlock(curX
, curY
).getHp()) {
187 GP
.world
.destroyForeMap(curX
, curY
);
190 } else if (GP
.world
.hasBackAt(curX
, curY
)) {
191 if (blockDamage
>= GP
.world
.getBackMapBlock(curX
, curY
).getHp()) {
192 GP
.world
.destroyBackMap(curX
, curY
);
197 if (GP
.world
.hasForeAt(curX
, curY
)) {
198 GP
.world
.placeToForeground(curX
, curY
, 0);
199 } else if (GP
.world
.hasBackAt(curX
, curY
)) {
200 GP
.world
.placeToBackground(curX
, curY
, 0);
207 private boolean insideHotbar(float x
, float y
) {
208 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
209 return y
< hotbar
.getRegionHeight() &&
210 Range
.open(GP
.renderer
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2,
211 GP
.renderer
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2).contains(x
);
214 private void holdMB() {
215 if (touchDownBtn
== Input
.Buttons
.RIGHT
) {
216 useItem(curX
, curY
, GP
.player
.inventory
[GP
.player
.slot
], true);
219 if (insideHotbar(touchDownX
, touchDownY
)) {
220 CaveGame
.GAME_STATE
= GameState
.CREATIVE_INV
;
226 public void keyDown(int keycode
) {
228 keyDownCode
= keycode
;
234 case Input
.Keys
.SPACE
:
235 case Input
.Keys
.CONTROL_LEFT
:
236 wasdPressed(keycode
);
239 case Input
.Keys
.ALT_LEFT
:
240 if (CaveGame
.TOUCH
) {
241 GP
.controlMode
= GP
.controlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
246 if (CaveGame
.GAME_STATE
== GameState
.PLAY
) {
247 switch (GP
.player
.gameMode
) {
252 CaveGame
.GAME_STATE
= GameState
.CREATIVE_INV
;
256 CaveGame
.GAME_STATE
= GameState
.PLAY
;
261 GP
.mobs
.add(new Pig(curX
* 16, curY
* 16));
265 GP
.world
.placeToForeground(curX
, curY
, 8);
268 case Input
.Keys
.ESCAPE
:
269 case Input
.Keys
.BACK
:
270 CaveGame
.APP_STATE
= AppState
.SAVE
;
271 CaveGame
.GAME_STATE
= GameState
.PAUSE
;
275 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
279 GameScreen
.SHOW_MAP
= !GameScreen
.SHOW_MAP
;
284 public void keyUp(int keycode
) {
288 GP
.player
.getMove().x
= 0;
289 if (CaveGame
.TOUCH
&& GP
.player
.swim
) {
290 GP
.player
.swim
= false;
296 case Input
.Keys
.SPACE
:
297 case Input
.Keys
.CONTROL_LEFT
:
298 if (GP
.player
.isFlyMode()) {
299 GP
.player
.getMove().y
= 0;
301 if (GP
.player
.swim
) {
302 GP
.player
.swim
= false;
308 public void touchDown(float touchX
, float touchY
, int button
) {
309 touchDownTime
= TimeUtils
.millis();
311 touchDownBtn
= button
;
316 public void touchUp(float screenX
, float screenY
, int button
) {
322 if (CaveGame
.TOUCH
&& keyDown
) {
326 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
327 TextureRegion creative
= Assets
.textureRegions
.get("creative");
329 if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
&& insideCreativeInv(screenX
, screenY
)) {
330 int ix
= (int) (screenX
- (GP
.renderer
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
331 int iy
= (int) (screenY
- (GP
.renderer
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
332 int item
= creativeScroll
* 8 + (ix
+ iy
* 8);
333 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) {
336 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
337 System
.arraycopy(GP
.player
.inventory
, 0, GP
.player
.inventory
, 1, 8);
338 GP
.player
.inventory
[0] = item
;
340 } else if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
) {
341 CaveGame
.GAME_STATE
= GameState
.PLAY
;
342 } else if (screenY
< hotbar
.getRegionHeight() &&
343 screenX
> GP
.renderer
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
344 screenX
< GP
.renderer
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
345 GP
.player
.slot
= (int) ((screenX
- (GP
.renderer
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
346 } else if (button
== Input
.Buttons
.RIGHT
) {
348 GP
.player
.inventory
[GP
.player
.slot
], false);
349 } else if (button
== Input
.Buttons
.LEFT
) {
356 public void touchDragged(float screenX
, float screenY
) {
358 if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
&& Math
.abs(screenY
- touchDownY
) > 16) {
359 if (insideCreativeInv(screenX
, screenY
)) {
360 creativeScroll
-= (screenY
- touchDownY
) / 16;
361 touchDownX
= screenX
;
362 touchDownY
= screenY
;
363 if (creativeScroll
< 0) {
366 if (creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
367 creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
373 public void scrolled(int amount
) {
374 switch (CaveGame
.GAME_STATE
) {
376 GP
.player
.slot
+= amount
;
377 if (GP
.player
.slot
< 0) {
380 if (GP
.player
.slot
> 8) {
385 creativeScroll
+= amount
;
386 if (creativeScroll
< 0) {
389 if (creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
390 creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
396 public int getKeyDownCode() {
400 public boolean isKeyDown() {
404 int getBlockDamage() {
416 int getCreativeScroll() {
417 return creativeScroll
;
422 if (touchedDown
&& touchDownBtn
== Input
.Buttons
.LEFT
) {
425 if (touchedDown
&& TimeUtils
.timeSinceMillis(touchDownTime
) > 500) {