From 3bafeb347e0785f1afa73a1738c958b2e7c52f4a Mon Sep 17 00:00:00 2001 From: fredboy Date: Fri, 3 May 2024 19:44:15 +0700 Subject: [PATCH] Fix swimming --- .../game/input/KeyboardInputHandlersModule.kt | 14 ++++++++ .../action/keys/KeyboardInputActionKey.kt | 3 +- .../keyboard/FlyUpKeyboardInputHandler.kt | 3 +- .../keyboard/JumpKeyboardInputHandler.kt | 2 +- ...eCursorControlsModeKeyboardInputHandler.kt | 4 +-- .../keyboard/StopSwimKeyboardInputHandler.kt | 30 ++++++++++++++++ .../keyboard/SwimUpKeyboardInputHandler.kt | 36 +++++++++++++++++++ .../TurnOnFlyModeKeyboardInputHandler.kt | 3 +- .../input/mapper/KeyboardInputActionMapper.kt | 2 +- 9 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/StopSwimKeyboardInputHandler.kt create mode 100644 core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/SwimUpKeyboardInputHandler.kt diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/KeyboardInputHandlersModule.kt b/core/src/ru/deadsoftware/cavedroid/game/input/KeyboardInputHandlersModule.kt index f590398..80533d9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/KeyboardInputHandlersModule.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/KeyboardInputHandlersModule.kt @@ -115,4 +115,18 @@ object KeyboardInputHandlersModule { return handler } + @Binds + @IntoSet + @GameScope + fun bindSwimUpKeyboardInputHandler(handler: SwimUpKeyboardInputHandler): IGameInputHandler { + return handler + } + + @Binds + @IntoSet + @GameScope + fun bindStopSwimKeyboardInputHandler(handler: StopSwimKeyboardInputHandler): IGameInputHandler { + return handler + } + } \ No newline at end of file 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 fb59efb..cfac132 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 @@ -5,8 +5,7 @@ sealed interface KeyboardInputActionKey { data object Left : KeyboardInputActionKey data object Right : KeyboardInputActionKey data object Down : KeyboardInputActionKey - - data object Jump : KeyboardInputActionKey + data object Up : KeyboardInputActionKey data object Crouch : KeyboardInputActionKey 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 2c6efc9..6038c27 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 @@ -16,7 +16,8 @@ class FlyUpKeyboardInputHandler @Inject constructor( ) : IGameInputHandler { override fun checkConditions(action: KeyboardInputAction): Boolean { - return action.actionKey is KeyboardInputActionKey.Jump && + return action.actionKey is KeyboardInputActionKey.Up && + !mobsController.player.swim && mobsController.player.isFlyMode && (mobsController.player.controlMode == Player.ControlMode.WALK || !mainConfig.isTouch) } 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 1fbc9fb..762763b 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 @@ -16,7 +16,7 @@ class JumpKeyboardInputHandler @Inject constructor( ) : IGameInputHandler { override fun checkConditions(action: KeyboardInputAction): Boolean { - return action.actionKey is KeyboardInputActionKey.Jump && + return action.actionKey is KeyboardInputActionKey.Up && mobsController.player.canJump() && !mobsController.player.isFlyMode && action.isKeyDown && (mobsController.player.controlMode == Player.ControlMode.WALK || !mainConfig.isTouch) 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 ac82cb8..acae4b1 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 @@ -23,7 +23,7 @@ class MoveCursorControlsModeKeyboardInputHandler @Inject constructor( mobsController.player.controlMode == Player.ControlMode.CURSOR && action.isKeyDown && (action.actionKey is KeyboardInputActionKey.Left || action.actionKey is KeyboardInputActionKey.Right || - action.actionKey is KeyboardInputActionKey.Jump || + action.actionKey is KeyboardInputActionKey.Up || action.actionKey is KeyboardInputActionKey.Down) } @@ -48,7 +48,7 @@ class MoveCursorControlsModeKeyboardInputHandler @Inject constructor( when (action.actionKey) { KeyboardInputActionKey.Left -> player.cursorX-- KeyboardInputActionKey.Right -> player.cursorX++ - KeyboardInputActionKey.Jump -> player.cursorY-- + KeyboardInputActionKey.Up -> player.cursorY-- KeyboardInputActionKey.Down -> player.cursorY++ else -> return } 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 new file mode 100644 index 0000000..317ddf6 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/StopSwimKeyboardInputHandler.kt @@ -0,0 +1,30 @@ +package ru.deadsoftware.cavedroid.game.input.handler.keyboard + +import ru.deadsoftware.cavedroid.MainConfig +import ru.deadsoftware.cavedroid.game.GameScope +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.world.GameWorld +import javax.inject.Inject + +@GameScope +class StopSwimKeyboardInputHandler @Inject constructor( + private val mainConfig: MainConfig, + private val mobsController: MobsController, + private val gameWorld: GameWorld, +) : IGameInputHandler { + + override fun checkConditions(action: KeyboardInputAction): Boolean { + return action.actionKey is KeyboardInputActionKey.Up && !action.isKeyDown && + mobsController.player.swim && + (mobsController.player.controlMode == Player.ControlMode.WALK || !mainConfig.isTouch) + } + + override fun handle(action: KeyboardInputAction) { + mobsController.player.swim = false + } + +} \ No newline at end of file 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 new file mode 100644 index 0000000..ef0d5eb --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/SwimUpKeyboardInputHandler.kt @@ -0,0 +1,36 @@ +package ru.deadsoftware.cavedroid.game.input.handler.keyboard + +import ru.deadsoftware.cavedroid.MainConfig +import ru.deadsoftware.cavedroid.game.GameScope +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.world.GameWorld +import javax.inject.Inject + +@GameScope +class SwimUpKeyboardInputHandler @Inject constructor( + private val mainConfig: MainConfig, + private val mobsController: MobsController, + private val gameWorld: GameWorld, +) : IGameInputHandler { + + private fun checkSwim(): Boolean { + return gameWorld.getForeMap(mobsController.player.mapX, mobsController.player.lowerMapY).isFluid() + } + + override fun checkConditions(action: KeyboardInputAction): Boolean { + return action.actionKey is KeyboardInputActionKey.Up && action.isKeyDown && + !mobsController.player.swim && + !mobsController.player.canJump() && + checkSwim() && !mobsController.player.isFlyMode && + (mobsController.player.controlMode == Player.ControlMode.WALK || !mainConfig.isTouch) + } + + override fun handle(action: KeyboardInputAction) { + mobsController.player.swim = true + } + +} \ No newline at end of file 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 6a73a9b..0598977 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 @@ -16,7 +16,8 @@ class TurnOnFlyModeKeyboardInputHandler @Inject constructor( ) : IGameInputHandler { override fun checkConditions(action: KeyboardInputAction): Boolean { - return mobsController.player.gameMode == 1 && action.actionKey is KeyboardInputActionKey.Jump && + return mobsController.player.gameMode == 1 && action.actionKey is KeyboardInputActionKey.Up && + !mobsController.player.swim && !mobsController.player.isFlyMode && !mobsController.player.canJump() && action.isKeyDown && (mobsController.player.controlMode == Player.ControlMode.WALK || !mainConfig.isTouch) } 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 ca91f48..f2ed126 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/mapper/KeyboardInputActionMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/mapper/KeyboardInputActionMapper.kt @@ -13,7 +13,7 @@ class KeyboardInputActionMapper @Inject constructor() { val actionKey = when (key) { Input.Keys.A, Input.Keys.LEFT -> KeyboardInputActionKey.Left Input.Keys.D, Input.Keys.RIGHT -> KeyboardInputActionKey.Right - Input.Keys.W, Input.Keys.SPACE -> KeyboardInputActionKey.Jump + Input.Keys.W, Input.Keys.SPACE -> KeyboardInputActionKey.Up Input.Keys.S -> KeyboardInputActionKey.Down Input.Keys.E -> KeyboardInputActionKey.OpenInventory -- 2.29.2