DEADSOFTWARE

Add player head rotation
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInput.java
index 3a5e3c4ff47044665b0d69cac4384f0c7bd7072c..29c8188d9d6c44f07946a8f464913a01fab1e0ea 100644 (file)
@@ -3,6 +3,7 @@ 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.MathUtils;
 import com.badlogic.gdx.utils.TimeUtils;
 import com.google.common.collect.Range;
 import ru.deadsoftware.cavedroid.MainConfig;
@@ -10,6 +11,8 @@ 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.objects.DropController;
+import ru.deadsoftware.cavedroid.game.world.GameWorld;
 import ru.deadsoftware.cavedroid.misc.Assets;
 import ru.deadsoftware.cavedroid.misc.ControlMode;
 
@@ -22,6 +25,7 @@ public class GameInput {
 
     private final MainConfig mMainConfig;
     private final GameWorld mGameWorld;
+    private final DropController mDropController;
     private final MobsController mMobsController;
     private final Player mPlayer;
 
@@ -45,9 +49,11 @@ public class GameInput {
     @Inject
     public GameInput(MainConfig mainConfig,
                      GameWorld gameWorld,
+                     DropController dropController,
                      MobsController mobsController) {
         mMainConfig = mainConfig;
         mGameWorld = gameWorld;
+        mDropController = dropController;
         mMobsController = mobsController;
 
         mPlayer = mMobsController.getPlayer();
@@ -63,12 +69,12 @@ public class GameInput {
         if (checkSwim()) {
             mPlayer.swim = true;
         } else if (mPlayer.canJump()) {
-            mPlayer.getMove().add(0, -7);
+            mPlayer.getVelocity().add(0, GamePhysics.PL_JUMP_VELOCITY);
         } 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 = -GamePhysics.PL_SPEED;
         }
     }
 
@@ -85,14 +91,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 = -GamePhysics.PL_SPEED;
                     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 = GamePhysics.PL_SPEED;
                     mPlayer.setDir(Mob.Direction.RIGHT);
                     if (mMainConfig.isTouch() && checkSwim()) {
                         mPlayer.swim = true;
@@ -104,7 +110,7 @@ public class GameInput {
                     break;
                 case Input.Keys.S:
                 case Input.Keys.CONTROL_LEFT:
-                    mPlayer.getMove().y = GamePhysics.PL_SPEED;
+                    mPlayer.getVelocity().y = GamePhysics.PL_SPEED;
                     break;
             }
         } else {
@@ -160,14 +166,22 @@ public class GameInput {
                 mCurX += 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());
+            mCurX = tmpX / 16;
 
-            mCurY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() /
-                    Gdx.graphics.getHeight()) + gameRenderer.getCamY()) / 16;
-            if (mCurX < 0) {
+            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) {
@@ -243,7 +257,7 @@ public class GameInput {
             mTouchedDown = false;
         } else {
             if (insideHotbar(mTouchDownX, mTouchDownY)) {
-//                CaveGame.GAME_STATE = GameState.CREATIVE_INV;
+                mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
                 mTouchedDown = false;
             }
         }
@@ -291,11 +305,11 @@ public class GameInput {
                 mGameWorld.placeToForeground(mCurX, mCurY, 8);
                 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 +325,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 +336,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;
@@ -380,6 +394,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)) {
@@ -396,10 +414,10 @@ public class GameInput {
         }
     }
 
-    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,7 +426,7 @@ public class GameInput {
                 }
                 break;
             case CREATIVE_INVENTORY:
-                mCreativeScroll += amount;
+                mCreativeScroll += (int) amountY;
                 if (mCreativeScroll < 0) {
                     mCreativeScroll = 0;
                 }