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
;
85 public void keyUp(int keycode
) {
87 case Input
.Keys
.A
: case Input
.Keys
.D
:
88 gameProc
.player
.moveX
.x
= 0;
91 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
92 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
97 public void mouseMoved(int screenX
, int screenY
) {
100 public void touchDown(int screenX
, int screenY
, int button
) {
101 if (CaveGame
.STATE
== GameState
.GAME_CREATIVE_INV
&&
102 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
103 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
104 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
105 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
106 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
107 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
108 int item
= ix
+ iy
* 8;
109 if (item
>= 0 && item
< Items
.BLOCKS
.size
) {
110 for (int i
= 8; i
> 0; i
--) {
111 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
113 gameProc
.player
.inventory
[0] = item
;
115 } else if (CaveGame
.STATE
== GameState
.GAME_CREATIVE_INV
) {
116 CaveGame
.STATE
= GameState
.GAME_PLAY
;
118 gameProc
.touchDownX
= screenX
;
119 gameProc
.touchDownY
= screenY
;
120 gameProc
.touchDownTime
= TimeUtils
.millis();
121 gameProc
.isTouchDown
= true;
122 gameProc
.touchDownButton
= button
;
126 public void touchUp(int screenX
, int screenY
, int button
) {
127 if (gameProc
.isKeyDown
) {
128 keyUp(gameProc
.keyDownCode
);
129 gameProc
.isKeyDown
= false;
131 if (gameProc
.isTouchDown
) {
132 if (button
== Input
.Buttons
.RIGHT
){
133 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
134 gameProc
.player
.inventory
[gameProc
.invSlot
]);
135 } else if (button
== Input
.Buttons
.LEFT
) {
136 if (screenY
<Assets
.invBar
.getRegionHeight() &&
137 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
138 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
139 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
140 } else if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
141 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
142 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
143 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
148 gameProc
.isTouchDown
= false;
151 public void touchDragged(int screenX
, int screenY
) {
154 public void scrolled(int amount
) {
155 gameProc
.invSlot
+= amount
;
156 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
157 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;