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
.MathUtils
;
7 import com
.badlogic
.gdx
.utils
.TimeUtils
;
8 import com
.google
.common
.collect
.Range
;
9 import ru
.deadsoftware
.cavedroid
.MainConfig
;
10 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
11 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
12 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Pig
;
13 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
14 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
15 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
16 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
17 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
19 import javax
.inject
.Inject
;
21 import static ru
.deadsoftware
.cavedroid
.game
.GameItems
.*;
24 public class GameInput
{
26 private final MainConfig mMainConfig
;
27 private final GameWorld mGameWorld
;
28 private final DropController mDropController
;
29 private final MobsController mMobsController
;
30 private final Player mPlayer
;
32 private ControlMode mControlMode
;
34 private boolean mKeyDown
;
35 private boolean mTouchedDown
;
36 private boolean mDragging
;
38 private int mKeyDownCode
;
39 private int mTouchDownBtn
;
40 private float mTouchDownX
;
41 private float mTouchDownY
;
42 private long mTouchDownTime
;
46 private int mCreativeScroll
;
47 private int mBlockDamage
;
50 public GameInput(MainConfig mainConfig
,
52 DropController dropController
,
53 MobsController mobsController
) {
54 mMainConfig
= mainConfig
;
55 mGameWorld
= gameWorld
;
56 mDropController
= dropController
;
57 mMobsController
= mobsController
;
59 mPlayer
= mMobsController
.getPlayer();
61 mControlMode
= mMainConfig
.isTouch() ? ControlMode
.WALK
: ControlMode
.CURSOR
;
64 private boolean checkSwim() {
65 return GameItems
.isFluid(mGameWorld
.getForeMap(mPlayer
.getMapX(), mPlayer
.getLowerMapY()));
68 private void goUpwards() {
71 } else if (mPlayer
.canJump()) {
73 } else if (!mPlayer
.isFlyMode() && mPlayer
.gameMode
== 1) {
74 mPlayer
.setFlyMode(true);
75 mPlayer
.getVelocity().y
= 0;
76 } else if (mPlayer
.isFlyMode()) {
77 mPlayer
.getVelocity().y
= -mPlayer
.getSpeed();
81 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
82 private boolean insideCreativeInv(float screenX
, float screenY
) {
83 TextureRegion creative
= Assets
.textureRegions
.get("creative");
84 return (screenX
> mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 &&
85 screenX
< mMainConfig
.getWidth() / 2 + creative
.getRegionWidth() / 2 &&
86 screenY
> mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 &&
87 screenY
< mMainConfig
.getHeight() / 2 + creative
.getRegionHeight() / 2);
90 private void wasdPressed(int keycode
) {
91 if (mControlMode
== ControlMode
.WALK
|| !mMainConfig
.isTouch()) {
94 mPlayer
.getVelocity().x
= -mPlayer
.getSpeed();
95 mPlayer
.setDir(Mob
.Direction
.LEFT
);
96 if (mMainConfig
.isTouch() && checkSwim()) {
101 mPlayer
.getVelocity().x
= mPlayer
.getSpeed();
102 mPlayer
.setDir(Mob
.Direction
.RIGHT
);
103 if (mMainConfig
.isTouch() && checkSwim()) {
108 case Input
.Keys
.SPACE
:
112 case Input
.Keys
.CONTROL_LEFT
:
113 mPlayer
.getVelocity().y
= mPlayer
.getSpeed();
135 private boolean isNotAutoselectable(int x
, int y
) {
136 return (!mGameWorld
.hasForeAt(x
, y
) || !mGameWorld
.getForeMapBlock(x
, y
).hasCollision());
139 private void checkCursorBounds() {
142 } else if (mCurY
>= mGameWorld
.getHeight()) {
143 mCurY
= mGameWorld
.getHeight() - 1;
146 if (mControlMode
== ControlMode
.CURSOR
) {
147 if (mCurX
* 16 + 8 < mPlayer
.getX() + mPlayer
.getWidth() / 2) {
148 mPlayer
.setDir(Mob
.Direction
.LEFT
);
150 mPlayer
.setDir(Mob
.Direction
.RIGHT
);
155 public void moveCursor(GameRenderer gameRenderer
) {
159 if (mControlMode
== ControlMode
.WALK
&& mMainConfig
.isTouch()) {
160 mCurX
= mPlayer
.getMapX() + (mPlayer
.looksLeft() ?
-1 : 1);
161 mCurY
= mPlayer
.getUpperMapY();
162 for (int i
= 0; i
< 2 && isNotAutoselectable(mCurX
, mCurY
); i
++) {
165 if (isNotAutoselectable(mCurX
, mCurY
)) {
166 mCurX
+= mPlayer
.looksLeft() ?
1 : -1;
168 } else if (!mMainConfig
.isTouch()) {
169 final int tmpX
= (int) (Gdx
.input
.getX() * (mMainConfig
.getWidth() /
170 Gdx
.graphics
.getWidth()) + gameRenderer
.getCamX());
173 final int tmpY
= (int) (Gdx
.input
.getY() * (mMainConfig
.getHeight() /
174 Gdx
.graphics
.getHeight()) + gameRenderer
.getCamY());
181 final double a
= tmpX
- mPlayer
.x
;
182 final double b
= tmpY
- mPlayer
.y
;
184 mPlayer
.headRotation
= (float) Math
.atan(b
/ a
) * MathUtils
.radDeg
;
187 if (pastX
!= mCurX
|| pastY
!= mCurY
) {
194 private void useItem(int x
, int y
, int id
, boolean bg
) {
195 String key
= getItem(id
).isBlock() ?
getBlockKey(id
) : getItemKey(id
);
197 if (getItem(id
).isBlock()) {
199 mGameWorld
.placeToForeground(x
, y
, getBlockIdByItemId(id
));
201 mGameWorld
.placeToBackground(x
, y
, getBlockIdByItemId(id
));
206 mGameWorld
.placeToForeground(x
, y
, getBlockId("water"));
207 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
210 mGameWorld
.placeToForeground(x
, y
, getBlockId("lava"));
211 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
218 private void pressLMB() {
219 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
) &&
220 ((mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp() >= 0) ||
221 (!mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.hasBackAt(mCurX
, mCurY
) &&
222 mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp() >= 0))) {
223 if (mPlayer
.gameMode
== 0) {
225 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
226 if (mBlockDamage
>= mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp()) {
227 mGameWorld
.destroyForeMap(mCurX
, mCurY
);
230 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
231 if (mBlockDamage
>= mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp()) {
232 mGameWorld
.destroyBackMap(mCurX
, mCurY
);
237 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
238 mGameWorld
.placeToForeground(mCurX
, mCurY
, 0);
239 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
240 mGameWorld
.placeToBackground(mCurX
, mCurY
, 0);
242 mTouchedDown
= false;
247 private boolean insideHotbar(float x
, float y
) {
248 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
249 return y
< hotbar
.getRegionHeight() &&
250 Range
.open(mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2,
251 mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2).contains(x
);
254 private void holdMB() {
255 if (mTouchDownBtn
== Input
.Buttons
.RIGHT
) {
256 useItem(mCurX
, mCurY
, mPlayer
.inventory
[mPlayer
.slot
], true);
257 mTouchedDown
= false;
259 if (insideHotbar(mTouchDownX
, mTouchDownY
)) {
260 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
261 mTouchedDown
= false;
266 public void keyDown(int keycode
) {
268 mKeyDownCode
= keycode
;
274 case Input
.Keys
.SPACE
:
275 case Input
.Keys
.CONTROL_LEFT
:
276 wasdPressed(keycode
);
279 case Input
.Keys
.ALT_LEFT
:
280 if (mMainConfig
.isTouch()) {
281 mControlMode
= mControlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
286 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
287 switch (mPlayer
.gameMode
) {
292 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
296 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
301 mMobsController
.addMob(Pig
.class, mCurX
* 16, mCurY
* 16);
305 mGameWorld
.placeToForeground(mCurX
, mCurY
, 8);
308 case Input
.Keys
.ESCAPE
:
309 case Input
.Keys
.BACK
:
310 GameSaver
.save(mMainConfig
, mDropController
, mMobsController
, mGameWorld
);
311 mMainConfig
.getCaveGame().quitGame();
315 mMainConfig
.setShowInfo(!mMainConfig
.isShowInfo());
319 mMainConfig
.setShowMap(!mMainConfig
.isShowMap());
324 public void keyUp(int keycode
) {
328 mPlayer
.getVelocity().x
= 0;
329 if (mMainConfig
.isTouch() && mPlayer
.swim
) {
330 mPlayer
.swim
= false;
336 case Input
.Keys
.SPACE
:
337 case Input
.Keys
.CONTROL_LEFT
:
338 if (mPlayer
.isFlyMode()) {
339 mPlayer
.getVelocity().y
= 0;
342 mPlayer
.swim
= false;
348 public void touchDown(float touchX
, float touchY
, int button
) {
349 mTouchDownTime
= TimeUtils
.millis();
351 mTouchDownBtn
= button
;
352 mTouchDownX
= touchX
;
353 mTouchDownY
= touchY
;
356 public void touchUp(float screenX
, float screenY
, int button
) {
362 if (mMainConfig
.isTouch() && mKeyDown
) {
366 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
367 TextureRegion creative
= Assets
.textureRegions
.get("creative");
369 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && insideCreativeInv(screenX
, screenY
)) {
370 int ix
= (int) (screenX
- (mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
371 int iy
= (int) (screenY
- (mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
372 int item
= mCreativeScroll
* 8 + (ix
+ iy
* 8);
373 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) {
376 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
377 System
.arraycopy(mPlayer
.inventory
, 0, mPlayer
.inventory
, 1, 8);
378 mPlayer
.inventory
[0] = item
;
380 } else if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
)) {
381 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
382 } else if (screenY
< hotbar
.getRegionHeight() &&
383 screenX
> mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
384 screenX
< mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
385 mPlayer
.slot
= (int) ((screenX
- (mMainConfig
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
386 } else if (button
== Input
.Buttons
.RIGHT
) {
387 useItem(mCurX
, mCurY
,
388 mPlayer
.inventory
[mPlayer
.slot
], false);
389 } else if (button
== Input
.Buttons
.LEFT
) {
393 mTouchedDown
= false;
396 public void touchDragged(float screenX
, float screenY
) {
397 if (Math
.abs(screenX
- mTouchDownX
) < 16 && Math
.abs(screenY
- mTouchDownY
) < 16) {
402 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && Math
.abs(screenY
- mTouchDownY
) > 16) {
403 if (insideCreativeInv(screenX
, screenY
)) {
404 mCreativeScroll
-= (screenY
- mTouchDownY
) / 16;
405 mTouchDownX
= screenX
;
406 mTouchDownY
= screenY
;
407 if (mCreativeScroll
< 0) {
410 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
411 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
417 public void scrolled(float amountX
, float amountY
) {
418 switch (mMainConfig
.getGameUiWindow()) {
420 mPlayer
.slot
+= (int) amountY
;
421 if (mPlayer
.slot
< 0) {
424 if (mPlayer
.slot
> 8) {
428 case CREATIVE_INVENTORY
:
429 mCreativeScroll
+= (int) amountY
;
430 if (mCreativeScroll
< 0) {
433 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
434 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
440 public int getKeyDownCode() {
444 public boolean isKeyDown() {
448 int getBlockDamage() {
460 int getCreativeScroll() {
461 return mCreativeScroll
;
464 public ControlMode
getControlMode() {
468 public void setControlMode(ControlMode controlMode
) {
469 mControlMode
= controlMode
;
473 if (mTouchedDown
&& mTouchDownBtn
== Input
.Buttons
.LEFT
) {
476 if (mTouchedDown
&& TimeUtils
.timeSinceMillis(mTouchDownTime
) > 500) {