1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Input
;
4 import com
.badlogic
.gdx
.utils
.TimeUtils
;
5 import ru
.deadsoftware
.cavecraft
.AppState
;
6 import ru
.deadsoftware
.cavecraft
.Assets
;
7 import ru
.deadsoftware
.cavecraft
.CaveGame
;
8 import ru
.deadsoftware
.cavecraft
.Items
;
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 CaveGame
.STATE
= AppState
.GOTO_NEW_GAME
;
86 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
87 GameSaver
.save(gameProc
);
88 CaveGame
.STATE
= AppState
.GOTO_MENU
;
93 public void keyUp(int keycode
) {
95 case Input
.Keys
.A
: case Input
.Keys
.D
:
96 gameProc
.player
.moveX
.x
= 0;
99 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
100 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
105 public void mouseMoved(int screenX
, int screenY
) {
108 public void touchDown(int screenX
, int screenY
, int button
) {
109 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
110 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
111 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
112 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
113 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
114 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
115 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
116 int item
= ix
+ iy
* 8;
117 if (item
>= 0 && item
< Items
.BLOCKS
.size
) {
118 for (int i
= 8; i
> 0; i
--) {
119 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
121 gameProc
.player
.inventory
[0] = item
;
123 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
124 CaveGame
.STATE
= AppState
.GAME_PLAY
;
126 gameProc
.touchDownX
= screenX
;
127 gameProc
.touchDownY
= screenY
;
128 gameProc
.touchDownTime
= TimeUtils
.millis();
129 gameProc
.isTouchDown
= true;
130 gameProc
.touchDownButton
= button
;
134 public void touchUp(int screenX
, int screenY
, int button
) {
135 if (gameProc
.isKeyDown
) {
136 keyUp(gameProc
.keyDownCode
);
137 gameProc
.isKeyDown
= false;
139 if (gameProc
.isTouchDown
) {
140 if (button
== Input
.Buttons
.RIGHT
){
141 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
142 gameProc
.player
.inventory
[gameProc
.invSlot
]);
143 } else if (button
== Input
.Buttons
.LEFT
) {
144 if (screenY
<Assets
.invBar
.getRegionHeight() &&
145 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
146 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
147 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
148 } else if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
149 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
150 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
151 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
156 gameProc
.isTouchDown
= false;
159 public void touchDragged(int screenX
, int screenY
) {
162 public void scrolled(int amount
) {
163 gameProc
.invSlot
+= amount
;
164 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
165 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;