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 void wasdPressed(int keycode
) {
21 if (gameProc
.ctrlMode
==0 || !CaveGame
.TOUCH
) {
24 gameProc
.player
.moveX
.x
= -GamePhysics
.PL_SPEED
;
25 gameProc
.player
.dir
= 0;
28 gameProc
.player
.moveX
.x
= GamePhysics
.PL_SPEED
;
29 gameProc
.player
.dir
= 1;
32 } else if (CaveGame
.TOUCH
){
50 public void keyDown(int keycode
) {
51 gameProc
.isKeyDown
= true;
52 gameProc
.keyDownCode
= keycode
;
53 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
54 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
56 } else switch (keycode
) {
57 case Input
.Keys
.ALT_LEFT
:
60 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
64 case Input
.Keys
.SPACE
:
65 if (Items
.isFluid(gameProc
.world
.getForeMap((int)(gameProc
.player
.position
.x
+gameProc
.player
.width
/2)/16,
66 (int)(gameProc
.player
.position
.y
+gameProc
.player
.height
/4*3)/16))) {
68 } else if (gameProc
.player
.canJump
) {
69 gameProc
.player
.moveY
.add(0, -7);
70 } else if (!gameProc
.player
.flyMode
) {
71 gameProc
.player
.flyMode
= true;
72 gameProc
.player
.moveY
.setZero();
74 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
78 case Input
.Keys
.CONTROL_LEFT
:
79 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
83 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
84 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
88 gameProc
.mobs
.add(new Pig(gameProc
.cursorX
*16, gameProc
.cursorY
*16));
91 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
92 CaveGame
.STATE
= AppState
.GOTO_MENU
;
96 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
101 public void keyUp(int keycode
) {
103 case Input
.Keys
.A
: case Input
.Keys
.D
:
104 gameProc
.player
.moveX
.x
= 0;
107 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
108 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
109 gameProc
.swim
= false;
114 public void mouseMoved(int screenX
, int screenY
) {
117 public void touchDown(int screenX
, int screenY
, int button
) {
118 gameProc
.touchDownTime
= TimeUtils
.millis();
119 gameProc
.isTouchDown
= true;
120 gameProc
.touchDownButton
= button
;
121 gameProc
.touchDownX
= screenX
;
122 gameProc
.touchDownY
= screenY
;
125 public void touchUp(int screenX
, int screenY
, int button
) {
126 if (CaveGame
.TOUCH
&& gameProc
.isKeyDown
) {
127 keyUp(gameProc
.keyDownCode
);
128 gameProc
.isKeyDown
= false;
130 if (gameProc
.isTouchDown
) {
131 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
132 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
133 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
134 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
135 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
136 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
137 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
138 int item
= gameProc
.creativeScroll
*8+(ix
+ iy
* 8);
139 if (ix
>=8 || ix
<0 || iy
<0 || iy
>=5) item
=-1;
140 if (item
>= 0 && item
< Items
.BLOCKS
.size
) {
141 for (int i
= 8; i
> 0; i
--) {
142 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
144 gameProc
.player
.inventory
[0] = item
;
146 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
147 CaveGame
.STATE
= AppState
.GAME_PLAY
;
148 } else if (screenY
<Assets
.invBar
.getRegionHeight() &&
149 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
150 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
151 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
152 } else if (button
== Input
.Buttons
.RIGHT
){
153 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
154 gameProc
.player
.inventory
[gameProc
.invSlot
]);
155 } else if (button
== Input
.Buttons
.LEFT
) {
156 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
157 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
158 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
159 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
164 gameProc
.isTouchDown
= false;
167 public void touchDragged(int screenX
, int screenY
) {
168 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
-gameProc
.touchDownY
)>16) {
169 if (screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
170 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
171 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
172 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
173 gameProc
.creativeScroll
-= (screenY
- gameProc
.touchDownY
) / 16;
174 gameProc
.touchDownX
= screenX
;
175 gameProc
.touchDownY
= screenY
;
176 if (gameProc
.creativeScroll
< 0) gameProc
.creativeScroll
= 0;
177 if (gameProc
.creativeScroll
> gameProc
.maxCreativeScroll
)
178 gameProc
.creativeScroll
= gameProc
.maxCreativeScroll
;
183 public void scrolled(int amount
) {
184 switch (CaveGame
.STATE
) {
186 gameProc
.invSlot
+= amount
;
187 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
188 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;
190 case GAME_CREATIVE_INV
:
191 gameProc
.creativeScroll
+=amount
;
192 if (gameProc
.creativeScroll
<0) gameProc
.creativeScroll
=0;
193 if (gameProc
.creativeScroll
>gameProc
.maxCreativeScroll
)
194 gameProc
.creativeScroll
=gameProc
.maxCreativeScroll
;