fix keycode mapping and checking

viewport-stuff
MehdiAyadi 2 years ago
parent 0a0e5334b5
commit 90532e9147

@ -1,5 +1,6 @@
package com.last.commit package com.last.commit
import GameState
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input.Keys import com.badlogic.gdx.Input.Keys
import com.badlogic.gdx.InputProcessor import com.badlogic.gdx.InputProcessor
@ -15,12 +16,12 @@ import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.math.Vector3
import com.badlogic.gdx.utils.Json import com.badlogic.gdx.utils.Json
import com.last.commit.config.ActionCommand
import com.last.commit.config.GameConfig import com.last.commit.config.GameConfig
import com.last.commit.map.Interactable import com.last.commit.map.Interactable
import com.last.commit.map.TimeMap import com.last.commit.map.TimeMap
import com.last.commit.stages.InventoryStage import com.last.commit.stages.InventoryStage
import kotlin.math.floor import kotlin.math.floor
import GameState
/** First screen of the application. Displayed after the application is created. */ /** First screen of the application. Displayed after the application is created. */
class FirstScreen(val gameState: GameState) : Screen, InputProcessor { class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
@ -245,26 +246,25 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
} }
override fun keyUp(keycode: Int): Boolean { override fun keyUp(keycode: Int): Boolean {
if (keycode == Keys.ESCAPE) { if (gameState.settings.getAction(keycode) == ActionCommand.OPEN_MENU) {
Gdx.app.exit() Gdx.app.exit()
} }
return false return false
} }
override fun keyTyped(character: Char): Boolean { override fun keyTyped(character: Char): Boolean {
val keyCode = character.code val characterUpperCase = character.uppercase()
val characterKey = Keys.valueOf(characterUpperCase)
if (gameState.settings.isInteractPressed(keyCode)) { if (gameState.settings.getAction(characterKey) == ActionCommand.INTERACT) {
openDoor() openDoor()
} else if (gameState.settings.isTimeTravelPressed(keyCode)) { } else if (gameState.settings.getAction(characterKey) == ActionCommand.TIME_TRAVEL) {
map.teleport(player) map.teleport(player)
} else if (gameState.settings.isOpenInventoryPressed(keyCode)) { } else if (gameState.settings.getAction(characterKey) == ActionCommand.OPEN_INVENTORY) {
inventoryStage.visible = !inventoryStage.visible inventoryStage.visible = !inventoryStage.visible
} else if (character == 'p') { } else if (character == 'p') {
gameState.inventory.add("compass") gameState.inventory.add("compass")
inventoryStage.refresh() inventoryStage.refresh()
} else if (gameState.settings.isInteractPressed(keyCode)) {
this.openDoor()
} }
return false return false
} }

@ -1,9 +1,9 @@
package com.last.commit package com.last.commit
import GameState
import com.badlogic.gdx.Game import com.badlogic.gdx.Game
import com.last.commit.config.GameSettings
import com.last.commit.inventory.Inventory import com.last.commit.inventory.Inventory
import com.last.commit.config.PlayerSettings
import GameState
/** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */ /** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */
class Game : Game() { class Game : Game() {
@ -18,7 +18,7 @@ class Game : Game() {
fun createState() { fun createState() {
state = GameState( state = GameState(
Inventory(), Inventory(),
PlayerSettings() GameSettings()
) )
} }
} }

@ -1,7 +1,7 @@
import com.last.commit.inventory.Inventory import com.last.commit.inventory.Inventory
import com.last.commit.config.PlayerSettings import com.last.commit.config.GameSettings
public data class GameState( data class GameState(
public val inventory: Inventory, val inventory: Inventory,
public val settings: PlayerSettings, val settings: GameSettings,
) )

@ -3,21 +3,20 @@ package com.last.commit.config
import com.badlogic.gdx.Input.Keys import com.badlogic.gdx.Input.Keys
import java.util.* import java.util.*
class PlayerSettings { class GameSettings {
val fullscreen: Boolean = false val fullscreen: Boolean = false
private val actionKeys: EnumMap<ActionCommand, List<Int>> = EnumMap(ActionCommand::class.java) private val actionKeys: EnumMap<ActionCommand, List<Int>> = EnumMap(ActionCommand::class.java)
private val actionKeysReversed: HashMap<Int, ActionCommand> = hashMapOf() private val actionKeysReversed: HashMap<Int, ActionCommand> = hashMapOf()
init { init {
actionKeys[ActionCommand.UP] = listOf(Keys.UP, Keys.W) actionKeys[ActionCommand.UP] = listOf(Keys.UP, Keys.W)
actionKeys[ActionCommand.DOWN] = listOf(Keys.DOWN, Keys.S) actionKeys[ActionCommand.DOWN] = listOf(Keys.DOWN, Keys.S)
actionKeys[ActionCommand.LEFT] = listOf(Keys.LEFT, Keys.A) actionKeys[ActionCommand.LEFT] = listOf(Keys.LEFT, Keys.A)
actionKeys[ActionCommand.RIGHT] = listOf(Keys.RIGHT, Keys.D) actionKeys[ActionCommand.RIGHT] = listOf(Keys.RIGHT, Keys.D)
actionKeys[ActionCommand.OPEN_MENU] = listOf(Keys.ESCAPE) actionKeys[ActionCommand.OPEN_MENU] = listOf(Keys.ESCAPE)
actionKeys[ActionCommand.OPEN_INVENTORY] = listOf(Keys.I, Keys.A) actionKeys[ActionCommand.OPEN_INVENTORY] = listOf(Keys.I)
actionKeys[ActionCommand.TIME_TRAVEL] = listOf(Keys.T) actionKeys[ActionCommand.TIME_TRAVEL] = listOf(Keys.T)
actionKeys[ActionCommand.INTERACT] = listOf(Keys.E) actionKeys[ActionCommand.INTERACT] = listOf(Keys.E)
@ -38,41 +37,10 @@ class PlayerSettings {
return actionKeysReversed[keyCode] return actionKeysReversed[keyCode]
} }
fun getKeyCode(actionCommand: ActionCommand): List<Int>? { fun getKeyCode(actionCommand: ActionCommand): List<Int>? {
return actionKeys[actionCommand] 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)
}
} }
Loading…
Cancel
Save