2b9e1ddb2486ba3c6743e1ff251a3ffb1e30fed7
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 void wasdPressed(int keycode
) {
24 if (gp
.ctrlMode
== 0 || !CaveGame
.TOUCH
) {
27 gp
.player
.mov
.x
= -GamePhysics
.PL_SPEED
;
29 if (CaveGame
.TOUCH
&& checkSwim()) gp
.swim
= true;
32 gp
.player
.mov
.x
= GamePhysics
.PL_SPEED
;
34 if (CaveGame
.TOUCH
&& checkSwim()) gp
.swim
= true;
56 public void keyDown(int keycode
) {
58 gp
.keyDownCode
= keycode
;
59 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
60 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
62 } else switch (keycode
) {
63 case Input
.Keys
.ALT_LEFT
:
66 if (gp
.ctrlMode
> 1) gp
.ctrlMode
= 0;
70 case Input
.Keys
.SPACE
:
73 } else if (gp
.player
.canJump
) {
74 gp
.player
.mov
.add(0, -7);
75 } else if (!gp
.player
.flyMode
&& gp
.player
.gameMode
== 1) {
76 gp
.player
.flyMode
= true;
78 } else if (gp
.player
.flyMode
) {
79 gp
.player
.mov
.y
= -GamePhysics
.PL_SPEED
;
83 case Input
.Keys
.CONTROL_LEFT
:
84 gp
.player
.mov
.y
= GamePhysics
.PL_SPEED
;
88 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) switch (gp
.player
.gameMode
) {
93 CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
96 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
100 gp
.mobs
.add(new Pig(gp
.curX
* 16, gp
.curY
* 16));
103 case Input
.Keys
.ESCAPE
:
104 case Input
.Keys
.BACK
:
105 CaveGame
.STATE
= AppState
.GOTO_MENU
;
109 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
114 public void keyUp(int keycode
) {
119 if (CaveGame
.TOUCH
&& gp
.swim
) gp
.swim
= false;
122 case Input
.Keys
.SPACE
:
123 case Input
.Keys
.CONTROL_LEFT
:
124 if (gp
.player
.flyMode
) gp
.player
.mov
.y
= 0;
125 if (gp
.swim
) gp
.swim
= false;
130 public void mouseMoved(int screenX
, int screenY
) {
133 public void touchDown(int screenX
, int screenY
, int button
) {
134 gp
.touchDownTime
= TimeUtils
.millis();
135 gp
.isTouchDown
= true;
136 gp
.touchDownBtn
= button
;
137 gp
.touchDownX
= screenX
;
138 gp
.touchDownY
= screenY
;
141 public void touchUp(int screenX
, int screenY
, int button
) {
142 if (CaveGame
.TOUCH
&& gp
.isKeyDown
) {
143 keyUp(gp
.keyDownCode
);
144 gp
.isKeyDown
= false;
146 if (gp
.isTouchDown
) {
147 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
148 screenX
> gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
149 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
150 screenY
> gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
151 screenY
< gp
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2) {
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
.slot
= (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
.slot
], 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 (screenX
> gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
181 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
182 screenY
> gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
183 screenY
< gp
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2) {
184 gp
.creativeScroll
-= (screenY
- gp
.touchDownY
) / 16;
185 gp
.touchDownX
= screenX
;
186 gp
.touchDownY
= screenY
;
187 if (gp
.creativeScroll
< 0) gp
.creativeScroll
= 0;
188 if (gp
.creativeScroll
> gp
.maxCreativeScroll
)
189 gp
.creativeScroll
= gp
.maxCreativeScroll
;
194 public void scrolled(int amount
) {
195 switch (CaveGame
.STATE
) {
198 if (gp
.slot
< 0) gp
.slot
= 8;
199 if (gp
.slot
> 8) gp
.slot
= 0;
201 case GAME_CREATIVE_INV
:
202 gp
.creativeScroll
+= amount
;
203 if (gp
.creativeScroll
< 0) gp
.creativeScroll
= 0;
204 if (gp
.creativeScroll
> gp
.maxCreativeScroll
)
205 gp
.creativeScroll
= gp
.maxCreativeScroll
;