X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2FGameInput.java;h=a45e7045446eb51feef1afb024dc211a54b3da5a;hb=409a22b3fe7c43b7f686f33cc2f01b6277edec78;hp=3a5e3c4ff47044665b0d69cac4384f0c7bd7072c;hpb=f4d52e3e4a3712050532786fca0aded5ff8b5a03;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 3a5e3c4..a45e704 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -3,26 +3,42 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.math.Intersector; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.TimeUtils; import com.google.common.collect.Range; import ru.deadsoftware.cavedroid.MainConfig; +import ru.deadsoftware.cavedroid.game.actions.CommonBlockActionUtilsKt; +import ru.deadsoftware.cavedroid.game.actions.placeblock.IPlaceBlockAction; +import ru.deadsoftware.cavedroid.game.actions.useitem.IUseItemAction; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.Pig; import ru.deadsoftware.cavedroid.game.mobs.Player; +import ru.deadsoftware.cavedroid.game.model.item.Item; +import ru.deadsoftware.cavedroid.game.objects.DropController; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.ControlMode; +import javax.annotation.CheckForNull; import javax.inject.Inject; -import static ru.deadsoftware.cavedroid.game.GameItems.*; +import java.util.Map; @GameScope public class GameInput { + private static final String TAG = "GameInput"; + private final MainConfig mMainConfig; private final GameWorld mGameWorld; + private final DropController mDropController; private final MobsController mMobsController; + private final GameItemsHolder mGameItemsHolder; + private final Map mUseItemActionMap; + private final Map mPlaceBlockActionMap; + private final Player mPlayer; private ControlMode mControlMode; @@ -36,19 +52,24 @@ public class GameInput { private float mTouchDownX; private float mTouchDownY; private long mTouchDownTime; - - private int mCurX; - private int mCurY; + private int mCreativeScroll; - private int mBlockDamage; @Inject public GameInput(MainConfig mainConfig, GameWorld gameWorld, - MobsController mobsController) { + DropController dropController, + MobsController mobsController, + GameItemsHolder gameItemsHolder, + Map useItemActionMap, + Map placeBlockActionMap) { mMainConfig = mainConfig; mGameWorld = gameWorld; + mDropController = dropController; mMobsController = mobsController; + mGameItemsHolder = gameItemsHolder; + mUseItemActionMap = useItemActionMap; + mPlaceBlockActionMap = placeBlockActionMap; mPlayer = mMobsController.getPlayer(); @@ -56,19 +77,19 @@ public class GameInput { } private boolean checkSwim() { - return GameItems.isFluid(mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY())); + return mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY()).isFluid(); } private void goUpwards() { if (checkSwim()) { mPlayer.swim = true; } else if (mPlayer.canJump()) { - mPlayer.getMove().add(0, -7); + mPlayer.jump(); } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) { mPlayer.setFlyMode(true); - mPlayer.getMove().y = 0; + mPlayer.getVelocity().y = 0; } else if (mPlayer.isFlyMode()) { - mPlayer.getMove().y = -GamePhysics.PL_SPEED; + mPlayer.getVelocity().y = -mPlayer.getSpeed(); } } @@ -85,14 +106,14 @@ public class GameInput { if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) { switch (keycode) { case Input.Keys.A: - mPlayer.getMove().x = -GamePhysics.PL_SPEED; + mPlayer.getVelocity().x = -mPlayer.getSpeed(); mPlayer.setDir(Mob.Direction.LEFT); if (mMainConfig.isTouch() && checkSwim()) { mPlayer.swim = true; } break; case Input.Keys.D: - mPlayer.getMove().x = GamePhysics.PL_SPEED; + mPlayer.getVelocity().x = mPlayer.getSpeed(); mPlayer.setDir(Mob.Direction.RIGHT); if (mMainConfig.isTouch() && checkSwim()) { mPlayer.swim = true; @@ -104,41 +125,41 @@ public class GameInput { break; case Input.Keys.S: case Input.Keys.CONTROL_LEFT: - mPlayer.getMove().y = GamePhysics.PL_SPEED; + mPlayer.getVelocity().y = mPlayer.getSpeed(); break; } } else { switch (keycode) { case Input.Keys.A: - mCurX--; + mPlayer.cursorX--; break; case Input.Keys.D: - mCurX++; + mPlayer.cursorX++; break; case Input.Keys.W: - mCurY--; + mPlayer.cursorY--; break; case Input.Keys.S: - mCurY++; + mPlayer.cursorY++; break; } - mBlockDamage = 0; + mPlayer.blockDamage = 0; } } private boolean isNotAutoselectable(int x, int y) { - return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMapBlock(x, y).hasCollision()); + return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMap(x, y).hasCollision()); } private void checkCursorBounds() { - if (mCurY < 0) { - mCurY = 0; - } else if (mCurY >= mGameWorld.getHeight()) { - mCurY = mGameWorld.getHeight() - 1; + if (mPlayer.cursorY < 0) { + mPlayer.cursorY = 0; + } else if (mPlayer.cursorY >= mGameWorld.getHeight()) { + mPlayer.cursorY = mGameWorld.getHeight() - 1; } if (mControlMode == ControlMode.CURSOR) { - if (mCurX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) { + if (mPlayer.cursorX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) { mPlayer.setDir(Mob.Direction.LEFT); } else { mPlayer.setDir(Mob.Direction.RIGHT); @@ -147,84 +168,108 @@ public class GameInput { } public void moveCursor(GameRenderer gameRenderer) { - int pastX = mCurX; - int pastY = mCurY; + int pastX = mPlayer.cursorX; + int pastY = mPlayer.cursorY; if (mControlMode == ControlMode.WALK && mMainConfig.isTouch()) { - mCurX = mPlayer.getMapX() + (mPlayer.looksLeft() ? -1 : 1); - mCurY = mPlayer.getUpperMapY(); - for (int i = 0; i < 2 && isNotAutoselectable(mCurX, mCurY); i++) { - mCurY++; + mPlayer.cursorX = mPlayer.getMapX() + (mPlayer.looksLeft() ? -1 : 1); + mPlayer.cursorY = mPlayer.getUpperMapY(); + for (int i = 0; i < 2 && isNotAutoselectable(mPlayer.cursorX, mPlayer.cursorY); i++) { + mPlayer.cursorY++; } - if (isNotAutoselectable(mCurX, mCurY)) { - mCurX += mPlayer.looksLeft() ? 1 : -1; + if (isNotAutoselectable(mPlayer.cursorX, mPlayer.cursorY)) { + mPlayer.cursorX += mPlayer.looksLeft() ? 1 : -1; } } else if (!mMainConfig.isTouch()) { - mCurX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() / - Gdx.graphics.getWidth()) + gameRenderer.getCamX()) / 16; + final int tmpX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() / + Gdx.graphics.getWidth()) + gameRenderer.getCamX()); + mPlayer.cursorX = tmpX / 16; - mCurY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() / - Gdx.graphics.getHeight()) + gameRenderer.getCamY()) / 16; - if (mCurX < 0) { - mCurX--; + final int tmpY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() / + Gdx.graphics.getHeight()) + gameRenderer.getCamY()); + mPlayer.cursorY = tmpY / 16; + + if (tmpX < 0) { + mPlayer.cursorX--; } + + final double a = tmpX - mPlayer.x; + final double b = tmpY - mPlayer.y; + + mPlayer.headRotation = (float) Math.atan(b / a) * MathUtils.radDeg; } - if (pastX != mCurX || pastY != mCurY) { - mBlockDamage = 0; + if (pastX != mPlayer.cursorX || pastY != mPlayer.cursorY) { + mPlayer.blockDamage = 0; } checkCursorBounds(); } - private void useItem(int x, int y, int id, boolean bg) { - String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id); - if (id > 0) { - if (getItem(id).isBlock()) { - if (!bg) { - mGameWorld.placeToForeground(x, y, getBlockIdByItemId(id)); - } else { - mGameWorld.placeToBackground(x, y, getBlockIdByItemId(id)); - } + private void useItem(int x, int y, @CheckForNull Item item, boolean bg) { + mPlayer.startHitting(); + + if (item == null) { + return; + } + + if (item instanceof Item.Placeable) { + if (!bg) { + CommonBlockActionUtilsKt.placeToForegroundAction(mPlaceBlockActionMap, (Item.Placeable) item, x, y); } else { - switch (key) { - case "bucket_water": - mGameWorld.placeToForeground(x, y, getBlockId("water")); - mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty"); - break; - case "bucket_lava": - mGameWorld.placeToForeground(x, y, getBlockId("lava")); - mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty"); - break; - } + CommonBlockActionUtilsKt.placeToBackgroundAction(mPlaceBlockActionMap, (Item.Placeable) item, x, y); + } + } else if (item instanceof Item.Usable) { + final String actionKey = ((Item.Usable) item).getUseActionKey(); + final IUseItemAction useItemAction = mUseItemActionMap.get(actionKey); + + if (useItemAction != null) { + useItemAction.perform((Item.Usable) item, x, y); + } else { + Gdx.app.error(TAG, "use item action " + actionKey + " not found"); } } } + private void hitMobs() { + final Player player = mMobsController.getPlayer(); + mMobsController.getMobs().forEach((mob) -> { + if (Intersector.overlaps(mob, player)) { + mob.damage(5); + mob.jump(); + } + }); + } + private void pressLMB() { - if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE) && - ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMapBlock(mCurX, mCurY).getHp() >= 0) || - (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) && - mGameWorld.getBackMapBlock(mCurX, mCurY).getHp() >= 0))) { - if (mPlayer.gameMode == 0) { - mBlockDamage++; - if (mGameWorld.hasForeAt(mCurX, mCurY)) { - if (mBlockDamage >= mGameWorld.getForeMapBlock(mCurX, mCurY).getHp()) { - mGameWorld.destroyForeMap(mCurX, mCurY); - mBlockDamage = 0; + if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) { + mPlayer.startHitting(); + + if ((mGameWorld.hasForeAt(mPlayer.cursorX, mPlayer.cursorY) && mGameWorld.getForeMap(mPlayer.cursorX, mPlayer.cursorY).getHp() >= 0) || + (!mGameWorld.hasForeAt(mPlayer.cursorX, mPlayer.cursorY) && mGameWorld.hasBackAt(mPlayer.cursorX, mPlayer.cursorY) && + mGameWorld.getBackMap(mPlayer.cursorX, mPlayer.cursorY).getHp() >= 0)) { + if (mPlayer.gameMode == 0) { + if (mGameWorld.hasForeAt(mPlayer.cursorX, mPlayer.cursorY)) { + if (mPlayer.blockDamage >= mGameWorld.getForeMap(mPlayer.cursorX, mPlayer.cursorY).getHp()) { + mGameWorld.destroyForeMap(mPlayer.cursorX, mPlayer.cursorY); + mPlayer.blockDamage = 0; + } + } else if (mGameWorld.hasBackAt(mPlayer.cursorX, mPlayer.cursorY)) { + if (mPlayer.blockDamage >= mGameWorld.getBackMap(mPlayer.cursorX, mPlayer.cursorY).getHp()) { + mGameWorld.destroyBackMap(mPlayer.cursorX, mPlayer.cursorY); + mPlayer.blockDamage = 0; + } } - } else if (mGameWorld.hasBackAt(mCurX, mCurY)) { - if (mBlockDamage >= mGameWorld.getBackMapBlock(mCurX, mCurY).getHp()) { - mGameWorld.destroyBackMap(mCurX, mCurY); - mBlockDamage = 0; + } else { + if (mGameWorld.hasForeAt(mPlayer.cursorX, mPlayer.cursorY)) { + mGameWorld.placeToForeground(mPlayer.cursorX, mPlayer.cursorY, mGameItemsHolder.getFallbackBlock()); + } else if (mGameWorld.hasBackAt(mPlayer.cursorX, mPlayer.cursorY)) { + mGameWorld.placeToBackground(mPlayer.cursorX, mPlayer.cursorY, mGameItemsHolder.getFallbackBlock()); } + mTouchedDown = false; } } else { - if (mGameWorld.hasForeAt(mCurX, mCurY)) { - mGameWorld.placeToForeground(mCurX, mCurY, 0); - } else if (mGameWorld.hasBackAt(mCurX, mCurY)) { - mGameWorld.placeToBackground(mCurX, mCurY, 0); - } + hitMobs(); mTouchedDown = false; } } @@ -239,11 +284,11 @@ public class GameInput { private void holdMB() { if (mTouchDownBtn == Input.Buttons.RIGHT) { - useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true); + useItem(mPlayer.cursorX, mPlayer.cursorY, mPlayer.inventory[mPlayer.slot].getItem(), true); mTouchedDown = false; } else { if (insideHotbar(mTouchDownX, mTouchDownY)) { -// CaveGame.GAME_STATE = GameState.CREATIVE_INV; + mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY); mTouchedDown = false; } } @@ -284,18 +329,19 @@ public class GameInput { break; case Input.Keys.G: - mMobsController.addMob(Pig.class, mCurX * 16, mCurY * 16); + final Mob pig = new Pig(mPlayer.cursorX * 16, mPlayer.cursorY * 16); + pig.attachToController(mMobsController); break; - case Input.Keys.Q: - mGameWorld.placeToForeground(mCurX, mCurY, 8); + case Input.Keys.GRAVE: + mMobsController.getPlayer().gameMode = (mMobsController.getPlayer().gameMode + 1) % 2; break; -// case Input.Keys.ESCAPE: -// case Input.Keys.BACK: -// CaveGame.APP_STATE = AppState.SAVE; -// CaveGame.GAME_STATE = GameState.PAUSE; -// break; + case Input.Keys.ESCAPE: + case Input.Keys.BACK: + GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld); + mMainConfig.getCaveGame().quitGame(); + break; case Input.Keys.F1: mMainConfig.setShowInfo(!mMainConfig.isShowInfo()); @@ -311,7 +357,7 @@ public class GameInput { switch (keycode) { case Input.Keys.A: case Input.Keys.D: - mPlayer.getMove().x = 0; + mPlayer.getVelocity().x = 0; if (mMainConfig.isTouch() && mPlayer.swim) { mPlayer.swim = false; } @@ -322,7 +368,7 @@ public class GameInput { case Input.Keys.SPACE: case Input.Keys.CONTROL_LEFT: if (mPlayer.isFlyMode()) { - mPlayer.getMove().y = 0; + mPlayer.getVelocity().y = 0; } if (mPlayer.swim) { mPlayer.swim = false; @@ -355,14 +401,14 @@ public class GameInput { if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && insideCreativeInv(screenX, screenY)) { int ix = (int) (screenX - (mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18; int iy = (int) (screenY - (mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18; - int item = mCreativeScroll * 8 + (ix + iy * 8); + int itemPos = mCreativeScroll * 8 + (ix + iy * 8); if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) { - item = -1; - } - if (item >= 0 && item < GameItems.getItemsSize()) { - System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8); - mPlayer.inventory[0] = item; + itemPos = -1; } + + System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8); + final Item newItem = mGameItemsHolder.getItemFromCreativeInventory(itemPos); + mPlayer.inventory[0] = newItem.toInventoryItem(newItem.getParams().getMaxStack()); } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) { mMainConfig.setGameUiWindow(GameUiWindow.NONE); } else if (screenY < hotbar.getRegionHeight() && @@ -370,16 +416,21 @@ public class GameInput { screenX < mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) { mPlayer.slot = (int) ((screenX - (mMainConfig.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20); } else if (button == Input.Buttons.RIGHT) { - useItem(mCurX, mCurY, - mPlayer.inventory[mPlayer.slot], false); + useItem(mPlayer.cursorX, mPlayer.cursorY, + mPlayer.inventory[mPlayer.slot].getItem(), false); } else if (button == Input.Buttons.LEFT) { - mBlockDamage = 0; + mPlayer.stopHitting(); + mPlayer.blockDamage = 0; } } mTouchedDown = false; } public void touchDragged(float screenX, float screenY) { + if (Math.abs(screenX - mTouchDownX) < 16 && Math.abs(screenY - mTouchDownY) < 16) { + return; + } + mDragging = true; if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && Math.abs(screenY - mTouchDownY) > 16) { if (insideCreativeInv(screenX, screenY)) { @@ -389,17 +440,19 @@ public class GameInput { if (mCreativeScroll < 0) { mCreativeScroll = 0; } - if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) { - mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL; + + final int maxScroll = mGameItemsHolder.getCreativeScrollAmount(); + if (mCreativeScroll > maxScroll) { + mCreativeScroll = maxScroll; } } } } - public void scrolled(int amount) { + public void scrolled(float amountX, float amountY) { switch (mMainConfig.getGameUiWindow()) { case NONE: - mPlayer.slot += amount; + mPlayer.slot += (int) amountY; if (mPlayer.slot < 0) { mPlayer.slot = 8; } @@ -408,12 +461,14 @@ public class GameInput { } break; case CREATIVE_INVENTORY: - mCreativeScroll += amount; + mCreativeScroll += (int) amountY; if (mCreativeScroll < 0) { mCreativeScroll = 0; } - if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) { - mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL; + + final int maxScroll = mGameItemsHolder.getCreativeScrollAmount(); + if (mCreativeScroll > maxScroll) { + mCreativeScroll = maxScroll; } break; } @@ -427,19 +482,7 @@ public class GameInput { return mKeyDown; } - int getBlockDamage() { - return mBlockDamage; - } - - int getCurX() { - return mCurX; - } - - int getCurY() { - return mCurY; - } - - int getCreativeScroll() { + public int getCreativeScroll() { return mCreativeScroll; } @@ -452,9 +495,15 @@ public class GameInput { } void update() { - if (mTouchedDown && mTouchDownBtn == Input.Buttons.LEFT) { + if (!mTouchedDown) { + mPlayer.stopHitting(); + return; + } + + if (mTouchDownBtn == Input.Buttons.LEFT) { pressLMB(); } + if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) { holdMB(); }