672c4c957c95eff7129ab2a375e41dd342fa8201
1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Input
;
4 import com
.badlogic
.gdx
.utils
.TimeUtils
;
5 import ru
.deadsoftware
.cavecraft
.CaveGame
;
6 import ru
.deadsoftware
.cavecraft
.GameScreen
;
7 import ru
.deadsoftware
.cavecraft
.game
.mobs
.FallingSand
;
8 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Pig
;
9 import ru
.deadsoftware
.cavecraft
.misc
.AppState
;
10 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
12 public class GameInput
{
14 private GameProc gameProc
;
16 public GameInput(GameProc gameProc
) {
17 this.gameProc
= gameProc
;
20 private boolean checkSwim() {
21 return (Items
.isFluid(gameProc
.world
.getForeMap((int)(gameProc
.player
.position
.x
+gameProc
.player
.width
/2)/16,
22 (int)(gameProc
.player
.position
.y
+gameProc
.player
.height
/4*3)/16)));
25 private void wasdPressed(int keycode
) {
26 if (gameProc
.ctrlMode
==0 || !CaveGame
.TOUCH
) {
29 gameProc
.player
.moveX
.x
= -GamePhysics
.PL_SPEED
;
30 gameProc
.player
.dir
= 0;
31 if (CaveGame
.TOUCH
&& checkSwim()) gameProc
.swim
= true;
34 gameProc
.player
.moveX
.x
= GamePhysics
.PL_SPEED
;
35 gameProc
.player
.dir
= 1;
36 if (CaveGame
.TOUCH
&& checkSwim()) gameProc
.swim
= true;
39 } else if (CaveGame
.TOUCH
){
57 public void keyDown(int keycode
) {
58 gameProc
.isKeyDown
= true;
59 gameProc
.keyDownCode
= keycode
;
60 if (keycode
== Input
.Keys
.W
|| keycode
== Input
.Keys
.A
||
61 keycode
== Input
.Keys
.S
|| keycode
== Input
.Keys
.D
) {
63 } else switch (keycode
) {
64 case Input
.Keys
.ALT_LEFT
:
67 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
71 case Input
.Keys
.SPACE
:
74 } else if (gameProc
.player
.canJump
) {
75 gameProc
.player
.moveY
.add(0, -7);
76 } else if (!gameProc
.player
.flyMode
) {
77 gameProc
.player
.flyMode
= true;
78 gameProc
.player
.moveY
.setZero();
80 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
84 case Input
.Keys
.CONTROL_LEFT
:
85 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
89 if (CaveGame
.STATE
== AppState
.GAME_PLAY
) CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
90 else CaveGame
.STATE
= AppState
.GAME_PLAY
;
94 gameProc
.mobs
.add(new Pig(gameProc
.cursorX
*16, gameProc
.cursorY
*16));
97 case Input
.Keys
.ESCAPE
: case Input
.Keys
.BACK
:
98 CaveGame
.STATE
= AppState
.GOTO_MENU
;
102 GameScreen
.SHOW_DEBUG
= !GameScreen
.SHOW_DEBUG
;
107 public void keyUp(int keycode
) {
109 case Input
.Keys
.A
: case Input
.Keys
.D
:
110 gameProc
.player
.moveX
.x
= 0;
111 if (CaveGame
.TOUCH
&& gameProc
.swim
) gameProc
.swim
= false;
114 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
115 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
116 if (gameProc
.swim
) gameProc
.swim
= false;
121 public void mouseMoved(int screenX
, int screenY
) {
124 public void touchDown(int screenX
, int screenY
, int button
) {
125 gameProc
.touchDownTime
= TimeUtils
.millis();
126 gameProc
.isTouchDown
= true;
127 gameProc
.touchDownButton
= button
;
128 gameProc
.touchDownX
= screenX
;
129 gameProc
.touchDownY
= screenY
;
132 public void touchUp(int screenX
, int screenY
, int button
) {
133 if (CaveGame
.TOUCH
&& gameProc
.isKeyDown
) {
134 keyUp(gameProc
.keyDownCode
);
135 gameProc
.isKeyDown
= false;
137 if (gameProc
.isTouchDown
) {
138 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&&
139 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
140 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
141 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
142 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
143 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
144 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
145 int item
= gameProc
.creativeScroll
*8+(ix
+ iy
* 8);
146 if (ix
>=8 || ix
<0 || iy
<0 || iy
>=5) item
=-1;
147 if (item
>= 0 && item
< Items
.ITEMS
.size()) {
148 for (int i
= 8; i
> 0; i
--) {
149 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
151 gameProc
.player
.inventory
[0] = item
;
153 } else if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
) {
154 CaveGame
.STATE
= AppState
.GAME_PLAY
;
155 } else if (screenY
<Assets
.invBar
.getRegionHeight() &&
156 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
157 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
158 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
159 } else if (button
== Input
.Buttons
.RIGHT
){
160 gameProc
.useItem(gameProc
.cursorX
, gameProc
.cursorY
,
161 gameProc
.player
.inventory
[gameProc
.invSlot
], false);
162 } else if (button
== Input
.Buttons
.LEFT
) {
163 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
164 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
165 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
166 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
171 gameProc
.isTouchDown
= false;
174 public void touchDragged(int screenX
, int screenY
) {
175 if (CaveGame
.STATE
== AppState
.GAME_CREATIVE_INV
&& Math
.abs(screenY
-gameProc
.touchDownY
)>16) {
176 if (screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
177 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
178 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
179 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
180 gameProc
.creativeScroll
-= (screenY
- gameProc
.touchDownY
) / 16;
181 gameProc
.touchDownX
= screenX
;
182 gameProc
.touchDownY
= screenY
;
183 if (gameProc
.creativeScroll
< 0) gameProc
.creativeScroll
= 0;
184 if (gameProc
.creativeScroll
> gameProc
.maxCreativeScroll
)
185 gameProc
.creativeScroll
= gameProc
.maxCreativeScroll
;
190 public void scrolled(int amount
) {
191 switch (CaveGame
.STATE
) {
193 gameProc
.invSlot
+= amount
;
194 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
195 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;
197 case GAME_CREATIVE_INV
:
198 gameProc
.creativeScroll
+=amount
;
199 if (gameProc
.creativeScroll
<0) gameProc
.creativeScroll
=0;
200 if (gameProc
.creativeScroll
>gameProc
.maxCreativeScroll
)
201 gameProc
.creativeScroll
=gameProc
.maxCreativeScroll
;