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
.ControlMode
;
9 import ru
.deadsoftware
.cavedroid
.misc
.states
.AppState
;
10 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
11 import ru
.deadsoftware
.cavedroid
.misc
.states
.GameState
;
13 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
15 public class GameInput
{
17 private boolean checkSwim() {
18 return GameItems
.isFluid(GP
.world
.getForeMap(GP
.player
.getMapX(), GP
.player
.getLowerMapY()));
21 private boolean insideCreativeInv(int screenX
, int screenY
) {
22 return (screenX
> GP
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
23 screenX
< GP
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
24 screenY
> GP
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
25 screenY
< GP
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2);
28 private void wasdPressed(int keycode
) {
29 if (GP
.controlMode
== ControlMode
.WALK
|| !CaveGame
.TOUCH
) {
32 GP
.player
.mov
.x
= -GamePhysics
.PL_SPEED
;
34 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
37 GP
.player
.mov
.x
= GamePhysics
.PL_SPEED
;
39 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
61 public void keyDown(int keycode
) {
63 GP
.keyDownCode
= keycode
;
64 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
65 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
67 } else switch (keycode
) {
68 case Input
.Keys
.ALT_LEFT
:
70 GP
.controlMode
= GP
.controlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
74 case Input
.Keys
.SPACE
:
76 GP
.player
.swim
= true;
77 } else if (GP
.player
.canJump
) {
78 GP
.player
.mov
.add(0, -7);
79 } else if (!GP
.player
.flyMode
&& GP
.player
.gameMode
== 1) {
80 GP
.player
.flyMode
= true;
82 } else if (GP
.player
.flyMode
) {
83 GP
.player
.mov
.y
= -GamePhysics
.PL_SPEED
;
87 case Input
.Keys
.CONTROL_LEFT
:
88 GP
.player
.mov
.y
= GamePhysics
.PL_SPEED
;
92 if (CaveGame
.GAME_STATE
== GameState
.PLAY
){
93 switch (GP
.player
.gameMode
) {
98 CaveGame
.GAME_STATE
= GameState
.CREATIVE_INV
;
102 CaveGame
.GAME_STATE
= GameState
.PLAY
;
107 GP
.mobs
.add(new Pig(GP
.curX
* 16, GP
.curY
* 16));
111 GP
.world
.placeToForeground(GP
.curX
, GP
.curY
, 8);
114 case Input
.Keys
.ESCAPE
:
115 case Input
.Keys
.BACK
:
116 CaveGame
.APP_STATE
= AppState
.SAVE
;
117 CaveGame
.GAME_STATE
= GameState
.PAUSE
;
121 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
125 GameScreen
.SHOW_MAP
= !GameScreen
.SHOW_MAP
;
130 public void keyUp(int keycode
) {
135 if (CaveGame
.TOUCH
&& GP
.player
.swim
) GP
.player
.swim
= false;
138 case Input
.Keys
.SPACE
:
139 case Input
.Keys
.CONTROL_LEFT
:
140 if (GP
.player
.flyMode
) GP
.player
.mov
.y
= 0;
141 if (GP
.player
.swim
) GP
.player
.swim
= false;
146 public void touchDown(int screenX
, int screenY
, int button
) {
147 GP
.touchDownTime
= TimeUtils
.millis();
148 GP
.isTouchDown
= true;
149 GP
.touchDownBtn
= button
;
150 GP
.touchDownX
= screenX
;
151 GP
.touchDownY
= screenY
;
154 public void touchUp(int screenX
, int screenY
, int button
) {
155 if (CaveGame
.TOUCH
&& GP
.isKeyDown
) {
156 keyUp(GP
.keyDownCode
);
157 GP
.isKeyDown
= false;
159 if (GP
.isTouchDown
) {
160 if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
&& insideCreativeInv(screenX
, screenY
)) {
161 int ix
= (int) (screenX
- (GP
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
162 int iy
= (int) (screenY
- (GP
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
163 int item
= GP
.creativeScroll
* 8 + (ix
+ iy
* 8);
164 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) item
= -1;
165 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
166 System
.arraycopy(GP
.player
.inventory
, 0, GP
.player
.inventory
, 1, 8);
167 GP
.player
.inventory
[0] = item
;
169 } else if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
) {
170 CaveGame
.GAME_STATE
= GameState
.PLAY
;
171 } else if (screenY
< Assets
.invBar
.getRegionHeight() &&
172 screenX
> GP
.renderer
.getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2 &&
173 screenX
< GP
.renderer
.getWidth() / 2 + (float) Assets
.invBar
.getRegionWidth() / 2) {
174 GP
.player
.slot
= (int) ((screenX
- (GP
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2)) / 20);
175 } else if (button
== Input
.Buttons
.RIGHT
) {
176 GP
.useItem(GP
.curX
, GP
.curY
,
177 GP
.player
.inventory
[GP
.player
.slot
], false);
178 } else if (button
== Input
.Buttons
.LEFT
) {
182 GP
.isTouchDown
= false;
185 public void touchDragged(int screenX
, int screenY
) {
186 if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
&& Math
.abs(screenY
- GP
.touchDownY
) > 16) {
187 if (insideCreativeInv(screenX
, screenY
)) {
188 GP
.creativeScroll
-= (screenY
- GP
.touchDownY
) / 16;
189 GP
.touchDownX
= screenX
;
190 GP
.touchDownY
= screenY
;
191 if (GP
.creativeScroll
< 0) GP
.creativeScroll
= 0;
192 if (GP
.creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
)
193 GP
.creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
198 public void scrolled(int amount
) {
199 switch (CaveGame
.GAME_STATE
) {
201 GP
.player
.slot
+= amount
;
202 if (GP
.player
.slot
< 0) GP
.player
.slot
= 8;
203 if (GP
.player
.slot
> 8) GP
.player
.slot
= 0;
206 GP
.creativeScroll
+= amount
;
207 if (GP
.creativeScroll
< 0) GP
.creativeScroll
= 0;
208 if (GP
.creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
)
209 GP
.creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;