X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2FGameInput.java;h=27c4df02e9b4a9ec9e5b05c8850867ef52e6e00f;hb=546c7c80eb7884183a3df1db7bb5627a18396dca;hp=3da4e2e10c44b28e88bfb0236878acab48c15de6;hpb=2aa65a4cdb47df8014f28342d460fc6639bed885;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 3da4e2e..27c4df0 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -3,6 +3,8 @@ 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; @@ -68,12 +70,12 @@ public class GameInput { if (checkSwim()) { mPlayer.swim = true; } else if (mPlayer.canJump()) { - mPlayer.getVelocity().add(0, -180); + mPlayer.jump(); } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) { mPlayer.setFlyMode(true); mPlayer.getVelocity().y = 0; } else if (mPlayer.isFlyMode()) { - mPlayer.getVelocity().y = -GamePhysics.PL_SPEED; + mPlayer.getVelocity().y = -mPlayer.getSpeed(); } } @@ -90,14 +92,14 @@ public class GameInput { if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) { switch (keycode) { case Input.Keys.A: - mPlayer.getVelocity().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.getVelocity().x = GamePhysics.PL_SPEED; + mPlayer.getVelocity().x = mPlayer.getSpeed(); mPlayer.setDir(Mob.Direction.RIGHT); if (mMainConfig.isTouch() && checkSwim()) { mPlayer.swim = true; @@ -109,7 +111,7 @@ public class GameInput { break; case Input.Keys.S: case Input.Keys.CONTROL_LEFT: - mPlayer.getVelocity().y = GamePhysics.PL_SPEED; + mPlayer.getVelocity().y = mPlayer.getSpeed(); break; } } else { @@ -169,11 +171,18 @@ public class GameInput { Gdx.graphics.getWidth()) + gameRenderer.getCamX()); mCurX = tmpX / 16; - mCurY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() / - Gdx.graphics.getHeight()) + gameRenderer.getCamY()) / 16; + final int tmpY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() / + Gdx.graphics.getHeight()) + gameRenderer.getCamY()); + mCurY = tmpY / 16; + if (tmpX < 0) { mCurX--; } + + 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) { @@ -184,6 +193,7 @@ public class GameInput { } private void useItem(int x, int y, int id, boolean bg) { + mPlayer.startHitting(); String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id); if (id > 0) { if (getItem(id).isBlock()) { @@ -207,30 +217,46 @@ public class GameInput { } } + 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(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; + } } - } else if (mGameWorld.hasBackAt(mCurX, mCurY)) { - if (mBlockDamage >= mGameWorld.getBackMapBlock(mCurX, mCurY).getHp()) { - mGameWorld.destroyBackMap(mCurX, mCurY); - mBlockDamage = 0; + } else { + if (mGameWorld.hasForeAt(mCurX, mCurY)) { + mGameWorld.placeToForeground(mCurX, mCurY, 0); + } else if (mGameWorld.hasBackAt(mCurX, mCurY)) { + mGameWorld.placeToBackground(mCurX, mCurY, 0); } + 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; } } @@ -290,13 +316,17 @@ public class GameInput { break; case Input.Keys.G: - mMobsController.addMob(Pig.class, mCurX * 16, mCurY * 16); + mMobsController.addMob(new Pig(mCurX * 16, mCurY * 16)); break; case Input.Keys.Q: mGameWorld.placeToForeground(mCurX, mCurY, 8); break; + case Input.Keys.GRAVE: + mMobsController.getPlayer().gameMode = (mMobsController.getPlayer().gameMode + 1) % 2; + break; + case Input.Keys.ESCAPE: case Input.Keys.BACK: GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld); @@ -386,6 +416,10 @@ public class GameInput { } 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)) { @@ -458,9 +492,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(); }