DEADSOFTWARE

Move block damage and cursor to player class
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInput.java
index 32826528c62ba6ed0ce38772a5b44a45ff965f13..e1257d518fce877884bde4e669a5b089918e2037 100644 (file)
@@ -21,12 +21,11 @@ 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 java.util.Map;
 
-import static ru.deadsoftware.cavedroid.game.GameItems.*;
-
 @GameScope
 public class GameInput {
 
@@ -53,11 +52,8 @@ 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,
@@ -135,19 +131,19 @@ public class GameInput {
         } 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;
         }
     }
 
@@ -156,14 +152,14 @@ public class GameInput {
     }
 
     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);
@@ -172,29 +168,29 @@ 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()) {
             final int tmpX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() /
                     Gdx.graphics.getWidth()) + gameRenderer.getCamX());
-            mCurX = tmpX / 16;
+            mPlayer.cursorX = tmpX / 16;
 
             final int tmpY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() /
                     Gdx.graphics.getHeight()) + gameRenderer.getCamY());
-            mCurY = tmpY / 16;
+            mPlayer.cursorY = tmpY / 16;
 
             if (tmpX < 0) {
-                mCurX--;
+                mPlayer.cursorX--;
             }
 
             final double a = tmpX - mPlayer.x;
@@ -203,34 +199,34 @@ public class GameInput {
             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) {
+    private void useItem(int x, int y, @CheckForNull Item item, boolean bg) {
         mPlayer.startHitting();
 
-        if (id > 0) {
-            final Item item = getItem(id);
+        if (item == null) {
+            return;
+        }
 
-            if (item instanceof Item.Placeable) {
-                if (!bg) {
-                    CommonBlockActionUtilsKt.placeToForegroundAction(mPlaceBlockActionMap, (Item.Placeable) item, x, y);
-                } else {
-                    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 (item instanceof Item.Placeable) {
+            if (!bg) {
+                CommonBlockActionUtilsKt.placeToForegroundAction(mPlaceBlockActionMap, (Item.Placeable) item, x, y);
+            } else {
+                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");
-                }
+            if (useItemAction != null) {
+                useItemAction.perform((Item.Usable) item, x, y);
+            } else {
+                Gdx.app.error(TAG, "use item action " + actionKey + " not found");
             }
         }
     }
@@ -249,27 +245,26 @@ public class GameInput {
         if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
             mPlayer.startHitting();
 
-            if ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMap(mCurX, mCurY).getHp() >= 0) ||
-                    (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
-                            mGameWorld.getBackMap(mCurX, mCurY).getHp() >= 0)) {
+            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) {
-                    mBlockDamage++;
-                    if (mGameWorld.hasForeAt(mCurX, mCurY)) {
-                        if (mBlockDamage >= mGameWorld.getForeMap(mCurX, mCurY).getHp()) {
-                            mGameWorld.destroyForeMap(mCurX, mCurY);
-                            mBlockDamage = 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(mCurX, mCurY)) {
-                        if (mBlockDamage >= mGameWorld.getBackMap(mCurX, mCurY).getHp()) {
-                            mGameWorld.destroyBackMap(mCurX, mCurY);
-                            mBlockDamage = 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.hasForeAt(mCurX, mCurY)) {
-                        mGameWorld.placeToForeground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
-                    } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
-                        mGameWorld.placeToBackground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
+                    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;
                 }
@@ -289,7 +284,7 @@ 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)) {
@@ -334,7 +329,7 @@ public class GameInput {
                 break;
 
             case Input.Keys.G:
-                final Mob pig = new Pig(mCurX * 16, mCurY * 16);
+                final Mob pig = new Pig(mPlayer.cursorX * 16, mPlayer.cursorY * 16);
                 pig.attachToController(mMobsController);
                 break;
 
@@ -406,14 +401,13 @@ 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);
+                mPlayer.inventory[0] = mGameItemsHolder.getItemFromCreativeInventory(itemPos).toInventoryItem();
             } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) {
                 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
             } else if (screenY < hotbar.getRegionHeight() &&
@@ -421,10 +415,11 @@ 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;
@@ -444,8 +439,10 @@ 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;
                 }
             }
         }
@@ -467,8 +464,10 @@ 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;
                 }
                 break;
         }
@@ -482,19 +481,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;
     }