From: fredboy Date: Tue, 7 May 2024 12:56:12 +0000 (+0700) Subject: Inventory class X-Git-Tag: alpha0.7.1~5 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=c1b8f5c93482cb9933dd608c48f41622ce2994c6;p=cavedroid.git Inventory class --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 248a47f..a2354f0 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -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); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index 94cd34b..d797680 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -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; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index e96f3af..35f318a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -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; diff --git a/core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt b/core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt index c8147d1..f34903b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt @@ -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}" ) diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyDownKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyDownKeyboardInputHandler.kt index 90347c4..0bb0a77 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyDownKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyDownKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyUpKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyUpKeyboardInputHandler.kt index 6038c27..bf12c1a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyUpKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/FlyUpKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoLeftKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoLeftKeyboardInputHandler.kt index 07ddb52..421e47e 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoLeftKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoLeftKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoRightKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoRightKeyboardInputHandler.kt index 8f8f038..e3f5bf9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoRightKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/GoRightKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/JumpKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/JumpKeyboardInputHandler.kt index 762763b..1e68cea 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/JumpKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/JumpKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/MoveCursorControlsModeKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/MoveCursorControlsModeKeyboardInputHandler.kt index acae4b1..e6d0053 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/MoveCursorControlsModeKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/MoveCursorControlsModeKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/StopSwimKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/StopSwimKeyboardInputHandler.kt index 317ddf6..e25a99f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/StopSwimKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/StopSwimKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/SwimUpKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/SwimUpKeyboardInputHandler.kt index ef0d5eb..3159e7f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/SwimUpKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/SwimUpKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/ToggleControlsModeKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/ToggleControlsModeKeyboardInputHandler.kt index 3deacbd..0f656d8 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/ToggleControlsModeKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/ToggleControlsModeKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/TurnOnFlyModeKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/TurnOnFlyModeKeyboardInputHandler.kt index 0598977..f3b65eb 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/TurnOnFlyModeKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/TurnOnFlyModeKeyboardInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CursorMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CursorMouseInputHandler.kt index 54aacf2..062859c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CursorMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CursorMouseInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt index 31827d2..c7f54ac 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt index 7005a67..4395337 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt @@ -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, window, itemIndex) } else { - onRightClick(mobsController.player.inventory, window, itemIndex) + onRightClick(mobsController.player.inventory.items as MutableList, window, itemIndex) } Gdx.app.debug( diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCreativeInventoryItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCreativeInventoryItemMouseInputHandler.kt index 7c587b6..032bade 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCreativeInventoryItemMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCreativeInventoryItemMouseInputHandler.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt index 61e1b18..828f02f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt @@ -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, window, itemIndex) } else { - onRightClick(mobsController.player.inventory, window, itemIndex) + onRightClick(mobsController.player.inventory.items as MutableList, window, itemIndex) } Gdx.app.debug( diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt index e2d3f1d..c3b5245 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/UseItemMouseInputHandler.kt @@ -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) diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.kt index 0ec111b..df2a6de 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.kt @@ -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() - val player: Player = Player(gameItemsHolder) + val player: Player = + Player(gameItemsHolder) val mobs: List 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 index 0000000..31f4c78 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt @@ -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 + + 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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java 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 47d48d8..bcca826 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java @@ -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 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; diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt index 6c15983..aa646cb 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt @@ -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) diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt index b3f8990..c5e02d4 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt @@ -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 ) } diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt index 5c95d69..c768d5f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/windows/CraftingWindowRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/windows/CraftingWindowRenderer.kt index 3b08ed7..cc8c1ba 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/windows/CraftingWindowRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/windows/CraftingWindowRenderer.kt @@ -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, diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt index 4c43600..dede82f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/windows/CreativeWindowRenderer.kt @@ -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, diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/windows/SurvivalWindowRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/windows/SurvivalWindowRenderer.kt index 56df2ca..ef23be5 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/windows/SurvivalWindowRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/windows/SurvivalWindowRenderer.kt @@ -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, diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index f2fd27d..8c31da7 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -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;