1 package ru
.deadsoftware
.cavedroid
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.Input
;
5 import com
.badlogic
.gdx
.graphics
.g2d
.TextureRegion
;
6 import com
.badlogic
.gdx
.utils
.TimeUtils
;
7 import com
.google
.common
.collect
.Range
;
8 import ru
.deadsoftware
.cavedroid
.MainConfig
;
9 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
10 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
11 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Pig
;
12 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
13 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
14 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
15 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
17 import javax
.inject
.Inject
;
19 import static ru
.deadsoftware
.cavedroid
.game
.GameItems
.*;
22 public class GameInput
{
24 private final MainConfig mMainConfig
;
25 private final GameWorld mGameWorld
;
26 private final DropController mDropController
;
27 private final MobsController mMobsController
;
28 private final Player mPlayer
;
30 private ControlMode mControlMode
;
32 private boolean mKeyDown
;
33 private boolean mTouchedDown
;
34 private boolean mDragging
;
36 private int mKeyDownCode
;
37 private int mTouchDownBtn
;
38 private float mTouchDownX
;
39 private float mTouchDownY
;
40 private long mTouchDownTime
;
44 private int mCreativeScroll
;
45 private int mBlockDamage
;
48 public GameInput(MainConfig mainConfig
,
50 DropController dropController
,
51 MobsController mobsController
) {
52 mMainConfig
= mainConfig
;
53 mGameWorld
= gameWorld
;
54 mDropController
= dropController
;
55 mMobsController
= mobsController
;
57 mPlayer
= mMobsController
.getPlayer();
59 mControlMode
= mMainConfig
.isTouch() ? ControlMode
.WALK
: ControlMode
.CURSOR
;
62 private boolean checkSwim() {
63 return GameItems
.isFluid(mGameWorld
.getForeMap(mPlayer
.getMapX(), mPlayer
.getLowerMapY()));
66 private void goUpwards() {
69 } else if (mPlayer
.canJump()) {
70 mPlayer
.getMove().add(0, -7);
71 } else if (!mPlayer
.isFlyMode() && mPlayer
.gameMode
== 1) {
72 mPlayer
.setFlyMode(true);
73 mPlayer
.getMove().y
= 0;
74 } else if (mPlayer
.isFlyMode()) {
75 mPlayer
.getMove().y
= -GamePhysics
.PL_SPEED
;
79 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
80 private boolean insideCreativeInv(float screenX
, float screenY
) {
81 TextureRegion creative
= Assets
.textureRegions
.get("creative");
82 return (screenX
> mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 &&
83 screenX
< mMainConfig
.getWidth() / 2 + creative
.getRegionWidth() / 2 &&
84 screenY
> mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 &&
85 screenY
< mMainConfig
.getHeight() / 2 + creative
.getRegionHeight() / 2);
88 private void wasdPressed(int keycode
) {
89 if (mControlMode
== ControlMode
.WALK
|| !mMainConfig
.isTouch()) {
92 mPlayer
.getMove().x
= -GamePhysics
.PL_SPEED
;
93 mPlayer
.setDir(Mob
.Direction
.LEFT
);
94 if (mMainConfig
.isTouch() && checkSwim()) {
99 mPlayer
.getMove().x
= GamePhysics
.PL_SPEED
;
100 mPlayer
.setDir(Mob
.Direction
.RIGHT
);
101 if (mMainConfig
.isTouch() && checkSwim()) {
106 case Input
.Keys
.SPACE
:
110 case Input
.Keys
.CONTROL_LEFT
:
111 mPlayer
.getMove().y
= GamePhysics
.PL_SPEED
;
133 private boolean isNotAutoselectable(int x
, int y
) {
134 return (!mGameWorld
.hasForeAt(x
, y
) || !mGameWorld
.getForeMapBlock(x
, y
).hasCollision());
137 private void checkCursorBounds() {
140 } else if (mCurY
>= mGameWorld
.getHeight()) {
141 mCurY
= mGameWorld
.getHeight() - 1;
144 if (mControlMode
== ControlMode
.CURSOR
) {
145 if (mCurX
* 16 + 8 < mPlayer
.getX() + mPlayer
.getWidth() / 2) {
146 mPlayer
.setDir(Mob
.Direction
.LEFT
);
148 mPlayer
.setDir(Mob
.Direction
.RIGHT
);
153 public void moveCursor(GameRenderer gameRenderer
) {
157 if (mControlMode
== ControlMode
.WALK
&& mMainConfig
.isTouch()) {
158 mCurX
= mPlayer
.getMapX() + (mPlayer
.looksLeft() ?
-1 : 1);
159 mCurY
= mPlayer
.getUpperMapY();
160 for (int i
= 0; i
< 2 && isNotAutoselectable(mCurX
, mCurY
); i
++) {
163 if (isNotAutoselectable(mCurX
, mCurY
)) {
164 mCurX
+= mPlayer
.looksLeft() ?
1 : -1;
166 } else if (!mMainConfig
.isTouch()) {
167 mCurX
= (int) (Gdx
.input
.getX() * (mMainConfig
.getWidth() /
168 Gdx
.graphics
.getWidth()) + gameRenderer
.getCamX()) / 16;
170 mCurY
= (int) (Gdx
.input
.getY() * (mMainConfig
.getHeight() /
171 Gdx
.graphics
.getHeight()) + gameRenderer
.getCamY()) / 16;
177 if (pastX
!= mCurX
|| pastY
!= mCurY
) {
184 private void useItem(int x
, int y
, int id
, boolean bg
) {
185 String key
= getItem(id
).isBlock() ?
getBlockKey(id
) : getItemKey(id
);
187 if (getItem(id
).isBlock()) {
189 mGameWorld
.placeToForeground(x
, y
, getBlockIdByItemId(id
));
191 mGameWorld
.placeToBackground(x
, y
, getBlockIdByItemId(id
));
196 mGameWorld
.placeToForeground(x
, y
, getBlockId("water"));
197 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
200 mGameWorld
.placeToForeground(x
, y
, getBlockId("lava"));
201 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
208 private void pressLMB() {
209 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
) &&
210 ((mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp() >= 0) ||
211 (!mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.hasBackAt(mCurX
, mCurY
) &&
212 mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp() >= 0))) {
213 if (mPlayer
.gameMode
== 0) {
215 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
216 if (mBlockDamage
>= mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp()) {
217 mGameWorld
.destroyForeMap(mCurX
, mCurY
);
220 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
221 if (mBlockDamage
>= mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp()) {
222 mGameWorld
.destroyBackMap(mCurX
, mCurY
);
227 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
228 mGameWorld
.placeToForeground(mCurX
, mCurY
, 0);
229 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
230 mGameWorld
.placeToBackground(mCurX
, mCurY
, 0);
232 mTouchedDown
= false;
237 private boolean insideHotbar(float x
, float y
) {
238 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
239 return y
< hotbar
.getRegionHeight() &&
240 Range
.open(mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2,
241 mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2).contains(x
);
244 private void holdMB() {
245 if (mTouchDownBtn
== Input
.Buttons
.RIGHT
) {
246 useItem(mCurX
, mCurY
, mPlayer
.inventory
[mPlayer
.slot
], true);
247 mTouchedDown
= false;
249 if (insideHotbar(mTouchDownX
, mTouchDownY
)) {
250 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
251 mTouchedDown
= false;
256 public void keyDown(int keycode
) {
258 mKeyDownCode
= keycode
;
264 case Input
.Keys
.SPACE
:
265 case Input
.Keys
.CONTROL_LEFT
:
266 wasdPressed(keycode
);
269 case Input
.Keys
.ALT_LEFT
:
270 if (mMainConfig
.isTouch()) {
271 mControlMode
= mControlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
276 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
277 switch (mPlayer
.gameMode
) {
282 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
286 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
291 mMobsController
.addMob(Pig
.class, mCurX
* 16, mCurY
* 16);
295 mGameWorld
.placeToForeground(mCurX
, mCurY
, 8);
298 case Input
.Keys
.ESCAPE
:
299 case Input
.Keys
.BACK
:
300 GameSaver
.save(mMainConfig
, mDropController
, mMobsController
, mGameWorld
);
301 mMainConfig
.getCaveGame().quitGame();
305 mMainConfig
.setShowInfo(!mMainConfig
.isShowInfo());
309 mMainConfig
.setShowMap(!mMainConfig
.isShowMap());
314 public void keyUp(int keycode
) {
318 mPlayer
.getMove().x
= 0;
319 if (mMainConfig
.isTouch() && mPlayer
.swim
) {
320 mPlayer
.swim
= false;
326 case Input
.Keys
.SPACE
:
327 case Input
.Keys
.CONTROL_LEFT
:
328 if (mPlayer
.isFlyMode()) {
329 mPlayer
.getMove().y
= 0;
332 mPlayer
.swim
= false;
338 public void touchDown(float touchX
, float touchY
, int button
) {
339 mTouchDownTime
= TimeUtils
.millis();
341 mTouchDownBtn
= button
;
342 mTouchDownX
= touchX
;
343 mTouchDownY
= touchY
;
346 public void touchUp(float screenX
, float screenY
, int button
) {
352 if (mMainConfig
.isTouch() && mKeyDown
) {
356 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
357 TextureRegion creative
= Assets
.textureRegions
.get("creative");
359 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && insideCreativeInv(screenX
, screenY
)) {
360 int ix
= (int) (screenX
- (mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
361 int iy
= (int) (screenY
- (mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
362 int item
= mCreativeScroll
* 8 + (ix
+ iy
* 8);
363 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) {
366 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
367 System
.arraycopy(mPlayer
.inventory
, 0, mPlayer
.inventory
, 1, 8);
368 mPlayer
.inventory
[0] = item
;
370 } else if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
)) {
371 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
372 } else if (screenY
< hotbar
.getRegionHeight() &&
373 screenX
> mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
374 screenX
< mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
375 mPlayer
.slot
= (int) ((screenX
- (mMainConfig
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
376 } else if (button
== Input
.Buttons
.RIGHT
) {
377 useItem(mCurX
, mCurY
,
378 mPlayer
.inventory
[mPlayer
.slot
], false);
379 } else if (button
== Input
.Buttons
.LEFT
) {
383 mTouchedDown
= false;
386 public void touchDragged(float screenX
, float screenY
) {
388 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && Math
.abs(screenY
- mTouchDownY
) > 16) {
389 if (insideCreativeInv(screenX
, screenY
)) {
390 mCreativeScroll
-= (screenY
- mTouchDownY
) / 16;
391 mTouchDownX
= screenX
;
392 mTouchDownY
= screenY
;
393 if (mCreativeScroll
< 0) {
396 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
397 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
403 public void scrolled(float amountX
, float amountY
) {
404 switch (mMainConfig
.getGameUiWindow()) {
406 mPlayer
.slot
+= (int) amountY
;
407 if (mPlayer
.slot
< 0) {
410 if (mPlayer
.slot
> 8) {
414 case CREATIVE_INVENTORY
:
415 mCreativeScroll
+= (int) amountY
;
416 if (mCreativeScroll
< 0) {
419 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
420 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
426 public int getKeyDownCode() {
430 public boolean isKeyDown() {
434 int getBlockDamage() {
446 int getCreativeScroll() {
447 return mCreativeScroll
;
450 public ControlMode
getControlMode() {
454 public void setControlMode(ControlMode controlMode
) {
455 mControlMode
= controlMode
;
459 if (mTouchedDown
&& mTouchDownBtn
== Input
.Buttons
.LEFT
) {
462 if (mTouchedDown
&& TimeUtils
.timeSinceMillis(mTouchDownTime
) > 500) {