From: fredboy Date: Tue, 7 May 2024 15:49:41 +0000 (+0700) Subject: Drop items on Q and add empty hearts to health bar X-Git-Tag: alpha0.7.1~1 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=9af7e45e07d903996eab693d958679ed699dcac7;p=cavedroid.git Drop items on Q and add empty hearts to health bar --- diff --git a/android/assets/health.png b/android/assets/health.png index 93314e6..99011d5 100644 Binary files a/android/assets/health.png and b/android/assets/health.png differ diff --git a/android/assets/json/texture_regions.json b/android/assets/json/texture_regions.json index 8000980..6ccbffa 100644 --- a/android/assets/json/texture_regions.json +++ b/android/assets/json/texture_regions.json @@ -103,6 +103,10 @@ "heart_half": { "x": 9, "w": 9 + }, + "heart_empty": { + "x": 18, + "w": 9 } } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/KeyboardInputHandlersModule.kt b/core/src/ru/deadsoftware/cavedroid/game/input/KeyboardInputHandlersModule.kt index 80533d9..0a33d93 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/KeyboardInputHandlersModule.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/KeyboardInputHandlersModule.kt @@ -111,7 +111,7 @@ object KeyboardInputHandlersModule { @Binds @IntoSet @GameScope - fun bindOpenCraftingKeyboardInputHandler(handler: OpenCraftingKeyboardInputHandler): IGameInputHandler { + fun bindOpenCraftingKeyboardInputHandler(handler: DropItemKeyboardInputHandler): IGameInputHandler { return handler } diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/action/keys/KeyboardInputActionKey.kt b/core/src/ru/deadsoftware/cavedroid/game/input/action/keys/KeyboardInputActionKey.kt index cfac132..89ff506 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/action/keys/KeyboardInputActionKey.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/action/keys/KeyboardInputActionKey.kt @@ -9,6 +9,8 @@ sealed interface KeyboardInputActionKey { data object Crouch : KeyboardInputActionKey + data object DropItem : KeyboardInputActionKey + data object SwitchControlsMode : KeyboardInputActionKey data object OpenInventory : KeyboardInputActionKey @@ -19,5 +21,4 @@ sealed interface KeyboardInputActionKey { data object SpawnPig : KeyboardInputActionKey data object SwitchGameMode : KeyboardInputActionKey data object ShowMap : KeyboardInputActionKey - data object OpenCraft : KeyboardInputActionKey } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/DropItemKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/DropItemKeyboardInputHandler.kt new file mode 100644 index 0000000..7e6454c --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/DropItemKeyboardInputHandler.kt @@ -0,0 +1,56 @@ +package ru.deadsoftware.cavedroid.game.input.handler.keyboard + +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.GameUiWindow +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.model.item.Item +import ru.deadsoftware.cavedroid.game.objects.Drop +import ru.deadsoftware.cavedroid.game.objects.DropController +import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager +import javax.inject.Inject + +@GameScope +class DropItemKeyboardInputHandler @Inject constructor( + private val gameWindowsManager: GameWindowsManager, + private val mobsController: MobsController, + private val dropController: DropController, +) : IGameInputHandler { + + override fun checkConditions(action: KeyboardInputAction): Boolean { + return action.actionKey is KeyboardInputActionKey.DropItem && + action.isKeyDown && gameWindowsManager.getCurrentWindow() == GameUiWindow.NONE && + !mobsController.player.inventory.activeItem.item.isNone() + } + + private fun createDrop(item: Item, playerX: Float, playerY: Float) { + dropController.addDrop(playerX + ((DROP_DISTANCE - Drop.DROP_SIZE / 2) * mobsController.player.direction.basis), playerY, item) + } + + override fun handle(action: KeyboardInputAction) { + val player = mobsController.player + val currentItem = player.inventory.activeItem + + if (!currentItem.item.isTool()) { + createDrop(currentItem.item, player.x, player.y) + } else { + for (i in 1..currentItem.amount) { + createDrop(currentItem.item, player.x, player.y) + } + } + + player.inventory.decreaseCurrentItemAmount( + if (currentItem.item.isTool()) { + currentItem.amount + } else { + 1 + } + ) + } + + companion object { + private const val DROP_DISTANCE = 20f + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/OpenCraftingKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/OpenCraftingKeyboardInputHandler.kt deleted file mode 100644 index d2e333c..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/OpenCraftingKeyboardInputHandler.kt +++ /dev/null @@ -1,26 +0,0 @@ -package ru.deadsoftware.cavedroid.game.input.handler.keyboard - -import ru.deadsoftware.cavedroid.game.GameScope -import ru.deadsoftware.cavedroid.game.GameUiWindow -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.objects.DropController -import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager -import javax.inject.Inject - -@GameScope -class OpenCraftingKeyboardInputHandler @Inject constructor( - private val gameWindowsManager: GameWindowsManager, -) : IGameInputHandler { - - override fun checkConditions(action: KeyboardInputAction): Boolean { - return action.actionKey is KeyboardInputActionKey.OpenCraft && - action.isKeyDown && gameWindowsManager.getCurrentWindow() == GameUiWindow.NONE - } - - override fun handle(action: KeyboardInputAction) { - gameWindowsManager.openCrafting() - } -} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/mapper/KeyboardInputActionMapper.kt b/core/src/ru/deadsoftware/cavedroid/game/input/mapper/KeyboardInputActionMapper.kt index f2ed126..61e9793 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/mapper/KeyboardInputActionMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/mapper/KeyboardInputActionMapper.kt @@ -25,7 +25,7 @@ class KeyboardInputActionMapper @Inject constructor() { Input.Keys.GRAVE -> KeyboardInputActionKey.SwitchGameMode Input.Keys.M -> KeyboardInputActionKey.ShowMap - Input.Keys.T -> KeyboardInputActionKey.OpenCraft + Input.Keys.Q -> KeyboardInputActionKey.DropItem else -> null } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt index 4469038..b82aa44 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt @@ -12,7 +12,11 @@ class Inventory( gameItemsHolder: GameItemsHolder ) : Serializable { + @Transient + private lateinit var fallbackItem: InventoryItem + init { + fallbackItem = gameItemsHolder.fallbackItem.toInventoryItem() if (size < 0 || hotbarSize < 0 || hotbarSize > size) { throw IllegalArgumentException("Invalid inventory sizes: hotbarSize=$hotbarSize; size=$size") } @@ -34,6 +38,7 @@ class Inventory( val activeItem get() = items[activeSlot] fun initItems(gameItemsHolder: GameItemsHolder) { + fallbackItem = gameItemsHolder.fallbackItem.toInventoryItem() items.forEach { item -> item.init(gameItemsHolder) } @@ -82,4 +87,12 @@ class Inventory( _items[0] = item.toInventoryItem(item.params.maxStack) } + + @JvmOverloads + fun decreaseCurrentItemAmount(count: Int = 1) { + activeItem.subtract(count) + if (activeItem.amount <= 0) { + _items[activeSlot] = fallbackItem + } + } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java index bcca826..dcc77ef 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java @@ -21,8 +21,8 @@ public class Player extends Mob { private static final float SPEED = 69.072f; private static final float JUMP_VELOCITY = -133.332f; - private static final int MAX_HEALTH = 20; + public static final int MAX_HEALTH = 20; public static final int INVENTORY_SIZE = 36; public static final int HOTBAR_SIZE = 9; 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 5121ab3..7652243 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt @@ -12,9 +12,18 @@ import java.io.Serializable class InventoryItem @JvmOverloads constructor( val itemKey: String, - var amount: Int = 1, + _amount: Int = 1, ) : Serializable { + var amount = _amount + set(value) { + field = if (value < 0) { + 0 + } else { + value + } + } + @Transient lateinit var item: Item private set @@ -33,11 +42,19 @@ class InventoryItem @JvmOverloads constructor( @JvmOverloads fun add(count: Int = 1) { + if (count > 0 && Int.MAX_VALUE - count < amount) { + throw IllegalArgumentException("$amount + $count exceeds Int.MAX_VALUE") + } + amount += count } @JvmOverloads fun subtract(count: Int = 1) { + if (count < 0) { + throw IllegalArgumentException("Can't subtract negative amount") + } + add(-count) } diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt index d7c6a45..5b10fed 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt @@ -47,7 +47,7 @@ class Drop( } companion object { - private const val MAGNET_DISTANCE = 4f + private const val MAGNET_DISTANCE = 8f const val MAGNET_VELOCITY = 256f const val DROP_SIZE = 8f diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt index c5e02d4..1053a9f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt @@ -5,6 +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.Player import ru.deadsoftware.cavedroid.game.mobs.player.Player.ControlMode import ru.deadsoftware.cavedroid.game.world.GameWorld import ru.deadsoftware.cavedroid.misc.Assets @@ -23,6 +24,7 @@ class HudRenderer @Inject constructor( private val hotbarTexture get() = requireNotNull(Assets.textureRegions[HOTBAR_KEY]) private val hotbarSelectorTexture get() = requireNotNull(Assets.textureRegions[HOTBAR_SELECTOR_KEY]) private val wholeHeartTexture get() = requireNotNull(Assets.textureRegions[WHOLE_HEART_KEY]) + private val emptyHeartTexture get() = requireNotNull(Assets.textureRegions[EMPTY_HEART_KEY]) private val halfHeartTexture get() = requireNotNull(Assets.textureRegions[HALF_HEART_KEY]) private fun drawCursor(spriteBatch: SpriteBatch, viewport: Rectangle) { @@ -45,15 +47,25 @@ class HudRenderer @Inject constructor( } val wholeHeart = wholeHeartTexture + val halfHeart = halfHeartTexture + val emptyHeart = emptyHeartTexture + + val totalHearts = Player.MAX_HEALTH / 2 val wholeHearts = player.health / 2 - for (i in 0..