1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Input
;
4 import com
.badlogic
.gdx
.utils
.TimeUtils
;
5 import ru
.deadsoftware
.cavecraft
.CaveGame
;
6 import ru
.deadsoftware
.cavecraft
.GameScreen
;
7 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Pig
;
8 import ru
.deadsoftware
.cavecraft
.misc
.AppState
;
9 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
11 public class GameInput
{
15 public GameInput(GameProc gp
) {
19 private boolean checkSwim() {
20 return GameItems
.isFluid(gp
.world
.getForeMap(gp
.player
.getMapX(), gp
.player
.getMapY()));
23 private boolean insideCreativeInv(int screenX
, int screenY
) {
24 return (screenX
> gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
25 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
26 screenY
> gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
27 screenY
< gp
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2);
30 private void wasdPressed(int keycode
) {
31 if (gp
.ctrlMode
== 0 || !CaveGame
.TOUCH
) {
34 gp
.player
.mov
.x
= -GamePhysics
.PL_SPEED
;
36 if (CaveGame
.TOUCH
&& checkSwim()) gp
.player
.swim
= true;
39 gp
.player
.mov
.x
= GamePhysics
.PL_SPEED
;
41 if (CaveGame
.TOUCH
&& checkSwim()) gp
.player
.swim
= true;
63 public void keyDown(int keycode
) {
65 gp
.keyDownCode
= keycode
;
66 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
67 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
69 } else switch (keycode
) {
70 case Input
.Keys
.ALT_LEFT
:
73 if (gp
.ctrlMode
> 1) gp
.ctrlMode
= 0;
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
.STATE
== AppState
.GAME_PLAY
) switch (gp
.player
.gameMode
) {
100 CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
103 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
107 gp
.mobs
.add(new Pig(gp
.curX
* 16, gp
.curY
* 16));
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
;