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 (CaveGame
.TOUCH
&& 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;
33 gameProc
.player
.moveX
.x
= GamePhysics
.PL_SPEED
;
34 gameProc
.player
.dir
= 1;
37 } else if (CaveGame
.TOUCH
){
55 public void keyDown(int keycode
) {
56 gameProc
.isKeyDown
= true;
57 gameProc
.keyDownCode
= keycode
;
58 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
59 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
61 } else switch (keycode
) {
62 case Input
.Keys
.ALT_LEFT
:
65 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
69 case Input
.Keys
.SPACE
:
72 } else if (gameProc
.player
.canJump
) {
73 gameProc
.player
.moveY
.add(0, -7);
74 } else if (!gameProc
.player
.flyMode
) {
75 gameProc
.player
.flyMode
= true;
76 gameProc
.player
.moveY
.setZero();
78 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
82 case Input
.Keys
.CONTROL_LEFT
:
83 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
87 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
88 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
92 gameProc
.mobs
.add(new Pig(gameProc
.cursorX
*16, gameProc
.cursorY
*16));
95 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
96 CaveGame
.STATE
= AppState
.GOTO_MENU
;
100 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
105 public void keyUp(int keycode
) {
107 case Input
.Keys
.A
: case Input
.Keys
.D
:
108 gameProc
.player
.moveX
.x
= 0;
109 if (gameProc
.swim
) gameProc
.swim
= false;
112 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
113 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
114 if (gameProc
.swim
) gameProc
.swim
= false;
119 public void mouseMoved(int screenX
, int screenY
) {
122 public void touchDown(int screenX
, int screenY
, int button
) {
123 gameProc
.touchDownTime
= TimeUtils
.millis();
124 gameProc
.isTouchDown
= true;
125 gameProc
.touchDownButton
= button
;
126 gameProc
.touchDownX
= screenX
;
127 gameProc
.touchDownY
= screenY
;
130 public void touchUp(int screenX
, int screenY
, int button
) {
131 if (CaveGame
.TOUCH
&& gameProc
.isKeyDown
) {
132 keyUp(gameProc
.keyDownCode
);
133 gameProc
.isKeyDown
= false;
135 if (gameProc
.isTouchDown
) {
136 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
137 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
138 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
139 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
140 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
141 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
142 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
143 int item
= gameProc
.creativeScroll
*8+(ix
+ iy
* 8);
144 if (ix
>=8 || ix
<0 || iy
<0 || iy
>=5) item
=-1;
145 if (item
>= 0 && item
< Items
.BLOCKS
.size
) {
146 for (int i
= 8; i
> 0; i
--) {
147 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
149 gameProc
.player
.inventory
[0] = item
;
151 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
152 CaveGame
.STATE
= AppState
.GAME_PLAY
;
153 } else if (screenY
<Assets
.invBar
.getRegionHeight() &&
154 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
155 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
156 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
157 } else if (button
== Input
.Buttons
.RIGHT
){
158 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
159 gameProc
.player
.inventory
[gameProc
.invSlot
]);
160 } else if (button
== Input
.Buttons
.LEFT
) {
161 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
162 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
163 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
164 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
169 gameProc
.isTouchDown
= false;
172 public void touchDragged(int screenX
, int screenY
) {
173 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
-gameProc
.touchDownY
)>16) {
174 if (screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
175 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
176 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
177 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
178 gameProc
.creativeScroll
-= (screenY
- gameProc
.touchDownY
) / 16;
179 gameProc
.touchDownX
= screenX
;
180 gameProc
.touchDownY
= screenY
;
181 if (gameProc
.creativeScroll
< 0) gameProc
.creativeScroll
= 0;
182 if (gameProc
.creativeScroll
> gameProc
.maxCreativeScroll
)
183 gameProc
.creativeScroll
= gameProc
.maxCreativeScroll
;
188 public void scrolled(int amount
) {
189 switch (CaveGame
.STATE
) {
191 gameProc
.invSlot
+= amount
;
192 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
193 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;
195 case GAME_CREATIVE_INV
:
196 gameProc
.creativeScroll
+=amount
;
197 if (gameProc
.creativeScroll
<0) gameProc
.creativeScroll
=0;
198 if (gameProc
.creativeScroll
>gameProc
.maxCreativeScroll
)
199 gameProc
.creativeScroll
=gameProc
.maxCreativeScroll
;