DEADSOFTWARE

acae4b15b2e07a6af0b5f2cf5210463b26d5d684
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.keyboard
3 import com.badlogic.gdx.math.MathUtils
4 import ru.deadsoftware.cavedroid.MainConfig
5 import ru.deadsoftware.cavedroid.game.GameScope
6 import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
7 import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction
8 import ru.deadsoftware.cavedroid.game.input.action.keys.KeyboardInputActionKey
9 import ru.deadsoftware.cavedroid.game.mobs.MobsController
10 import ru.deadsoftware.cavedroid.game.mobs.Player
11 import ru.deadsoftware.cavedroid.game.world.GameWorld
12 import javax.inject.Inject
14 @GameScope
15 class MoveCursorControlsModeKeyboardInputHandler @Inject constructor(
16 private val mainConfig: MainConfig,
17 private val mobsController: MobsController,
18 private val gameWorld: GameWorld,
19 ) : IGameInputHandler<KeyboardInputAction> {
21 override fun checkConditions(action: KeyboardInputAction): Boolean {
22 return mainConfig.isTouch &&
23 mobsController.player.controlMode == Player.ControlMode.CURSOR && action.isKeyDown &&
24 (action.actionKey is KeyboardInputActionKey.Left ||
25 action.actionKey is KeyboardInputActionKey.Right ||
26 action.actionKey is KeyboardInputActionKey.Up ||
27 action.actionKey is KeyboardInputActionKey.Down)
28 }
30 private fun checkCursorBounds() {
31 val player = mobsController.player
32 if (player.gameMode == 0) {
33 val minCursorX = player.mapX - SURVIVAL_CURSOR_RANGE
34 val maxCursorX = player.mapX + SURVIVAL_CURSOR_RANGE
35 val minCursorY = player.middleMapY - SURVIVAL_CURSOR_RANGE
36 val maxCursorY = player.middleMapY + SURVIVAL_CURSOR_RANGE
38 player.cursorX = MathUtils.clamp(player.cursorX, minCursorX, maxCursorX)
39 player.cursorY = MathUtils.clamp(player.cursorY, minCursorY, maxCursorY)
40 }
42 player.cursorY = MathUtils.clamp(player.cursorY, 0, gameWorld.height - 1)
43 }
45 override fun handle(action: KeyboardInputAction) {
46 val player = mobsController.player
48 when (action.actionKey) {
49 KeyboardInputActionKey.Left -> player.cursorX--
50 KeyboardInputActionKey.Right -> player.cursorX++
51 KeyboardInputActionKey.Up -> player.cursorY--
52 KeyboardInputActionKey.Down -> player.cursorY++
53 else -> return
54 }
56 checkCursorBounds()
57 }
59 companion object {
60 private const val SURVIVAL_CURSOR_RANGE = 4
61 }
62 }