DEADSOFTWARE

Refactor rendering
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInput.java
index 2376bb110d28e9ee1f8c30639c5c9ac8d34fd5f2..e5b08381fbd10d0ad9285c4805dfe3a5bea00179 100644 (file)
@@ -3,28 +3,42 @@ 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;
+import ru.deadsoftware.cavedroid.game.actions.CommonBlockActionUtilsKt;
+import ru.deadsoftware.cavedroid.game.actions.placeblock.IPlaceBlockAction;
+import ru.deadsoftware.cavedroid.game.actions.useitem.IUseItemAction;
 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.model.item.Item;
 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;
 
+import javax.annotation.CheckForNull;
 import javax.inject.Inject;
 
-import static ru.deadsoftware.cavedroid.game.GameItems.*;
+import java.util.Map;
 
 @GameScope
 public class GameInput {
 
+    private static final String TAG = "GameInput";
+
     private final MainConfig mMainConfig;
     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;
+
     private final Player mPlayer;
 
     private ControlMode mControlMode;
@@ -48,11 +62,17 @@ public class GameInput {
     public GameInput(MainConfig mainConfig,
                      GameWorld gameWorld,
                      DropController dropController,
-                     MobsController mobsController) {
+                     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;
 
         mPlayer = mMobsController.getPlayer();
 
@@ -60,19 +80,19 @@ 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() {
         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();
         }
     }
 
@@ -89,14 +109,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;
@@ -108,7 +128,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 {
@@ -131,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() {
@@ -168,11 +188,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) {
@@ -182,54 +209,71 @@ public class GameInput {
         checkCursorBounds();
     }
 
-    private void useItem(int x, int y, int id, boolean bg) {
-        String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id);
-        if (id > 0) {
-            if (getItem(id).isBlock()) {
-                if (!bg) {
-                    mGameWorld.placeToForeground(x, y, getBlockIdByItemId(id));
-                } else {
-                    mGameWorld.placeToBackground(x, y, getBlockIdByItemId(id));
-                }
+    private void useItem(int x, int y, @CheckForNull Item item, boolean bg) {
+        mPlayer.startHitting();
+
+        if (item == null) {
+            return;
+        }
+
+        if (item instanceof Item.Placeable) {
+            if (!bg) {
+                CommonBlockActionUtilsKt.placeToForegroundAction(mPlaceBlockActionMap, (Item.Placeable) item, x, y);
             } else {
-                switch (key) {
-                    case "bucket_water":
-                        mGameWorld.placeToForeground(x, y, getBlockId("water"));
-                        mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
-                        break;
-                    case "bucket_lava":
-                        mGameWorld.placeToForeground(x, y, getBlockId("lava"));
-                        mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
-                        break;
-                }
+                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");
             }
         }
     }
 
+    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.getForeMap(mCurX, mCurY).getHp() >= 0) ||
+                    (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
+                            mGameWorld.getBackMap(mCurX, mCurY).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;
+                        }
+                    } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
+                        if (mBlockDamage >= mGameWorld.getBackMap(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, mGameItemsHolder.getFallbackBlock());
+                    } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
+                        mGameWorld.placeToBackground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
                     }
+                    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;
             }
         }
@@ -244,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)) {
@@ -289,11 +333,12 @@ public class GameInput {
                 break;
 
             case Input.Keys.G:
-                mMobsController.addMob(Pig.class, mCurX * 16, mCurY * 16);
+                final Mob pig = new Pig(mCurX * 16, mCurY * 16);
+                pig.attachToController(mMobsController);
                 break;
 
-            case Input.Keys.Q:
-                mGameWorld.placeToForeground(mCurX, mCurY, 8);
+            case Input.Keys.GRAVE:
+                mMobsController.getPlayer().gameMode = (mMobsController.getPlayer().gameMode + 1) % 2;
                 break;
 
             case Input.Keys.ESCAPE:
@@ -360,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() &&
@@ -376,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;
             }
@@ -385,6 +429,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)) {
@@ -394,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;
                 }
             }
         }
@@ -417,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;
         }
@@ -432,19 +484,19 @@ public class GameInput {
         return mKeyDown;
     }
 
-    int getBlockDamage() {
+    public int getBlockDamage() {
         return mBlockDamage;
     }
 
-    int getCurX() {
+    public int getCurX() {
         return mCurX;
     }
 
-    int getCurY() {
+    public int getCurY() {
         return mCurY;
     }
 
-    int getCreativeScroll() {
+    public int getCreativeScroll() {
         return mCreativeScroll;
     }
 
@@ -457,9 +509,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();
         }