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
.position
.x
+ gp
.player
.width
/ 2) / 16,
21 (int) (gp
.player
.position
.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
) {
77 gp
.player
.flyMode
= true;
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
) CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
90 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
94 gp
.mobs
.add(new Pig(gp
.curX
* 16, gp
.curY
* 16));
97 case Input
.Keys
.ESCAPE
:
99 CaveGame
.STATE
= AppState
.GOTO_MENU
;
103 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
108 public void keyUp(int keycode
) {
112 gp
.player
.move
.x
= 0;
113 if (CaveGame
.TOUCH
&& gp
.swim
) gp
.swim
= false;
116 case Input
.Keys
.SPACE
:
117 case Input
.Keys
.CONTROL_LEFT
:
118 if (gp
.player
.flyMode
) gp
.player
.move
.y
= 0;
119 if (gp
.swim
) gp
.swim
= false;
124 public void mouseMoved(int screenX
, int screenY
) {
127 public void touchDown(int screenX
, int screenY
, int button
) {
128 gp
.touchDownTime
= TimeUtils
.millis();
129 gp
.isTouchDown
= true;
130 gp
.touchDownBtn
= button
;
131 gp
.touchDownX
= screenX
;
132 gp
.touchDownY
= screenY
;
135 public void touchUp(int screenX
, int screenY
, int button
) {
136 if (CaveGame
.TOUCH
&& gp
.isKeyDown
) {
137 keyUp(gp
.keyDownCode
);
138 gp
.isKeyDown
= false;
140 if (gp
.isTouchDown
) {
141 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
142 screenX
> gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
143 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
144 screenY
> gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
145 screenY
< gp
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2) {
146 int ix
= (int) (screenX
- (gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
147 int iy
= (int) (screenY
- (gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
148 int item
= gp
.creativeScroll
* 8 + (ix
+ iy
* 8);
149 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) item
= -1;
150 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
151 for (int i
= 8; i
> 0; i
--) {
152 gp
.player
.inv
[i
] = gp
.player
.inv
[i
- 1];
154 gp
.player
.inv
[0] = item
;
156 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
157 CaveGame
.STATE
= AppState
.GAME_PLAY
;
158 } else if (screenY
< Assets
.invBar
.getRegionHeight() &&
159 screenX
> gp
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2 &&
160 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.invBar
.getRegionWidth() / 2) {
161 gp
.slot
= (int) ((screenX
- (gp
.renderer
.getWidth() / 2 - Assets
.invBar
.getRegionWidth() / 2)) / 20);
162 } else if (button
== Input
.Buttons
.RIGHT
) {
163 gp
.useItem(gp
.curX
, gp
.curY
,
164 gp
.player
.inv
[gp
.slot
], false);
165 } else if (button
== Input
.Buttons
.LEFT
) {
169 gp
.isTouchDown
= false;
172 public void touchDragged(int screenX
, int screenY
) {
173 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
- gp
.touchDownY
) > 16) {
174 if (screenX
> gp
.renderer
.getWidth() / 2 - Assets
.creativeInv
.getRegionWidth() / 2 &&
175 screenX
< gp
.renderer
.getWidth() / 2 + Assets
.creativeInv
.getRegionWidth() / 2 &&
176 screenY
> gp
.renderer
.getHeight() / 2 - Assets
.creativeInv
.getRegionHeight() / 2 &&
177 screenY
< gp
.renderer
.getHeight() / 2 + Assets
.creativeInv
.getRegionHeight() / 2) {
178 gp
.creativeScroll
-= (screenY
- gp
.touchDownY
) / 16;
179 gp
.touchDownX
= screenX
;
180 gp
.touchDownY
= screenY
;
181 if (gp
.creativeScroll
< 0) gp
.creativeScroll
= 0;
182 if (gp
.creativeScroll
> gp
.maxCreativeScroll
)
183 gp
.creativeScroll
= gp
.maxCreativeScroll
;
188 public void scrolled(int amount
) {
189 switch (CaveGame
.STATE
) {
192 if (gp
.slot
< 0) gp
.slot
= 8;
193 if (gp
.slot
> 8) gp
.slot
= 0;
195 case GAME_CREATIVE_INV
:
196 gp
.creativeScroll
+= amount
;
197 if (gp
.creativeScroll
< 0) gp
.creativeScroll
= 0;
198 if (gp
.creativeScroll
> gp
.maxCreativeScroll
)
199 gp
.creativeScroll
= gp
.maxCreativeScroll
;