1 package ru
.deadsoftware
.cavedroid
.game
;
3 import com
.badlogic
.gdx
.Input
;
4 import com
.badlogic
.gdx
.graphics
.g2d
.TextureRegion
;
5 import com
.badlogic
.gdx
.utils
.TimeUtils
;
6 import ru
.deadsoftware
.cavedroid
.CaveGame
;
7 import ru
.deadsoftware
.cavedroid
.GameScreen
;
8 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Pig
;
9 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
10 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
11 import ru
.deadsoftware
.cavedroid
.misc
.states
.AppState
;
12 import ru
.deadsoftware
.cavedroid
.misc
.states
.GameState
;
14 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
16 public class GameInput
{
18 private boolean checkSwim() {
19 return GameItems
.isFluid(GP
.world
.getForeMap(GP
.player
.getMapX(), GP
.player
.getLowerMapY()));
22 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
23 private boolean insideCreativeInv(float screenX
, float screenY
) {
24 TextureRegion creative
= Assets
.textureRegions
.get("creative");
25 return (screenX
> GP
.renderer
.getWidth() / 2 - creative
.getRegionWidth() / 2 &&
26 screenX
< GP
.renderer
.getWidth() / 2 + creative
.getRegionWidth() / 2 &&
27 screenY
> GP
.renderer
.getHeight() / 2 - creative
.getRegionHeight() / 2 &&
28 screenY
< GP
.renderer
.getHeight() / 2 + creative
.getRegionHeight() / 2);
31 private void wasdPressed(int keycode
) {
32 if (GP
.controlMode
== ControlMode
.WALK
|| !CaveGame
.TOUCH
) {
35 GP
.player
.mov
.x
= -GamePhysics
.PL_SPEED
;
37 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
40 GP
.player
.mov
.x
= GamePhysics
.PL_SPEED
;
42 if (CaveGame
.TOUCH
&& checkSwim()) GP
.player
.swim
= true;
64 public void keyDown(int keycode
) {
66 GP
.keyDownCode
= keycode
;
67 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
68 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
70 } else switch (keycode
) {
71 case Input
.Keys
.ALT_LEFT
:
73 GP
.controlMode
= GP
.controlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
77 case Input
.Keys
.SPACE
:
79 GP
.player
.swim
= true;
80 } else if (GP
.player
.canJump
) {
81 GP
.player
.mov
.add(0, -7);
82 } else if (!GP
.player
.flyMode
&& GP
.player
.gameMode
== 1) {
83 GP
.player
.flyMode
= true;
85 } else if (GP
.player
.flyMode
) {
86 GP
.player
.mov
.y
= -GamePhysics
.PL_SPEED
;
90 case Input
.Keys
.CONTROL_LEFT
:
91 GP
.player
.mov
.y
= GamePhysics
.PL_SPEED
;
95 if (CaveGame
.GAME_STATE
== GameState
.PLAY
){
96 switch (GP
.player
.gameMode
) {
101 CaveGame
.GAME_STATE
= GameState
.CREATIVE_INV
;
105 CaveGame
.GAME_STATE
= GameState
.PLAY
;
110 GP
.mobs
.add(new Pig(GP
.curX
* 16, GP
.curY
* 16));
114 GP
.world
.placeToForeground(GP
.curX
, GP
.curY
, 8);
117 case Input
.Keys
.ESCAPE
:
118 case Input
.Keys
.BACK
:
119 CaveGame
.APP_STATE
= AppState
.SAVE
;
120 CaveGame
.GAME_STATE
= GameState
.PAUSE
;
124 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
128 GameScreen
.SHOW_MAP
= !GameScreen
.SHOW_MAP
;
133 public void keyUp(int keycode
) {
138 if (CaveGame
.TOUCH
&& GP
.player
.swim
) GP
.player
.swim
= false;
141 case Input
.Keys
.SPACE
:
142 case Input
.Keys
.CONTROL_LEFT
:
143 if (GP
.player
.flyMode
) GP
.player
.mov
.y
= 0;
144 if (GP
.player
.swim
) GP
.player
.swim
= false;
149 public void touchDown(float touchX
, float touchY
, int button
) {
150 GP
.touchDownTime
= TimeUtils
.millis();
151 GP
.isTouchDown
= true;
152 GP
.touchDownBtn
= button
;
153 GP
.touchDownX
= touchX
;
154 GP
.touchDownY
= touchY
;
157 public void touchUp(float screenX
, float screenY
, int button
) {
158 if (CaveGame
.TOUCH
&& GP
.isKeyDown
) {
159 keyUp(GP
.keyDownCode
);
160 GP
.isKeyDown
= false;
162 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
163 TextureRegion creative
= Assets
.textureRegions
.get("creative");
164 if (GP
.isTouchDown
) {
165 if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
&& insideCreativeInv(screenX
, screenY
)) {
166 int ix
= (int) (screenX
- (GP
.renderer
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
167 int iy
= (int) (screenY
- (GP
.renderer
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
168 int item
= GP
.creativeScroll
* 8 + (ix
+ iy
* 8);
169 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) item
= -1;
170 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
171 System
.arraycopy(GP
.player
.inventory
, 0, GP
.player
.inventory
, 1, 8);
172 GP
.player
.inventory
[0] = item
;
174 } else if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
) {
175 CaveGame
.GAME_STATE
= GameState
.PLAY
;
176 } else if (screenY
< hotbar
.getRegionHeight() &&
177 screenX
> GP
.renderer
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
178 screenX
< GP
.renderer
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
179 GP
.player
.slot
= (int) ((screenX
- (GP
.renderer
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
180 } else if (button
== Input
.Buttons
.RIGHT
) {
181 GP
.useItem(GP
.curX
, GP
.curY
,
182 GP
.player
.inventory
[GP
.player
.slot
], false);
183 } else if (button
== Input
.Buttons
.LEFT
) {
187 GP
.isTouchDown
= false;
190 public void touchDragged(float screenX
, float screenY
) {
191 if (CaveGame
.GAME_STATE
== GameState
.CREATIVE_INV
&& Math
.abs(screenY
- GP
.touchDownY
) > 16) {
192 if (insideCreativeInv(screenX
, screenY
)) {
193 GP
.creativeScroll
-= (screenY
- GP
.touchDownY
) / 16;
194 GP
.touchDownX
= screenX
;
195 GP
.touchDownY
= screenY
;
196 if (GP
.creativeScroll
< 0) GP
.creativeScroll
= 0;
197 if (GP
.creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
)
198 GP
.creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
203 public void scrolled(int amount
) {
204 switch (CaveGame
.GAME_STATE
) {
206 GP
.player
.slot
+= amount
;
207 if (GP
.player
.slot
< 0) GP
.player
.slot
= 8;
208 if (GP
.player
.slot
> 8) GP
.player
.slot
= 0;
211 GP
.creativeScroll
+= amount
;
212 if (GP
.creativeScroll
< 0) GP
.creativeScroll
= 0;
213 if (GP
.creativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
)
214 GP
.creativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;