687c6a9e5e6d919f96ab41b7dc0ee5ffbfd1bd06
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((int) (gp
.player
.pos
.x
+ gp
.player
.width
/ 2) / 16,
21 (int) (gp
.player
.pos
.y
+ gp
.player
.height
/ 4 * 3) / 16)));
24 private void wasdPressed(int keycode
) {
25 if (gp
.ctrlMode
== 0 || !CaveGame
.TOUCH
) {
28 gp
.player
.move
.x
= -GamePhysics
.PL_SPEED
;
30 if (CaveGame
.TOUCH
&& checkSwim()) gp
.swim
= true;
33 gp
.player
.move
.x
= GamePhysics
.PL_SPEED
;
35 if (CaveGame
.TOUCH
&& checkSwim()) gp
.swim
= true;
38 } else if (CaveGame
.TOUCH
) {
57 public void keyDown(int keycode
) {
59 gp
.keyDownCode
= keycode
;
60 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
61 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
63 } else switch (keycode
) {
64 case Input
.Keys
.ALT_LEFT
:
67 if (gp
.ctrlMode
> 1) gp
.ctrlMode
= 0;
71 case Input
.Keys
.SPACE
:
74 } else if (gp
.player
.canJump
) {
75 gp
.player
.move
.add(0, -7);
76 } else if (!gp
.player
.flyMode
&& gp
.player
.gameMode
== 1) {
77 gp
.player
.flyMode
= true;
79 } else if (gp
.player
.flyMode
) {
80 gp
.player
.move
.y
= -GamePhysics
.PL_SPEED
;
84 case Input
.Keys
.CONTROL_LEFT
:
85 gp
.player
.move
.y
= GamePhysics
.PL_SPEED
;
89 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) switch (gp
.player
.gameMode
) {
94 CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
97 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
101 gp
.mobs
.add(new Pig(gp
.curX
* 16, gp
.curY
* 16));
104 case Input
.Keys
.ESCAPE
:
105 case Input
.Keys
.BACK
:
106 CaveGame
.STATE
= AppState
.GOTO_MENU
;
110 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
115 public void keyUp(int keycode
) {
119 gp
.player
.move
.x
= 0;
120 if (CaveGame
.TOUCH
&& gp
.swim
) gp
.swim
= false;
123 case Input
.Keys
.SPACE
:
124 case Input
.Keys
.CONTROL_LEFT
:
125 if (gp
.player
.flyMode
) gp
.player
.move
.y
= 0;
126 if (gp
.swim
) gp
.swim
= false;
131 public void mouseMoved(int screenX
, int screenY
) {
134 public void touchDown(int screenX
, int screenY
, int button
) {
135 gp
.touchDownTime
= TimeUtils
.millis();
136 gp
.isTouchDown
= true;
137 gp
.touchDownBtn
= button
;
138 gp
.touchDownX
= screenX
;
139 gp
.touchDownY
= screenY
;
142 public void touchUp(int screenX
, int screenY
, int button
) {
143 if (CaveGame
.TOUCH
&& gp
.isKeyDown
) {
144 keyUp(gp
.keyDownCode
);
145 gp
.isKeyDown
= false;
147 if (gp
.isTouchDown
) {
148 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
149 screenX
> gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
150 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
151 screenY
> gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
152 screenY
< gp
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2) {
153 int ix
= (int) (screenX
- (gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
154 int iy
= (int) (screenY
- (gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
155 int item
= gp
.creativeScroll
* 8 + (ix
+ iy
* 8);
156 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) item
= -1;
157 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
158 for (int i
= 8; i
> 0; i
--) {
159 gp
.player
.inv
[i
] = gp
.player
.inv
[i
- 1];
161 gp
.player
.inv
[0] = item
;
163 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
164 CaveGame
.STATE
= AppState
.GAME_PLAY
;
165 } else if (screenY
< Assets
.invBar
.getRegionHeight() &&
166 screenX
> gp
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2 &&
167 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.invBar
.getRegionWidth() / 2) {
168 gp
.slot
= (int) ((screenX
- (gp
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2)) / 20);
169 } else if (button
== Input
.Buttons
.RIGHT
) {
170 gp
.useItem(gp
.curX
, gp
.curY
,
171 gp
.player
.inv
[gp
.slot
], false);
172 } else if (button
== Input
.Buttons
.LEFT
) {
176 gp
.isTouchDown
= false;
179 public void touchDragged(int screenX
, int screenY
) {
180 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
- gp
.touchDownY
) > 16) {
181 if (screenX
> gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
182 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
183 screenY
> gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
184 screenY
< gp
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2) {
185 gp
.creativeScroll
-= (screenY
- gp
.touchDownY
) / 16;
186 gp
.touchDownX
= screenX
;
187 gp
.touchDownY
= screenY
;
188 if (gp
.creativeScroll
< 0) gp
.creativeScroll
= 0;
189 if (gp
.creativeScroll
> gp
.maxCreativeScroll
)
190 gp
.creativeScroll
= gp
.maxCreativeScroll
;
195 public void scrolled(int amount
) {
196 switch (CaveGame
.STATE
) {
199 if (gp
.slot
< 0) gp
.slot
= 8;
200 if (gp
.slot
> 8) gp
.slot
= 0;
202 case GAME_CREATIVE_INV
:
203 gp
.creativeScroll
+= amount
;
204 if (gp
.creativeScroll
< 0) gp
.creativeScroll
= 0;
205 if (gp
.creativeScroll
> gp
.maxCreativeScroll
)
206 gp
.creativeScroll
= gp
.maxCreativeScroll
;