DEADSOFTWARE

Fix saves
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInput.java
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.annotation.CheckForNull;
25 import javax.inject.Inject;
27 import java.util.Map;
29 @GameScope
30 public class GameInput {
32 private static final String TAG = "GameInput";
34 private final MainConfig mMainConfig;
35 private final GameWorld mGameWorld;
36 private final DropController mDropController;
37 private final MobsController mMobsController;
38 private final GameItemsHolder mGameItemsHolder;
39 private final Map<String, IUseItemAction> mUseItemActionMap;
40 private final Map<String, IPlaceBlockAction> mPlaceBlockActionMap;
42 private final Player mPlayer;
44 private ControlMode mControlMode;
46 private boolean mKeyDown;
47 private boolean mTouchedDown;
48 private boolean mDragging;
50 private int mKeyDownCode;
51 private int mTouchDownBtn;
52 private float mTouchDownX;
53 private float mTouchDownY;
54 private long mTouchDownTime;
56 private int mCurX;
57 private int mCurY;
58 private int mCreativeScroll;
59 private int mBlockDamage;
61 @Inject
62 public GameInput(MainConfig mainConfig,
63 GameWorld gameWorld,
64 DropController dropController,
65 MobsController mobsController,
66 GameItemsHolder gameItemsHolder,
67 Map<String, IUseItemAction> useItemActionMap,
68 Map<String, IPlaceBlockAction> placeBlockActionMap) {
69 mMainConfig = mainConfig;
70 mGameWorld = gameWorld;
71 mDropController = dropController;
72 mMobsController = mobsController;
73 mGameItemsHolder = gameItemsHolder;
74 mUseItemActionMap = useItemActionMap;
75 mPlaceBlockActionMap = placeBlockActionMap;
77 mPlayer = mMobsController.getPlayer();
79 mControlMode = mMainConfig.isTouch() ? ControlMode.WALK : ControlMode.CURSOR;
80 }
82 private boolean checkSwim() {
83 return mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY()).isFluid();
84 }
86 private void goUpwards() {
87 if (checkSwim()) {
88 mPlayer.swim = true;
89 } else if (mPlayer.canJump()) {
90 mPlayer.jump();
91 } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) {
92 mPlayer.setFlyMode(true);
93 mPlayer.getVelocity().y = 0;
94 } else if (mPlayer.isFlyMode()) {
95 mPlayer.getVelocity().y = -mPlayer.getSpeed();
96 }
97 }
99 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
100 private boolean insideCreativeInv(float screenX, float screenY) {
101 TextureRegion creative = Assets.textureRegions.get("creative");
102 return (screenX > mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 &&
103 screenX < mMainConfig.getWidth() / 2 + creative.getRegionWidth() / 2 &&
104 screenY > mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 &&
105 screenY < mMainConfig.getHeight() / 2 + creative.getRegionHeight() / 2);
108 private void wasdPressed(int keycode) {
109 if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) {
110 switch (keycode) {
111 case Input.Keys.A:
112 mPlayer.getVelocity().x = -mPlayer.getSpeed();
113 mPlayer.setDir(Mob.Direction.LEFT);
114 if (mMainConfig.isTouch() && checkSwim()) {
115 mPlayer.swim = true;
117 break;
118 case Input.Keys.D:
119 mPlayer.getVelocity().x = mPlayer.getSpeed();
120 mPlayer.setDir(Mob.Direction.RIGHT);
121 if (mMainConfig.isTouch() && checkSwim()) {
122 mPlayer.swim = true;
124 break;
125 case Input.Keys.W:
126 case Input.Keys.SPACE:
127 goUpwards();
128 break;
129 case Input.Keys.S:
130 case Input.Keys.CONTROL_LEFT:
131 mPlayer.getVelocity().y = mPlayer.getSpeed();
132 break;
134 } else {
135 switch (keycode) {
136 case Input.Keys.A:
137 mCurX--;
138 break;
139 case Input.Keys.D:
140 mCurX++;
141 break;
142 case Input.Keys.W:
143 mCurY--;
144 break;
145 case Input.Keys.S:
146 mCurY++;
147 break;
149 mBlockDamage = 0;
153 private boolean isNotAutoselectable(int x, int y) {
154 return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMap(x, y).hasCollision());
157 private void checkCursorBounds() {
158 if (mCurY < 0) {
159 mCurY = 0;
160 } else if (mCurY >= mGameWorld.getHeight()) {
161 mCurY = mGameWorld.getHeight() - 1;
164 if (mControlMode == ControlMode.CURSOR) {
165 if (mCurX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) {
166 mPlayer.setDir(Mob.Direction.LEFT);
167 } else {
168 mPlayer.setDir(Mob.Direction.RIGHT);
173 public void moveCursor(GameRenderer gameRenderer) {
174 int pastX = mCurX;
175 int pastY = mCurY;
177 if (mControlMode == ControlMode.WALK && mMainConfig.isTouch()) {
178 mCurX = mPlayer.getMapX() + (mPlayer.looksLeft() ? -1 : 1);
179 mCurY = mPlayer.getUpperMapY();
180 for (int i = 0; i < 2 && isNotAutoselectable(mCurX, mCurY); i++) {
181 mCurY++;
183 if (isNotAutoselectable(mCurX, mCurY)) {
184 mCurX += mPlayer.looksLeft() ? 1 : -1;
186 } else if (!mMainConfig.isTouch()) {
187 final int tmpX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() /
188 Gdx.graphics.getWidth()) + gameRenderer.getCamX());
189 mCurX = tmpX / 16;
191 final int tmpY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() /
192 Gdx.graphics.getHeight()) + gameRenderer.getCamY());
193 mCurY = tmpY / 16;
195 if (tmpX < 0) {
196 mCurX--;
199 final double a = tmpX - mPlayer.x;
200 final double b = tmpY - mPlayer.y;
202 mPlayer.headRotation = (float) Math.atan(b / a) * MathUtils.radDeg;
205 if (pastX != mCurX || pastY != mCurY) {
206 mBlockDamage = 0;
209 checkCursorBounds();
212 private void useItem(int x, int y, @CheckForNull Item item, boolean bg) {
213 mPlayer.startHitting();
215 if (item == null) {
216 return;
219 if (item instanceof Item.Placeable) {
220 if (!bg) {
221 CommonBlockActionUtilsKt.placeToForegroundAction(mPlaceBlockActionMap, (Item.Placeable) item, x, y);
222 } else {
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);
231 } else {
232 Gdx.app.error(TAG, "use item action " + actionKey + " not found");
237 private void hitMobs() {
238 final Player player = mMobsController.getPlayer();
239 mMobsController.getMobs().forEach((mob) -> {
240 if (Intersector.overlaps(mob, player)) {
241 mob.damage(5);
242 mob.jump();
244 });
247 private void pressLMB() {
248 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
249 mPlayer.startHitting();
251 if ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMap(mCurX, mCurY).getHp() >= 0) ||
252 (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
253 mGameWorld.getBackMap(mCurX, mCurY).getHp() >= 0)) {
254 if (mPlayer.gameMode == 0) {
255 mBlockDamage++;
256 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
257 if (mBlockDamage >= mGameWorld.getForeMap(mCurX, mCurY).getHp()) {
258 mGameWorld.destroyForeMap(mCurX, mCurY);
259 mBlockDamage = 0;
261 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
262 if (mBlockDamage >= mGameWorld.getBackMap(mCurX, mCurY).getHp()) {
263 mGameWorld.destroyBackMap(mCurX, mCurY);
264 mBlockDamage = 0;
267 } else {
268 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
269 mGameWorld.placeToForeground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
270 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
271 mGameWorld.placeToBackground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
273 mTouchedDown = false;
275 } else {
276 hitMobs();
277 mTouchedDown = false;
282 private boolean insideHotbar(float x, float y) {
283 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
284 return y < hotbar.getRegionHeight() &&
285 Range.open(mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2,
286 mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x);
289 private void holdMB() {
290 if (mTouchDownBtn == Input.Buttons.RIGHT) {
291 useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot].getItem(), true);
292 mTouchedDown = false;
293 } else {
294 if (insideHotbar(mTouchDownX, mTouchDownY)) {
295 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
296 mTouchedDown = false;
301 public void keyDown(int keycode) {
302 mKeyDown = true;
303 mKeyDownCode = keycode;
304 switch (keycode) {
305 case Input.Keys.A:
306 case Input.Keys.D:
307 case Input.Keys.W:
308 case Input.Keys.S:
309 case Input.Keys.SPACE:
310 case Input.Keys.CONTROL_LEFT:
311 wasdPressed(keycode);
312 break;
314 case Input.Keys.ALT_LEFT:
315 if (mMainConfig.isTouch()) {
316 mControlMode = mControlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
318 break;
320 case Input.Keys.E:
321 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
322 switch (mPlayer.gameMode) {
323 case 0:
324 //TODO survival inv
325 break;
326 case 1:
327 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
328 break;
330 } else {
331 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
333 break;
335 case Input.Keys.G:
336 final Mob pig = new Pig(mCurX * 16, mCurY * 16);
337 pig.attachToController(mMobsController);
338 break;
340 case Input.Keys.GRAVE:
341 mMobsController.getPlayer().gameMode = (mMobsController.getPlayer().gameMode + 1) % 2;
342 break;
344 case Input.Keys.ESCAPE:
345 case Input.Keys.BACK:
346 GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld);
347 mMainConfig.getCaveGame().quitGame();
348 break;
350 case Input.Keys.F1:
351 mMainConfig.setShowInfo(!mMainConfig.isShowInfo());
352 break;
354 case Input.Keys.M:
355 mMainConfig.setShowMap(!mMainConfig.isShowMap());
356 break;
360 public void keyUp(int keycode) {
361 switch (keycode) {
362 case Input.Keys.A:
363 case Input.Keys.D:
364 mPlayer.getVelocity().x = 0;
365 if (mMainConfig.isTouch() && mPlayer.swim) {
366 mPlayer.swim = false;
368 break;
370 case Input.Keys.W:
371 case Input.Keys.S:
372 case Input.Keys.SPACE:
373 case Input.Keys.CONTROL_LEFT:
374 if (mPlayer.isFlyMode()) {
375 mPlayer.getVelocity().y = 0;
377 if (mPlayer.swim) {
378 mPlayer.swim = false;
380 break;
384 public void touchDown(float touchX, float touchY, int button) {
385 mTouchDownTime = TimeUtils.millis();
386 mTouchedDown = true;
387 mTouchDownBtn = button;
388 mTouchDownX = touchX;
389 mTouchDownY = touchY;
392 public void touchUp(float screenX, float screenY, int button) {
393 if (mDragging) {
394 mDragging = false;
395 return;
398 if (mMainConfig.isTouch() && mKeyDown) {
399 keyUp(mKeyDownCode);
400 mKeyDown = false;
402 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
403 TextureRegion creative = Assets.textureRegions.get("creative");
404 if (mTouchedDown) {
405 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && insideCreativeInv(screenX, screenY)) {
406 int ix = (int) (screenX - (mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18;
407 int iy = (int) (screenY - (mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18;
408 int itemPos = mCreativeScroll * 8 + (ix + iy * 8);
409 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) {
410 itemPos = -1;
413 System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8);
414 mPlayer.inventory[0] = mGameItemsHolder.getItemFromCreativeInventory(itemPos).toInventoryItem();
415 } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) {
416 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
417 } else if (screenY < hotbar.getRegionHeight() &&
418 screenX > mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 &&
419 screenX < mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) {
420 mPlayer.slot = (int) ((screenX - (mMainConfig.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20);
421 } else if (button == Input.Buttons.RIGHT) {
422 useItem(mCurX, mCurY,
423 mPlayer.inventory[mPlayer.slot].getItem(), false);
424 } else if (button == Input.Buttons.LEFT) {
425 mBlockDamage = 0;
428 mTouchedDown = false;
431 public void touchDragged(float screenX, float screenY) {
432 if (Math.abs(screenX - mTouchDownX) < 16 && Math.abs(screenY - mTouchDownY) < 16) {
433 return;
436 mDragging = true;
437 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && Math.abs(screenY - mTouchDownY) > 16) {
438 if (insideCreativeInv(screenX, screenY)) {
439 mCreativeScroll -= (screenY - mTouchDownY) / 16;
440 mTouchDownX = screenX;
441 mTouchDownY = screenY;
442 if (mCreativeScroll < 0) {
443 mCreativeScroll = 0;
446 final int maxScroll = mGameItemsHolder.getCreativeScrollAmount();
447 if (mCreativeScroll > maxScroll) {
448 mCreativeScroll = maxScroll;
454 public void scrolled(float amountX, float amountY) {
455 switch (mMainConfig.getGameUiWindow()) {
456 case NONE:
457 mPlayer.slot += (int) amountY;
458 if (mPlayer.slot < 0) {
459 mPlayer.slot = 8;
461 if (mPlayer.slot > 8) {
462 mPlayer.slot = 0;
464 break;
465 case CREATIVE_INVENTORY:
466 mCreativeScroll += (int) amountY;
467 if (mCreativeScroll < 0) {
468 mCreativeScroll = 0;
471 final int maxScroll = mGameItemsHolder.getCreativeScrollAmount();
472 if (mCreativeScroll > maxScroll) {
473 mCreativeScroll = maxScroll;
475 break;
479 public int getKeyDownCode() {
480 return mKeyDownCode;
483 public boolean isKeyDown() {
484 return mKeyDown;
487 int getBlockDamage() {
488 return mBlockDamage;
491 int getCurX() {
492 return mCurX;
495 int getCurY() {
496 return mCurY;
499 int getCreativeScroll() {
500 return mCreativeScroll;
503 public ControlMode getControlMode() {
504 return mControlMode;
507 public void setControlMode(ControlMode controlMode) {
508 mControlMode = controlMode;
511 void update() {
512 if (!mTouchedDown) {
513 mPlayer.stopHitting();
514 return;
517 if (mTouchDownBtn == Input.Buttons.LEFT) {
518 pressLMB();
521 if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) {
522 holdMB();