1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.Input
;
5 import com
.badlogic
.gdx
.utils
.TimeUtils
;
6 import ru
.deadsoftware
.cavecraft
.*;
8 public class GameInputHandler
{
10 private GameProc gameProc
;
12 public GameInputHandler(GameProc gameProc
) {
13 this.gameProc
= gameProc
;
16 private void wasdPressed(int keyCode
) {
17 if (gameProc
.ctrlMode
==0 || !CaveGame
.TOUCH
) {
20 gameProc
.player
.moveX
.x
= -GamePhysics
.PL_SPEED
;
21 gameProc
.player
.dir
= 0;
24 gameProc
.player
.moveX
.x
= GamePhysics
.PL_SPEED
;
25 gameProc
.player
.dir
= 1;
28 } else if (CaveGame
.TOUCH
){
46 public void keyDown(int keyCode
) {
47 if (keyCode
== Input
.Keys
.W
|| keyCode
== Input
.Keys
.A
||
48 keyCode
== Input
.Keys
.S
|| keyCode
== Input
.Keys
.D
) {
50 } else switch (keyCode
) {
51 case Input
.Keys
.ALT_LEFT
:
54 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
58 case Input
.Keys
.SPACE
:
59 if (gameProc
.player
.canJump
) {
60 gameProc
.player
.moveY
.add(0, -7);
61 } else if (!gameProc
.player
.flyMode
) {
62 gameProc
.player
.flyMode
= true;
63 gameProc
.player
.moveY
.setZero();
65 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
69 case Input
.Keys
.CONTROL_LEFT
:
70 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
74 if (CaveGame
.STATE
== GameState
.GAME_PLAY
) CaveGame
.STATE
= GameState
.GAME_CREATIVE_INV
;
75 else CaveGame
.STATE
= GameState
.GAME_PLAY
;
80 public void keyUp(int keyCode
) {
82 case Input
.Keys
.A
: case Input
.Keys
.D
:
83 gameProc
.player
.moveX
.x
= 0;
86 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
87 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
92 public void mouseMoved(int screenX
, int screenY
) {
95 public void touchDown(int screenX
, int screenY
, int button
) {
96 if (CaveGame
.STATE
== GameState
.GAME_CREATIVE_INV
&&
97 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
98 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
99 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
100 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
102 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
103 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
104 int item
= ix
+ iy
* 8;
105 for (int i
= 8; i
> 0; i
--) {
106 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
108 if (item
>= 0 && item
< Items
.BLOCKS
.size
) gameProc
.player
.inventory
[0] = item
;
109 } catch (Exception e
) {
110 Gdx
.app
.error("GameInputHandler", e
.toString());
112 } else if (CaveGame
.STATE
== GameState
.GAME_CREATIVE_INV
) {
113 CaveGame
.STATE
= GameState
.GAME_PLAY
;
115 gameProc
.touchDownX
= screenX
;
116 gameProc
.touchDownY
= screenY
;
117 gameProc
.touchDownTime
= TimeUtils
.millis();
118 gameProc
.isTouchDown
= true;
119 gameProc
.touchDownButton
= button
;
123 public void touchUp(int screenX
, int screenY
, int button
) {
124 if (gameProc
.isTouchDown
) {
125 if (button
== Input
.Buttons
.RIGHT
){
126 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
127 gameProc
.player
.inventory
[gameProc
.invSlot
]);
128 } else if (button
== Input
.Buttons
.LEFT
) {
129 if (screenY
<Assets
.invBar
.getRegionHeight() &&
130 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
131 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
132 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
133 } else if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
134 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
135 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
136 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
141 gameProc
.isTouchDown
= false;
144 public void touchDragged(int screenX
, int screenY
) {
147 public void scrolled(int amount
) {
148 gameProc
.invSlot
+= amount
;
149 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
150 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;