merge and added settings can now modify soundengine

main
MehdiAyadi 1 year ago
parent 198855360f
commit bd6286bc9f

@ -1,5 +1,7 @@
package com.last.commit; package com.last.commit;
import com.badlogic.gdx.math.MathUtils
class ColorState { class ColorState {
public var red = 0f public var red = 0f
private set; private set;

@ -52,10 +52,13 @@ class Game : Game() {
} }
fun createState() { fun createState() {
val settings = GameSettings()
val soundEngine = SoundEngine(settings.sfxVolume, settings.musicVolume)
settings.soundEngin = soundEngine
state = GameState( state = GameState(
Inventory(), Inventory(),
GameSettings(), settings,
SoundEngine(), soundEngine,
TimeTravelAssetManager(), TimeTravelAssetManager(),
null, null,
DialogStage(Skin(Gdx.files.internal("ui/uiskin.json"))) DialogStage(Skin(Gdx.files.internal("ui/uiskin.json")))

@ -36,7 +36,7 @@ class Player(private val textureRegion: TextureRegion, private val gameState: Ga
updateCollider() updateCollider()
if (System.currentTimeMillis() - lastStep > 500) { if (System.currentTimeMillis() - lastStep > 500) {
gameState.soundEngine.play(GameSoundEffect.STEPS_INDOOR, 0.5f) gameState.soundEngine.play(GameSoundEffect.STEPS_INDOOR) //0.5F
lastStep = System.currentTimeMillis() lastStep = System.currentTimeMillis()
} }
} }

@ -4,24 +4,34 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.audio.Music import com.badlogic.gdx.audio.Music
import com.badlogic.gdx.audio.Sound import com.badlogic.gdx.audio.Sound
public class SoundEngine { public class SoundEngine(private val sfx: Float, private val music: Float) {
var volumeSfx = sfx
var volumeMusic = music
set(newValue) {
field = newValue
resume()
}
private val sounds: ThreadLocal<HashMap<String, Sound>> = private val sounds: ThreadLocal<HashMap<String, Sound>> =
ThreadLocal.withInitial() { HashMap() } ThreadLocal.withInitial() { HashMap() }
private val musicTracks: ThreadLocal<HashMap<String, Music>> = private val musicTracks: ThreadLocal<HashMap<String, Music>> =
ThreadLocal.withInitial() { HashMap() } ThreadLocal.withInitial() { HashMap() }
lateinit var backgroundMusic: Music var backgroundMusic: Music
init {
backgroundMusic = loadMusic("world_music.mp3")
}
fun play(gameSound: GameSound, volume: Float = 1f) { fun play(gameSound: GameSound, ) {
if (gameSound is GameSoundEffect) { if (gameSound is GameSoundEffect) {
val sound = loadEffect(gameSound.name) val sound = loadEffect(gameSound.name)
sound.play(volume) sound.play(volumeSfx)
} else if (gameSound is GameMusic) { } else if (gameSound is GameMusic) {
backgroundMusic = loadMusic(gameSound.name)
backgroundMusic.volume = volume
backgroundMusic.setLooping(true) backgroundMusic.setLooping(true)
backgroundMusic.play() resume()
} }
} }
@ -30,9 +40,11 @@ public class SoundEngine {
} }
fun resume() { fun resume() {
backgroundMusic.play() backgroundMusic.volume = volumeMusic
if (!backgroundMusic.isPlaying) {
backgroundMusic.play()
}
} }
private fun loadEffect(name: String): Sound { private fun loadEffect(name: String): Sound {
return loadSound("effects/$name") return loadSound("effects/$name")
} }

@ -2,24 +2,56 @@ package com.last.commit.config
import com.badlogic.gdx.Input.Keys import com.badlogic.gdx.Input.Keys
import com.badlogic.gdx.math.MathUtils import com.badlogic.gdx.math.MathUtils
import com.last.commit.GameState
import com.last.commit.audio.SoundEngine
import java.util.* import java.util.*
class GameSettings { class GameSettings() {
lateinit var soundEngin : SoundEngine
private 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() private val actionKeysReversed: HashMap<Int, ActionCommand> = hashMapOf()
var musicEnabled: Boolean = true var musicEnabled: Boolean = true
set(newValue: Boolean) {
field = newValue
soundEngin?.volumeMusic = musicVolume
}
var sfxEnabled: Boolean = true var sfxEnabled: Boolean = true
set(newValue: Boolean) {
field = newValue
soundEngin?.volumeSfx = sfxVolume
}
var musicVolume: Float = 0.5F var musicVolume: Float = 0.5F
get() {
return if (musicEnabled) {
field
} else {
0F
}
}
set(newValue: Float) { set(newValue: Float) {
field = MathUtils.clamp(newValue, 1f, 0f) field = newValue
soundEngin?.volumeMusic = musicVolume
} }
var sfxVolume: Float = 0.5F var sfxVolume: Float = 0.5F
get() {
return if (sfxEnabled) {
field
} else {
0F
}
}
set(newValue: Float) { set(newValue: Float) {
field = MathUtils.clamp(newValue, 1f, 0f) field = newValue
println("settings volume sfx :$sfxVolume")
soundEngin?.volumeSfx = sfxVolume
soundEngin?. let {
println("soundengine volume sfx :$sfxVolume")
}
} }
@ -33,8 +65,6 @@ class GameSettings {
actionKeys[ActionCommand.INTERACT] = listOf(Keys.E) actionKeys[ActionCommand.INTERACT] = listOf(Keys.E)
actionKeys[ActionCommand.JUMP] = listOf(Keys.SPACE) actionKeys[ActionCommand.JUMP] = listOf(Keys.SPACE)
musicVolume = 0.5F
setReversed(actionKeys) setReversed(actionKeys)
} }

@ -50,7 +50,6 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
val highlightColor = Color(0f, 0f, 1f, 0.5f) val highlightColor = Color(0f, 0f, 1f, 0.5f)
var pause = true
var uiStage: UIStage var uiStage: UIStage
val promptStage: PromptStage val promptStage: PromptStage
@ -66,7 +65,6 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
uiStage = UIStage("sprites/genericItems_spritesheet_colored", gameState) uiStage = UIStage("sprites/genericItems_spritesheet_colored", gameState)
shapeRenderer.setAutoShapeType(true) shapeRenderer.setAutoShapeType(true)
gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f)
promptStage = PromptStage(Skin(Gdx.files.internal("ui/uiskin.json"))) promptStage = PromptStage(Skin(Gdx.files.internal("ui/uiskin.json")))
promptStage.addText(""" promptStage.addText("""
You are stranded in time. You are stranded in time.
@ -99,7 +97,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
} }
override fun handleKeyInput(action: ActionCommand) { override fun handleKeyInput(action: ActionCommand) {
if (!pause) {
when (action) { when (action) {
ActionCommand.INTERACT -> { ActionCommand.INTERACT -> {
openDoor() openDoor()
@ -120,20 +118,21 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
//Gdx.app.exit() //Gdx.app.exit()
parent.changeScreen(Screens.MAIN_MENU) parent.changeScreen(Screens.MAIN_MENU)
} }
}
} }
override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) { override fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int) {
if (!pause) {
val mouseCoordinates: Vector2 = toWorldCoordinates(screenX.toFloat(), screenY.toFloat())
val playerDirection: Vector2 = player.getAbsoluteDirection() val playerDirection: Vector2 = player.getAbsoluteDirection()
map.interactWith(playerDirection.x, playerDirection.y, player.getCollider()) map.interactWith(playerDirection.x, playerDirection.y, player.getCollider())
}
} }
override fun show() { override fun show() {
// Prepare your screen here.
resume() resume()
} }
@ -151,7 +150,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
promptStage.clearText() promptStage.clearText()
promptStage.addText("You won!") promptStage.addText("You won!")
promptStage.draw() promptStage.draw()
} else if (!pause) { } else {
uiStage.act(delta) uiStage.act(delta)
gameState.dialogStage.act(delta) gameState.dialogStage.act(delta)
promptStage.act(delta) promptStage.act(delta)
@ -296,21 +295,18 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
override fun pause() { override fun pause() {
pause = true
gameState.soundEngine.stop()
} }
override fun resume() { override fun resume() {
pause = false
gameState.soundEngine.resume();
this.viewport.apply() this.viewport.apply()
this.updateCamera() this.updateCamera()
// Invoked when your application is resumed after pause. // Invoked when your application is resumed after pause.
} }
override fun hide() { override fun hide() {
pause()
//gameState.soundEngine.stop()
} }
override fun dispose() { override fun dispose() {

@ -3,6 +3,7 @@ package com.last.commit.screen
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.InputProcessor import com.badlogic.gdx.InputProcessor
import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Skin import com.badlogic.gdx.scenes.scene2d.ui.Skin
@ -21,6 +22,7 @@ class MainMenu(val parent: Game) : TimeTravelScreen() {
var table = Table() var table = Table()
val uiSkin: Skin val uiSkin: Skin
val state = ColorState()
init { init {
parent.state.assetManager.finishLoading() parent.state.assetManager.finishLoading()
@ -93,13 +95,17 @@ class MainMenu(val parent: Game) : TimeTravelScreen() {
} }
val state = ColorState()
override fun render(delta: Float) { override fun render(delta: Float) {
state.step((delta * 1000).toLong()) state.step((delta * 1000).toLong())
// Draw your screen here. "delta" is the time since last render in seconds. // Draw your screen here. "delta" is the time since last render in seconds.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glClearColor(state.red, state.green, state.blue, 1f)
var red = MathUtils.clamp(state.red,0.1F, 0.5F)
var blue = MathUtils.clamp(state.green,0.1F, 0.5F)
var green = MathUtils.clamp(state.blue,0.1F, 0.5F)
Gdx.gl.glClearColor(red, green, blue, 1f)
// tell our stage to do actions and draw itself // tell our stage to do actions and draw itself
stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f)); stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));

@ -3,15 +3,16 @@ package com.last.commit.screen
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.InputProcessor import com.badlogic.gdx.InputProcessor
import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.* import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.utils.viewport.ScreenViewport import com.badlogic.gdx.utils.viewport.ScreenViewport
import com.last.commit.ColorState
import com.last.commit.Game import com.last.commit.Game
import com.last.commit.config.ActionCommand import com.last.commit.config.ActionCommand
class Settings(val parent: Game) : TimeTravelScreen() { class Settings(val parent: Game) : TimeTravelScreen() {
var stage: Stage var stage: Stage
val skin: Skin val skin: Skin
@ -22,84 +23,96 @@ class Settings(val parent: Game) : TimeTravelScreen() {
lateinit var musicOnOffLabel: Label lateinit var musicOnOffLabel: Label
lateinit var soundOnOffLabel: Label lateinit var soundOnOffLabel: Label
var sfxMusicSlider: Slider
var volumeMusicSlider: Slider
var musicCheckbox: CheckBox
var soundEffectsCheckbox: CheckBox
var backButton: Button
val state = ColorState()
val table = Table() val table = Table()
init { init {
parent.state.assetManager.finishLoading() parent.state.assetManager.finishLoading()
stage = Stage(ScreenViewport()) stage = Stage(ScreenViewport())
skin = parent.state.assetManager.getUiTexture() 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 getInputProcessors(): Array<InputProcessor> {
return emptyArray()
}
override fun show() {
stage.clear()
// Create a table that fills the screen. Everything else will go inside
// this table.
}
fun renderTable(x: Float, y: Float) {
table.reset()
table.setFillParent(true)
//table.setDebug(true);
stage.addActor(table)
// music volume // music volume
val volumeMusicSlider = Slider(0f, 1f, 0.1f, false, skin) volumeMusicSlider = Slider(0f, 1f, 0.1f, false, skin)
volumeMusicSlider.value = parent.state.settings.musicVolume volumeMusicSlider.value = parent.state.settings.musicVolume
volumeMusicSlider.addListener { volumeMusicSlider.addListener {
println("volume slide ${volumeMusicSlider.value}")
parent.state.settings.musicVolume = volumeMusicSlider.value parent.state.settings.musicVolume = volumeMusicSlider.value
// updateVolumeLabel(); // updateVolumeLabel();
false false
} }
// sound volume // sound volume
val soundMusicSlider = Slider(0f, 1f, 0.1f, false, skin) sfxMusicSlider = Slider(0f, 1f, 0.1f, false, skin)
soundMusicSlider.value = parent.state.settings.sfxVolume sfxMusicSlider.value = parent.state.settings.sfxVolume
soundMusicSlider.addListener { sfxMusicSlider.addListener {
parent.state.settings.sfxVolume = soundMusicSlider.value
println("volume sfx :${sfxMusicSlider.value}")
parent.state.settings.sfxVolume = sfxMusicSlider.value
// updateVolumeLabel(); // updateVolumeLabel();
false false
} }
// music on/off // music on/off
val musicCheckbox = CheckBox(null, skin) musicCheckbox = CheckBox(null, skin)
musicCheckbox.isChecked = parent.state.settings.musicEnabled musicCheckbox.isChecked = !parent.state.settings.musicEnabled
musicCheckbox.addListener { musicCheckbox.addListener {
val enabled: Boolean = musicCheckbox.isChecked() val enabled: Boolean = musicCheckbox.isChecked()
parent.state.settings.musicEnabled = enabled parent.state.settings.musicEnabled = !enabled
false false
} }
// sound on/off // sound on/off
val soundEffectsCheckbox = CheckBox(null, skin) soundEffectsCheckbox = CheckBox(null, skin)
soundEffectsCheckbox.isChecked = parent.state.settings.sfxEnabled soundEffectsCheckbox.isChecked = !parent.state.settings.sfxEnabled
soundEffectsCheckbox.addListener { soundEffectsCheckbox.addListener {
val enabled: Boolean = soundEffectsCheckbox.isChecked() val enabled: Boolean = soundEffectsCheckbox.isChecked()
parent.state.settings.sfxEnabled = enabled parent.state.settings.sfxEnabled = !enabled
false false
} }
// return to main screen button // return to main screen button
val backButton = TextButton("Back", skin) backButton = TextButton("Back", skin)
backButton.addListener { backButton.addListener {
parent.changeScreen(Screens.MAIN_MENU) parent.changeScreen(Screens.MAIN_MENU)
false false
} }
}
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 getInputProcessors(): Array<InputProcessor> {
return emptyArray()
}
override fun show() {
stage.clear()
}
fun renderTable(x: Float, y: Float) {
table.reset()
table.setFillParent(true)
//table.setDebug(true);
stage.addActor(table)
titleLabel = Label("Preferences", skin) titleLabel = Label("Preferences", skin)
volumeMusicLabel = Label("Music Volume", skin) volumeMusicLabel = Label("Music Volume", skin)
volumeSoundLabel = Label("Sound Volume", skin) volumeSoundLabel = Label("Sound Volume", skin)
@ -117,7 +130,7 @@ class Settings(val parent: Game) : TimeTravelScreen() {
table.row().size(x, y); table.row().size(x, y);
table.row().pad(10F, 0F, 0F, 10F) table.row().pad(10F, 0F, 0F, 10F)
table.add(volumeSoundLabel).left() table.add(volumeSoundLabel).left()
table.add(soundMusicSlider) table.add(sfxMusicSlider)
table.row().size(x, y); table.row().size(x, y);
table.row().pad(10F, 0F, 0F, 10F) table.row().pad(10F, 0F, 0F, 10F)
table.add(soundOnOffLabel).left() table.add(soundOnOffLabel).left()
@ -128,9 +141,17 @@ class Settings(val parent: Game) : TimeTravelScreen() {
} }
override fun render(delta: Float) { override fun render(delta: Float) {
// clear the screen ready for next set of images to be drawn
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
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.glClear(GL20.GL_COLOR_BUFFER_BIT)
var red = MathUtils.clamp(state.red,0.1F, 0.5F)
var blue = MathUtils.clamp(state.green,0.1F, 0.5F)
var green = MathUtils.clamp(state.blue,0.1F, 0.5F)
Gdx.gl.glClearColor(red, green, blue, 1f)
// tell our stage to do actions and draw itself // tell our stage to do actions and draw itself
stage.act(Math.min(Gdx.graphics.deltaTime, 1 / 30f)) stage.act(Math.min(Gdx.graphics.deltaTime, 1 / 30f))
@ -158,5 +179,6 @@ class Settings(val parent: Game) : TimeTravelScreen() {
} }
override fun dispose() { override fun dispose() {
stage.dispose();
} }
} }
Loading…
Cancel
Save