From eeba41edbcc43dd69ea93da62bad5649a0efdd7c Mon Sep 17 00:00:00 2001 From: MehdiAyadi <91869915+MehdiAyadi@users.noreply.github.com> Date: Sat, 19 Nov 2022 19:53:32 +0100 Subject: [PATCH] made GameInputProcessor.kt not defined in screen added support for multiple screens --- core/src/main/kotlin/com/last/commit/Game.kt | 37 ++++- .../com/last/commit/GameInputProcessor.kt | 49 +++++++ .../com/last/commit/screen/FirstScreen.kt | 135 +++++++----------- .../kotlin/com/last/commit/screen/MainMenu.kt | 49 +++++++ .../kotlin/com/last/commit/screen/Menu.kt | 4 - .../kotlin/com/last/commit/screen/Screens.kt | 7 + .../kotlin/com/last/commit/screen/Settings.kt | 48 +++++++ .../last/commit/screen/TimeTravelScreen.kt | 9 ++ 8 files changed, 246 insertions(+), 92 deletions(-) create mode 100644 core/src/main/kotlin/com/last/commit/GameInputProcessor.kt create mode 100644 core/src/main/kotlin/com/last/commit/screen/MainMenu.kt delete mode 100644 core/src/main/kotlin/com/last/commit/screen/Menu.kt create mode 100644 core/src/main/kotlin/com/last/commit/screen/Screens.kt create mode 100644 core/src/main/kotlin/com/last/commit/screen/Settings.kt create mode 100644 core/src/main/kotlin/com/last/commit/screen/TimeTravelScreen.kt diff --git a/core/src/main/kotlin/com/last/commit/Game.kt b/core/src/main/kotlin/com/last/commit/Game.kt index 0168e2c..8fd0d7b 100644 --- a/core/src/main/kotlin/com/last/commit/Game.kt +++ b/core/src/main/kotlin/com/last/commit/Game.kt @@ -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() { diff --git a/core/src/main/kotlin/com/last/commit/GameInputProcessor.kt b/core/src/main/kotlin/com/last/commit/GameInputProcessor.kt new file mode 100644 index 0000000..ea204d0 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/GameInputProcessor.kt @@ -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 + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt index ec6e29c..cf36271 100644 --- a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt @@ -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): Boolean{ + private fun isKeyPressed(keyCodes: List): 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 - } } diff --git a/core/src/main/kotlin/com/last/commit/screen/MainMenu.kt b/core/src/main/kotlin/com/last/commit/screen/MainMenu.kt new file mode 100644 index 0000000..52e7be6 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/screen/MainMenu.kt @@ -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() { + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/screen/Menu.kt b/core/src/main/kotlin/com/last/commit/screen/Menu.kt deleted file mode 100644 index a37627d..0000000 --- a/core/src/main/kotlin/com/last/commit/screen/Menu.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.last.commit.screen - -class Menu { -} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/screen/Screens.kt b/core/src/main/kotlin/com/last/commit/screen/Screens.kt new file mode 100644 index 0000000..57b5ef5 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/screen/Screens.kt @@ -0,0 +1,7 @@ +package com.last.commit.screen + +enum class Screens { + MAIN_MENU, + SETTINGS, + GAME +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/screen/Settings.kt b/core/src/main/kotlin/com/last/commit/screen/Settings.kt new file mode 100644 index 0000000..230ba42 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/screen/Settings.kt @@ -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() { + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/screen/TimeTravelScreen.kt b/core/src/main/kotlin/com/last/commit/screen/TimeTravelScreen.kt new file mode 100644 index 0000000..2660e20 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/screen/TimeTravelScreen.kt @@ -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) +} \ No newline at end of file