use Singleton SoundEngine instead of GameState

main
Matthias 1 year ago
parent 9f91a7c424
commit e7dfc8fa78

@ -5,7 +5,6 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.g2d.BitmapFont import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.scenes.scene2d.ui.Skin import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.last.commit.audio.SoundEngine
import com.last.commit.inventory.Inventory import com.last.commit.inventory.Inventory
import com.last.commit.screen.* import com.last.commit.screen.*
import com.last.commit.stages.DialogStage import com.last.commit.stages.DialogStage
@ -51,7 +50,6 @@ class Game : Game() {
fun createState() { fun createState() {
state = GameState( state = GameState(
Inventory(), Inventory(),
SoundEngine,
null, null,
DialogStage(Skin(Gdx.files.internal("ui/uiskin.json"))) DialogStage(Skin(Gdx.files.internal("ui/uiskin.json")))
) )

@ -1,13 +1,11 @@
package com.last.commit package com.last.commit
import com.last.commit.audio.SoundEngine
import com.last.commit.inventory.Inventory import com.last.commit.inventory.Inventory
import com.last.commit.map.MapState import com.last.commit.map.MapState
import com.last.commit.stages.DialogStage import com.last.commit.stages.DialogStage
data class GameState( data class GameState(
val inventory: Inventory, val inventory: Inventory,
val soundEngine: SoundEngine,
var map: MapState? = null, var map: MapState? = null,
val dialogStage: DialogStage val dialogStage: DialogStage
) { ) {

@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector2
import com.last.commit.audio.SoundEngine
class Player(private val textureRegion: TextureRegion, private val gameState: GameState) : Collidable { class Player(private val textureRegion: TextureRegion, private val gameState: GameState) : Collidable {
@ -35,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("STEPS_INDOOR") //0.5F SoundEngine.play("STEPS_INDOOR") //0.5F
lastStep = System.currentTimeMillis() lastStep = System.currentTimeMillis()
} }
} }

@ -4,6 +4,7 @@ import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.last.commit.GameState import com.last.commit.GameState
import com.last.commit.audio.SoundEngine
class Collectible( class Collectible(
name: String, name: String,
@ -23,7 +24,7 @@ class Collectible(
override fun interact(otherCollider: Rectangle, state: GameState): Boolean { override fun interact(otherCollider: Rectangle, state: GameState): Boolean {
println("Interacting with item $name") println("Interacting with item $name")
state.soundEngine.play("GRAB") SoundEngine.play("GRAB")
if (state.inventory.hasItem(this.name)) { if (state.inventory.hasItem(this.name)) {
state.dialogStage.setTexts("You already have this item.") state.dialogStage.setTexts("You already have this item.")
state.dialogStage.show() state.dialogStage.show()

@ -4,6 +4,7 @@ import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell
import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Rectangle
import com.last.commit.GameState import com.last.commit.GameState
import com.last.commit.Wall import com.last.commit.Wall
import com.last.commit.audio.SoundEngine
import com.last.commit.inventory.InventoryItem import com.last.commit.inventory.InventoryItem
class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) : class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) :
@ -33,13 +34,13 @@ class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) :
override fun interact(otherCollider: Rectangle, state: GameState): Boolean { override fun interact(otherCollider: Rectangle, state: GameState): Boolean {
println("interacting with door $this") println("interacting with door $this")
if (isClosed) { if (isClosed) {
state.soundEngine.play("DOOR_OPEN") SoundEngine.play("DOOR_OPEN")
isOpen = true isOpen = true
} else if (isOpen) { } else if (isOpen) {
if (getCollider().overlaps(otherCollider)) { if (getCollider().overlaps(otherCollider)) {
// can't close the door cause it is colliding with given collider // can't close the door cause it is colliding with given collider
} else { } else {
state.soundEngine.play("DOOR_CLOSE") SoundEngine.play("DOOR_CLOSE")
isOpen = false isOpen = false
} }
} }

@ -43,11 +43,12 @@ class MapState(val map: TiledMap, val textureLoader: SpritesheetTextureLoader) {
for (obj in map.layers["Teleporter"].objects) { for (obj in map.layers["Teleporter"].objects) {
if (obj is RectangleMapObject) { if (obj is RectangleMapObject) {
this.teleporters.add(obj) this.teleporters.add(obj)
println("Teleporter ${obj}")
} }
} }
if (this.teleporters.isEmpty()) { if (this.teleporters.isEmpty()) {
println("No Teleporters defined!") println("No Teleporters defined!")
} else {
println("Found ${teleporters.size} teleporters")
} }
} }

@ -12,6 +12,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.last.commit.Collidable import com.last.commit.Collidable
import com.last.commit.GameState import com.last.commit.GameState
import com.last.commit.Player import com.last.commit.Player
import com.last.commit.audio.SoundEngine
import com.last.commit.inventory.SpritesheetTextureLoader import com.last.commit.inventory.SpritesheetTextureLoader
@ -57,7 +58,7 @@ class TimeMap(fileName: String, val state: GameState) {
it.rectangle.contains(player.getX(), player.getY()) it.rectangle.contains(player.getX(), player.getY())
} }
if (teleporter != null) { if (teleporter != null) {
state.soundEngine.play("TIME_TRAVEL") SoundEngine.play("TIME_TRAVEL")
val targetMap = teleporter.properties.get("target", String::class.java) val targetMap = teleporter.properties.get("target", String::class.java)
System.out.println("Teleporting to targetMap $targetMap") System.out.println("Teleporting to targetMap $targetMap")
loadMap("tiled/$targetMap") loadMap("tiled/$targetMap")

Loading…
Cancel
Save