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()));
36 private void goUpwards() {
38 GP
.player
.swim
= true;
39 } else if (GP
.player
.canJump()) {
40 GP
.player
.getMov().add(0, -7);
41 } else if (!GP
.player
.isFlyMode() && GP
.player
.gameMode
== 1) {
42 GP
.player
.setFlyMode(true);
43 GP
.player
.getMov().y
= 0;
44 } else if (GP
.player
.isFlyMode()) {
45 GP
.player
.getMov().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
.getMov().x
= -GamePhysics
.PL_SPEED
;
63 GP
.player
.setDir(Mob
.LEFT
);
64 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
67 GP
.player
.getMov().x
= GamePhysics
.PL_SPEED
;
68 GP
.player
.setDir(Mob
.RIGHT
);
69 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
72 case Input
.Keys
.SPACE
:
76 case Input
.Keys
.CONTROL_LEFT
:
77 GP
.player
.getMov().y
= GamePhysics
.PL_SPEED
;
99 private boolean isNotAutoselectable(int x
, int y
) {
100 return (!GP
.world
.hasForeAt(x
, y
) || !GP
.world
.getForeMapBlock(x
, y
).hasCollision());
103 private void checkCursorBounds() {
106 } else if (curY
>= GP
.world
.getHeight()) {
107 curY
= GP
.world
.getHeight() - 1;
110 if (GP
.controlMode
== ControlMode
.CURSOR
) {
111 if (curX
* 16 + 8 < GP
.player
.getX() + GP
.player
.getWidth() / 2) {
112 GP
.player
.setDir(Mob
.LEFT
);
114 GP
.player
.setDir(Mob
.RIGHT
);
119 private void moveCursor() {
123 if (GP
.controlMode
== ControlMode
.WALK
&& CaveGame
.TOUCH
) {
124 curX
= GP
.player
.getMapX() + (GP
.player
.looksLeft() ?
-1 : 1);
125 curY
= GP
.player
.getUpperMapY();
126 for (int i
= 0; i
< 2 && isNotAutoselectable(curX
, curY
); i
++) {
129 if (isNotAutoselectable(curX
, curY
)) {
130 curX
+= GP
.player
.looksLeft() ?
1 : -1;
132 } else if (!CaveGame
.TOUCH
) {
133 curX
= (int) (Gdx
.input
.getX() * (GP
.renderer
.getWidth() / GameScreen
.getWidth()) + GP
.renderer
.getCamX()) / 16;
134 curY
= (int) (Gdx
.input
.getY() * (GP
.renderer
.getHeight() / GameScreen
.getHeight()) + GP
.renderer
.getCamY()) / 16;
135 if (curX
< 0) curX
--;
138 if (pastX
!= curX
|| pastY
!= curY
) {
145 private void useItem(int x
, int y
, int id
, boolean bg
) {
146 String key
= getItem(id
).isBlock() ?
getBlockKey(id
) : getItemKey(id
);
148 if (getItem(id
).isBlock()) {
150 GP
.world
.placeToForeground(x
, y
, getBlockIdByItemId(id
));
152 GP
.world
.placeToBackground(x
, y
, getBlockIdByItemId(id
));
157 GP
.world
.placeToForeground(x
, y
, getBlockId("water"));
158 GP
.player
.inventory
[GP
.player
.slot
] = getItemId("bucket_empty");
161 GP
.world
.placeToForeground(x
, y
, getBlockId("lava"));
162 GP
.player
.inventory
[GP
.player
.slot
] = getItemId("bucket_empty");
169 private void pressLMB() {
170 if ((GP
.world
.hasForeAt(curX
, curY
) && GP
.world
.getForeMapBlock(curX
, curY
).getHp() >= 0) ||
171 (!GP
.world
.hasForeAt(curX
, curY
) && GP
.world
.hasBackAt(curX
, curY
) &&
172 GP
.world
.getBackMapBlock(curX
, curY
).getHp() >= 0)) {
173 if (GP
.player
.gameMode
== 0) {
175 if (GP
.world
.hasForeAt(curX
, curY
)) {
176 if (blockDamage
>= GP
.world
.getForeMapBlock(curX
, curY
).getHp()) {
177 GP
.world
.destroyForeMap(curX
, curY
);
180 } else if (GP
.world
.hasBackAt(curX
, curY
)) {
181 if (blockDamage
>= GP
.world
.getBackMapBlock(curX
, curY
).getHp()) {
182 GP
.world
.destroyBackMap(curX
, curY
);
187 if (GP
.world
.hasForeAt(curX
, curY
)) {
188 GP
.world
.placeToForeground(curX
, curY
, 0);
189 } else if (GP
.world
.hasBackAt(curX
, curY
)) {
190 GP
.world
.placeToBackground(curX
, curY
, 0);
197 private boolean insideHotbar(float x
, float y
) {
198 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
199 return y
< hotbar
.getRegionHeight() &&
200 Range
.open(GP
.renderer
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2,
201 GP
.renderer
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2).contains(x
);
204 private void holdMB() {
205 if (touchDownBtn
== Input
.Buttons
.RIGHT
) {
206 useItem(curX
, curY
, GP
.player
.inventory
[GP
.player
.slot
], true);
209 if (insideHotbar(touchDownX
, touchDownY
)) {
210 CaveGame
.GAME_STATE
= GameState
.CREATIVE_INV
;
216 public void keyDown(int keycode
) {
218 keyDownCode
= keycode
;
224 case Input
.Keys
.SPACE
:
225 case Input
.Keys
.CONTROL_LEFT
:
226 wasdPressed(keycode
);
229 case Input
.Keys
.ALT_LEFT
:
230 if (CaveGame
.TOUCH
) {
231 GP
.controlMode
= GP
.controlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
236 if (CaveGame
.GAME_STATE
== GameState
.PLAY
) {
237 switch (GP
.player
.gameMode
) {
242 CaveGame
.GAME_STATE
= GameState
.CREATIVE_INV
;
246 CaveGame
.GAME_STATE
= GameState
.PLAY
;
251 GP
.mobs
.add(new Pig(curX
* 16, curY
* 16));
255 GP
.world
.placeToForeground(curX
, curY
, 8);
258 case Input
.Keys
.ESCAPE
:
259 case Input
.Keys
.BACK
:
260 CaveGame
.APP_STATE
= AppState
.SAVE
;
261 CaveGame
.GAME_STATE
= GameState
.PAUSE
;
265 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
269 GameScreen
.SHOW_MAP
= !GameScreen
.SHOW_MAP
;
274 public void keyUp(int keycode
) {
278 GP
.player
.getMov().x
= 0;
279 if (CaveGame
.TOUCH
&& GP
.player
.swim
) GP
.player
.swim
= false;
284 case Input
.Keys
.SPACE
:
285 case Input
.Keys
.CONTROL_LEFT
:
286 if (GP
.player
.isFlyMode()) GP
.player
.getMov().y
= 0;
287 if (GP
.player
.swim
) GP
.player
.swim
= false;
292 public void touchDown(float touchX
, float touchY
, int button
) {
293 touchDownTime
= TimeUtils
.millis();
295 touchDownBtn
= button
;
300 public void touchUp(float screenX
, float screenY
, int button
) {
301 if (CaveGame
.TOUCH
&& keyDown
) {
305 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
306 TextureRegion creative
= Assets
.textureRegions
.get("creative");
308 if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
&& insideCreativeInv(screenX
, screenY
)) {
309 int ix
= (int) (screenX
- (GP
.renderer
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
310 int iy
= (int) (screenY
- (GP
.renderer
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
311 int item
= creativeScroll
* 8 + (ix
+ iy
* 8);
312 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) item
= -1;
313 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
314 System
.arraycopy(GP
.player
.inventory
, 0, GP
.player
.inventory
, 1, 8);
315 GP
.player
.inventory
[0] = item
;
317 } else if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
) {
318 CaveGame
.GAME_STATE
= GameState
.PLAY
;
319 } else if (screenY
< hotbar
.getRegionHeight() &&
320 screenX
> GP
.renderer
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
321 screenX
< GP
.renderer
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
322 GP
.player
.slot
= (int) ((screenX
- (GP
.renderer
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
323 } else if (button
== Input
.Buttons
.RIGHT
) {
325 GP
.player
.inventory
[GP
.player
.slot
], false);
326 } else if (button
== Input
.Buttons
.LEFT
) {
333 public void touchDragged(float screenX
, float screenY
) {
334 if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
&& Math
.abs(screenY
- touchDownY
) > 16) {
335 if (insideCreativeInv(screenX
, screenY
)) {
336 creativeScroll
-= (screenY
- touchDownY
) / 16;
337 touchDownX
= screenX
;
338 touchDownY
= screenY
;
339 if (creativeScroll
< 0) creativeScroll
= 0;
340 if (creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
)
341 creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
346 public void scrolled(int amount
) {
347 switch (CaveGame
.GAME_STATE
) {
349 GP
.player
.slot
+= amount
;
350 if (GP
.player
.slot
< 0) GP
.player
.slot
= 8;
351 if (GP
.player
.slot
> 8) GP
.player
.slot
= 0;
354 creativeScroll
+= amount
;
355 if (creativeScroll
< 0) creativeScroll
= 0;
356 if (creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
)
357 creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
362 public int getKeyDownCode() {
366 public boolean isKeyDown() {
370 int getBlockDamage() {
382 int getCreativeScroll() {
383 return creativeScroll
;
388 if (touchedDown
&& touchDownBtn
== Input
.Buttons
.LEFT
) {
391 if (touchedDown
&& TimeUtils
.timeSinceMillis(touchDownTime
) > 500) {