X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2FGameInput.java;h=ca603176453f1ac7e219c6d825c11dfeb1f8f528;hb=5b3244dceab39900a44eeb4d6108715c56735626;hp=85d736dd4cd99c1e21d8b3cf7fa0231b6066aa6c;hpb=084218324e5251bde73361f90549563c16026b34;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 85d736d..ca60317 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -1,123 +1,312 @@ 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.utils.TimeUtils; -import ru.deadsoftware.cavedroid.CaveGame; -import ru.deadsoftware.cavedroid.GameScreen; +import com.google.common.collect.Range; +import ru.deadsoftware.cavedroid.MainConfig; +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.misc.AppState; +import ru.deadsoftware.cavedroid.game.mobs.Player; +import ru.deadsoftware.cavedroid.game.objects.DropController; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.ControlMode; -import static ru.deadsoftware.cavedroid.GameScreen.GP; +import javax.inject.Inject; +import static ru.deadsoftware.cavedroid.game.GameItems.*; + +@GameScope public class GameInput { + private final MainConfig mMainConfig; + private final GameWorld mGameWorld; + private final DropController mDropController; + private final MobsController mMobsController; + private final Player mPlayer; + + private ControlMode mControlMode; + + private boolean mKeyDown; + private boolean mTouchedDown; + private boolean mDragging; + + private int mKeyDownCode; + private int mTouchDownBtn; + 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, + DropController dropController, + MobsController mobsController) { + mMainConfig = mainConfig; + mGameWorld = gameWorld; + mDropController = dropController; + mMobsController = mobsController; + + mPlayer = mMobsController.getPlayer(); + + mControlMode = mMainConfig.isTouch() ? ControlMode.WALK : ControlMode.CURSOR; + } + private boolean checkSwim() { - return GameItems.isFluid(GP.world.getForeMap(GP.player.getMapX(), GP.player.getMapY())); + return GameItems.isFluid(mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY())); } - private boolean insideCreativeInv(int screenX, int screenY) { - return (screenX > GP.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 && - screenX < GP.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 && - screenY > GP.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 && - screenY < GP.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2); + private void goUpwards() { + if (checkSwim()) { + mPlayer.swim = true; + } else if (mPlayer.canJump()) { + mPlayer.getMove().add(0, -7); + } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) { + mPlayer.setFlyMode(true); + mPlayer.getMove().y = 0; + } else if (mPlayer.isFlyMode()) { + mPlayer.getMove().y = -GamePhysics.PL_SPEED; + } + } + + @SuppressWarnings("IntegerDivisionInFloatingPointContext") + private boolean insideCreativeInv(float screenX, float screenY) { + TextureRegion creative = Assets.textureRegions.get("creative"); + return (screenX > mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 && + screenX < mMainConfig.getWidth() / 2 + creative.getRegionWidth() / 2 && + screenY > mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 && + screenY < mMainConfig.getHeight() / 2 + creative.getRegionHeight() / 2); } private void wasdPressed(int keycode) { - if (GP.ctrlMode == 0 || !CaveGame.TOUCH) { + if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) { switch (keycode) { case Input.Keys.A: - GP.player.mov.x = -GamePhysics.PL_SPEED; - GP.player.setDir(0); - if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true; + mPlayer.getMove().x = -GamePhysics.PL_SPEED; + mPlayer.setDir(Mob.Direction.LEFT); + if (mMainConfig.isTouch() && checkSwim()) { + mPlayer.swim = true; + } break; case Input.Keys.D: - GP.player.mov.x = GamePhysics.PL_SPEED; - GP.player.setDir(1); - if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true; + mPlayer.getMove().x = GamePhysics.PL_SPEED; + mPlayer.setDir(Mob.Direction.RIGHT); + if (mMainConfig.isTouch() && checkSwim()) { + mPlayer.swim = true; + } + break; + case Input.Keys.W: + case Input.Keys.SPACE: + goUpwards(); + break; + case Input.Keys.S: + case Input.Keys.CONTROL_LEFT: + mPlayer.getMove().y = GamePhysics.PL_SPEED; break; } } else { switch (keycode) { case Input.Keys.A: - GP.curX--; + mCurX--; break; case Input.Keys.D: - GP.curX++; + mCurX++; break; case Input.Keys.W: - GP.curY--; + mCurY--; break; case Input.Keys.S: - GP.curY++; + mCurY++; break; } - GP.blockDmg = 0; + mBlockDamage = 0; } } - public void keyDown(int keycode) { - GP.isKeyDown = true; - GP.keyDownCode = keycode; - if (keycode == Input.Keys.W || keycode == Input.Keys.A || - keycode == Input.Keys.S || keycode == Input.Keys.D) { - wasdPressed(keycode); - } else switch (keycode) { - case Input.Keys.ALT_LEFT: - if (CaveGame.TOUCH) { - GP.ctrlMode++; - if (GP.ctrlMode > 1) GP.ctrlMode = 0; + private boolean isNotAutoselectable(int x, int y) { + return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMapBlock(x, y).hasCollision()); + } + + private void checkCursorBounds() { + if (mCurY < 0) { + mCurY = 0; + } else if (mCurY >= mGameWorld.getHeight()) { + mCurY = mGameWorld.getHeight() - 1; + } + + if (mControlMode == ControlMode.CURSOR) { + if (mCurX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) { + mPlayer.setDir(Mob.Direction.LEFT); + } else { + mPlayer.setDir(Mob.Direction.RIGHT); + } + } + } + + public void moveCursor(GameRenderer gameRenderer) { + int pastX = mCurX; + int pastY = mCurY; + + 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++; + } + if (isNotAutoselectable(mCurX, mCurY)) { + mCurX += mPlayer.looksLeft() ? 1 : -1; + } + } else if (!mMainConfig.isTouch()) { + mCurX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() / + Gdx.graphics.getWidth()) + gameRenderer.getCamX()) / 16; + + mCurY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() / + Gdx.graphics.getHeight()) + gameRenderer.getCamY()) / 16; + if (mCurX < 0) { + mCurX--; + } + } + + if (pastX != mCurX || pastY != mCurY) { + mBlockDamage = 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)); } - break; + } 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; + } + } + } + } - case Input.Keys.SPACE: - if (checkSwim()) { - GP.player.swim = true; - } else if (GP.player.canJump) { - GP.player.mov.add(0, -7); - } else if (!GP.player.flyMode && GP.player.gameMode == 1) { - GP.player.flyMode = true; - GP.player.mov.y = 0; - } else if (GP.player.flyMode) { - GP.player.mov.y = -GamePhysics.PL_SPEED; + 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; + } + } else if (mGameWorld.hasBackAt(mCurX, mCurY)) { + if (mBlockDamage >= mGameWorld.getBackMapBlock(mCurX, mCurY).getHp()) { + mGameWorld.destroyBackMap(mCurX, mCurY); + mBlockDamage = 0; + } } - break; + } else { + if (mGameWorld.hasForeAt(mCurX, mCurY)) { + mGameWorld.placeToForeground(mCurX, mCurY, 0); + } else if (mGameWorld.hasBackAt(mCurX, mCurY)) { + mGameWorld.placeToBackground(mCurX, mCurY, 0); + } + mTouchedDown = false; + } + } + } + + private boolean insideHotbar(float x, float y) { + TextureRegion hotbar = Assets.textureRegions.get("hotbar"); + return y < hotbar.getRegionHeight() && + Range.open(mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2, + mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x); + } + + private void holdMB() { + if (mTouchDownBtn == Input.Buttons.RIGHT) { + useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true); + mTouchedDown = false; + } else { + if (insideHotbar(mTouchDownX, mTouchDownY)) { + mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY); + mTouchedDown = false; + } + } + } + public void keyDown(int keycode) { + mKeyDown = true; + mKeyDownCode = keycode; + switch (keycode) { + case Input.Keys.A: + case Input.Keys.D: + case Input.Keys.W: + case Input.Keys.S: + case Input.Keys.SPACE: case Input.Keys.CONTROL_LEFT: - GP.player.mov.y = GamePhysics.PL_SPEED; + wasdPressed(keycode); + break; + + case Input.Keys.ALT_LEFT: + if (mMainConfig.isTouch()) { + mControlMode = mControlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK; + } break; case Input.Keys.E: - if (CaveGame.STATE == AppState.GAME_PLAY) switch (GP.player.gameMode) { - case 0: - //TODO survival inv - break; - case 1: - CaveGame.STATE = AppState.GAME_CREATIVE_INV; - break; + if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) { + switch (mPlayer.gameMode) { + case 0: + //TODO survival inv + break; + case 1: + mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY); + break; + } + } else { + mMainConfig.setGameUiWindow(GameUiWindow.NONE); } - else CaveGame.STATE = AppState.GAME_PLAY; break; case Input.Keys.G: - GP.mobs.add(new Pig(GP.curX * 16, GP.curY * 16)); + mMobsController.addMob(Pig.class, mCurX * 16, mCurY * 16); break; case Input.Keys.Q: - GP.world.placeToForeground(GP.curX, GP.curY, 8); + mGameWorld.placeToForeground(mCurX, mCurY, 8); break; case Input.Keys.ESCAPE: case Input.Keys.BACK: - CaveGame.STATE = AppState.GOTO_MENU; + GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld); + mMainConfig.getCaveGame().quitGame(); break; case Input.Keys.F1: - GameScreen.SHOW_DEBUG = !GameScreen.SHOW_DEBUG; + mMainConfig.setShowInfo(!mMainConfig.isShowInfo()); break; case Input.Keys.M: - GameScreen.SHOW_MAP = !GameScreen.SHOW_MAP; + mMainConfig.setShowMap(!mMainConfig.isShowMap()); break; } } @@ -126,86 +315,153 @@ public class GameInput { switch (keycode) { case Input.Keys.A: case Input.Keys.D: - GP.player.mov.x = 0; - if (CaveGame.TOUCH && GP.player.swim) GP.player.swim = false; + mPlayer.getMove().x = 0; + if (mMainConfig.isTouch() && mPlayer.swim) { + mPlayer.swim = false; + } break; + case Input.Keys.W: + case Input.Keys.S: case Input.Keys.SPACE: case Input.Keys.CONTROL_LEFT: - if (GP.player.flyMode) GP.player.mov.y = 0; - if (GP.player.swim) GP.player.swim = false; + if (mPlayer.isFlyMode()) { + mPlayer.getMove().y = 0; + } + if (mPlayer.swim) { + mPlayer.swim = false; + } break; } } - public void touchDown(int screenX, int screenY, int button) { - GP.touchDownTime = TimeUtils.millis(); - GP.isTouchDown = true; - GP.touchDownBtn = button; - GP.touchDownX = screenX; - GP.touchDownY = screenY; + public void touchDown(float touchX, float touchY, int button) { + mTouchDownTime = TimeUtils.millis(); + mTouchedDown = true; + mTouchDownBtn = button; + mTouchDownX = touchX; + mTouchDownY = touchY; } - public void touchUp(int screenX, int screenY, int button) { - if (CaveGame.TOUCH && GP.isKeyDown) { - keyUp(GP.keyDownCode); - GP.isKeyDown = false; + public void touchUp(float screenX, float screenY, int button) { + if (mDragging) { + mDragging = false; + return; + } + + if (mMainConfig.isTouch() && mKeyDown) { + keyUp(mKeyDownCode); + mKeyDown = false; } - if (GP.isTouchDown) { - if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && insideCreativeInv(screenX, screenY)) { - int ix = (int) (screenX - (GP.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18; - int iy = (int) (screenY - (GP.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18; - int item = GP.creativeScroll * 8 + (ix + iy * 8); - if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) item = -1; + TextureRegion hotbar = Assets.textureRegions.get("hotbar"); + TextureRegion creative = Assets.textureRegions.get("creative"); + if (mTouchedDown) { + 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); + if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) { + item = -1; + } if (item >= 0 && item < GameItems.getItemsSize()) { - for (int i = 8; i > 0; i--) { - GP.player.inv[i] = GP.player.inv[i - 1]; - } - GP.player.inv[0] = item; - } - } else if (CaveGame.STATE == AppState.GAME_CREATIVE_INV) { - CaveGame.STATE = AppState.GAME_PLAY; - } else if (screenY < Assets.invBar.getRegionHeight() && - screenX > GP.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 && - screenX < GP.renderer.getWidth() / 2 + Assets.invBar.getRegionWidth() / 2) { - GP.player.invSlot = (int) ((screenX - (GP.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20); + System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8); + mPlayer.inventory[0] = item; + } + } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) { + mMainConfig.setGameUiWindow(GameUiWindow.NONE); + } else if (screenY < hotbar.getRegionHeight() && + screenX > mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 && + 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) { - GP.useItem(GP.curX, GP.curY, - GP.player.inv[GP.player.invSlot], false); + useItem(mCurX, mCurY, + mPlayer.inventory[mPlayer.slot], false); } else if (button == Input.Buttons.LEFT) { - GP.blockDmg = 0; + mBlockDamage = 0; } } - GP.isTouchDown = false; + mTouchedDown = false; } - public void touchDragged(int screenX, int screenY) { - if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && Math.abs(screenY - GP.touchDownY) > 16) { + public void touchDragged(float screenX, float screenY) { + mDragging = true; + if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && Math.abs(screenY - mTouchDownY) > 16) { if (insideCreativeInv(screenX, screenY)) { - GP.creativeScroll -= (screenY - GP.touchDownY) / 16; - GP.touchDownX = screenX; - GP.touchDownY = screenY; - if (GP.creativeScroll < 0) GP.creativeScroll = 0; - if (GP.creativeScroll > GP.maxCreativeScroll) - GP.creativeScroll = GP.maxCreativeScroll; + mCreativeScroll -= (screenY - mTouchDownY) / 16; + mTouchDownX = screenX; + mTouchDownY = screenY; + if (mCreativeScroll < 0) { + mCreativeScroll = 0; + } + if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) { + mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL; + } } } } public void scrolled(int amount) { - switch (CaveGame.STATE) { - case GAME_PLAY: - GP.player.invSlot += amount; - if (GP.player.invSlot < 0) GP.player.invSlot = 8; - if (GP.player.invSlot > 8) GP.player.invSlot = 0; + switch (mMainConfig.getGameUiWindow()) { + case NONE: + mPlayer.slot += amount; + if (mPlayer.slot < 0) { + mPlayer.slot = 8; + } + if (mPlayer.slot > 8) { + mPlayer.slot = 0; + } break; - case GAME_CREATIVE_INV: - GP.creativeScroll += amount; - if (GP.creativeScroll < 0) GP.creativeScroll = 0; - if (GP.creativeScroll > GP.maxCreativeScroll) - GP.creativeScroll = GP.maxCreativeScroll; + case CREATIVE_INVENTORY: + mCreativeScroll += amount; + if (mCreativeScroll < 0) { + mCreativeScroll = 0; + } + if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) { + mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL; + } break; } } + public int getKeyDownCode() { + return mKeyDownCode; + } + + public boolean isKeyDown() { + return mKeyDown; + } + + int getBlockDamage() { + return mBlockDamage; + } + + int getCurX() { + return mCurX; + } + + int getCurY() { + return mCurY; + } + + int getCreativeScroll() { + return mCreativeScroll; + } + + public ControlMode getControlMode() { + return mControlMode; + } + + public void setControlMode(ControlMode controlMode) { + mControlMode = controlMode; + } + + void update() { + if (mTouchedDown && mTouchDownBtn == Input.Buttons.LEFT) { + pressLMB(); + } + if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) { + holdMB(); + } + } + }