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
.game
.mobs
.Pig
;
7 import ru
.deadsoftware
.cavecraft
.misc
.AppState
;
8 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
10 public class GameInput
{
12 private GameProc gameProc
;
14 public GameInput(GameProc gameProc
) {
15 this.gameProc
= gameProc
;
18 private void wasdPressed(int keycode
) {
19 if (gameProc
.ctrlMode
==0 || !CaveGame
.TOUCH
) {
22 gameProc
.player
.moveX
.x
= -GamePhysics
.PL_SPEED
;
23 gameProc
.player
.dir
= 0;
26 gameProc
.player
.moveX
.x
= GamePhysics
.PL_SPEED
;
27 gameProc
.player
.dir
= 1;
30 } else if (CaveGame
.TOUCH
){
48 public void keyDown(int keycode
) {
49 gameProc
.isKeyDown
= true;
50 gameProc
.keyDownCode
= keycode
;
51 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
52 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
54 } else switch (keycode
) {
55 case Input
.Keys
.ALT_LEFT
:
58 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
62 case Input
.Keys
.SPACE
:
63 if (gameProc
.player
.canJump
) {
64 gameProc
.player
.moveY
.add(0, -7);
65 } else if (!gameProc
.player
.flyMode
) {
66 gameProc
.player
.flyMode
= true;
67 gameProc
.player
.moveY
.setZero();
69 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
73 case Input
.Keys
.CONTROL_LEFT
:
74 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
78 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
79 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
83 gameProc
.mobs
.add(new Pig(gameProc
.cursorX
*16, gameProc
.cursorY
*16));
86 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
87 CaveGame
.STATE
= AppState
.GOTO_MENU
;
92 public void keyUp(int keycode
) {
94 case Input
.Keys
.A
: case Input
.Keys
.D
:
95 gameProc
.player
.moveX
.x
= 0;
98 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
99 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
104 public void mouseMoved(int screenX
, int screenY
) {
107 public void touchDown(int screenX
, int screenY
, int button
) {
108 gameProc
.touchDownTime
= TimeUtils
.millis();
109 gameProc
.isTouchDown
= true;
110 gameProc
.touchDownButton
= button
;
111 gameProc
.touchDownX
= screenX
;
112 gameProc
.touchDownY
= screenY
;
115 public void touchUp(int screenX
, int screenY
, int button
) {
116 if (gameProc
.isKeyDown
) {
117 keyUp(gameProc
.keyDownCode
);
118 gameProc
.isKeyDown
= false;
120 if (gameProc
.isTouchDown
) {
121 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
122 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
123 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
124 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
125 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
126 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
127 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
128 int item
= gameProc
.creativeScroll
*40+(ix
+ iy
* 8);
129 if (ix
>=8 || ix
<0 || iy
<0 || iy
>=5) item
=-1;
130 if (item
>= 0 && item
< Items
.BLOCKS
.size
) {
131 for (int i
= 8; i
> 0; i
--) {
132 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
134 gameProc
.player
.inventory
[0] = item
;
136 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
137 CaveGame
.STATE
= AppState
.GAME_PLAY
;
138 } else if (screenY
<Assets
.invBar
.getRegionHeight() &&
139 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
140 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
141 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
142 } else if (button
== Input
.Buttons
.RIGHT
){
143 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
144 gameProc
.player
.inventory
[gameProc
.invSlot
]);
145 } else if (button
== Input
.Buttons
.LEFT
) {
146 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
147 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
148 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
149 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
154 gameProc
.isTouchDown
= false;
157 public void touchDragged(int screenX
, int screenY
) {
158 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
-gameProc
.touchDownY
)>16) {
159 if (screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
160 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
161 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
162 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
163 if (screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2-24)
164 gameProc
.creativeScroll
-= (screenY
- gameProc
.touchDownY
) / 16;
165 else gameProc
.creativeScroll
+= (screenY
- gameProc
.touchDownY
) / 16;
166 gameProc
.touchDownX
= screenX
;
167 gameProc
.touchDownY
= screenY
;
168 if (gameProc
.creativeScroll
< 0) gameProc
.creativeScroll
= 0;
169 if (gameProc
.creativeScroll
> gameProc
.maxCreativeScroll
)
170 gameProc
.creativeScroll
= gameProc
.maxCreativeScroll
;
175 public void scrolled(int amount
) {
176 switch (CaveGame
.STATE
) {
178 gameProc
.invSlot
+= amount
;
179 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
180 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;
182 case GAME_CREATIVE_INV
:
183 gameProc
.creativeScroll
+=amount
;
184 if (gameProc
.creativeScroll
<0) gameProc
.creativeScroll
=0;
185 if (gameProc
.creativeScroll
>gameProc
.maxCreativeScroll
)
186 gameProc
.creativeScroll
=gameProc
.maxCreativeScroll
;