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
{
13 private GameProc gameProc
;
15 public GameInput(GameProc gameProc
) {
16 this.gameProc
= gameProc
;
19 private void wasdPressed(int keycode
) {
20 if (gameProc
.ctrlMode
==0 || !CaveGame
.TOUCH
) {
23 gameProc
.player
.moveX
.x
= -GamePhysics
.PL_SPEED
;
24 gameProc
.player
.dir
= 0;
27 gameProc
.player
.moveX
.x
= GamePhysics
.PL_SPEED
;
28 gameProc
.player
.dir
= 1;
31 } else if (CaveGame
.TOUCH
){
49 public void keyDown(int keycode
) {
50 gameProc
.isKeyDown
= true;
51 gameProc
.keyDownCode
= keycode
;
52 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
53 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
55 } else switch (keycode
) {
56 case Input
.Keys
.ALT_LEFT
:
59 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
63 case Input
.Keys
.SPACE
:
64 if (gameProc
.player
.canJump
) {
65 gameProc
.player
.moveY
.add(0, -7);
66 } else if (!gameProc
.player
.flyMode
) {
67 gameProc
.player
.flyMode
= true;
68 gameProc
.player
.moveY
.setZero();
70 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
74 case Input
.Keys
.CONTROL_LEFT
:
75 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
79 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
80 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
84 gameProc
.mobs
.add(new Pig(gameProc
.cursorX
*16, gameProc
.cursorY
*16));
87 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
88 CaveGame
.STATE
= AppState
.GOTO_MENU
;
92 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
97 public void keyUp(int keycode
) {
99 case Input
.Keys
.A
: case Input
.Keys
.D
:
100 gameProc
.player
.moveX
.x
= 0;
103 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
104 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
109 public void mouseMoved(int screenX
, int screenY
) {
112 public void touchDown(int screenX
, int screenY
, int button
) {
113 gameProc
.touchDownTime
= TimeUtils
.millis();
114 gameProc
.isTouchDown
= true;
115 gameProc
.touchDownButton
= button
;
116 gameProc
.touchDownX
= screenX
;
117 gameProc
.touchDownY
= screenY
;
120 public void touchUp(int screenX
, int screenY
, int button
) {
121 if (gameProc
.isKeyDown
) {
122 keyUp(gameProc
.keyDownCode
);
123 gameProc
.isKeyDown
= false;
125 if (gameProc
.isTouchDown
) {
126 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
127 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
128 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
129 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
130 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
131 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
132 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
133 int item
= gameProc
.creativeScroll
*8+(ix
+ iy
* 8);
134 if (ix
>=8 || ix
<0 || iy
<0 || iy
>=5) item
=-1;
135 if (item
>= 0 && item
< Items
.BLOCKS
.size
) {
136 for (int i
= 8; i
> 0; i
--) {
137 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
139 gameProc
.player
.inventory
[0] = item
;
141 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
142 CaveGame
.STATE
= AppState
.GAME_PLAY
;
143 } else if (screenY
<Assets
.invBar
.getRegionHeight() &&
144 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
145 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
146 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
147 } else if (button
== Input
.Buttons
.RIGHT
){
148 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
149 gameProc
.player
.inventory
[gameProc
.invSlot
]);
150 } else if (button
== Input
.Buttons
.LEFT
) {
151 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
152 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
153 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
154 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
159 gameProc
.isTouchDown
= false;
162 public void touchDragged(int screenX
, int screenY
) {
163 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
-gameProc
.touchDownY
)>16) {
164 if (screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
165 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
166 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
167 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
168 gameProc
.creativeScroll
-= (screenY
- gameProc
.touchDownY
) / 16;
169 gameProc
.touchDownX
= screenX
;
170 gameProc
.touchDownY
= screenY
;
171 if (gameProc
.creativeScroll
< 0) gameProc
.creativeScroll
= 0;
172 if (gameProc
.creativeScroll
> gameProc
.maxCreativeScroll
)
173 gameProc
.creativeScroll
= gameProc
.maxCreativeScroll
;
178 public void scrolled(int amount
) {
179 switch (CaveGame
.STATE
) {
181 gameProc
.invSlot
+= amount
;
182 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
183 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;
185 case GAME_CREATIVE_INV
:
186 gameProc
.creativeScroll
+=amount
;
187 if (gameProc
.creativeScroll
<0) gameProc
.creativeScroll
=0;
188 if (gameProc
.creativeScroll
>gameProc
.maxCreativeScroll
)
189 gameProc
.creativeScroll
=gameProc
.maxCreativeScroll
;