DEADSOFTWARE

Fix saves
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInput.java
index bab16fd0533b3733701e803a7f944fc84fe3dba3..fb20d8fb0520ff9fe125d29ce1de5a4645f1161c 100644 (file)
@@ -26,8 +26,6 @@ import javax.inject.Inject;
 
 import java.util.Map;
 
-import static ru.deadsoftware.cavedroid.game.GameItems.*;
-
 @GameScope
 public class GameInput {
 
@@ -37,6 +35,7 @@ public class GameInput {
     private final GameWorld mGameWorld;
     private final DropController mDropController;
     private final MobsController mMobsController;
+    private final GameItemsHolder mGameItemsHolder;
     private final Map<String, IUseItemAction> mUseItemActionMap;
     private final Map<String, IPlaceBlockAction> mPlaceBlockActionMap;
 
@@ -64,12 +63,14 @@ public class GameInput {
                      GameWorld gameWorld,
                      DropController dropController,
                      MobsController mobsController,
+                     GameItemsHolder gameItemsHolder,
                      Map<String, IUseItemAction> useItemActionMap,
                      Map<String, IPlaceBlockAction> placeBlockActionMap) {
         mMainConfig = mainConfig;
         mGameWorld = gameWorld;
         mDropController = dropController;
         mMobsController = mobsController;
+        mGameItemsHolder = gameItemsHolder;
         mUseItemActionMap = useItemActionMap;
         mPlaceBlockActionMap = placeBlockActionMap;
 
@@ -79,7 +80,7 @@ public class GameInput {
     }
 
     private boolean checkSwim() {
-        return GameItems.isFluid(mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY()));
+        return mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY()).isFluid();
     }
 
     private void goUpwards() {
@@ -150,7 +151,7 @@ public class GameInput {
     }
 
     private boolean isNotAutoselectable(int x, int y) {
-        return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMapBlock(x, y).hasCollision());
+        return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMap(x, y).hasCollision());
     }
 
     private void checkCursorBounds() {
@@ -208,27 +209,27 @@ public class GameInput {
         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");
             }
         }
     }
@@ -247,27 +248,27 @@ public class GameInput {
         if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
             mPlayer.startHitting();
 
-            if ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMapBlock(mCurX, mCurY).getHp() >= 0) ||
+            if ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMap(mCurX, mCurY).getHp() >= 0) ||
                     (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
-                            mGameWorld.getBackMapBlock(mCurX, mCurY).getHp() >= 0)) {
+                            mGameWorld.getBackMap(mCurX, mCurY).getHp() >= 0)) {
                 if (mPlayer.gameMode == 0) {
                     mBlockDamage++;
                     if (mGameWorld.hasForeAt(mCurX, mCurY)) {
-                        if (mBlockDamage >= mGameWorld.getForeMapBlock(mCurX, mCurY).getHp()) {
+                        if (mBlockDamage >= mGameWorld.getForeMap(mCurX, mCurY).getHp()) {
                             mGameWorld.destroyForeMap(mCurX, mCurY);
                             mBlockDamage = 0;
                         }
                     } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
-                        if (mBlockDamage >= mGameWorld.getBackMapBlock(mCurX, mCurY).getHp()) {
+                        if (mBlockDamage >= mGameWorld.getBackMap(mCurX, mCurY).getHp()) {
                             mGameWorld.destroyBackMap(mCurX, mCurY);
                             mBlockDamage = 0;
                         }
                     }
                 } else {
                     if (mGameWorld.hasForeAt(mCurX, mCurY)) {
-                        mGameWorld.placeToForeground(mCurX, mCurY, 0);
+                        mGameWorld.placeToForeground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
                     } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
-                        mGameWorld.placeToBackground(mCurX, mCurY, 0);
+                        mGameWorld.placeToBackground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
                     }
                     mTouchedDown = false;
                 }
@@ -287,7 +288,7 @@ public class GameInput {
 
     private void holdMB() {
         if (mTouchDownBtn == Input.Buttons.RIGHT) {
-            useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true);
+            useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot].getItem(), true);
             mTouchedDown = false;
         } else {
             if (insideHotbar(mTouchDownX, mTouchDownY)) {
@@ -336,10 +337,6 @@ public class GameInput {
                 pig.attachToController(mMobsController);
                 break;
 
-            case Input.Keys.Q:
-                mGameWorld.placeToForeground(mCurX, mCurY, 8);
-                break;
-
             case Input.Keys.GRAVE:
                 mMobsController.getPlayer().gameMode = (mMobsController.getPlayer().gameMode + 1) % 2;
                 break;
@@ -408,14 +405,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() &&
@@ -424,7 +420,7 @@ public class GameInput {
                 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);
+                        mPlayer.inventory[mPlayer.slot].getItem(), false);
             } else if (button == Input.Buttons.LEFT) {
                 mBlockDamage = 0;
             }
@@ -446,8 +442,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;
                 }
             }
         }
@@ -469,8 +467,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;
         }