DEADSOFTWARE

Fix hitting animation
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInput.java
index 291311a68077622850ed90b533622ccf30ab6557..27c4df02e9b4a9ec9e5b05c8850867ef52e6e00f 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.Intersector;
 import com.badlogic.gdx.math.MathUtils;
 import com.badlogic.gdx.utils.TimeUtils;
 import com.google.common.collect.Range;
@@ -192,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()) {
@@ -215,6 +217,16 @@ 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)) {
             mPlayer.startHitting();
@@ -244,6 +256,7 @@ public class GameInput {
                     mTouchedDown = false;
                 }
             } else {
+                hitMobs();
                 mTouchedDown = false;
             }
         }
@@ -303,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);
@@ -475,11 +492,15 @@ public class GameInput {
     }
 
     void update() {
-        if (mTouchedDown && mTouchDownBtn == Input.Buttons.LEFT) {
-            pressLMB();
-        } else {
+        if (!mTouchedDown) {
             mPlayer.stopHitting();
+            return;
+        }
+
+        if (mTouchDownBtn == Input.Buttons.LEFT) {
+            pressLMB();
         }
+
         if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) {
             holdMB();
         }