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 mPlayer
.startHitting();
197 String key
= getItem(id
).isBlock() ?
getBlockKey(id
) : getItemKey(id
);
199 if (getItem(id
).isBlock()) {
201 mGameWorld
.placeToForeground(x
, y
, getBlockIdByItemId(id
));
203 mGameWorld
.placeToBackground(x
, y
, getBlockIdByItemId(id
));
208 mGameWorld
.placeToForeground(x
, y
, getBlockId("water"));
209 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
212 mGameWorld
.placeToForeground(x
, y
, getBlockId("lava"));
213 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
220 private void hitMobs() {
221 final Player player
= mMobsController
.getPlayer();
222 mMobsController
.getMobs().forEach((mob
) -> {
223 if (Intersector
.overlaps(mob
, player
)) {
230 private void pressLMB() {
231 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
232 mPlayer
.startHitting();
234 if ((mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp() >= 0) ||
235 (!mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.hasBackAt(mCurX
, mCurY
) &&
236 mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp() >= 0)) {
237 if (mPlayer
.gameMode
== 0) {
239 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
240 if (mBlockDamage
>= mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp()) {
241 mGameWorld
.destroyForeMap(mCurX
, mCurY
);
244 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
245 if (mBlockDamage
>= mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp()) {
246 mGameWorld
.destroyBackMap(mCurX
, mCurY
);
251 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
252 mGameWorld
.placeToForeground(mCurX
, mCurY
, 0);
253 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
254 mGameWorld
.placeToBackground(mCurX
, mCurY
, 0);
256 mTouchedDown
= false;
260 mTouchedDown
= false;
265 private boolean insideHotbar(float x
, float y
) {
266 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
267 return y
< hotbar
.getRegionHeight() &&
268 Range
.open(mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2,
269 mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2).contains(x
);
272 private void holdMB() {
273 if (mTouchDownBtn
== Input
.Buttons
.RIGHT
) {
274 useItem(mCurX
, mCurY
, mPlayer
.inventory
[mPlayer
.slot
], true);
275 mTouchedDown
= false;
277 if (insideHotbar(mTouchDownX
, mTouchDownY
)) {
278 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
279 mTouchedDown
= false;
284 public void keyDown(int keycode
) {
286 mKeyDownCode
= keycode
;
292 case Input
.Keys
.SPACE
:
293 case Input
.Keys
.CONTROL_LEFT
:
294 wasdPressed(keycode
);
297 case Input
.Keys
.ALT_LEFT
:
298 if (mMainConfig
.isTouch()) {
299 mControlMode
= mControlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
304 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
305 switch (mPlayer
.gameMode
) {
310 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
314 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
319 mMobsController
.addMob(new Pig(mCurX
* 16, mCurY
* 16));
323 mGameWorld
.placeToForeground(mCurX
, mCurY
, 8);
326 case Input
.Keys
.GRAVE
:
327 mMobsController
.getPlayer().gameMode
= (mMobsController
.getPlayer().gameMode
+ 1) % 2;
330 case Input
.Keys
.ESCAPE
:
331 case Input
.Keys
.BACK
:
332 GameSaver
.save(mMainConfig
, mDropController
, mMobsController
, mGameWorld
);
333 mMainConfig
.getCaveGame().quitGame();
337 mMainConfig
.setShowInfo(!mMainConfig
.isShowInfo());
341 mMainConfig
.setShowMap(!mMainConfig
.isShowMap());
346 public void keyUp(int keycode
) {
350 mPlayer
.getVelocity().x
= 0;
351 if (mMainConfig
.isTouch() && mPlayer
.swim
) {
352 mPlayer
.swim
= false;
358 case Input
.Keys
.SPACE
:
359 case Input
.Keys
.CONTROL_LEFT
:
360 if (mPlayer
.isFlyMode()) {
361 mPlayer
.getVelocity().y
= 0;
364 mPlayer
.swim
= false;
370 public void touchDown(float touchX
, float touchY
, int button
) {
371 mTouchDownTime
= TimeUtils
.millis();
373 mTouchDownBtn
= button
;
374 mTouchDownX
= touchX
;
375 mTouchDownY
= touchY
;
378 public void touchUp(float screenX
, float screenY
, int button
) {
384 if (mMainConfig
.isTouch() && mKeyDown
) {
388 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
389 TextureRegion creative
= Assets
.textureRegions
.get("creative");
391 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && insideCreativeInv(screenX
, screenY
)) {
392 int ix
= (int) (screenX
- (mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
393 int iy
= (int) (screenY
- (mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
394 int item
= mCreativeScroll
* 8 + (ix
+ iy
* 8);
395 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) {
398 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
399 System
.arraycopy(mPlayer
.inventory
, 0, mPlayer
.inventory
, 1, 8);
400 mPlayer
.inventory
[0] = item
;
402 } else if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
)) {
403 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
404 } else if (screenY
< hotbar
.getRegionHeight() &&
405 screenX
> mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
406 screenX
< mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
407 mPlayer
.slot
= (int) ((screenX
- (mMainConfig
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
408 } else if (button
== Input
.Buttons
.RIGHT
) {
409 useItem(mCurX
, mCurY
,
410 mPlayer
.inventory
[mPlayer
.slot
], false);
411 } else if (button
== Input
.Buttons
.LEFT
) {
415 mTouchedDown
= false;
418 public void touchDragged(float screenX
, float screenY
) {
419 if (Math
.abs(screenX
- mTouchDownX
) < 16 && Math
.abs(screenY
- mTouchDownY
) < 16) {
424 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && Math
.abs(screenY
- mTouchDownY
) > 16) {
425 if (insideCreativeInv(screenX
, screenY
)) {
426 mCreativeScroll
-= (screenY
- mTouchDownY
) / 16;
427 mTouchDownX
= screenX
;
428 mTouchDownY
= screenY
;
429 if (mCreativeScroll
< 0) {
432 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
433 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
439 public void scrolled(float amountX
, float amountY
) {
440 switch (mMainConfig
.getGameUiWindow()) {
442 mPlayer
.slot
+= (int) amountY
;
443 if (mPlayer
.slot
< 0) {
446 if (mPlayer
.slot
> 8) {
450 case CREATIVE_INVENTORY
:
451 mCreativeScroll
+= (int) amountY
;
452 if (mCreativeScroll
< 0) {
455 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
456 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
462 public int getKeyDownCode() {
466 public boolean isKeyDown() {
470 int getBlockDamage() {
482 int getCreativeScroll() {
483 return mCreativeScroll
;
486 public ControlMode
getControlMode() {
490 public void setControlMode(ControlMode controlMode
) {
491 mControlMode
= controlMode
;
496 mPlayer
.stopHitting();
500 if (mTouchDownBtn
== Input
.Buttons
.LEFT
) {
504 if (mTouchedDown
&& TimeUtils
.timeSinceMillis(mTouchDownTime
) > 500) {