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
.math
.Intersector
;
7 import com
.badlogic
.gdx
.math
.MathUtils
;
8 import com
.badlogic
.gdx
.utils
.TimeUtils
;
9 import com
.google
.common
.collect
.Range
;
10 import ru
.deadsoftware
.cavedroid
.MainConfig
;
11 import ru
.deadsoftware
.cavedroid
.game
.actions
.CommonBlockActionUtilsKt
;
12 import ru
.deadsoftware
.cavedroid
.game
.actions
.placeblock
.IPlaceBlockAction
;
13 import ru
.deadsoftware
.cavedroid
.game
.actions
.useitem
.IUseItemAction
;
14 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
15 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
16 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Pig
;
17 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
18 import ru
.deadsoftware
.cavedroid
.game
.model
.item
.Item
;
19 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
20 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
21 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
22 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
24 import javax
.inject
.Inject
;
28 import static ru
.deadsoftware
.cavedroid
.game
.GameItems
.*;
31 public class GameInput
{
33 private static final String TAG
= "GameInput";
35 private final MainConfig mMainConfig
;
36 private final GameWorld mGameWorld
;
37 private final DropController mDropController
;
38 private final MobsController mMobsController
;
39 private final GameItemsHolder mGameItemsHolder
;
40 private final Map
<String
, IUseItemAction
> mUseItemActionMap
;
41 private final Map
<String
, IPlaceBlockAction
> mPlaceBlockActionMap
;
43 private final Player mPlayer
;
45 private ControlMode mControlMode
;
47 private boolean mKeyDown
;
48 private boolean mTouchedDown
;
49 private boolean mDragging
;
51 private int mKeyDownCode
;
52 private int mTouchDownBtn
;
53 private float mTouchDownX
;
54 private float mTouchDownY
;
55 private long mTouchDownTime
;
59 private int mCreativeScroll
;
60 private int mBlockDamage
;
63 public GameInput(MainConfig mainConfig
,
65 DropController dropController
,
66 MobsController mobsController
,
67 GameItemsHolder gameItemsHolder
,
68 Map
<String
, IUseItemAction
> useItemActionMap
,
69 Map
<String
, IPlaceBlockAction
> placeBlockActionMap
) {
70 mMainConfig
= mainConfig
;
71 mGameWorld
= gameWorld
;
72 mDropController
= dropController
;
73 mMobsController
= mobsController
;
74 mGameItemsHolder
= gameItemsHolder
;
75 mUseItemActionMap
= useItemActionMap
;
76 mPlaceBlockActionMap
= placeBlockActionMap
;
78 mPlayer
= mMobsController
.getPlayer();
80 mControlMode
= mMainConfig
.isTouch() ? ControlMode
.WALK
: ControlMode
.CURSOR
;
83 private boolean checkSwim() {
84 return mGameWorld
.getForeMap(mPlayer
.getMapX(), mPlayer
.getLowerMapY()).isFluid();
87 private void goUpwards() {
90 } else if (mPlayer
.canJump()) {
92 } else if (!mPlayer
.isFlyMode() && mPlayer
.gameMode
== 1) {
93 mPlayer
.setFlyMode(true);
94 mPlayer
.getVelocity().y
= 0;
95 } else if (mPlayer
.isFlyMode()) {
96 mPlayer
.getVelocity().y
= -mPlayer
.getSpeed();
100 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
101 private boolean insideCreativeInv(float screenX
, float screenY
) {
102 TextureRegion creative
= Assets
.textureRegions
.get("creative");
103 return (screenX
> mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 &&
104 screenX
< mMainConfig
.getWidth() / 2 + creative
.getRegionWidth() / 2 &&
105 screenY
> mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 &&
106 screenY
< mMainConfig
.getHeight() / 2 + creative
.getRegionHeight() / 2);
109 private void wasdPressed(int keycode
) {
110 if (mControlMode
== ControlMode
.WALK
|| !mMainConfig
.isTouch()) {
113 mPlayer
.getVelocity().x
= -mPlayer
.getSpeed();
114 mPlayer
.setDir(Mob
.Direction
.LEFT
);
115 if (mMainConfig
.isTouch() && checkSwim()) {
120 mPlayer
.getVelocity().x
= mPlayer
.getSpeed();
121 mPlayer
.setDir(Mob
.Direction
.RIGHT
);
122 if (mMainConfig
.isTouch() && checkSwim()) {
127 case Input
.Keys
.SPACE
:
131 case Input
.Keys
.CONTROL_LEFT
:
132 mPlayer
.getVelocity().y
= mPlayer
.getSpeed();
154 private boolean isNotAutoselectable(int x
, int y
) {
155 return (!mGameWorld
.hasForeAt(x
, y
) || !mGameWorld
.getForeMap(x
, y
).hasCollision());
158 private void checkCursorBounds() {
161 } else if (mCurY
>= mGameWorld
.getHeight()) {
162 mCurY
= mGameWorld
.getHeight() - 1;
165 if (mControlMode
== ControlMode
.CURSOR
) {
166 if (mCurX
* 16 + 8 < mPlayer
.getX() + mPlayer
.getWidth() / 2) {
167 mPlayer
.setDir(Mob
.Direction
.LEFT
);
169 mPlayer
.setDir(Mob
.Direction
.RIGHT
);
174 public void moveCursor(GameRenderer gameRenderer
) {
178 if (mControlMode
== ControlMode
.WALK
&& mMainConfig
.isTouch()) {
179 mCurX
= mPlayer
.getMapX() + (mPlayer
.looksLeft() ?
-1 : 1);
180 mCurY
= mPlayer
.getUpperMapY();
181 for (int i
= 0; i
< 2 && isNotAutoselectable(mCurX
, mCurY
); i
++) {
184 if (isNotAutoselectable(mCurX
, mCurY
)) {
185 mCurX
+= mPlayer
.looksLeft() ?
1 : -1;
187 } else if (!mMainConfig
.isTouch()) {
188 final int tmpX
= (int) (Gdx
.input
.getX() * (mMainConfig
.getWidth() /
189 Gdx
.graphics
.getWidth()) + gameRenderer
.getCamX());
192 final int tmpY
= (int) (Gdx
.input
.getY() * (mMainConfig
.getHeight() /
193 Gdx
.graphics
.getHeight()) + gameRenderer
.getCamY());
200 final double a
= tmpX
- mPlayer
.x
;
201 final double b
= tmpY
- mPlayer
.y
;
203 mPlayer
.headRotation
= (float) Math
.atan(b
/ a
) * MathUtils
.radDeg
;
206 if (pastX
!= mCurX
|| pastY
!= mCurY
) {
213 private void useItem(int x
, int y
, int id
, boolean bg
) {
214 mPlayer
.startHitting();
217 final Item item
= getItem(id
);
219 if (item
instanceof Item
.Placeable
) {
221 CommonBlockActionUtilsKt
.placeToForegroundAction(mPlaceBlockActionMap
, (Item
.Placeable
) item
, x
, y
);
223 CommonBlockActionUtilsKt
.placeToBackgroundAction(mPlaceBlockActionMap
, (Item
.Placeable
) item
, x
, y
);
225 } else if (item
instanceof Item
.Usable
) {
226 final String actionKey
= ((Item
.Usable
)item
).getUseActionKey();
227 final IUseItemAction useItemAction
= mUseItemActionMap
.get(actionKey
);
229 if (useItemAction
!= null) {
230 useItemAction
.perform((Item
.Usable
) item
, x
, y
);
232 Gdx
.app
.error(TAG
, "use item action " + actionKey
+ " not found");
238 private void hitMobs() {
239 final Player player
= mMobsController
.getPlayer();
240 mMobsController
.getMobs().forEach((mob
) -> {
241 if (Intersector
.overlaps(mob
, player
)) {
248 private void pressLMB() {
249 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
250 mPlayer
.startHitting();
252 if ((mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.getForeMap(mCurX
, mCurY
).getHp() >= 0) ||
253 (!mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.hasBackAt(mCurX
, mCurY
) &&
254 mGameWorld
.getBackMap(mCurX
, mCurY
).getHp() >= 0)) {
255 if (mPlayer
.gameMode
== 0) {
257 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
258 if (mBlockDamage
>= mGameWorld
.getForeMap(mCurX
, mCurY
).getHp()) {
259 mGameWorld
.destroyForeMap(mCurX
, mCurY
);
262 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
263 if (mBlockDamage
>= mGameWorld
.getBackMap(mCurX
, mCurY
).getHp()) {
264 mGameWorld
.destroyBackMap(mCurX
, mCurY
);
269 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
270 mGameWorld
.placeToForeground(mCurX
, mCurY
, mGameItemsHolder
.getFallbackBlock());
271 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
272 mGameWorld
.placeToBackground(mCurX
, mCurY
, mGameItemsHolder
.getFallbackBlock());
274 mTouchedDown
= false;
278 mTouchedDown
= false;
283 private boolean insideHotbar(float x
, float y
) {
284 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
285 return y
< hotbar
.getRegionHeight() &&
286 Range
.open(mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2,
287 mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2).contains(x
);
290 private void holdMB() {
291 if (mTouchDownBtn
== Input
.Buttons
.RIGHT
) {
292 useItem(mCurX
, mCurY
, mPlayer
.inventory
[mPlayer
.slot
], true);
293 mTouchedDown
= false;
295 if (insideHotbar(mTouchDownX
, mTouchDownY
)) {
296 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
297 mTouchedDown
= false;
302 public void keyDown(int keycode
) {
304 mKeyDownCode
= keycode
;
310 case Input
.Keys
.SPACE
:
311 case Input
.Keys
.CONTROL_LEFT
:
312 wasdPressed(keycode
);
315 case Input
.Keys
.ALT_LEFT
:
316 if (mMainConfig
.isTouch()) {
317 mControlMode
= mControlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
322 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
323 switch (mPlayer
.gameMode
) {
328 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
332 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
337 final Mob pig
= new Pig(mCurX
* 16, mCurY
* 16);
338 pig
.attachToController(mMobsController
);
341 case Input
.Keys
.GRAVE
:
342 mMobsController
.getPlayer().gameMode
= (mMobsController
.getPlayer().gameMode
+ 1) % 2;
345 case Input
.Keys
.ESCAPE
:
346 case Input
.Keys
.BACK
:
347 GameSaver
.save(mMainConfig
, mDropController
, mMobsController
, mGameWorld
);
348 mMainConfig
.getCaveGame().quitGame();
352 mMainConfig
.setShowInfo(!mMainConfig
.isShowInfo());
356 mMainConfig
.setShowMap(!mMainConfig
.isShowMap());
361 public void keyUp(int keycode
) {
365 mPlayer
.getVelocity().x
= 0;
366 if (mMainConfig
.isTouch() && mPlayer
.swim
) {
367 mPlayer
.swim
= false;
373 case Input
.Keys
.SPACE
:
374 case Input
.Keys
.CONTROL_LEFT
:
375 if (mPlayer
.isFlyMode()) {
376 mPlayer
.getVelocity().y
= 0;
379 mPlayer
.swim
= false;
385 public void touchDown(float touchX
, float touchY
, int button
) {
386 mTouchDownTime
= TimeUtils
.millis();
388 mTouchDownBtn
= button
;
389 mTouchDownX
= touchX
;
390 mTouchDownY
= touchY
;
393 public void touchUp(float screenX
, float screenY
, int button
) {
399 if (mMainConfig
.isTouch() && mKeyDown
) {
403 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
404 TextureRegion creative
= Assets
.textureRegions
.get("creative");
406 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && insideCreativeInv(screenX
, screenY
)) {
407 int ix
= (int) (screenX
- (mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
408 int iy
= (int) (screenY
- (mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
409 int item
= mCreativeScroll
* 8 + (ix
+ iy
* 8);
410 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) {
413 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
414 System
.arraycopy(mPlayer
.inventory
, 0, mPlayer
.inventory
, 1, 8);
415 mPlayer
.inventory
[0] = item
;
417 } else if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
)) {
418 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
419 } else if (screenY
< hotbar
.getRegionHeight() &&
420 screenX
> mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
421 screenX
< mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
422 mPlayer
.slot
= (int) ((screenX
- (mMainConfig
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
423 } else if (button
== Input
.Buttons
.RIGHT
) {
424 useItem(mCurX
, mCurY
,
425 mPlayer
.inventory
[mPlayer
.slot
], false);
426 } else if (button
== Input
.Buttons
.LEFT
) {
430 mTouchedDown
= false;
433 public void touchDragged(float screenX
, float screenY
) {
434 if (Math
.abs(screenX
- mTouchDownX
) < 16 && Math
.abs(screenY
- mTouchDownY
) < 16) {
439 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && Math
.abs(screenY
- mTouchDownY
) > 16) {
440 if (insideCreativeInv(screenX
, screenY
)) {
441 mCreativeScroll
-= (screenY
- mTouchDownY
) / 16;
442 mTouchDownX
= screenX
;
443 mTouchDownY
= screenY
;
444 if (mCreativeScroll
< 0) {
447 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
448 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
454 public void scrolled(float amountX
, float amountY
) {
455 switch (mMainConfig
.getGameUiWindow()) {
457 mPlayer
.slot
+= (int) amountY
;
458 if (mPlayer
.slot
< 0) {
461 if (mPlayer
.slot
> 8) {
465 case CREATIVE_INVENTORY
:
466 mCreativeScroll
+= (int) amountY
;
467 if (mCreativeScroll
< 0) {
470 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
471 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
477 public int getKeyDownCode() {
481 public boolean isKeyDown() {
485 int getBlockDamage() {
497 int getCreativeScroll() {
498 return mCreativeScroll
;
501 public ControlMode
getControlMode() {
505 public void setControlMode(ControlMode controlMode
) {
506 mControlMode
= controlMode
;
511 mPlayer
.stopHitting();
515 if (mTouchDownBtn
== Input
.Buttons
.LEFT
) {
519 if (mTouchedDown
&& TimeUtils
.timeSinceMillis(mTouchDownTime
) > 500) {