diff --git a/core/src/main/kotlin/com/last/commit/Game.kt b/core/src/main/kotlin/com/last/commit/Game.kt index bf5410a..4ec69b4 100644 --- a/core/src/main/kotlin/com/last/commit/Game.kt +++ b/core/src/main/kotlin/com/last/commit/Game.kt @@ -3,15 +3,12 @@ package com.last.commit import GameState import com.badlogic.gdx.Game import com.badlogic.gdx.Gdx -import com.badlogic.gdx.assets.AssetManager +import com.badlogic.gdx.InputMultiplexer import com.last.commit.audio.SoundEngine import com.last.commit.config.GameSettings import com.last.commit.config.TimeTravelAssetManager import com.last.commit.inventory.Inventory import com.last.commit.screen.* -import com.last.commit.map.MapState -import java.awt.AWTEventMulticaster -import java.sql.Time /** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */ @@ -19,17 +16,19 @@ class Game : Game() { lateinit var state: GameState - lateinit var menu : TimeTravelScreen + lateinit var menu: TimeTravelScreen lateinit var gameplay: TimeTravelScreen lateinit var settings: TimeTravelScreen + val inputProcessors = InputMultiplexer() lateinit var inputProcessor: GameInputProcessor override fun create() { createState() createScreens() - inputProcessor = GameInputProcessor(this) - Gdx.input.inputProcessor = inputProcessor + gameplay.getInputProcessors().forEach { it -> inputProcessors.addProcessor(it) } + inputProcessors.addProcessor(inputProcessor) + Gdx.input.inputProcessor = inputProcessors changeScreen(Screens.MAIN_MENU) } @@ -41,7 +40,7 @@ class Game : Game() { settings = Settings(this) } - fun changeScreen(screen : Screens) { + fun changeScreen(screen: Screens) { println("changing screen to $screen") when (screen) { Screens.MAIN_MENU -> setScreen(menu) 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 14a9479..da122ca 100644 --- a/core/src/main/kotlin/com/last/commit/config/ActionCommand.kt +++ b/core/src/main/kotlin/com/last/commit/config/ActionCommand.kt @@ -11,4 +11,6 @@ enum class ActionCommand { //program interaction OPEN_MENU, + + JUMP } \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/config/GameSettings.kt b/core/src/main/kotlin/com/last/commit/config/GameSettings.kt index c6b2bcf..d3de7dc 100644 --- a/core/src/main/kotlin/com/last/commit/config/GameSettings.kt +++ b/core/src/main/kotlin/com/last/commit/config/GameSettings.kt @@ -1,7 +1,7 @@ package com.last.commit.config import com.badlogic.gdx.Input.Keys -import com.badlogic.gdx.Preferences +import com.badlogic.gdx.math.MathUtils import java.util.* class GameSettings { @@ -15,11 +15,11 @@ class GameSettings { var musicVolume: Float = 0.5F set(newValue: Float) { - field = valueRegulator(newValue, 1F, 0F) as Float + field = MathUtils.clamp(newValue, 1f, 0f) } var sfxVolume: Float = 0.5F set(newValue: Float) { - field = valueRegulator(newValue, 1F, 0F) as Float + field = MathUtils.clamp(newValue, 1f, 0f) } @@ -31,6 +31,7 @@ class GameSettings { actionKeys[ActionCommand.OPEN_MENU] = listOf(Keys.ESCAPE) actionKeys[ActionCommand.TIME_TRAVEL] = listOf(Keys.T) actionKeys[ActionCommand.INTERACT] = listOf(Keys.E) + actionKeys[ActionCommand.JUMP] = listOf(Keys.SPACE) musicVolume = 0.5F @@ -38,19 +39,8 @@ class GameSettings { } - private fun valueRegulator(newValue: T, maxValue: T, minValue: T): T where T : Number, T : Comparable{ - return if (newValue.compareTo(maxValue) > 0) { - maxValue - } else if (newValue.compareTo(minValue) < 0) { - minValue - } else { - newValue - } - - } - private fun setReversed(actionKeys: EnumMap>) { - for (actionCode in actionKeys.keys){ + for (actionCode in actionKeys.keys) { for (key in actionKeys.getValue(actionCode)) { actionKeysReversed[key] = actionCode } 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 e0b431c..2cf2693 100644 --- a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt @@ -1,6 +1,7 @@ package com.last.commit.screen import com.badlogic.gdx.Gdx +import com.badlogic.gdx.InputProcessor import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.OrthographicCamera @@ -11,6 +12,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.math.MathUtils import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector3 +import com.badlogic.gdx.scenes.scene2d.ui.Skin import com.badlogic.gdx.utils.Json import com.last.commit.Game import com.last.commit.Player @@ -19,6 +21,7 @@ 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.DialogStage import com.last.commit.stages.UIStage import kotlin.math.floor @@ -45,6 +48,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { var pause = true var uiStage: UIStage + val dialogStage = DialogStage(Skin(Gdx.files.internal("ui/uiskin.json"))) init { @@ -65,6 +69,10 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { } + override fun getInputProcessors(): Array { + return arrayOf(dialogStage) + } + override fun handleKeyInput(action: ActionCommand) { if (!pause) { when (action) { @@ -76,6 +84,11 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { map.teleport(player) } + ActionCommand.JUMP -> { + dialogStage.setTexts("Hello", "Please read the documentation") + dialogStage.show() + } + else -> {} } if (action == ActionCommand.OPEN_MENU) { @@ -111,6 +124,8 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { override fun render(delta: Float) { if (!pause) { + uiStage.act(delta) + dialogStage.act(delta) handleInput(delta) handleMapBorderCollision() @@ -126,8 +141,8 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { val interactables = map.getInteractablesAt(player.getAbsoluteDirection()) renderInteractables(interactables) - uiStage.act(delta) uiStage.draw() + dialogStage.draw() } } @@ -250,6 +265,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { override fun resize(width: Int, height: Int) { // Resize your screen here. The parameters represent the new window size. uiStage.resize(width, height) + dialogStage.resize(width, height) handleRatioChange() } @@ -292,7 +308,6 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { } fun openDoor() { - println("Attempt to toggle door") val playerDirection: Vector2 = player.getAbsoluteDirection() map.interactWith(playerDirection.x, playerDirection.y, player.getCollider()) } diff --git a/core/src/main/kotlin/com/last/commit/screen/MainMenu.kt b/core/src/main/kotlin/com/last/commit/screen/MainMenu.kt index adea6ea..7f80f8a 100644 --- a/core/src/main/kotlin/com/last/commit/screen/MainMenu.kt +++ b/core/src/main/kotlin/com/last/commit/screen/MainMenu.kt @@ -1,6 +1,7 @@ package com.last.commit.screen import com.badlogic.gdx.Gdx +import com.badlogic.gdx.InputProcessor import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.Stage @@ -26,6 +27,7 @@ class MainMenu(val parent: Game) : TimeTravelScreen() { stage = Stage(ScreenViewport()) uiSkin = parent.state.assetManager.getUiTexture() } + override fun handleKeyInput(action: ActionCommand) { parent.changeScreen(Screens.GAME) } @@ -35,6 +37,10 @@ class MainMenu(val parent: Game) : TimeTravelScreen() { stage.touchUp(screenX, screenY, pointer, button) } + override fun getInputProcessors(): Array { + return emptyArray() + } + override fun show() { stage.addActor(table); @@ -85,7 +91,6 @@ class MainMenu(val parent: Game) : TimeTravelScreen() { }) - } val state = ColorState() diff --git a/core/src/main/kotlin/com/last/commit/screen/Settings.kt b/core/src/main/kotlin/com/last/commit/screen/Settings.kt index f1c2689..0ee6139 100644 --- a/core/src/main/kotlin/com/last/commit/screen/Settings.kt +++ b/core/src/main/kotlin/com/last/commit/screen/Settings.kt @@ -1,17 +1,15 @@ package com.last.commit.screen import com.badlogic.gdx.Gdx +import com.badlogic.gdx.InputProcessor import com.badlogic.gdx.graphics.GL20 -import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.ui.* -import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener import com.badlogic.gdx.utils.viewport.ScreenViewport -import com.last.commit.ColorState import com.last.commit.Game import com.last.commit.config.ActionCommand -class Settings (val parent: Game) : TimeTravelScreen() { +class Settings(val parent: Game) : TimeTravelScreen() { var stage: Stage @@ -38,6 +36,9 @@ class Settings (val parent: Game) : TimeTravelScreen() { stage.touchUp(screenX, screenY, pointer, button) } + override fun getInputProcessors(): Array { + return emptyArray() + } override fun show() { diff --git a/core/src/main/kotlin/com/last/commit/screen/TimeTravelScreen.kt b/core/src/main/kotlin/com/last/commit/screen/TimeTravelScreen.kt index 38e35b0..d7fd578 100644 --- a/core/src/main/kotlin/com/last/commit/screen/TimeTravelScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/TimeTravelScreen.kt @@ -1,10 +1,13 @@ package com.last.commit.screen +import com.badlogic.gdx.InputProcessor import com.badlogic.gdx.Screen import com.last.commit.config.ActionCommand -abstract class TimeTravelScreen : Screen{ - - abstract fun handleKeyInput(action : ActionCommand) +abstract class TimeTravelScreen : Screen { + + abstract fun handleKeyInput(action: ActionCommand) abstract fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) + + abstract fun getInputProcessors(): Array } \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt new file mode 100644 index 0000000..dff56d1 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt @@ -0,0 +1,83 @@ +package com.last.commit.stages + +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.Input +import com.badlogic.gdx.scenes.scene2d.Stage +import com.badlogic.gdx.scenes.scene2d.ui.Skin +import com.badlogic.gdx.scenes.scene2d.ui.TextArea + + +/** + * Stage for dialog + */ +class DialogStage(skin: Skin?) : Stage() { + private var isVisible = false + private val texts = com.badlogic.gdx.utils.Array() + private val area: TextArea + + init { + area = TextArea("#", skin) + area.width = Gdx.graphics.width.toFloat() + area.height = 100f + addActor(area) + } + + fun setTexts(vararg texts: String?) { + this.texts.clear() + this.texts.addAll(*texts) + next() + } + + fun show() { + isVisible = true + } + + fun hide() { + isVisible = false + } + + fun resize(width: Int, height: Int) { + viewport.update(width, height, true) + } + + override fun draw() { + if (isVisible) { + super.draw() + } + } + + override fun act() { + if (isVisible) { + super.act() + } + } + + override fun act(delta: Float) { + if (isVisible) { + super.act(delta) + } + } + + override fun keyDown(keyCode: Int): Boolean { + if (!isVisible) { + return false + } + if (keyCode == Input.Keys.SPACE) { + if (texts.size > 0) { + //set next dialog text + next() + } else { + // hide dialog area cause there is no more text to show + hide() + } + } + return true + } + + private operator fun next() { + area.clear() + area.text = texts.first() + texts.removeIndex(0) + } + +} \ No newline at end of file