From 6d486d23b0ed6c7f78b1651f1fd1ef209d7b9c2c Mon Sep 17 00:00:00 2001 From: MehdiAyadi <91869915+MehdiAyadi@users.noreply.github.com> Date: Sat, 19 Nov 2022 13:22:54 +0100 Subject: [PATCH] refactored partial input added methods for ActionCommand checking --- .../kotlin/com/last/commit/FirstScreen.kt | 19 ++++-- core/src/main/kotlin/com/last/commit/Game.kt | 5 +- .../src/main/kotlin/com/last/commit/Player.kt | 2 - core/src/main/kotlin/com/last/commit/Wall.kt | 4 +- .../com/last/commit/config/ActionCommand.kt | 3 +- .../com/last/commit/config/PlayerSettings.kt | 59 ++++++++++++++++++- 6 files changed, 78 insertions(+), 14 deletions(-) diff --git a/core/src/main/kotlin/com/last/commit/FirstScreen.kt b/core/src/main/kotlin/com/last/commit/FirstScreen.kt index c7d0565..5273ea1 100644 --- a/core/src/main/kotlin/com/last/commit/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/FirstScreen.kt @@ -23,7 +23,7 @@ import kotlin.math.floor /** First screen of the application. Displayed after the application is created. */ -class FirstScreen : Screen, InputProcessor { +class FirstScreen(val game: Game) : Screen, InputProcessor { private var delta = 0f private var isColliding = false val state = ColorState() @@ -126,7 +126,13 @@ class FirstScreen : Screen, InputProcessor { } private fun handleInput() { + val horizontalMovement = Vector2() + + + //if (Gdx.input.isKeyPressed(game.settings.actionKeys.get(ActionCommand.LEFT))) + + if (Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT)) { horizontalMovement.sub(Vector2.X) } @@ -225,21 +231,22 @@ class FirstScreen : Screen, InputProcessor { } override fun keyUp(keycode: Int): Boolean { - if (keycode == Keys.ESCAPE) { + if (game.settings.isOpenMenuPressed(keycode)) { Gdx.app.exit() } return false } override fun keyTyped(character: Char): Boolean { - if (character == 'e') { + val keyCode = character.code + + if (game.settings.isInteractPressed(keyCode)) { openDoor() - } else if (character == 't') { + } else if (game.settings.isTimeTravelPressed(keyCode)) { map.teleport(player) - } else if (character == 'i') { + } else if (game.settings.isOpenInventoryPressed(keyCode)) { inventoryStage.visible = !inventoryStage.visible } - // TODO Auto-generated method stub return false } diff --git a/core/src/main/kotlin/com/last/commit/Game.kt b/core/src/main/kotlin/com/last/commit/Game.kt index 898697c..5310dc7 100644 --- a/core/src/main/kotlin/com/last/commit/Game.kt +++ b/core/src/main/kotlin/com/last/commit/Game.kt @@ -1,11 +1,14 @@ package com.last.commit import com.badlogic.gdx.Game +import com.last.commit.config.PlayerSettings /** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */ class Game : Game() { + var settings = PlayerSettings() + override fun create() { - setScreen(FirstScreen()) + setScreen(FirstScreen(this)) } } \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/Player.kt b/core/src/main/kotlin/com/last/commit/Player.kt index abc0402..f0fb432 100644 --- a/core/src/main/kotlin/com/last/commit/Player.kt +++ b/core/src/main/kotlin/com/last/commit/Player.kt @@ -4,7 +4,6 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Vector2 -import com.last.commit.config.PlayerSettings import com.last.commit.inventory.Inventory @@ -15,7 +14,6 @@ class Player(private val textureRegion: TextureRegion) : Collidable { private var direction = Vector2.Zero private val movementSpeed = 200f private val interactionRange = 60f - private val settings = PlayerSettings() val inventory = Inventory() diff --git a/core/src/main/kotlin/com/last/commit/Wall.kt b/core/src/main/kotlin/com/last/commit/Wall.kt index 5d84bec..d05e0b9 100644 --- a/core/src/main/kotlin/com/last/commit/Wall.kt +++ b/core/src/main/kotlin/com/last/commit/Wall.kt @@ -17,12 +17,12 @@ open class Wall(var gridX: Int, var gridY: Int, wallCollider: Rectangle, cell: C } fun collidesWidth(collidable: Collidable): Boolean { - return if (isColidable) { + return if (isCollidable) { wallCollider.overlaps(collidable.getCollider()) } else false } - open val isColidable: Boolean + open val isCollidable: Boolean get() = true override fun toString(): String { diff --git a/core/src/main/kotlin/com/last/commit/config/ActionCommand.kt b/core/src/main/kotlin/com/last/commit/config/ActionCommand.kt index 8f1ef6f..64bf6f1 100644 --- a/core/src/main/kotlin/com/last/commit/config/ActionCommand.kt +++ b/core/src/main/kotlin/com/last/commit/config/ActionCommand.kt @@ -7,7 +7,8 @@ enum class ActionCommand { //interaction OPEN_INVENTORY, TIME_TRAVEL, + INTERACT, //program interaction - OPEN_MENU + OPEN_MENU, } \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/config/PlayerSettings.kt b/core/src/main/kotlin/com/last/commit/config/PlayerSettings.kt index 1e53c2a..f24066a 100644 --- a/core/src/main/kotlin/com/last/commit/config/PlayerSettings.kt +++ b/core/src/main/kotlin/com/last/commit/config/PlayerSettings.kt @@ -2,12 +2,13 @@ package com.last.commit.config import com.badlogic.gdx.Input.Keys import java.util.* -import javax.swing.Action class PlayerSettings { val fullscreen: Boolean = false - val actionKeys: EnumMap> = EnumMap(ActionCommand::class.java) + private val actionKeys: EnumMap> = EnumMap(ActionCommand::class.java) + private val actionKeysReversed: HashMap = hashMapOf() + init { @@ -18,6 +19,60 @@ class PlayerSettings { actionKeys[ActionCommand.OPEN_MENU] = listOf(Keys.ESCAPE) actionKeys[ActionCommand.OPEN_INVENTORY] = listOf(Keys.I, Keys.A) actionKeys[ActionCommand.TIME_TRAVEL] = listOf(Keys.T) + actionKeys[ActionCommand.INTERACT] = listOf(Keys.E) + + setReversed(actionKeys) + + } + + private fun setReversed(actionKeys: EnumMap>) { + for (actionCode in actionKeys.keys){ + for (key in actionKeys.getValue(actionCode)) { + actionKeysReversed[key] = actionCode + } + } + + } + + fun getAction(keyCode: Int): ActionCommand? { + return actionKeysReversed[keyCode] + } + + fun getKeyCode(actionCommand: ActionCommand): List? { + return actionKeys[actionCommand] + + } + private fun isActionPressed(actionCommand: ActionCommand, keyCode: Int): Boolean { + return actionCommand == getAction(keyCode) + } + + fun isOpenMenuPressed(keyCode: Int): Boolean { + return isActionPressed(ActionCommand.OPEN_MENU, keyCode) + } + fun isOpenInventoryPressed(keyCode: Int): Boolean { + return isActionPressed(ActionCommand.OPEN_INVENTORY, keyCode) + } + + fun isUpPressed(keyCode: Int): Boolean { + return isActionPressed(ActionCommand.UP, keyCode) + } + + fun isDownPressed(keyCode: Int): Boolean { + return isActionPressed(ActionCommand.DOWN, keyCode) + } + + fun isLeftPressed(keyCode: Int): Boolean { + return isActionPressed(ActionCommand.LEFT, keyCode) + } + fun isRightPressed(keyCode: Int): Boolean { + return isActionPressed(ActionCommand.RIGHT, keyCode) + } + fun isTimeTravelPressed(keyCode: Int): Boolean { + return isActionPressed(ActionCommand.TIME_TRAVEL, keyCode) + } + + fun isInteractPressed(keyCode: Int): Boolean { + return isActionPressed(ActionCommand.INTERACT, keyCode) } } \ No newline at end of file