2ff2fa3d933e0d2093fd0bfcb24d215c351279d6
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
;
82 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
83 CaveGame
.STATE
= AppState
.GOTO_MENU
;
88 public void keyUp(int keycode
) {
90 case Input
.Keys
.A
: case Input
.Keys
.D
:
91 gameProc
.player
.moveX
.x
= 0;
94 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
95 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
100 public void mouseMoved(int screenX
, int screenY
) {
103 public void touchDown(int screenX
, int screenY
, int button
) {
104 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
105 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
106 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
107 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
108 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
109 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
110 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
111 int item
= ix
+ iy
* 8;
112 if (item
>= 0 && item
< Items
.BLOCKS
.size
) {
113 for (int i
= 8; i
> 0; i
--) {
114 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
116 gameProc
.player
.inventory
[0] = item
;
118 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
119 CaveGame
.STATE
= AppState
.GAME_PLAY
;
121 gameProc
.touchDownX
= screenX
;
122 gameProc
.touchDownY
= screenY
;
123 gameProc
.touchDownTime
= TimeUtils
.millis();
124 gameProc
.isTouchDown
= true;
125 gameProc
.touchDownButton
= button
;
129 public void touchUp(int screenX
, int screenY
, int button
) {
130 if (gameProc
.isKeyDown
) {
131 keyUp(gameProc
.keyDownCode
);
132 gameProc
.isKeyDown
= false;
134 if (gameProc
.isTouchDown
) {
135 if (button
== Input
.Buttons
.RIGHT
){
136 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
137 gameProc
.player
.inventory
[gameProc
.invSlot
]);
138 } else if (button
== Input
.Buttons
.LEFT
) {
139 if (screenY
<Assets
.invBar
.getRegionHeight() &&
140 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
141 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
142 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
143 } else if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
144 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
145 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
146 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
151 gameProc
.isTouchDown
= false;
154 public void touchDragged(int screenX
, int screenY
) {
157 public void scrolled(int amount
) {
158 gameProc
.invSlot
+= amount
;
159 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
160 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;