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 GameInput
{
10 private GameProc gameProc
;
12 public GameInput(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 gameProc
.isKeyDown
= true;
48 gameProc
.keyDownCode
= keycode
;
49 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
50 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
52 } else switch (keycode
) {
53 case Input
.Keys
.ALT_LEFT
:
56 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
60 case Input
.Keys
.SPACE
:
61 if (gameProc
.player
.canJump
) {
62 gameProc
.player
.moveY
.add(0, -7);
63 } else if (!gameProc
.player
.flyMode
) {
64 gameProc
.player
.flyMode
= true;
65 gameProc
.player
.moveY
.setZero();
67 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
71 case Input
.Keys
.CONTROL_LEFT
:
72 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
76 if (CaveGame
.STATE
== GameState
.GAME_PLAY
) CaveGame
.STATE
= GameState
.GAME_CREATIVE_INV
;
77 else CaveGame
.STATE
= GameState
.GAME_PLAY
;
81 CaveGame
.STATE
= GameState
.RESTART
;
84 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
85 CaveGame
.STATE
= GameState
.GOTO_MENU
;
90 public void keyUp(int keycode
) {
92 case Input
.Keys
.A
: case Input
.Keys
.D
:
93 gameProc
.player
.moveX
.x
= 0;
96 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
97 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
102 public void mouseMoved(int screenX
, int screenY
) {
105 public void touchDown(int screenX
, int screenY
, int button
) {
106 if (CaveGame
.STATE
== GameState
.GAME_CREATIVE_INV
&&
107 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
108 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
109 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
110 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
111 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
112 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
113 int item
= ix
+ iy
* 8;
114 if (item
>= 0 && item
< Items
.BLOCKS
.size
) {
115 for (int i
= 8; i
> 0; i
--) {
116 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
118 gameProc
.player
.inventory
[0] = item
;
120 } else if (CaveGame
.STATE
== GameState
.GAME_CREATIVE_INV
) {
121 CaveGame
.STATE
= GameState
.GAME_PLAY
;
123 gameProc
.touchDownX
= screenX
;
124 gameProc
.touchDownY
= screenY
;
125 gameProc
.touchDownTime
= TimeUtils
.millis();
126 gameProc
.isTouchDown
= true;
127 gameProc
.touchDownButton
= button
;
131 public void touchUp(int screenX
, int screenY
, int button
) {
132 if (gameProc
.isKeyDown
) {
133 keyUp(gameProc
.keyDownCode
);
134 gameProc
.isKeyDown
= false;
136 if (gameProc
.isTouchDown
) {
137 if (button
== Input
.Buttons
.RIGHT
){
138 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
139 gameProc
.player
.inventory
[gameProc
.invSlot
]);
140 } else if (button
== Input
.Buttons
.LEFT
) {
141 if (screenY
<Assets
.invBar
.getRegionHeight() &&
142 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
143 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
144 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
145 } else if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
146 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
147 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
148 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
153 gameProc
.isTouchDown
= false;
156 public void touchDragged(int screenX
, int screenY
) {
159 public void scrolled(int amount
) {
160 gameProc
.invSlot
+= amount
;
161 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
162 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;