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 @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);
45 private void wasdPressed(int keycode
) {
46 if (GP
.controlMode
== ControlMode
.WALK
|| !CaveGame
.TOUCH
) {
49 GP
.player
.mov
.x
= -GamePhysics
.PL_SPEED
;
51 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
54 GP
.player
.mov
.x
= GamePhysics
.PL_SPEED
;
56 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
78 private boolean isNotAutoselectable(int x
, int y
) {
79 return (!GP
.world
.hasForeAt(x
, y
) || !GP
.world
.getForeMapBlock(x
, y
).hasCollision());
82 private void checkCursorBounds() {
85 } else if (curY
>= GP
.world
.getHeight()) {
86 curY
= GP
.world
.getHeight() - 1;
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
);
93 GP
.player
.setDir(Mob
.RIGHT
);
98 private void moveCursor() {
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
++) {
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
) {
124 private void useItem(int x
, int y
, int id
, boolean bg
) {
125 String key
= getItem(id
).isBlock() ?
getBlockKey(id
) : getItemKey(id
);
127 if (getItem(id
).isBlock()) {
129 GP
.world
.placeToForeground(x
, y
, getBlockIdByItemId(id
));
131 GP
.world
.placeToBackground(x
, y
, getBlockIdByItemId(id
));
136 GP
.world
.placeToForeground(x
, y
, getBlockId("water"));
137 GP
.player
.inventory
[GP
.player
.slot
] = getItemId("bucket_empty");
140 GP
.world
.placeToForeground(x
, y
, getBlockId("lava"));
141 GP
.player
.inventory
[GP
.player
.slot
] = getItemId("bucket_empty");
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) {
154 if (GP
.world
.hasForeAt(curX
, curY
)) {
155 if (blockDamage
>= GP
.world
.getForeMapBlock(curX
, curY
).getHp()) {
156 GP
.world
.destroyForeMap(curX
, curY
);
159 } else if (GP
.world
.hasBackAt(curX
, curY
)) {
160 if (blockDamage
>= GP
.world
.getBackMapBlock(curX
, curY
).getHp()) {
161 GP
.world
.destroyBackMap(curX
, curY
);
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);
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);
188 if (insideHotbar(touchDownX
, touchDownY
)) {
189 CaveGame
.GAME_STATE
= GameState
.CREATIVE_INV
;
195 public void keyDown(int keycode
) {
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
;
208 case Input
.Keys
.SPACE
:
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;
216 } else if (GP
.player
.flyMode
) {
217 GP
.player
.mov
.y
= -GamePhysics
.PL_SPEED
;
221 case Input
.Keys
.CONTROL_LEFT
:
222 GP
.player
.mov
.y
= GamePhysics
.PL_SPEED
;
226 if (CaveGame
.GAME_STATE
== GameState
.PLAY
) {
227 switch (GP
.player
.gameMode
) {
232 CaveGame
.GAME_STATE
= GameState
.CREATIVE_INV
;
236 CaveGame
.GAME_STATE
= GameState
.PLAY
;
241 GP
.mobs
.add(new Pig(curX
* 16, curY
* 16));
245 GP
.world
.placeToForeground(curX
, curY
, 8);
248 case Input
.Keys
.ESCAPE
:
249 case Input
.Keys
.BACK
:
250 CaveGame
.APP_STATE
= AppState
.SAVE
;
251 CaveGame
.GAME_STATE
= GameState
.PAUSE
;
255 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
259 GameScreen
.SHOW_MAP
= !GameScreen
.SHOW_MAP
;
264 public void keyUp(int keycode
) {
269 if (CaveGame
.TOUCH
&& GP
.player
.swim
) GP
.player
.swim
= false;
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;
280 public void touchDown(float touchX
, float touchY
, int button
) {
281 touchDownTime
= TimeUtils
.millis();
283 touchDownBtn
= button
;
288 public void touchUp(float screenX
, float screenY
, int button
) {
289 if (CaveGame
.TOUCH
&& keyDown
) {
293 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
294 TextureRegion creative
= Assets
.textureRegions
.get("creative");
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
) {
313 GP
.player
.inventory
[GP
.player
.slot
], false);
314 } else if (button
== Input
.Buttons
.LEFT
) {
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
) {
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;
342 creativeScroll
+= amount
;
343 if (creativeScroll
< 0) creativeScroll
= 0;
344 if (creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
)
345 creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
350 public int getKeyDownCode() {
354 public boolean isKeyDown() {
358 int getBlockDamage() {
370 int getCreativeScroll() {
371 return creativeScroll
;
376 if (touchedDown
&& touchDownBtn
== Input
.Buttons
.LEFT
) {
379 if (touchedDown
&& TimeUtils
.timeSinceMillis(touchDownTime
) > 500) {