DEADSOFTWARE

Inventory class
authorfredboy <fredboy@protonmail.com>
Tue, 7 May 2024 12:56:12 +0000 (19:56 +0700)
committerfredboy <fredboy@protonmail.com>
Tue, 7 May 2024 12:56:12 +0000 (19:56 +0700)
30 files changed:
core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
core/src/ru/deadsoftware/cavedroid/game/GameProc.java
core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java
core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyDownKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyUpKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoLeftKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoRightKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/JumpKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/MoveCursorControlsModeKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/StopSwimKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/SwimUpKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/ToggleControlsModeKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/TurnOnFlyModeKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CursorMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCreativeInventoryItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.kt
core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java [moved from core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java with 85% similarity]
core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt
core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/render/windows/CraftingWindowRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/render/windows/SurvivalWindowRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java

index 248a47ff92b7c353e63a1badaa27f7c0adbdace5..a2354f0c8f9ea68edd6d9b6242c1bda9f18a245f 100644 (file)
@@ -7,7 +7,7 @@ import com.badlogic.gdx.math.Vector2;
 import ru.deadsoftware.cavedroid.MainConfig;
 import ru.deadsoftware.cavedroid.game.mobs.Mob;
 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
-import ru.deadsoftware.cavedroid.game.mobs.Player;
+import ru.deadsoftware.cavedroid.game.mobs.player.Player;
 import ru.deadsoftware.cavedroid.game.model.block.Block;
 import ru.deadsoftware.cavedroid.game.objects.Drop;
 import ru.deadsoftware.cavedroid.game.objects.DropController;
@@ -113,7 +113,7 @@ public class GamePhysics {
     private Rectangle getShiftedMagnetingPlayerRect(Drop drop) {
         final Player player = mMobsController.getPlayer();
 
-        if (player.canPickUpDrop(drop) < 0) {
+        if (!player.inventory.canPickItem(drop.getItem())) {
             return null;
         }
 
@@ -138,7 +138,7 @@ public class GamePhysics {
         final Player player = mMobsController.getPlayer();
 
         if (Intersector.overlaps(shiftedPlayerTarget, drop)) {
-            player.pickUpDrop(drop);
+            player.inventory.pickDrop(drop);
         }
     }
 
index 94cd34ba9729bac41f22b0c6df010b5b9ac0c3f9..d797680d8dad99036c528ab620360a86542e0c28 100644 (file)
@@ -5,7 +5,7 @@ import com.badlogic.gdx.utils.Disposable;
 import com.badlogic.gdx.utils.Timer;
 import ru.deadsoftware.cavedroid.MainConfig;
 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
-import ru.deadsoftware.cavedroid.game.mobs.Player;
+import ru.deadsoftware.cavedroid.game.mobs.player.Player;
 import ru.deadsoftware.cavedroid.game.world.GameWorldBlocksLogicControllerTask;
 import ru.deadsoftware.cavedroid.game.world.GameWorldFluidsLogicControllerTask;
 import ru.deadsoftware.cavedroid.game.world.GameWorldMobDamageControllerTask;
index e96f3af48e3936fb31ccb99ecfac36a8b67d6774..35f318aada2057cfa9d537fa069e54925829dd90 100644 (file)
@@ -13,7 +13,7 @@ import ru.deadsoftware.cavedroid.game.input.handler.mouse.CursorMouseInputHandle
 import ru.deadsoftware.cavedroid.game.input.mapper.KeyboardInputActionMapper;
 import ru.deadsoftware.cavedroid.game.input.mapper.MouseInputActionMapper;
 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
-import ru.deadsoftware.cavedroid.game.mobs.Player;
+import ru.deadsoftware.cavedroid.game.mobs.player.Player;
 import ru.deadsoftware.cavedroid.game.objects.TouchButton;
 import ru.deadsoftware.cavedroid.game.render.IGameRenderer;
 import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager;
index c8147d1987ce8cb7abe2bb05e1990b0d315c2ce6..f34903b143f8d505f3738f67dc68047327662586 100644 (file)
@@ -28,7 +28,7 @@ class DebugInfoStringsProvider @Inject constructor(
             "Mobs: ${mobsController.mobs.size}",
             "Drops: ${dropController.size}",
             "Block: ${gameWorld.getForeMap(player.cursorX, player.cursorY).params.key}",
-            "Hand: ${player.inventory[player.slot].item.params.key}",
+            "Hand: ${player.inventory.activeItem.item.params.key}",
             "Game mode: ${player.gameMode}",
             "Block damage: ${player.blockDamage}"
         )
index 90347c44b86b5723950777f592b3cc50eb1c00cd..0bb0a772e8d0ae54f51f21464a46c02289cfb128 100644 (file)
@@ -6,7 +6,7 @@ import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import javax.inject.Inject
 
 @GameScope
index 6038c271940e3a381ca937316b2d7fca208993fd..bf12c1ad14aed9a0068ef42b9f2ba5e212148bbe 100644 (file)
@@ -6,7 +6,7 @@ import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import javax.inject.Inject
 
 @GameScope
index 07ddb52ca61e3f06f243c4c7f734fda96221c2cd..421e47e5a56ce72de6a3fc6ea602c19583edca48 100644 (file)
@@ -7,7 +7,7 @@ import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.Mob
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import javax.inject.Inject
 
 @GameScope
index 8f8f0385cf3af9a9e8c8d07a4038c34241b15b7c..e3f5bf99f054c658aa6e402994794578f9d707e1 100644 (file)
@@ -7,7 +7,7 @@ import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.Mob
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import javax.inject.Inject
 
 @GameScope
index 762763b2d293b515699a9a75c272d65942fc26a1..1e68cea9f4ca2639f882544553c359d673039268 100644 (file)
@@ -6,7 +6,7 @@ import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import javax.inject.Inject
 
 @GameScope
index acae4b15b2e07a6af0b5f2cf5210463b26d5d684..e6d00536239b1d7d03d369dbbc7967399a6becdf 100644 (file)
@@ -7,7 +7,7 @@ import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import ru.deadsoftware.cavedroid.game.world.GameWorld
 import javax.inject.Inject
 
index 317ddf672277bdbbbfe1753fe792f63c4d100e70..e25a99f7216551c151abb9b6a134c0db6d6f4ac3 100644 (file)
@@ -6,7 +6,7 @@ import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import ru.deadsoftware.cavedroid.game.world.GameWorld
 import javax.inject.Inject
 
index ef0d5eb246bac4cdcab553bd4d74ca41bd0148a3..3159e7fb363f321d95469748a69d07851ab048b4 100644 (file)
@@ -6,7 +6,7 @@ import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import ru.deadsoftware.cavedroid.game.world.GameWorld
 import javax.inject.Inject
 
index 3deacbd34b786f7cc25a2b93faa4fa34dd658ef5..0f656d8eac5a6ab38cb9f1e9e07a5a4d97d631e5 100644 (file)
@@ -6,7 +6,7 @@ import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import javax.inject.Inject
 
 @GameScope
index 05989775e0a5b9245c27e9ef51bc8919a6f92f08..f3b65eb0a226899e466dda68814586f56e574ca2 100644 (file)
@@ -6,7 +6,7 @@ import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import javax.inject.Inject
 
 @GameScope
index 54aacf2b5cd6c6ebeee03517a93bf7578bff8595..062859cf9cd53e95e6ed829098167607364014d5 100644 (file)
@@ -8,7 +8,7 @@ import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
 import ru.deadsoftware.cavedroid.game.mobs.Mob
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import ru.deadsoftware.cavedroid.game.model.block.Block
 import ru.deadsoftware.cavedroid.game.world.GameWorld
 import ru.deadsoftware.cavedroid.misc.utils.bl
index 31827d22a3e5c088ad03ee09fb278b63727f10d4..c7f54acf609f9b6740353b23b38c23b32ada02c6 100644 (file)
@@ -9,6 +9,7 @@ import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
 import ru.deadsoftware.cavedroid.game.input.isInsideHotbar
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import ru.deadsoftware.cavedroid.misc.Assets
 import javax.inject.Inject
 
@@ -51,7 +52,7 @@ class HotbarMouseInputHandler @Inject constructor(
     }
 
     private fun handleUp(action: MouseInputAction) {
-        mobsController.player.slot =
+        mobsController.player.inventory.activeSlot =
             ((action.screenX -
                     (action.cameraViewport.width / 2 - hotbarTexture.regionWidth / 2))
                     / HOTBAR_CELL_WIDTH).toInt()
@@ -61,11 +62,11 @@ class HotbarMouseInputHandler @Inject constructor(
         if (action.actionKey !is MouseInputActionKey.Scroll) {
             return
         }
-        mobsController.player.slot += action.actionKey.amountY.toInt()
-        if (mobsController.player.slot < 0) {
-            mobsController.player.slot = HOTBAR_ITEMS - 1
-        } else if (mobsController.player.slot >= HOTBAR_ITEMS){
-            mobsController.player.slot = 0
+        mobsController.player.inventory.activeSlot += action.actionKey.amountY.toInt()
+        if (mobsController.player.inventory.activeSlot < 0) {
+            mobsController.player.inventory.activeSlot = Player.HOTBAR_SIZE - 1
+        } else if (mobsController.player.inventory.activeSlot >= Player.HOTBAR_SIZE){
+            mobsController.player.inventory.activeSlot = 0
         }
     }
 
@@ -91,7 +92,6 @@ class HotbarMouseInputHandler @Inject constructor(
     companion object {
         private const val TOUCH_HOLD_TIME_SEC = 0.5f
         private const val HOTBAR_CELL_WIDTH = 20
-        private const val HOTBAR_ITEMS = 9
     }
 
 }
\ No newline at end of file
index 7005a679ebf87566e9a7bb1e74e94c8c30e8946b..4395337b1101bc2ec3ea5f22ba5ffbe1523ce8ec 100644 (file)
@@ -74,9 +74,9 @@ class SelectCraftingInventoryItemMouseInputHandler @Inject constructor(
         }
 
         if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
-            onLeftCLick(mobsController.player.inventory, window, itemIndex)
+            onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
         } else {
-            onRightClick(mobsController.player.inventory, window, itemIndex)
+            onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
         }
 
         Gdx.app.debug(
index 7c587b67b7162415ecceeb9525ce568fcdb2831c..032bade3b74875e6dfddbf40537a2ed3ce4d0331 100644 (file)
@@ -46,13 +46,7 @@ class SelectCreativeInventoryItemMouseInputHandler @Inject constructor(
         val itemIndex = (gameWindowsManager.creativeScrollAmount * GameWindowsConfigs.Creative.itemsInRow +
                 (xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Creative.itemsInRow))
         val item = gameItemsHolder.getItemFromCreativeInventory(itemIndex)
-        mobsController.player.inventory.reverse()
-        mobsController.player.inventory.add(item.toInventoryItem(amount = item.params.maxStack))
-        mobsController.player.inventory.reverse()
-
-        if (mobsController.player.inventory.size > 36) {
-            mobsController.player.inventory.dropLast(mobsController.player.inventory.size - 36)
-        }
+        mobsController.player.inventory.addItem(item)
     }
 
 }
\ No newline at end of file
index 61e1b18892e8970a8f65f79d52c841eea5e9088b..828f02fa8fcbb5c4ddde5bd39a4f48707b3a1eac 100644 (file)
@@ -74,9 +74,9 @@ class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
         }
 
         if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
-            onLeftCLick(mobsController.player.inventory, window, itemIndex)
+            onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
         } else {
-            onRightClick(mobsController.player.inventory, window, itemIndex)
+            onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
         }
 
         Gdx.app.debug(
index e2d3f1dc23d737842503ad15dc6316905cae5703..c3b52450b45f8c3ac558ca5796be0901ef08153c 100644 (file)
@@ -43,13 +43,13 @@ class UseItemMouseInputHandler @Inject constructor(
         cancelHold()
 
         val player = mobsController.player
-        val item = player.currentItem.item
+        val item = player.inventory.activeItem.item
         player.startHitting(false)
         player.stopHitting()
 
         if (item is Item.Placeable) {
             placeBlockActionMap.placeToBackgroundAction(
-                item = player.currentItem.item as Item.Placeable,
+                item = item,
                 x = player.cursorX,
                 y = player.cursorY
             )
@@ -69,7 +69,7 @@ class UseItemMouseInputHandler @Inject constructor(
 
     private fun handleUp(action: MouseInputAction) {
         val player = mobsController.player
-        val item = player.currentItem.item
+        val item = player.inventory.activeItem.item
         cancelHold()
 
         player.startHitting(false)
index 0ec111bbd66bd5b96ff38a9cdd7e323d53b7bf0c..df2a6dec5eef62240a5708edf676af8008e003ee 100644 (file)
@@ -2,6 +2,7 @@ package ru.deadsoftware.cavedroid.game.mobs
 
 import ru.deadsoftware.cavedroid.game.GameItemsHolder
 import ru.deadsoftware.cavedroid.game.GameScope
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
 import java.io.Serializable
 import java.util.*
 import javax.inject.Inject
@@ -13,7 +14,8 @@ class MobsController @Inject constructor(
 
     private val _mobs = LinkedList<Mob>()
 
-    val player: Player = Player(gameItemsHolder)
+    val player: Player =
+        Player(gameItemsHolder)
 
     val mobs: List<Mob>
         get() = _mobs
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt
new file mode 100644 (file)
index 0000000..31f4c78
--- /dev/null
@@ -0,0 +1,85 @@
+package ru.deadsoftware.cavedroid.game.mobs.player
+
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
+import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
+import ru.deadsoftware.cavedroid.game.model.item.Item
+import ru.deadsoftware.cavedroid.game.objects.Drop
+import java.io.Serializable
+
+class Inventory(
+    val size: Int,
+    val hotbarSize: Int,
+    gameItemsHolder: GameItemsHolder
+) : Serializable {
+
+    init {
+        if (size < 0 || hotbarSize < 0 || hotbarSize > size) {
+            throw IllegalArgumentException("Invalid inventory sizes: hotbarSize=$hotbarSize; size=$size")
+        }
+    }
+
+    private val _items = Array(size) { InventoryItem(gameItemsHolder.fallbackItem) }
+
+    val items get() = _items.asList() as MutableList<InventoryItem>
+
+    val hotbarItems get() = items.subList(0, hotbarSize)
+
+    var activeSlot = 0
+        set(value) {
+            if (value in 0 ..< hotbarSize) {
+                field = value
+            }
+        }
+
+    val activeItem get() = items[activeSlot]
+
+    fun initItems(gameItemsHolder: GameItemsHolder) {
+        items.forEach { item ->
+            item.init(gameItemsHolder)
+        }
+    }
+
+    private fun getItemPickSlot(item: Item): Int {
+        for (i in items.indices) {
+            val inventoryItem = items[i]
+
+            if (item == inventoryItem.item && inventoryItem.canBeAdded()) {
+                return i
+            }
+
+            if (inventoryItem.item.isNone()) {
+                return i
+            }
+        }
+
+        return -1
+    }
+
+    fun canPickItem(item: Item): Boolean {
+        return getItemPickSlot(item) >= 0
+    }
+
+    fun pickDrop(drop: Drop) {
+        val slot = getItemPickSlot(drop.item).takeIf { it >= 0 } ?: return
+        val inventoryItem = items[slot]
+
+        if (inventoryItem.item == drop.item) {
+            inventoryItem.add()
+            drop.pickedUp = true
+        } else {
+            _items[slot] = drop.item.toInventoryItem()
+            drop.pickedUp = true
+        }
+    }
+
+    fun addItem(item: Item) {
+        _items.copyInto(
+            destination = _items,
+            destinationOffset = 1,
+            startIndex = 0,
+            endIndex = size - 1
+        )
+
+        _items[0] = item.toInventoryItem()
+    }
+}
\ No newline at end of file
similarity index 85%
rename from core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
rename to core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java
index 47d48d8a415374d61279a2c2af1c04d3da291e6a..bcca82606eb711dea6add8f9003243a7e0b94d52 100644 (file)
@@ -1,10 +1,11 @@
-package ru.deadsoftware.cavedroid.game.mobs;
+package ru.deadsoftware.cavedroid.game.mobs.player;
 
 import com.badlogic.gdx.graphics.g2d.Sprite;
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.math.MathUtils;
 import com.badlogic.gdx.math.Vector2;
 import ru.deadsoftware.cavedroid.game.GameItemsHolder;
+import ru.deadsoftware.cavedroid.game.mobs.Mob;
 import ru.deadsoftware.cavedroid.game.model.block.Block;
 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem;
 import ru.deadsoftware.cavedroid.game.model.item.Item;
@@ -15,7 +16,6 @@ import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin;
 import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
 
 import javax.annotation.CheckForNull;
-import java.util.ArrayList;
 
 public class Player extends Mob {
 
@@ -23,12 +23,15 @@ public class Player extends Mob {
     private static final float JUMP_VELOCITY = -133.332f;
     private static final int MAX_HEALTH = 20;
 
+    public static final int INVENTORY_SIZE = 36;
+    public static final int HOTBAR_SIZE = 9;
+
     private boolean hitting = false, hittingWithDamage = false;
     private float hitAnim = 0f;
     private float hitAnimDelta = ANIMATION_SPEED;
 
-    public final ArrayList<InventoryItem> inventory;
-    public int slot;
+    public final Inventory inventory;
+
     public int gameMode;
     public boolean swim;
     public float headRotation = 0f;
@@ -49,17 +52,12 @@ public class Player extends Mob {
 
     public Player(GameItemsHolder gameItemsHolder) {
         super(0, 0, 4, 30, randomDir(), Type.MOB, MAX_HEALTH);
-        inventory = new ArrayList<>(36);
-        for (int i = 0; i < 36; i++) {
-            inventory.add(gameItemsHolder.getFallbackItem().toInventoryItem());
-        }
+        inventory = new Inventory(INVENTORY_SIZE, HOTBAR_SIZE, gameItemsHolder);
         swim = false;
     }
 
     public void initInventory(GameItemsHolder gameItemsHolder) {
-        for (InventoryItem invItem : inventory) {
-            invItem.init(gameItemsHolder);
-        }
+        inventory.initItems(gameItemsHolder);
     }
 
     public void respawn(GameWorld gameWorld, GameItemsHolder itemsHolder) {
@@ -75,52 +73,11 @@ public class Player extends Mob {
         if (gameMode == 1) {
             return;
         }
-        getCurrentItem().setAmount(getCurrentItem().getAmount() - 1);
-        if (getCurrentItem().getAmount() <= 0) {
-            setCurrentInventorySlotItem(gameItemsHolder.getFallbackItem());
-        }
-    }
-
-    public InventoryItem getCurrentItem() {
-        return inventory.get(slot);
-    }
-
-    /**
-     * @return index of inventory where this drop could be placed or -1 if cant pick up
-     */
-    public int canPickUpDrop(Drop drop) {
-        for (int i = 0; i < 36; i++) {
-            final InventoryItem invItem = inventory.get(i);
-
-            if (!invItem.getItem().isTool()
-                    && invItem.getItem() == drop.getItem()
-                    && invItem.getAmount() < invItem.getItem().getParams().getMaxStack()) {
-                return i;
-            }
-
-            if (invItem.getItem().isNone()) {
-                return i;
-            }
-        }
-
-        return -1;
-    }
-
-    public void pickUpDrop(Drop drop) {
-        int index = canPickUpDrop(drop);
-
-        if (index < 0) {
-            return;
-        }
 
-        final InventoryItem invItem = inventory.get(index);
-
-        if (invItem.getItem().equals(drop.getItem())) {
-            invItem.setAmount(invItem.getAmount() + 1);
-            drop.setPickedUp(true);
-        } else if (invItem.getItem().isNone()) {
-            inventory.set(index, drop.getItem().toInventoryItem());
-            drop.setPickedUp(true);
+        final InventoryItem item = inventory.getActiveItem();
+        item.subtract();
+        if (item.getAmount() <= 0) {
+            setCurrentInventorySlotItem(gameItemsHolder.getFallbackItem());
         }
     }
 
@@ -158,7 +115,7 @@ public class Player extends Mob {
     }
 
     public void setCurrentInventorySlotItem(Item item) {
-        inventory.set(slot, item.toInventoryItem());
+        inventory.getItems().set(inventory.getActiveSlot(), item.toInventoryItem());
     }
 
     @Override
@@ -235,7 +192,7 @@ public class Player extends Mob {
         final boolean canHitBlock = target != null;
 
         float multiplier = 1f;
-        final Item currentItem = inventory.get(slot).getItem();
+        final Item currentItem = inventory.getActiveItem().getItem();
         if (currentItem instanceof Item.Tool && canHitBlock) {
             if (target.getParams().getToolType() == currentItem.getClass()
                     && ((Item.Tool)currentItem).getLevel() >= target.getParams().getToolLevel()) {
@@ -277,7 +234,7 @@ public class Player extends Mob {
     }
 
     private void drawItem(SpriteBatch spriteBatch, float x, float y, float anim) {
-        final Item item = inventory.get(slot).getItem();
+        final Item item = inventory.getActiveItem().getItem();
 
         if (item == null || item.isNone()) {
             return;
index 6c1598315cb5037813c8a385ddb06f5f062846ea..aa646cbe8fb88437cae365ea8c4c724fd335319d 100644 (file)
@@ -31,6 +31,21 @@ class InventoryItem @JvmOverloads constructor(
         item = gameItemsHolder.getItem(itemKey)
     }
 
+    @JvmOverloads
+    fun add(count: Int = 1) {
+        amount += count
+    }
+
+    @JvmOverloads
+    fun subtract(count: Int = 1) {
+        add(-count)
+    }
+
+    @JvmOverloads
+    fun canBeAdded(count: Int = 1): Boolean {
+        return !item.isTool() && amount + count <= item.params.maxStack
+    }
+
     private fun drawAmountText(spriteBatch: SpriteBatch, text: String,  x: Float, y: Float) {
         spriteBatch.drawString(text, x + 1, y + 1, Color.BLACK)
         spriteBatch.drawString(text, x, y, Color.WHITE)
index b3f89909e4620db19e5b177cbe132319bad0bc96..c5e02d4db43c14ada35aeb9c6680a29cffac4029 100644 (file)
@@ -5,7 +5,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer
 import com.badlogic.gdx.math.Rectangle
 import ru.deadsoftware.cavedroid.game.GameScope
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player.ControlMode
+import ru.deadsoftware.cavedroid.game.mobs.player.Player.ControlMode
 import ru.deadsoftware.cavedroid.game.world.GameWorld
 import ru.deadsoftware.cavedroid.misc.Assets
 import ru.deadsoftware.cavedroid.misc.utils.px
@@ -57,7 +57,7 @@ class HudRenderer @Inject constructor(
     }
 
     private fun drawHotbarItems(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer,  hotbarX: Float) {
-        mobsController.player.inventory.asSequence().take(HotbarConfig.hotbarCells)
+        mobsController.player.inventory.items.asSequence().take(HotbarConfig.hotbarCells)
             .forEachIndexed { index, item ->
                 if (item.item.isNone()) {
                     return@forEachIndexed
@@ -77,7 +77,7 @@ class HudRenderer @Inject constructor(
         spriteBatch.draw(
             /* region = */ hotbarSelectorTexture,
             /* x = */ hotbarX - HotbarSelectorConfig.horizontalPadding
-                    + mobsController.player.slot * (HotbarConfig.itemSeparatorWidth + HotbarConfig.itemSlotSpace),
+                    + mobsController.player.inventory.activeSlot * (HotbarConfig.itemSeparatorWidth + HotbarConfig.itemSlotSpace),
             /* y = */ -HotbarSelectorConfig.verticalPadding
         )
     }
index 5c95d696c675cadba52dcaf7fd1d8186e9a79491..c768d5f07d1fc512d7b49fd964c0848dbb7d3618 100644 (file)
@@ -7,7 +7,7 @@ import ru.deadsoftware.cavedroid.MainConfig
 import ru.deadsoftware.cavedroid.game.GameScope
 import ru.deadsoftware.cavedroid.game.GameUiWindow
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.mobs.Player.ControlMode
+import ru.deadsoftware.cavedroid.game.mobs.player.Player.ControlMode
 import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager
 import ru.deadsoftware.cavedroid.misc.Assets
 import ru.deadsoftware.cavedroid.misc.utils.ArrayMapExtensions.component1
index 3b08ed7b4fcd712bc60ac1e2fbedc6d0683b06d7..cc8c1baf4522a6cd31fb625b6f6f1977c2475a7a 100644 (file)
@@ -42,7 +42,7 @@ class CraftingWindowRenderer @Inject constructor(
             shapeRenderer = shapeRenderer,
             gridX = windowX + GameWindowsConfigs.Crafting.itemsGridMarginLeft,
             gridY = windowY + GameWindowsConfigs.Crafting.itemsGridMarginTop,
-            items = mobsController.player.inventory.asSequence()
+            items = mobsController.player.inventory.items.asSequence()
                 .drop(GameWindowsConfigs.Crafting.hotbarCells)
                 .take(GameWindowsConfigs.Crafting.itemsInCol * GameWindowsConfigs.Crafting.itemsInRow)
                 .asIterable(),
@@ -56,7 +56,7 @@ class CraftingWindowRenderer @Inject constructor(
             shapeRenderer = shapeRenderer,
             gridX = windowX + GameWindowsConfigs.Crafting.itemsGridMarginLeft,
             gridY = windowY + windowTexture.regionHeight - GameWindowsConfigs.Crafting.hotbarOffsetFromBottom,
-            items = mobsController.player.inventory.asSequence()
+            items = mobsController.player.inventory.items.asSequence()
                 .take(GameWindowsConfigs.Crafting.hotbarCells)
                 .asIterable(),
             itemsInRow = GameWindowsConfigs.Crafting.hotbarCells,
index 4c43600959ee866972f56b68c909c011c0c6b0f4..dede82f94252a226c9fda7bcccbef89f94b88392 100644 (file)
@@ -69,7 +69,7 @@ class CreativeWindowRenderer @Inject constructor(
             shapeRenderer = shapeRenderer,
             gridX = windowX + GameWindowsConfigs.Creative.itemsGridMarginLeft,
             gridY = windowY + creativeWindow.regionHeight - GameWindowsConfigs.Creative.playerInventoryOffsetFromBottom,
-            items = mobsController.player.inventory.asSequence().take(GameWindowsConfigs.Creative.invItems).asIterable(),
+            items = mobsController.player.inventory.items.asSequence().take(GameWindowsConfigs.Creative.invItems).asIterable(),
             itemsInRow = GameWindowsConfigs.Creative.invItems,
             cellWidth = GameWindowsConfigs.Creative.itemsGridColWidth,
             cellHeight = GameWindowsConfigs.Creative.itemsGridRowHeight,
index 56df2cac3ad173f16bc9eeafc2808dbc0f69ce19..ef23be556e35397fb4a87714edcfb9bd1cf356d4 100644 (file)
@@ -78,7 +78,7 @@ class SurvivalWindowRenderer @Inject constructor(
             shapeRenderer = shapeRenderer,
             gridX = windowX + GameWindowsConfigs.Survival.itemsGridMarginLeft,
             gridY = windowY + GameWindowsConfigs.Survival.itemsGridMarginTop,
-            items = mobsController.player.inventory.asSequence()
+            items = mobsController.player.inventory.items.asSequence()
                 .drop(GameWindowsConfigs.Survival.hotbarCells)
                 .take(GameWindowsConfigs.Survival.itemsInCol * GameWindowsConfigs.Survival.itemsInRow)
                 .asIterable(),
@@ -92,7 +92,7 @@ class SurvivalWindowRenderer @Inject constructor(
             shapeRenderer = shapeRenderer,
             gridX = windowX + GameWindowsConfigs.Survival.itemsGridMarginLeft,
             gridY = windowY + windowTexture.regionHeight - GameWindowsConfigs.Survival.hotbarOffsetFromBottom,
-            items = mobsController.player.inventory.asSequence()
+            items = mobsController.player.inventory.items.asSequence()
                 .take(GameWindowsConfigs.Survival.hotbarCells)
                 .asIterable(),
             itemsInRow = GameWindowsConfigs.Survival.hotbarCells,
index f2fd27dd486fa4a3425bd5edce078769350deb19..8c31da7f22aaae2eb0b17678cb787f7ad1eb1fdf 100644 (file)
@@ -175,14 +175,14 @@ public class GameWorld {
     }
 
     private void playerDurateTool() {
-        final InventoryItem playerCurrentItem = mMobsController.getPlayer().getCurrentItem();
-        if (mMobsController.getPlayer().getCurrentItem().getItem().isTool()) {
+        final InventoryItem playerCurrentItem = mMobsController.getPlayer().inventory.getActiveItem();
+        if (playerCurrentItem.getItem().isTool()) {
             mMobsController.getPlayer().decreaseCurrentItemCount(mGameItemsHolder);
         }
     }
 
     private boolean shouldDrop(Block block) {
-        final Item item = mMobsController.getPlayer().getCurrentItem().getItem();
+        final Item item = mMobsController.getPlayer().inventory.getActiveItem().getItem();
         int toolLevel = item.isTool() ? ((Item.Tool)item).getLevel() : 0;
         if (item.isTool() && block.getParams().getToolType() != item.getClass()) {
             toolLevel = 0;