From 26bdc1e9b57af8ec8cbfe8a29f6e60fc213fcce2 Mon Sep 17 00:00:00 2001 From: MehdiAyadi <91869915+MehdiAyadi@users.noreply.github.com> Date: Sun, 20 Nov 2022 01:12:47 +0100 Subject: [PATCH] cleanup and filled gamesettings with life: TODO viewport, connection to soundengine and keybinding --- .../com/last/commit/config/GameSettings.kt | 28 ++++- .../com/last/commit/screen/FirstScreen.kt | 8 -- .../kotlin/com/last/commit/screen/Settings.kt | 114 ++++++++++++++++-- 3 files changed, 130 insertions(+), 20 deletions(-) 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 7d3f4cc..c6b2bcf 100644 --- a/core/src/main/kotlin/com/last/commit/config/GameSettings.kt +++ b/core/src/main/kotlin/com/last/commit/config/GameSettings.kt @@ -1,15 +1,28 @@ package com.last.commit.config import com.badlogic.gdx.Input.Keys +import com.badlogic.gdx.Preferences import java.util.* class GameSettings { - val fullscreen: Boolean = false private val actionKeys: EnumMap> = EnumMap(ActionCommand::class.java) private val actionKeysReversed: HashMap = hashMapOf() + var musicEnabled: Boolean = true + var sfxEnabled: Boolean = true + + var musicVolume: Float = 0.5F + set(newValue: Float) { + field = valueRegulator(newValue, 1F, 0F) as Float + } + var sfxVolume: Float = 0.5F + set(newValue: Float) { + field = valueRegulator(newValue, 1F, 0F) as Float + } + + init { actionKeys[ActionCommand.UP] = listOf(Keys.UP, Keys.W) actionKeys[ActionCommand.DOWN] = listOf(Keys.DOWN, Keys.S) @@ -19,10 +32,23 @@ class GameSettings { actionKeys[ActionCommand.TIME_TRAVEL] = listOf(Keys.T) actionKeys[ActionCommand.INTERACT] = listOf(Keys.E) + musicVolume = 0.5F + setReversed(actionKeys) } + 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 (key in actionKeys.getValue(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 18caa28..765782b 100644 --- a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt @@ -76,19 +76,11 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { } } println(action) - println(gameState.settings.getKeyCode(ActionCommand.OPEN_MENU)) if (action == ActionCommand.OPEN_MENU) { //Gdx.app.exit() parent.changeScreen(Screens.MAIN_MENU) } } - //TODO - /* - else if (action == Input.Keys.P) { - gameState.inventory.add("compass") - inventoryStage.refresh() - } - */ } 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 cd54205..f1c2689 100644 --- a/core/src/main/kotlin/com/last/commit/screen/Settings.kt +++ b/core/src/main/kotlin/com/last/commit/screen/Settings.kt @@ -1,35 +1,128 @@ package com.last.commit.screen import com.badlogic.gdx.Gdx -import com.badlogic.gdx.Screen 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() { - var open = true + + var stage: Stage + val skin: Skin + + + lateinit var titleLabel: Label + lateinit var volumeMusicLabel: Label + lateinit var volumeSoundLabel: Label + lateinit var musicOnOffLabel: Label + lateinit var soundOnOffLabel: Label + + init { + parent.state.assetManager.finishLoading() + stage = Stage(ScreenViewport()) + skin = parent.state.assetManager.getUiTexture() + } override fun handleKeyInput(action: ActionCommand) { } override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) { + stage.touchDown(screenX, screenY, pointer, button) + stage.touchUp(screenX, screenY, pointer, button) } + + override fun show() { - open = true + stage.clear() + + // Create a table that fills the screen. Everything else will go inside + // this table. + val table = Table() + table.setFillParent(true) + //table.setDebug(true); + stage.addActor(table) + + + // music volume + val volumeMusicSlider = Slider(0f, 1f, 0.1f, false, skin) + volumeMusicSlider.value = parent.state.settings.musicVolume + volumeMusicSlider.addListener { + parent.state.settings.musicVolume = volumeMusicSlider.value + // updateVolumeLabel(); + false + } + + // sound volume + val soundMusicSlider = Slider(0f, 1f, 0.1f, false, skin) + soundMusicSlider.value = parent.state.settings.sfxVolume + soundMusicSlider.addListener { + parent.state.settings.sfxVolume = soundMusicSlider.value + // updateVolumeLabel(); + false + } + + // music on/off + val musicCheckbox = CheckBox(null, skin) + musicCheckbox.isChecked = parent.state.settings.musicEnabled + musicCheckbox.addListener { + val enabled: Boolean = musicCheckbox.isChecked() + parent.state.settings.musicEnabled = enabled + false + } + + // sound on/off + val soundEffectsCheckbox = CheckBox(null, skin) + soundEffectsCheckbox.isChecked = parent.state.settings.sfxEnabled + soundEffectsCheckbox.addListener { + val enabled: Boolean = soundEffectsCheckbox.isChecked() + parent.state.settings.sfxEnabled = enabled + false + } + + // return to main screen button + val backButton = TextButton("Back", skin) + backButton.addListener { + parent.changeScreen(Screens.MAIN_MENU) + false + } + titleLabel = Label("Preferences", skin) + volumeMusicLabel = Label("Music Volume", skin) + volumeSoundLabel = Label("Sound Volume", skin) + musicOnOffLabel = Label("Music", skin) + soundOnOffLabel = Label("Sound Effect", skin) + table.add(titleLabel).colspan(2) + table.row().pad(10F, 0F, 0F, 10F) + table.add(volumeMusicLabel).left() + table.add(volumeMusicSlider) + table.row().pad(10F, 0F, 0F, 10F) + table.add(musicOnOffLabel).left() + table.add(musicCheckbox) + table.row().pad(10F, 0F, 0F, 10F) + table.add(volumeSoundLabel).left() + table.add(soundMusicSlider) + table.row().pad(10F, 0F, 0F, 10F) + table.add(soundOnOffLabel).left() + table.add(soundEffectsCheckbox) + table.row().pad(10F, 0F, 0F, 10F) + table.add(backButton).colspan(2) } - 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. + // clear the screen ready for next set of images to be drawn + Gdx.gl.glClearColor(0f, 0f, 0f, 1f) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) - Gdx.gl.glClearColor(state.red, state.green, state.blue, 1f) + + // tell our stage to do actions and draw itself + stage.act(Math.min(Gdx.graphics.deltaTime, 1 / 30f)) + stage.draw() } override fun resize(width: Int, height: Int) { @@ -42,7 +135,6 @@ class Settings (val parent: Game) : TimeTravelScreen() { } override fun hide() { - open = false } override fun dispose() {