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
;
121 public void keyUp(int keycode
) {
126 if (CaveGame
.TOUCH
&& GP
.player
.swim
) GP
.player
.swim
= false;
129 case Input
.Keys
.SPACE
:
130 case Input
.Keys
.CONTROL_LEFT
:
131 if (GP
.player
.flyMode
) GP
.player
.mov
.y
= 0;
132 if (GP
.player
.swim
) GP
.player
.swim
= false;
137 public void touchDown(int screenX
, int screenY
, int button
) {
138 GP
.touchDownTime
= TimeUtils
.millis();
139 GP
.isTouchDown
= true;
140 GP
.touchDownBtn
= button
;
141 GP
.touchDownX
= screenX
;
142 GP
.touchDownY
= screenY
;
145 public void touchUp(int screenX
, int screenY
, int button
) {
146 if (CaveGame
.TOUCH
&& GP
.isKeyDown
) {
147 keyUp(GP
.keyDownCode
);
148 GP
.isKeyDown
= false;
150 if (GP
.isTouchDown
) {
151 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& insideCreativeInv(screenX
, screenY
)) {
152 int ix
= (int) (screenX
- (GP
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
153 int iy
= (int) (screenY
- (GP
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
154 int item
= GP
.creativeScroll
* 8 + (ix
+ iy
* 8);
155 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) item
= -1;
156 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
157 for (int i
= 8; i
> 0; i
--) {
158 GP
.player
.inv
[i
] = GP
.player
.inv
[i
- 1];
160 GP
.player
.inv
[0] = item
;
162 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
163 CaveGame
.STATE
= AppState
.GAME_PLAY
;
164 } else if (screenY
< Assets
.invBar
.getRegionHeight() &&
165 screenX
> GP
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2 &&
166 screenX
< GP
.renderer
.getWidth() / 2 + Assets
.invBar
.getRegionWidth() / 2) {
167 GP
.player
.invSlot
= (int) ((screenX
- (GP
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2)) / 20);
168 } else if (button
== Input
.Buttons
.RIGHT
) {
169 GP
.useItem(GP
.curX
, GP
.curY
,
170 GP
.player
.inv
[GP
.player
.invSlot
], false);
171 } else if (button
== Input
.Buttons
.LEFT
) {
175 GP
.isTouchDown
= false;
178 public void touchDragged(int screenX
, int screenY
) {
179 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
- GP
.touchDownY
) > 16) {
180 if (insideCreativeInv(screenX
, screenY
)) {
181 GP
.creativeScroll
-= (screenY
- GP
.touchDownY
) / 16;
182 GP
.touchDownX
= screenX
;
183 GP
.touchDownY
= screenY
;
184 if (GP
.creativeScroll
< 0) GP
.creativeScroll
= 0;
185 if (GP
.creativeScroll
> GP
.maxCreativeScroll
)
186 GP
.creativeScroll
= GP
.maxCreativeScroll
;
191 public void scrolled(int amount
) {
192 switch (CaveGame
.STATE
) {
194 GP
.player
.invSlot
+= amount
;
195 if (GP
.player
.invSlot
< 0) GP
.player
.invSlot
= 8;
196 if (GP
.player
.invSlot
> 8) GP
.player
.invSlot
= 0;
198 case GAME_CREATIVE_INV
:
199 GP
.creativeScroll
+= amount
;
200 if (GP
.creativeScroll
< 0) GP
.creativeScroll
= 0;
201 if (GP
.creativeScroll
> GP
.maxCreativeScroll
)
202 GP
.creativeScroll
= GP
.maxCreativeScroll
;