refactored partial input

added methods for ActionCommand checking
viewport-stuff
MehdiAyadi 1 year ago
parent 16ea8479df
commit 6d486d23b0

@ -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
}

@ -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))
}
}

@ -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()

@ -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 {

@ -7,7 +7,8 @@ enum class ActionCommand {
//interaction
OPEN_INVENTORY, TIME_TRAVEL,
INTERACT,
//program interaction
OPEN_MENU
OPEN_MENU,
}

@ -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<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()
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<ActionCommand, List<Int>>) {
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<Int>? {
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