From dfe9ff3e9253c250f62ee029411f773916948601 Mon Sep 17 00:00:00 2001 From: fredboy Date: Tue, 23 Apr 2024 11:05:17 +0700 Subject: [PATCH] Add some survival inventory controls --- .../cavedroid/game/GameRenderer.java | 9 ++- .../game/input/MouseInputHandlersModule.kt | 7 ++ ...tSurvivalInventoryItemMouseInputHandler.kt | 65 +++++++++++++++++++ .../game/model/item/InventoryItem.kt | 13 ++++ .../game/render/TouchControlsRenderer.kt | 5 +- .../render/windows/SurvivalWindowRenderer.kt | 62 ++++++++---------- .../game/windows/GameWindowsConfigs.kt | 19 ++++++ .../game/windows/GameWindowsManager.kt | 2 + 8 files changed, 144 insertions(+), 38 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 7647e69..8118fec 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -16,6 +16,7 @@ import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.Player; import ru.deadsoftware.cavedroid.game.objects.TouchButton; import ru.deadsoftware.cavedroid.game.render.IGameRenderer; +import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager; import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.Renderer; @@ -39,6 +40,7 @@ public class GameRenderer extends Renderer { private final KeyboardInputActionMapper mKeyboardInputActionMapper; private final Set> mMouseInputHandlers; private final Set> mKeyboardInputHandlers; + private final GameWindowsManager mGameWindowsManager; @Inject GameRenderer(MainConfig mainConfig, @@ -48,7 +50,8 @@ public class GameRenderer extends Renderer { MouseInputActionMapper mouseInputActionMapper, KeyboardInputActionMapper keyboardInputActionMapper, Set> mouseInputHandlers, - Set> keyboardInputHandlers) { + Set> keyboardInputHandlers, + GameWindowsManager gameWindowsManager) { super(mainConfig.getWidth(), mainConfig.getHeight()); mMainConfig = mainConfig; @@ -60,6 +63,7 @@ public class GameRenderer extends Renderer { mKeyboardInputActionMapper = keyboardInputActionMapper; mMouseInputHandlers = mouseInputHandlers; mKeyboardInputHandlers = keyboardInputHandlers; + mGameWindowsManager = gameWindowsManager; Gdx.gl.glClearColor(0f, .6f, .6f, 1f); } @@ -134,6 +138,9 @@ public class GameRenderer extends Renderer { } private TouchButton getTouchedKey(float touchX, float touchY) { + if (mGameWindowsManager.getCurrentWindow() != GameUiWindow.NONE) { + return nullButton; + } for (ObjectMap.Entry entry : Assets.guiMap) { TouchButton button = entry.value; if (button.getRect().contains(touchX, touchY)) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/MouseInputHandlersModule.kt b/core/src/ru/deadsoftware/cavedroid/game/input/MouseInputHandlersModule.kt index e17662e..aef4c0b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/MouseInputHandlersModule.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/MouseInputHandlersModule.kt @@ -58,4 +58,11 @@ object MouseInputHandlersModule { fun bindUseItemMouseInputActionHandler(handler: UseItemMouseInputHandler): IGameInputHandler { return handler } + + @Binds + @IntoSet + @GameScope + fun bindSelectSurvivalInventoryItemMouseInputHandler(handler: SelectSurvivalInventoryItemMouseInputHandler): IGameInputHandler { + return handler + } } \ 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 new file mode 100644 index 0000000..559ccfc --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt @@ -0,0 +1,65 @@ +package ru.deadsoftware.cavedroid.game.input.handler.mouse + +import com.badlogic.gdx.Gdx +import ru.deadsoftware.cavedroid.game.GameItemsHolder +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.MouseInputAction +import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey +import ru.deadsoftware.cavedroid.game.input.isInsideWindow +import ru.deadsoftware.cavedroid.game.mobs.MobsController +import ru.deadsoftware.cavedroid.game.windows.GameWindowsConfigs +import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager +import ru.deadsoftware.cavedroid.misc.Assets +import javax.inject.Inject + +@GameScope +class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor( + private val gameWindowsManager: GameWindowsManager, + private val mobsController: MobsController, + private val gameItemsHolder: GameItemsHolder, +) : IGameInputHandler { + + private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"]) + + override fun checkConditions(action: MouseInputAction): Boolean { + return gameWindowsManager.getCurrentWindow() == GameUiWindow.SURVIVAL_INVENTORY && + isInsideWindow(action, survivalWindowTexture) && + (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) + && action.actionKey.touchUp + } + + override fun handle(action: MouseInputAction) { + val survivalTexture = survivalWindowTexture + val xOnGrid = (action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2 + + GameWindowsConfigs.Survival.itemsGridMarginLeft)) / + GameWindowsConfigs.Survival.itemsGridColWidth + val yOnGrid = (action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2 + + GameWindowsConfigs.Survival.itemsGridMarginTop)) / + GameWindowsConfigs.Survival.itemsGridRowHeight + + if (xOnGrid < 0 || xOnGrid >= GameWindowsConfigs.Survival.itemsInRow || + yOnGrid < 0 || yOnGrid > GameWindowsConfigs.Survival.itemsInCol) { + return + } + + var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Survival.itemsInRow)) + itemIndex += GameWindowsConfigs.Survival.hotbarCells + + if (itemIndex >= mobsController.player.inventory.size) { + itemIndex -= mobsController.player.inventory.size + } + + val item = mobsController.player.inventory[itemIndex] + mobsController.player.inventory[itemIndex] = gameWindowsManager.selectedItem ?: gameItemsHolder.fallbackItem.toInventoryItem() + gameWindowsManager.selectedItem = item + + Gdx.app.debug(TAG, "selected item: ${gameWindowsManager.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)") + } + + companion object { + private const val TAG = "SelectSurvivalInventoryItemMouseInputHandler" + + } +} \ No newline at end of file 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 49cc59d..8918659 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt @@ -35,6 +35,19 @@ class InventoryItem @JvmOverloads constructor( spriteBatch.drawString(text, x, y, Color.WHITE) } + fun drawSelected(spriteBatch: SpriteBatch, x: Float, y: Float) { + if (item.isNone()) { + return + } + + val sprite = item.sprite + sprite.setOriginCenter() + sprite.setPosition(x, y) + sprite.setScale(1.25f) + sprite.draw(spriteBatch) + sprite.setScale(1f) + } + fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, x: Float, y: Float) { if (item.isNone()) { return diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt index d9dc5c4..5c95d69 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt @@ -5,8 +5,10 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.math.Rectangle 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.windows.GameWindowsManager import ru.deadsoftware.cavedroid.misc.Assets import ru.deadsoftware.cavedroid.misc.utils.ArrayMapExtensions.component1 import ru.deadsoftware.cavedroid.misc.utils.ArrayMapExtensions.component2 @@ -16,6 +18,7 @@ import javax.inject.Inject class TouchControlsRenderer @Inject constructor( private val mainConfig: MainConfig, private val mobsController: MobsController, + private val gameWindowsManager: GameWindowsManager, ) : IGameRenderer { override val renderLayer get() = RENDER_LAYER @@ -23,7 +26,7 @@ class TouchControlsRenderer @Inject constructor( private val shadeTexture get() = Assets.textureRegions[SHADE_KEY] override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) { - if (!mainConfig.isTouch) { + if (!mainConfig.isTouch || gameWindowsManager.getCurrentWindow() != GameUiWindow.NONE) { return } 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 f8a6ac0..39580de 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/windows/SurvivalWindowRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/windows/SurvivalWindowRenderer.kt @@ -11,6 +11,8 @@ import ru.deadsoftware.cavedroid.game.mobs.Mob import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.render.IGameRenderer import ru.deadsoftware.cavedroid.game.render.WindowsRenderer +import ru.deadsoftware.cavedroid.game.windows.GameWindowsConfigs +import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager import ru.deadsoftware.cavedroid.misc.Assets import javax.inject.Inject import kotlin.math.atan @@ -19,6 +21,7 @@ import kotlin.math.atan class SurvivalWindowRenderer @Inject constructor( private val mainConfig: MainConfig, private val mobsController: MobsController, + private val gameWindowsManager: GameWindowsManager, ) : AbstractWindowRenderer(), IGameRenderer { override val renderLayer get() = WindowsRenderer.RENDER_LAYER @@ -47,10 +50,10 @@ class SurvivalWindowRenderer @Inject constructor( } private fun drawPlayerPortrait(spriteBatch: SpriteBatch, windowX: Float, windowY: Float, delta: Float) { - val portraitX = windowX + Config.portraitMarginLeft + - (Config.portraitWidth / 2 - mobsController.player.width / 2) - val portraitY = windowY + Config.portraitMarginTop + - (Config.portraitHeight / 2 - mobsController.player.height / 2) + val portraitX = windowX + GameWindowsConfigs.Survival.portraitMarginLeft + + (GameWindowsConfigs.Survival.portraitWidth / 2 - mobsController.player.width / 2) + val portraitY = windowY + GameWindowsConfigs.Survival.portraitMarginTop + + (GameWindowsConfigs.Survival.portraitHeight / 2 - mobsController.player.height / 2) setPortraitHeadRotation(portraitX, portraitY) mobsController.player.draw(spriteBatch, portraitX, portraitY, delta) @@ -69,51 +72,38 @@ class SurvivalWindowRenderer @Inject constructor( drawItemsGrid( spriteBatch = spriteBatch, shapeRenderer = shapeRenderer, - gridX = windowX + Config.itemsGridMarginLeft, - gridY = windowY + Config.itemsGridMarginTop, + gridX = windowX + GameWindowsConfigs.Survival.itemsGridMarginLeft, + gridY = windowY + GameWindowsConfigs.Survival.itemsGridMarginTop, items = mobsController.player.inventory.asSequence() - .drop(Config.hotbarCells) - .take(Config.itemsInCol * Config.itemsInRow) + .drop(GameWindowsConfigs.Survival.hotbarCells) + .take(GameWindowsConfigs.Survival.itemsInCol * GameWindowsConfigs.Survival.itemsInRow) .asIterable(), - itemsInRow = Config.itemsInRow, - cellWidth = Config.itemsGridColWidth, - cellHeight = Config.itemsGridRowHeight, + itemsInRow = GameWindowsConfigs.Survival.itemsInRow, + cellWidth = GameWindowsConfigs.Survival.itemsGridColWidth, + cellHeight = GameWindowsConfigs.Survival.itemsGridRowHeight, ) drawItemsGrid( spriteBatch = spriteBatch, shapeRenderer = shapeRenderer, - gridX = windowX + Config.itemsGridMarginLeft, - gridY = windowY + survivalWindow.regionHeight - Config.hotbarOffsetFromBottom, + gridX = windowX + GameWindowsConfigs.Survival.itemsGridMarginLeft, + gridY = windowY + survivalWindow.regionHeight - GameWindowsConfigs.Survival.hotbarOffsetFromBottom, items = mobsController.player.inventory.asSequence() - .take(Config.hotbarCells) + .take(GameWindowsConfigs.Survival.hotbarCells) .asIterable(), - itemsInRow = Config.hotbarCells, - cellWidth = Config.itemsGridColWidth, - cellHeight = Config.itemsGridRowHeight, + itemsInRow = GameWindowsConfigs.Survival.hotbarCells, + cellWidth = GameWindowsConfigs.Survival.itemsGridColWidth, + cellHeight = GameWindowsConfigs.Survival.itemsGridRowHeight, + ) + + gameWindowsManager.selectedItem?.drawSelected( + spriteBatch = spriteBatch, + x = Gdx.input.x * (viewport.width / Gdx.graphics.width), + y = Gdx.input.y * (viewport.height / Gdx.graphics.height) ) } companion object { private const val SURVIVAL_WINDOW_KEY = "survival" - - private data object Config { - const val itemsGridMarginLeft = 8f - const val itemsGridMarginTop = 84f - - const val itemsGridRowHeight = 18f - const val itemsGridColWidth = 18f - - const val itemsInRow = 8 - const val itemsInCol = 5 - - const val hotbarOffsetFromBottom = 24f - const val hotbarCells = 9 - - const val portraitMarginLeft = 24f - const val portraitMarginTop = 8f - const val portraitWidth = 48f - const val portraitHeight = 68f - } } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsConfigs.kt b/core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsConfigs.kt index cf81127..6dde753 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsConfigs.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsConfigs.kt @@ -21,4 +21,23 @@ object GameWindowsConfigs { val itemsOnPage get() = itemsInCol * itemsInRow } + + data object Survival { + const val itemsGridMarginLeft = 8f + const val itemsGridMarginTop = 84f + + const val itemsGridRowHeight = 18f + const val itemsGridColWidth = 18f + + const val itemsInRow = 9 + const val itemsInCol = 5 + + const val hotbarOffsetFromBottom = 24f + const val hotbarCells = 9 + + const val portraitMarginLeft = 24f + const val portraitMarginTop = 8f + const val portraitWidth = 48f + const val portraitHeight = 68f + } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsManager.kt b/core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsManager.kt index 2247635..302562f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsManager.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsManager.kt @@ -4,6 +4,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.model.item.InventoryItem import javax.inject.Inject @GameScope @@ -14,6 +15,7 @@ class GameWindowsManager @Inject constructor( var creativeScrollAmount = 0 var isDragging = false + var selectedItem: InventoryItem? = null fun getCurrentWindow(): GameUiWindow { return mainConfig.gameUiWindow -- 2.29.2