Add foodsteps

viewport-stuff
trivernis 2 years ago
parent 5ffc1f6676
commit 810e07cc44
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -16,6 +16,7 @@ class Player(private val textureRegion: TextureRegion, private val gameState: Ga
private var direction = Vector2.Zero private var direction = Vector2.Zero
private val movementSpeed = 200f private val movementSpeed = 200f
private val interactionRange = 60f private val interactionRange = 60f
private var lastStep = 0L
init { init {
val size = Math.max(textureRegion.regionWidth, textureRegion.regionHeight).toFloat() val size = Math.max(textureRegion.regionWidth, textureRegion.regionHeight).toFloat()
@ -36,8 +37,15 @@ class Player(private val textureRegion: TextureRegion, private val gameState: Ga
} }
fun move(v: Vector2, delta: Float) { fun move(v: Vector2, delta: Float) {
updatePosition(v, delta) if (v.x != 0f || v.y != 0f) {
updateCollider() updatePosition(v, delta)
updateCollider()
if (System.currentTimeMillis() - lastStep > 500) {
gameState.soundEngine.play(GameSoundEffect.STEPS_INDOOR, 0.5f)
lastStep = System.currentTimeMillis()
}
}
} }
private fun updatePosition(v: Vector2, delta: Float) { private fun updatePosition(v: Vector2, delta: Float) {

@ -4,7 +4,13 @@ import com.last.commit.audio.GameSound
public class GameSoundEffect(name: String): GameSound(name) { public class GameSoundEffect(name: String): GameSound(name) {
companion object { companion object {
val STEPS_INDOOR = GameSoundEffect("steps_indoor.mp3") val STEPS_INDOOR: GameSoundEffect
get() = listOf(
GameSoundEffect("steps/steps_indoor_1.mp3"),
GameSoundEffect("steps/steps_indoor_2.mp3"),
GameSoundEffect("steps/steps_indoor_3.mp3"),
).random()
val DOOR_OPEN = GameSoundEffect("door_open.mp3") val DOOR_OPEN = GameSoundEffect("door_open.mp3")
val DOOR_CLOSE = GameSoundEffect("door_close.mp3") val DOOR_CLOSE = GameSoundEffect("door_close.mp3")
} }

@ -1,18 +1,17 @@
package com.last.commit.audio; package com.last.commit.audio
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.audio.Sound
import com.badlogic.gdx.audio.Music import com.badlogic.gdx.audio.Music
import com.last.commit.audio.GameSound import com.badlogic.gdx.audio.Sound
import com.last.commit.audio.GameMusic
public class SoundEngine { public class SoundEngine {
private val sounds: HashMap<String, Sound> = HashMap() private val sounds: ThreadLocal<HashMap<String, Sound>> =
private val musicTracks: HashMap<String, Music> = HashMap() ThreadLocal.withInitial() { HashMap() }
private val musicTracks: ThreadLocal<HashMap<String, Music>> =
public fun play(gameSound: GameSound, volume: Float = 1f) { ThreadLocal.withInitial() { HashMap() }
fun play(gameSound: GameSound, volume: Float = 1f) {
if (gameSound is GameSoundEffect) { if (gameSound is GameSoundEffect) {
val sound = loadEffect(gameSound.name) val sound = loadEffect(gameSound.name)
sound.play(volume) sound.play(volume)
@ -24,28 +23,29 @@ public class SoundEngine {
music.play() music.play()
} }
} }
private fun loadEffect(name: String): Sound { private fun loadEffect(name: String): Sound {
return loadSound("effects/$name") return loadSound("effects/$name")
} }
private fun loadMusic(name: String): Music { private fun loadMusic(name: String): Music {
var music = musicTracks.get(name) var music = musicTracks.get().get(name)
if (music == null) { if (music == null) {
println("Loading sound $name") println("Loading sound $name")
music = Gdx.audio.newMusic(Gdx.files.internal("sounds/music/$name")) music = Gdx.audio.newMusic(Gdx.files.internal("sounds/music/$name"))
musicTracks[name] = music musicTracks.get()[name] = music
} }
return music!! return music!!
} }
private fun loadSound(name: String): Sound { private fun loadSound(name: String): Sound {
var sound = sounds.get(name) var sound = sounds.get().get(name)
if (sound == null) { if (sound == null) {
println("Loading sound $name") println("Loading sound $name")
sound = Gdx.audio.newSound(Gdx.files.internal("sounds/$name")) sound = Gdx.audio.newSound(Gdx.files.internal("sounds/$name"))
sounds[name] = sound sounds.get()[name] = sound
} }
return sound!! return sound!!

Loading…
Cancel
Save