made GameInputProcessor.kt not defined in screen

added support for multiple screens
imgbot
MehdiAyadi 2 years ago
parent 40d38ee057
commit eeba41edbc

@ -2,19 +2,48 @@ package com.last.commit
import GameState
import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.last.commit.audio.SoundEngine
import com.last.commit.config.GameSettings
import com.last.commit.inventory.Inventory
import com.last.commit.audio.SoundEngine
import com.last.commit.screen.FirstScreen
import com.last.commit.screen.*
import java.awt.AWTEventMulticaster
/** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */
class Game : Game() {
private lateinit var state: GameState
lateinit var state: GameState
lateinit var menu : TimeTravelScreen
lateinit var gameplay: TimeTravelScreen
lateinit var settings: TimeTravelScreen
lateinit var inputProcessor: GameInputProcessor
override fun create() {
createState()
setScreen(FirstScreen(state))
createScreens()
changeScreen(Screens.GAME)
inputProcessor = GameInputProcessor(this)
Gdx.input.inputProcessor = inputProcessor
}
private fun createScreens() {
menu = MainMenu(this)
gameplay = FirstScreen(this)
settings = Settings(this)
}
fun changeScreen(screen : Screens) {
when (screen) {
Screens.MAIN_MENU -> setScreen(menu)
Screens.SETTINGS -> setScreen(settings)
Screens.GAME -> setScreen(gameplay)
}
}
fun createState() {

@ -0,0 +1,49 @@
package com.last.commit
import com.badlogic.gdx.Input
import com.badlogic.gdx.InputProcessor
import com.last.commit.screen.TimeTravelScreen
class GameInputProcessor(val game: Game, ) : InputProcessor{
val activeScreen = game.screen as TimeTravelScreen
override fun keyUp(keycode: Int): Boolean {
//activeScreen.handleKeyInput(keycode)
return false
}
override fun keyTyped(character: Char): Boolean {
val characterUpperCase = character.uppercase()
val characterKey = Input.Keys.valueOf(characterUpperCase)
activeScreen.handleKeyInput(characterKey)
return false
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
activeScreen.handleMouseInput(screenX, screenY, pointer, button)
return false
}
override fun keyDown(keycode: Int): Boolean {
return false
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return false
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return false
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return false
}
override fun scrolled(amountX: Float, amountY: Float): Boolean {
return false
}
}

@ -1,10 +1,7 @@
package com.last.commit.screen
import GameState
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input.Keys
import com.badlogic.gdx.InputProcessor
import com.badlogic.gdx.Screen
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.OrthographicCamera
@ -16,52 +13,87 @@ import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.math.Vector3
import com.badlogic.gdx.utils.Json
import com.last.commit.ColorState
import com.last.commit.Game
import com.last.commit.Player
import com.last.commit.audio.GameMusic
import com.last.commit.config.ActionCommand
import com.last.commit.config.GameConfig
import com.last.commit.map.Interactable
import com.last.commit.map.TimeMap
import com.last.commit.stages.InventoryStage
import com.last.commit.audio.GameMusic
import kotlin.math.floor
/** First screen of the application. Displayed after the application is created. */
class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
class FirstScreen(private val parent: Game) : TimeTravelScreen() {
val gameState = parent.state
val viewportSize = 800f
private var delta = 0f
private var delta = 0.008f
private var isColliding = false
val state = ColorState()
val batch = SpriteBatch()
val camera = OrthographicCamera(viewportSize, viewportSize)
lateinit var map: TimeMap // = TimeMap("tiled/base.tmx")
var map: TimeMap
val playerTexture = Texture("sprites/characters.png")
val player = Player(TextureRegion(playerTexture, 300, 44, 35, 43), gameState)
var shapeRenderer = ShapeRenderer()
val highlightColor = Color(0f, 0f, 1f, 0.5f)
lateinit var inventoryStage: InventoryStage
var inventoryStage: InventoryStage
override fun show() {
// Prepare your screen here.
init {
val gameConfig = this.loadGameConfig()
val randomMap = gameConfig.getRandomMap()
map = TimeMap(randomMap, gameState)
handleRatioChange()
this.spawnPlayer()
this.updateCamera()
player.addItemToInventory("drill")
handleRatioChange()
inventoryStage = InventoryStage("sprites/genericItems_spritesheet_colored", gameState.inventory)
shapeRenderer.setAutoShapeType(true)
Gdx.input.setInputProcessor(this)
player.addItemToInventory("drill")
}
override fun handleKeyInput(keyCode: Int) {
if (gameState.settings.getAction(keyCode) == ActionCommand.INTERACT) {
openDoor()
} else if (gameState.settings.getAction(keyCode) == ActionCommand.TIME_TRAVEL) {
map.teleport(player)
} else if (gameState.settings.getAction(keyCode) == ActionCommand.OPEN_INVENTORY) {
inventoryStage.visible = !inventoryStage.visible
} else if (keyCode == Input.Keys.P) {
gameState.inventory.add("compass")
inventoryStage.refresh()
}
if (gameState.settings.getAction(keyCode) == ActionCommand.OPEN_MENU) {
//Gdx.app.exit()
parent.changeScreen(Screens.MAIN_MENU)
}
}
override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) {
val mouseCoordinates: Vector2 = toWorldCoordinates(screenX.toFloat(), screenY.toFloat())
println("Mouse World coordinates is ${mouseCoordinates.x}:${mouseCoordinates.y}")
val playerDirection: Vector2 = player.getAbsoluteDirection()
println("Player interactor is ${playerDirection.x}:${playerDirection.y}")
map.interactWith(playerDirection.x, playerDirection.y, player.getCollider())
}
override fun show() {
// Prepare your screen here.
gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f)
}
@ -73,18 +105,12 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
}
override fun render(delta: Float) {
this.delta = delta
// state.step((delta * 1000).toLong())
// Draw your screen here. "delta" is the time since last render in seconds.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glClearColor(state.red, state.green, state.blue, 1f)
handleInput()
handleMapBorderCollision()
val mousePosition: Vector2 = getMousePosition()
player.lookAt(mousePosition)
val interactables = map.getInteractablesAt(player.getAbsoluteDirection())
batch.projectionMatrix = camera.combined
batch.begin()
@ -92,6 +118,7 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
this.player.render(batch)
batch.end()
val interactables = map.getInteractablesAt(player.getAbsoluteDirection())
// TODO: auslagern in sperate Methode
renderInteractables(interactables)
@ -132,7 +159,6 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
}
private fun handleInput() {
val horizontalMovement = Vector2()
if (isKeyPressed(gameState.settings.getKeyCode(ActionCommand.LEFT))) {
horizontalMovement.sub(Vector2.X)
@ -159,13 +185,14 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
verticalMovement.rotateDeg(180f)
this.player.move(verticalMovement, delta)
}
val hasMoved = !horizontalMovement.isZero || !verticalMovement.isZero
if (hasMoved) {
updateCamera()
}
}
fun isKeyPressed(keyCodes: List<Int>): Boolean{
private fun isKeyPressed(keyCodes: List<Int>): Boolean{
for (key in keyCodes) {
if (Gdx.input.isKeyPressed(key)) {
return true
@ -245,7 +272,6 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
}
override fun hide() {
// This method is called when another screen replaces this one.
}
override fun dispose() {
@ -254,46 +280,6 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
shapeRenderer.dispose()
}
override fun keyDown(keycode: Int): Boolean {
// TODO: Auto-generated method stub
return false
}
override fun keyUp(keycode: Int): Boolean {
if (gameState.settings.getAction(keycode) == ActionCommand.OPEN_MENU) {
Gdx.app.exit()
}
return false
}
override fun keyTyped(character: Char): Boolean {
val characterUpperCase = character.uppercase()
val characterKey = Keys.valueOf(characterUpperCase)
if (gameState.settings.getAction(characterKey) == ActionCommand.INTERACT) {
openDoor()
} else if (gameState.settings.getAction(characterKey) == ActionCommand.TIME_TRAVEL) {
map.teleport(player)
} else if (gameState.settings.getAction(characterKey) == ActionCommand.OPEN_INVENTORY) {
inventoryStage.visible = !inventoryStage.visible
} else if (character == 'p') {
gameState.inventory.add("compass")
inventoryStage.refresh()
}
return false
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
val mouseCoordinates: Vector2 = toWorldCoordinates(screenX.toFloat(), screenY.toFloat())
println("Mouse World coordinates is ${mouseCoordinates.x}:${mouseCoordinates.y}")
val playerDirection: Vector2 = player.getAbsoluteDirection()
println("Player interactor is ${playerDirection.x}:${playerDirection.y}")
map.interactWith(playerDirection.x, playerDirection.y, player.getCollider())
// TODO Auto-generated method stub
return false
}
fun openDoor() {
println("Attempt to toggle door")
val playerDirection: Vector2 = player.getAbsoluteDirection()
@ -309,23 +295,4 @@ class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
)
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
// TODO: ("Not yet implemented")
return false
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
// TODO: ("Not yet implemented")
return false
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
// TODO: "Not yet implemented"
return false
}
override fun scrolled(amountX: Float, amountY: Float): Boolean {
// TODO: Not yet implemented
return false
}
}

@ -0,0 +1,49 @@
package com.last.commit.screen
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.GL20
import com.last.commit.ColorState
import com.last.commit.Game
class MainMenu(val parent: Game) : TimeTravelScreen() {
var open = true
override fun handleKeyInput(keyCode: Int) {
parent.changeScreen(Screens.GAME)
}
override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) {
}
override fun show() {
open = true
}
private var delta: Float = 0.0f
val state = ColorState()
override fun render(delta: Float) {
this.delta = delta
state.step((delta * 1000).toLong())
// Draw your screen here. "delta" is the time since last render in seconds.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glClearColor(state.red, state.green, state.blue, 1f)
}
override fun resize(width: Int, height: Int) {
}
override fun pause() {
}
override fun resume() {
}
override fun hide() {
open = false
}
override fun dispose() {
}
}

@ -1,4 +0,0 @@
package com.last.commit.screen
class Menu {
}

@ -0,0 +1,7 @@
package com.last.commit.screen
enum class Screens {
MAIN_MENU,
SETTINGS,
GAME
}

@ -0,0 +1,48 @@
package com.last.commit.screen
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.GL20
import com.last.commit.ColorState
import com.last.commit.Game
class Settings (val parent: Game) : TimeTravelScreen() {
var open = true
override fun handleKeyInput(keyCode: Int) {
}
override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) {
}
override fun show() {
open = true
}
private var delta: Float = 0.0f
val state = ColorState()
override fun render(delta: Float) {
this.delta = delta
state.step((delta * 1000).toLong())
// Draw your screen here. "delta" is the time since last render in seconds.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glClearColor(state.red, state.green, state.blue, 1f)
}
override fun resize(width: Int, height: Int) {
}
override fun pause() {
}
override fun resume() {
}
override fun hide() {
open = false
}
override fun dispose() {
}
}

@ -0,0 +1,9 @@
package com.last.commit.screen
import com.badlogic.gdx.Screen
abstract class TimeTravelScreen : Screen{
abstract fun handleKeyInput(keyCode : Int)
abstract fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int)
}
Loading…
Cancel
Save