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
.getVelocity().add(0, -180);
71 } else if (!mPlayer
.isFlyMode() && mPlayer
.gameMode
== 1) {
72 mPlayer
.setFlyMode(true);
73 mPlayer
.getVelocity().y
= 0;
74 } else if (mPlayer
.isFlyMode()) {
75 mPlayer
.getVelocity().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
.getVelocity().x
= -GamePhysics
.PL_SPEED
;
93 mPlayer
.setDir(Mob
.Direction
.LEFT
);
94 if (mMainConfig
.isTouch() && checkSwim()) {
99 mPlayer
.getVelocity().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
.getVelocity().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 final int tmpX
= (int) (Gdx
.input
.getX() * (mMainConfig
.getWidth() /
168 Gdx
.graphics
.getWidth()) + gameRenderer
.getCamX());
171 mCurY
= (int) (Gdx
.input
.getY() * (mMainConfig
.getHeight() /
172 Gdx
.graphics
.getHeight()) + gameRenderer
.getCamY()) / 16;
178 if (pastX
!= mCurX
|| pastY
!= mCurY
) {
185 private void useItem(int x
, int y
, int id
, boolean bg
) {
186 String key
= getItem(id
).isBlock() ?
getBlockKey(id
) : getItemKey(id
);
188 if (getItem(id
).isBlock()) {
190 mGameWorld
.placeToForeground(x
, y
, getBlockIdByItemId(id
));
192 mGameWorld
.placeToBackground(x
, y
, getBlockIdByItemId(id
));
197 mGameWorld
.placeToForeground(x
, y
, getBlockId("water"));
198 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
201 mGameWorld
.placeToForeground(x
, y
, getBlockId("lava"));
202 mPlayer
.inventory
[mPlayer
.slot
] = getItemId("bucket_empty");
209 private void pressLMB() {
210 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
) &&
211 ((mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp() >= 0) ||
212 (!mGameWorld
.hasForeAt(mCurX
, mCurY
) && mGameWorld
.hasBackAt(mCurX
, mCurY
) &&
213 mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp() >= 0))) {
214 if (mPlayer
.gameMode
== 0) {
216 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
217 if (mBlockDamage
>= mGameWorld
.getForeMapBlock(mCurX
, mCurY
).getHp()) {
218 mGameWorld
.destroyForeMap(mCurX
, mCurY
);
221 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
222 if (mBlockDamage
>= mGameWorld
.getBackMapBlock(mCurX
, mCurY
).getHp()) {
223 mGameWorld
.destroyBackMap(mCurX
, mCurY
);
228 if (mGameWorld
.hasForeAt(mCurX
, mCurY
)) {
229 mGameWorld
.placeToForeground(mCurX
, mCurY
, 0);
230 } else if (mGameWorld
.hasBackAt(mCurX
, mCurY
)) {
231 mGameWorld
.placeToBackground(mCurX
, mCurY
, 0);
233 mTouchedDown
= false;
238 private boolean insideHotbar(float x
, float y
) {
239 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
240 return y
< hotbar
.getRegionHeight() &&
241 Range
.open(mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2,
242 mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2).contains(x
);
245 private void holdMB() {
246 if (mTouchDownBtn
== Input
.Buttons
.RIGHT
) {
247 useItem(mCurX
, mCurY
, mPlayer
.inventory
[mPlayer
.slot
], true);
248 mTouchedDown
= false;
250 if (insideHotbar(mTouchDownX
, mTouchDownY
)) {
251 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
252 mTouchedDown
= false;
257 public void keyDown(int keycode
) {
259 mKeyDownCode
= keycode
;
265 case Input
.Keys
.SPACE
:
266 case Input
.Keys
.CONTROL_LEFT
:
267 wasdPressed(keycode
);
270 case Input
.Keys
.ALT_LEFT
:
271 if (mMainConfig
.isTouch()) {
272 mControlMode
= mControlMode
== ControlMode
.WALK ? ControlMode
.CURSOR
: ControlMode
.WALK
;
277 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.NONE
)) {
278 switch (mPlayer
.gameMode
) {
283 mMainConfig
.setGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
);
287 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
292 mMobsController
.addMob(Pig
.class, mCurX
* 16, mCurY
* 16);
296 mGameWorld
.placeToForeground(mCurX
, mCurY
, 8);
299 case Input
.Keys
.ESCAPE
:
300 case Input
.Keys
.BACK
:
301 GameSaver
.save(mMainConfig
, mDropController
, mMobsController
, mGameWorld
);
302 mMainConfig
.getCaveGame().quitGame();
306 mMainConfig
.setShowInfo(!mMainConfig
.isShowInfo());
310 mMainConfig
.setShowMap(!mMainConfig
.isShowMap());
315 public void keyUp(int keycode
) {
319 mPlayer
.getVelocity().x
= 0;
320 if (mMainConfig
.isTouch() && mPlayer
.swim
) {
321 mPlayer
.swim
= false;
327 case Input
.Keys
.SPACE
:
328 case Input
.Keys
.CONTROL_LEFT
:
329 if (mPlayer
.isFlyMode()) {
330 mPlayer
.getVelocity().y
= 0;
333 mPlayer
.swim
= false;
339 public void touchDown(float touchX
, float touchY
, int button
) {
340 mTouchDownTime
= TimeUtils
.millis();
342 mTouchDownBtn
= button
;
343 mTouchDownX
= touchX
;
344 mTouchDownY
= touchY
;
347 public void touchUp(float screenX
, float screenY
, int button
) {
353 if (mMainConfig
.isTouch() && mKeyDown
) {
357 TextureRegion hotbar
= Assets
.textureRegions
.get("hotbar");
358 TextureRegion creative
= Assets
.textureRegions
.get("creative");
360 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && insideCreativeInv(screenX
, screenY
)) {
361 int ix
= (int) (screenX
- (mMainConfig
.getWidth() / 2 - creative
.getRegionWidth() / 2 + 8)) / 18;
362 int iy
= (int) (screenY
- (mMainConfig
.getHeight() / 2 - creative
.getRegionHeight() / 2 + 18)) / 18;
363 int item
= mCreativeScroll
* 8 + (ix
+ iy
* 8);
364 if (ix
>= 8 || ix
< 0 || iy
< 0 || iy
>= 5) {
367 if (item
>= 0 && item
< GameItems
.getItemsSize()) {
368 System
.arraycopy(mPlayer
.inventory
, 0, mPlayer
.inventory
, 1, 8);
369 mPlayer
.inventory
[0] = item
;
371 } else if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
)) {
372 mMainConfig
.setGameUiWindow(GameUiWindow
.NONE
);
373 } else if (screenY
< hotbar
.getRegionHeight() &&
374 screenX
> mMainConfig
.getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 &&
375 screenX
< mMainConfig
.getWidth() / 2 + (float) hotbar
.getRegionWidth() / 2) {
376 mPlayer
.slot
= (int) ((screenX
- (mMainConfig
.getWidth() / 2 - hotbar
.getRegionWidth() / 2)) / 20);
377 } else if (button
== Input
.Buttons
.RIGHT
) {
378 useItem(mCurX
, mCurY
,
379 mPlayer
.inventory
[mPlayer
.slot
], false);
380 } else if (button
== Input
.Buttons
.LEFT
) {
384 mTouchedDown
= false;
387 public void touchDragged(float screenX
, float screenY
) {
389 if (mMainConfig
.checkGameUiWindow(GameUiWindow
.CREATIVE_INVENTORY
) && Math
.abs(screenY
- mTouchDownY
) > 16) {
390 if (insideCreativeInv(screenX
, screenY
)) {
391 mCreativeScroll
-= (screenY
- mTouchDownY
) / 16;
392 mTouchDownX
= screenX
;
393 mTouchDownY
= screenY
;
394 if (mCreativeScroll
< 0) {
397 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
398 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
404 public void scrolled(float amountX
, float amountY
) {
405 switch (mMainConfig
.getGameUiWindow()) {
407 mPlayer
.slot
+= (int) amountY
;
408 if (mPlayer
.slot
< 0) {
411 if (mPlayer
.slot
> 8) {
415 case CREATIVE_INVENTORY
:
416 mCreativeScroll
+= (int) amountY
;
417 if (mCreativeScroll
< 0) {
420 if (mCreativeScroll
> GameProc
.MAX_CREATIVE_SCROLL
) {
421 mCreativeScroll
= GameProc
.MAX_CREATIVE_SCROLL
;
427 public int getKeyDownCode() {
431 public boolean isKeyDown() {
435 int getBlockDamage() {
447 int getCreativeScroll() {
448 return mCreativeScroll
;
451 public ControlMode
getControlMode() {
455 public void setControlMode(ControlMode controlMode
) {
456 mControlMode
= controlMode
;
460 if (mTouchedDown
&& mTouchDownBtn
== Input
.Buttons
.LEFT
) {
463 if (mTouchedDown
&& TimeUtils
.timeSinceMillis(mTouchDownTime
) > 500) {