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
.mobs
.Mob
;
12 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
13 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Pig
;
14 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
15 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
16 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
17 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
18 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
20 import javax
.inject
.Inject
;
22 import static ru
.deadsoftware
.cavedroid
.game
.GameItems
.*;
25 public class GameInput
{
27 private final MainConfig mMainConfig
;
28 private final GameWorld mGameWorld
;
29 private final DropController mDropController
;
30 private final MobsController mMobsController
;
31 private final Player mPlayer
;
33 private ControlMode mControlMode
;
35 private boolean mKeyDown
;
36 private boolean mTouchedDown
;
37 private boolean mDragging
;
39 private int mKeyDownCode
;
40 private int mTouchDownBtn
;
41 private float mTouchDownX
;
42 private float mTouchDownY
;
43 private long mTouchDownTime
;
47 private int mCreativeScroll
;
48 private int mBlockDamage
;
51 public GameInput(MainConfig mainConfig
,
53 DropController dropController
,
54 MobsController mobsController
) {
55 mMainConfig
= mainConfig
;
56 mGameWorld
= gameWorld
;
57 mDropController
= dropController
;
58 mMobsController
= mobsController
;
60 mPlayer
= mMobsController
.getPlayer();
62 mControlMode
= mMainConfig
.isTouch() ? ControlMode
.WALK
: ControlMode
.CURSOR
;
65 private boolean checkSwim() {
66 return GameItems
.isFluid(mGameWorld
.getForeMap(mPlayer
.getMapX(), mPlayer
.getLowerMapY()));
69 private void goUpwards() {
72 } else if (mPlayer
.canJump()) {
74 } else if (!mPlayer
.isFlyMode() && mPlayer
.gameMode
== 1) {
75 mPlayer
.setFlyMode(true);
76 mPlayer
.getVelocity().y
= 0;
77 } else if (mPlayer
.isFlyMode()) {
78 mPlayer
.getVelocity().y
= -mPlayer
.getSpeed();
82 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
83 private boolean insideCreativeInv(float screenX
, float screenY
) {
84 TextureRegion creative
= Assets
.textureRegions
.get("creative");
85 return (screenX
> mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 &&
86 screenX
< mMainConfig
.getWidth() / 2 + creative
.getRegionWidth() / 2 &&
87 screenY
> mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 &&
88 screenY
< mMainConfig
.getHeight() / 2 + creative
.getRegionHeight() / 2);
91 private void wasdPressed(int keycode
) {
92 if (mControlMode
== ControlMode
.WALK
|| !mMainConfig
.isTouch()) {
95 mPlayer
.getVelocity().x
= -mPlayer
.getSpeed();
96 mPlayer
.setDir(Mob
.Direction
.LEFT
);
97 if (mMainConfig
.isTouch() && checkSwim()) {
102 mPlayer
.getVelocity().x
= mPlayer
.getSpeed();
103 mPlayer
.setDir(Mob
.Direction
.RIGHT
);
104 if (mMainConfig
.isTouch() && checkSwim()) {
109 case Input
.Keys
.SPACE
:
113 case Input
.Keys
.CONTROL_LEFT
:
114 mPlayer
.getVelocity().y
= mPlayer
.getSpeed();
136 private boolean isNotAutoselectable(int x
, int y
) {
137 return (!mGameWorld
.hasForeAt(x
, y
) || !mGameWorld
.getForeMapBlock(x
, y
).hasCollision());
140 private void checkCursorBounds() {
143 } else if (mCurY
>= mGameWorld
.getHeight()) {
144 mCurY
= mGameWorld
.getHeight() - 1;
147 if (mControlMode
== ControlMode
.CURSOR
) {
148 if (mCurX
* 16 + 8 < mPlayer
.getX() + mPlayer
.getWidth() / 2) {
149 mPlayer
.setDir(Mob
.Direction
.LEFT
);
151 mPlayer
.setDir(Mob
.Direction
.RIGHT
);
156 public void moveCursor(GameRenderer gameRenderer
) {
160 if (mControlMode
== ControlMode
.WALK
&& mMainConfig
.isTouch()) {
161 mCurX
= mPlayer
.getMapX() + (mPlayer
.looksLeft() ?
-1 : 1);
162 mCurY
= mPlayer
.getUpperMapY();
163 for (int i
= 0; i
< 2 && isNotAutoselectable(mCurX
, mCurY
); i
++) {
166 if (isNotAutoselectable(mCurX
, mCurY
)) {
167 mCurX
+= mPlayer
.looksLeft() ?
1 : -1;
169 } else if (!mMainConfig
.isTouch()) {
170 final int tmpX
= (int) (Gdx
.input
.getX() * (mMainConfig
.getWidth() /
171 Gdx
.graphics
.getWidth()) + gameRenderer
.getCamX());
174 final int tmpY
= (int) (Gdx
.input
.getY() * (mMainConfig
.getHeight() /
175 Gdx
.graphics
.getHeight()) + gameRenderer
.getCamY());
182 final double a
= tmpX
- mPlayer
.x
;
183 final double b
= tmpY
- mPlayer
.y
;
185 mPlayer
.headRotation
= (float) Math
.atan(b
/ a
) * MathUtils
.radDeg
;
188 if (pastX
!= mCurX
|| pastY
!= mCurY
) {
195 private void useItem(int x
, int y
, int id
, boolean bg
) {
196 String key
= getItem(id
).isBlock() ?
getBlockKey(id
) : getItemKey(id
);
198 if (getItem(id
).isBlock()) {
200 mGameWorld
.placeToForeground(x
, y
, getBlockIdByItemId(id
));
202 mGameWorld
.placeToBackground(x
, y
, getBlockIdByItemId(id
));
207 mGameWorld
.placeToForeground(x
, y
, getBlockId("water"));
208 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
211 mGameWorld
.placeToForeground(x
, y
, getBlockId("lava"));
212 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
219 private void hitMobs() {
220 final Player player
= mMobsController
.getPlayer();
221 mMobsController
.getMobs().forEach((mob
) -> {
222 if (Intersector
.overlaps(mob
, player
)) {
229 private void pressLMB() {
230 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
231 mPlayer
.startHitting();
233 if ((mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp() >= 0) ||
234 (!mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.hasBackAt(mCurX
, mCurY
) &&
235 mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp() >= 0)) {
236 if (mPlayer
.gameMode
== 0) {
238 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
239 if (mBlockDamage
>= mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp()) {
240 mGameWorld
.destroyForeMap(mCurX
, mCurY
);
243 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
244 if (mBlockDamage
>= mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp()) {
245 mGameWorld
.destroyBackMap(mCurX
, mCurY
);
250 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
251 mGameWorld
.placeToForeground(mCurX
, mCurY
, 0);
252 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
253 mGameWorld
.placeToBackground(mCurX
, mCurY
, 0);
255 mTouchedDown
= false;
259 mTouchedDown
= false;
264 private boolean insideHotbar(float x
, float y
) {
265 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
266 return y
< hotbar
.getRegionHeight() &&
267 Range
.open(mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2,
268 mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2).contains(x
);
271 private void holdMB() {
272 if (mTouchDownBtn
== Input
.Buttons
.RIGHT
) {
273 useItem(mCurX
, mCurY
, mPlayer
.inventory
[mPlayer
.slot
], true);
274 mTouchedDown
= false;
276 if (insideHotbar(mTouchDownX
, mTouchDownY
)) {
277 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
278 mTouchedDown
= false;
283 public void keyDown(int keycode
) {
285 mKeyDownCode
= keycode
;
291 case Input
.Keys
.SPACE
:
292 case Input
.Keys
.CONTROL_LEFT
:
293 wasdPressed(keycode
);
296 case Input
.Keys
.ALT_LEFT
:
297 if (mMainConfig
.isTouch()) {
298 mControlMode
= mControlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
303 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
304 switch (mPlayer
.gameMode
) {
309 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
313 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
318 mMobsController
.addMob(new Pig(mCurX
* 16, mCurY
* 16));
322 mGameWorld
.placeToForeground(mCurX
, mCurY
, 8);
325 case Input
.Keys
.ESCAPE
:
326 case Input
.Keys
.BACK
:
327 GameSaver
.save(mMainConfig
, mDropController
, mMobsController
, mGameWorld
);
328 mMainConfig
.getCaveGame().quitGame();
332 mMainConfig
.setShowInfo(!mMainConfig
.isShowInfo());
336 mMainConfig
.setShowMap(!mMainConfig
.isShowMap());
341 public void keyUp(int keycode
) {
345 mPlayer
.getVelocity().x
= 0;
346 if (mMainConfig
.isTouch() && mPlayer
.swim
) {
347 mPlayer
.swim
= false;
353 case Input
.Keys
.SPACE
:
354 case Input
.Keys
.CONTROL_LEFT
:
355 if (mPlayer
.isFlyMode()) {
356 mPlayer
.getVelocity().y
= 0;
359 mPlayer
.swim
= false;
365 public void touchDown(float touchX
, float touchY
, int button
) {
366 mTouchDownTime
= TimeUtils
.millis();
368 mTouchDownBtn
= button
;
369 mTouchDownX
= touchX
;
370 mTouchDownY
= touchY
;
373 public void touchUp(float screenX
, float screenY
, int button
) {
379 if (mMainConfig
.isTouch() && mKeyDown
) {
383 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
384 TextureRegion creative
= Assets
.textureRegions
.get("creative");
386 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && insideCreativeInv(screenX
, screenY
)) {
387 int ix
= (int) (screenX
- (mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
388 int iy
= (int) (screenY
- (mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
389 int item
= mCreativeScroll
* 8 + (ix
+ iy
* 8);
390 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) {
393 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
394 System
.arraycopy(mPlayer
.inventory
, 0, mPlayer
.inventory
, 1, 8);
395 mPlayer
.inventory
[0] = item
;
397 } else if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
)) {
398 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
399 } else if (screenY
< hotbar
.getRegionHeight() &&
400 screenX
> mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
401 screenX
< mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
402 mPlayer
.slot
= (int) ((screenX
- (mMainConfig
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
403 } else if (button
== Input
.Buttons
.RIGHT
) {
404 useItem(mCurX
, mCurY
,
405 mPlayer
.inventory
[mPlayer
.slot
], false);
406 } else if (button
== Input
.Buttons
.LEFT
) {
410 mTouchedDown
= false;
413 public void touchDragged(float screenX
, float screenY
) {
414 if (Math
.abs(screenX
- mTouchDownX
) < 16 && Math
.abs(screenY
- mTouchDownY
) < 16) {
419 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && Math
.abs(screenY
- mTouchDownY
) > 16) {
420 if (insideCreativeInv(screenX
, screenY
)) {
421 mCreativeScroll
-= (screenY
- mTouchDownY
) / 16;
422 mTouchDownX
= screenX
;
423 mTouchDownY
= screenY
;
424 if (mCreativeScroll
< 0) {
427 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
428 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
434 public void scrolled(float amountX
, float amountY
) {
435 switch (mMainConfig
.getGameUiWindow()) {
437 mPlayer
.slot
+= (int) amountY
;
438 if (mPlayer
.slot
< 0) {
441 if (mPlayer
.slot
> 8) {
445 case CREATIVE_INVENTORY
:
446 mCreativeScroll
+= (int) amountY
;
447 if (mCreativeScroll
< 0) {
450 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
451 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
457 public int getKeyDownCode() {
461 public boolean isKeyDown() {
465 int getBlockDamage() {
477 int getCreativeScroll() {
478 return mCreativeScroll
;
481 public ControlMode
getControlMode() {
485 public void setControlMode(ControlMode controlMode
) {
486 mControlMode
= controlMode
;
490 if (mTouchedDown
&& mTouchDownBtn
== Input
.Buttons
.LEFT
) {
493 mPlayer
.stopHitting();
495 if (mTouchedDown
&& TimeUtils
.timeSinceMillis(mTouchDownTime
) > 500) {