1 package ru
.deadsoftware
.cavedroid
.game
;
3 import com
.badlogic
.gdx
.Input
;
4 import com
.badlogic
.gdx
.utils
.TimeUtils
;
5 import ru
.deadsoftware
.cavedroid
.CaveGame
;
6 import ru
.deadsoftware
.cavedroid
.GameScreen
;
7 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Pig
;
8 import ru
.deadsoftware
.cavedroid
.misc
.AppState
;
9 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
11 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
13 public class GameInput
{
15 private boolean checkSwim() {
16 return GameItems
.isFluid(GP
.world
.getForeMap(GP
.player
.getMapX(), GP
.player
.getMapY()));
19 private boolean insideCreativeInv(int screenX
, int screenY
) {
20 return (screenX
> GP
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
21 screenX
< GP
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
22 screenY
> GP
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
23 screenY
< GP
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2);
26 private void wasdPressed(int keycode
) {
27 if (GP
.ctrlMode
== 0 || !CaveGame
.TOUCH
) {
30 GP
.player
.mov
.x
= -GamePhysics
.PL_SPEED
;
32 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
35 GP
.player
.mov
.x
= GamePhysics
.PL_SPEED
;
37 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
59 public void keyDown(int keycode
) {
61 GP
.keyDownCode
= keycode
;
62 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
63 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
65 } else switch (keycode
) {
66 case Input
.Keys
.ALT_LEFT
:
69 if (GP
.ctrlMode
> 1) GP
.ctrlMode
= 0;
73 case Input
.Keys
.SPACE
:
75 GP
.player
.swim
= true;
76 } else if (GP
.player
.canJump
) {
77 GP
.player
.mov
.add(0, -7);
78 } else if (!GP
.player
.flyMode
&& GP
.player
.gameMode
== 1) {
79 GP
.player
.flyMode
= true;
81 } else if (GP
.player
.flyMode
) {
82 GP
.player
.mov
.y
= -GamePhysics
.PL_SPEED
;
86 case Input
.Keys
.CONTROL_LEFT
:
87 GP
.player
.mov
.y
= GamePhysics
.PL_SPEED
;
91 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) switch (GP
.player
.gameMode
) {
96 CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
99 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
103 GP
.mobs
.add(new Pig(GP
.curX
* 16, GP
.curY
* 16));
107 GP
.world
.placeToForeground(GP
.curX
, GP
.curY
, 8);
110 case Input
.Keys
.ESCAPE
:
111 case Input
.Keys
.BACK
:
112 CaveGame
.STATE
= AppState
.GOTO_MENU
;
116 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
120 GameScreen
.SHOW_MAP
= !GameScreen
.SHOW_MAP
;
125 public void keyUp(int keycode
) {
130 if (CaveGame
.TOUCH
&& GP
.player
.swim
) GP
.player
.swim
= false;
133 case Input
.Keys
.SPACE
:
134 case Input
.Keys
.CONTROL_LEFT
:
135 if (GP
.player
.flyMode
) GP
.player
.mov
.y
= 0;
136 if (GP
.player
.swim
) GP
.player
.swim
= false;
141 public void touchDown(int screenX
, int screenY
, int button
) {
142 GP
.touchDownTime
= TimeUtils
.millis();
143 GP
.isTouchDown
= true;
144 GP
.touchDownBtn
= button
;
145 GP
.touchDownX
= screenX
;
146 GP
.touchDownY
= screenY
;
149 public void touchUp(int screenX
, int screenY
, int button
) {
150 if (CaveGame
.TOUCH
&& GP
.isKeyDown
) {
151 keyUp(GP
.keyDownCode
);
152 GP
.isKeyDown
= false;
154 if (GP
.isTouchDown
) {
155 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& insideCreativeInv(screenX
, screenY
)) {
156 int ix
= (int) (screenX
- (GP
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
157 int iy
= (int) (screenY
- (GP
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
158 int item
= GP
.creativeScroll
* 8 + (ix
+ iy
* 8);
159 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) item
= -1;
160 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
161 for (int i
= 8; i
> 0; i
--) {
162 GP
.player
.inv
[i
] = GP
.player
.inv
[i
- 1];
164 GP
.player
.inv
[0] = item
;
166 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
167 CaveGame
.STATE
= AppState
.GAME_PLAY
;
168 } else if (screenY
< Assets
.invBar
.getRegionHeight() &&
169 screenX
> GP
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2 &&
170 screenX
< GP
.renderer
.getWidth() / 2 + Assets
.invBar
.getRegionWidth() / 2) {
171 GP
.player
.invSlot
= (int) ((screenX
- (GP
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2)) / 20);
172 } else if (button
== Input
.Buttons
.RIGHT
) {
173 GP
.useItem(GP
.curX
, GP
.curY
,
174 GP
.player
.inv
[GP
.player
.invSlot
], false);
175 } else if (button
== Input
.Buttons
.LEFT
) {
179 GP
.isTouchDown
= false;
182 public void touchDragged(int screenX
, int screenY
) {
183 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
- GP
.touchDownY
) > 16) {
184 if (insideCreativeInv(screenX
, screenY
)) {
185 GP
.creativeScroll
-= (screenY
- GP
.touchDownY
) / 16;
186 GP
.touchDownX
= screenX
;
187 GP
.touchDownY
= screenY
;
188 if (GP
.creativeScroll
< 0) GP
.creativeScroll
= 0;
189 if (GP
.creativeScroll
> GP
.maxCreativeScroll
)
190 GP
.creativeScroll
= GP
.maxCreativeScroll
;
195 public void scrolled(int amount
) {
196 switch (CaveGame
.STATE
) {
198 GP
.player
.invSlot
+= amount
;
199 if (GP
.player
.invSlot
< 0) GP
.player
.invSlot
= 8;
200 if (GP
.player
.invSlot
> 8) GP
.player
.invSlot
= 0;
202 case GAME_CREATIVE_INV
:
203 GP
.creativeScroll
+= amount
;
204 if (GP
.creativeScroll
< 0) GP
.creativeScroll
= 0;
205 if (GP
.creativeScroll
> GP
.maxCreativeScroll
)
206 GP
.creativeScroll
= GP
.maxCreativeScroll
;