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
.FallingSand
;
8 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Pig
;
9 import ru
.deadsoftware
.cavecraft
.misc
.AppState
;
10 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
12 public class GameInput
{
14 private GameProc gameProc
;
16 public GameInput(GameProc gameProc
) {
17 this.gameProc
= gameProc
;
20 private boolean checkSwim() {
21 return (Items
.isFluid(gameProc
.world
.getForeMap((int)(gameProc
.player
.position
.x
+gameProc
.player
.width
/2)/16,
22 (int)(gameProc
.player
.position
.y
+gameProc
.player
.height
/4*3)/16)));
25 private void wasdPressed(int keycode
) {
26 if (gameProc
.ctrlMode
==0 || !CaveGame
.TOUCH
) {
29 gameProc
.player
.moveX
.x
= -GamePhysics
.PL_SPEED
;
30 gameProc
.player
.dir
= 0;
31 if (CaveGame
.TOUCH
&& checkSwim()) gameProc
.swim
= true;
34 gameProc
.player
.moveX
.x
= GamePhysics
.PL_SPEED
;
35 gameProc
.player
.dir
= 1;
36 if (CaveGame
.TOUCH
&& checkSwim()) gameProc
.swim
= true;
39 } else if (CaveGame
.TOUCH
){
54 gameProc
.blockDmg
= 0;
58 public void keyDown(int keycode
) {
59 gameProc
.isKeyDown
= true;
60 gameProc
.keyDownCode
= keycode
;
61 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
62 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
64 } else switch (keycode
) {
65 case Input
.Keys
.ALT_LEFT
:
68 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
72 case Input
.Keys
.SPACE
:
75 } else if (gameProc
.player
.canJump
) {
76 gameProc
.player
.moveY
.add(0, -7);
77 } else if (!gameProc
.player
.flyMode
) {
78 gameProc
.player
.flyMode
= true;
79 gameProc
.player
.moveY
.setZero();
81 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
85 case Input
.Keys
.CONTROL_LEFT
:
86 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
90 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
91 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
95 gameProc
.mobs
.add(new Pig(gameProc
.cursorX
*16, gameProc
.cursorY
*16));
98 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
99 CaveGame
.STATE
= AppState
.GOTO_MENU
;
103 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
108 public void keyUp(int keycode
) {
110 case Input
.Keys
.A
: case Input
.Keys
.D
:
111 gameProc
.player
.moveX
.x
= 0;
112 if (CaveGame
.TOUCH
&& gameProc
.swim
) gameProc
.swim
= false;
115 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
116 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
117 if (gameProc
.swim
) gameProc
.swim
= false;
122 public void mouseMoved(int screenX
, int screenY
) {
125 public void touchDown(int screenX
, int screenY
, int button
) {
126 gameProc
.touchDownTime
= TimeUtils
.millis();
127 gameProc
.isTouchDown
= true;
128 gameProc
.touchDownButton
= button
;
129 gameProc
.touchDownX
= screenX
;
130 gameProc
.touchDownY
= screenY
;
133 public void touchUp(int screenX
, int screenY
, int button
) {
134 if (CaveGame
.TOUCH
&& gameProc
.isKeyDown
) {
135 keyUp(gameProc
.keyDownCode
);
136 gameProc
.isKeyDown
= false;
138 if (gameProc
.isTouchDown
) {
139 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
140 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
141 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
142 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
143 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
144 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
145 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
146 int item
= gameProc
.creativeScroll
*8+(ix
+ iy
* 8);
147 if (ix
>=8 || ix
<0 || iy
<0 || iy
>=5) item
=-1;
148 if (item
>= 0 && item
< Items
.ITEMS
.size()) {
149 for (int i
= 8; i
> 0; i
--) {
150 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
152 gameProc
.player
.inventory
[0] = item
;
154 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
155 CaveGame
.STATE
= AppState
.GAME_PLAY
;
156 } else if (screenY
<Assets
.invBar
.getRegionHeight() &&
157 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
158 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
159 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
160 } else if (button
== Input
.Buttons
.RIGHT
){
161 gameProc
.useItem(gameProc
.cursorX
, gameProc
.cursorY
,
162 gameProc
.player
.inventory
[gameProc
.invSlot
], false);
163 } else if (button
== Input
.Buttons
.LEFT
) {
164 gameProc
.blockDmg
= 0;
167 gameProc
.isTouchDown
= false;
170 public void touchDragged(int screenX
, int screenY
) {
171 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
-gameProc
.touchDownY
)>16) {
172 if (screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
173 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
174 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
175 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
176 gameProc
.creativeScroll
-= (screenY
- gameProc
.touchDownY
) / 16;
177 gameProc
.touchDownX
= screenX
;
178 gameProc
.touchDownY
= screenY
;
179 if (gameProc
.creativeScroll
< 0) gameProc
.creativeScroll
= 0;
180 if (gameProc
.creativeScroll
> gameProc
.maxCreativeScroll
)
181 gameProc
.creativeScroll
= gameProc
.maxCreativeScroll
;
186 public void scrolled(int amount
) {
187 switch (CaveGame
.STATE
) {
189 gameProc
.invSlot
+= amount
;
190 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
191 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;
193 case GAME_CREATIVE_INV
:
194 gameProc
.creativeScroll
+=amount
;
195 if (gameProc
.creativeScroll
<0) gameProc
.creativeScroll
=0;
196 if (gameProc
.creativeScroll
>gameProc
.maxCreativeScroll
)
197 gameProc
.creativeScroll
=gameProc
.maxCreativeScroll
;