Huge refactoring
parent
9711e28341
commit
960577bcb1
@ -0,0 +1,62 @@
|
|||||||
|
package com.last.commit
|
||||||
|
|
||||||
|
import com.badlogic.gdx.assets.AssetManager
|
||||||
|
import com.badlogic.gdx.audio.Music
|
||||||
|
import com.badlogic.gdx.audio.Sound
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.ui.Skin
|
||||||
|
|
||||||
|
object TimeTravelAssetManager {
|
||||||
|
|
||||||
|
private val skin = "ui/uiskin.json"
|
||||||
|
|
||||||
|
private val assetManager = AssetManager()
|
||||||
|
fun loadSounds(vararg paths: String) {
|
||||||
|
println("Loading ${paths.size} sounds")
|
||||||
|
for (path in paths) {
|
||||||
|
assetManager.load(path, Sound::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadMusics(vararg paths: String) {
|
||||||
|
println("Loading ${paths.size} musics")
|
||||||
|
for (path in paths) {
|
||||||
|
assetManager.load(path, Music::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadSkins(vararg paths: String) {
|
||||||
|
println("Loading ${paths.size} skins")
|
||||||
|
for (path in paths) {
|
||||||
|
assetManager.load(path, Skin::class.java)
|
||||||
|
}
|
||||||
|
assetManager.load(skin, Skin::class.java);
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadTextures(vararg paths: String) {
|
||||||
|
println("Loading ${paths.size} textures")
|
||||||
|
for (path in paths) {
|
||||||
|
assetManager.load(path, Music::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getSkin(): Skin {
|
||||||
|
return assetManager.get(skin)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> get(path: String): T {
|
||||||
|
return assetManager.get(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun finishLoading() {
|
||||||
|
assetManager.finishLoading()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun update(): Boolean {
|
||||||
|
return assetManager.update()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun dispose() {
|
||||||
|
assetManager.dispose()
|
||||||
|
}
|
||||||
|
}
|
@ -1,46 +0,0 @@
|
|||||||
package com.last.commit
|
|
||||||
|
|
||||||
import com.badlogic.gdx.InputProcessor
|
|
||||||
import com.last.commit.screen.TimeTravelScreen
|
|
||||||
|
|
||||||
class GameInputProcessor(val game: Game) : InputProcessor {
|
|
||||||
|
|
||||||
lateinit var activeScreen: TimeTravelScreen
|
|
||||||
|
|
||||||
override fun keyUp(keycode: Int): Boolean {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun keyTyped(character: Char): Boolean {
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
|
||||||
activeScreen.handleMouseInput(screenX, screenY, pointer, button)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun keyDown(keycode: Int): Boolean {
|
|
||||||
game.state.settings.getAction(keycode)?.let {
|
|
||||||
activeScreen.handleKeyInput(it)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun scrolled(amountX: Float, amountY: Float): Boolean {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +1,13 @@
|
|||||||
package com.last.commit
|
package com.last.commit
|
||||||
|
|
||||||
import com.last.commit.audio.SoundEngine
|
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.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 settings: GameSettings,
|
|
||||||
val soundEngine: SoundEngine,
|
val soundEngine: SoundEngine,
|
||||||
val assetManager: TimeTravelAssetManager,
|
|
||||||
var map: MapState? = null,
|
var map: MapState? = null,
|
||||||
val dialogStage: DialogStage
|
val dialogStage: DialogStage
|
||||||
) {
|
) {
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package com.last.commit.audio
|
|
||||||
|
|
||||||
import com.last.commit.audio.GameSound
|
|
||||||
|
|
||||||
class GameMusic(name: String): GameSound(name) {
|
|
||||||
companion object {
|
|
||||||
val WORLD_MUSIC = GameMusic("world_music.mp3")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
|
|
||||||
package com.last.commit.audio;
|
|
||||||
|
|
||||||
public abstract class GameSound(public val name: String) {
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.last.commit.audio;
|
|
||||||
|
|
||||||
import com.last.commit.audio.GameSound
|
|
||||||
|
|
||||||
public class GameSoundEffect(name: String): GameSound(name) {
|
|
||||||
companion object {
|
|
||||||
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_CLOSE = GameSoundEffect("door_close.mp3")
|
|
||||||
val TIME_TRAVEL = GameSoundEffect("time_travel.mp3")
|
|
||||||
val GRAB = GameSoundEffect("grab.mp3")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,75 +1,80 @@
|
|||||||
package com.last.commit.audio
|
package com.last.commit.audio
|
||||||
|
|
||||||
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
|
||||||
|
import com.last.commit.TimeTravelAssetManager
|
||||||
|
|
||||||
public class SoundEngine(private val sfx: Float, private val music: Float) {
|
object SoundEngine {
|
||||||
|
|
||||||
|
var sfxVolume = 1.0f
|
||||||
var volumeSfx = sfx
|
var musicVolume = 1.0f
|
||||||
var volumeMusic = music
|
|
||||||
set(newValue) {
|
set(newValue) {
|
||||||
field = newValue
|
field = newValue
|
||||||
resume()
|
resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val sounds: ThreadLocal<HashMap<String, Sound>> =
|
fun play(gameSound: String) {
|
||||||
ThreadLocal.withInitial() { HashMap() }
|
when (gameSound) {
|
||||||
private val musicTracks: ThreadLocal<HashMap<String, Music>> =
|
"STEPS_INDOOR" -> {
|
||||||
ThreadLocal.withInitial() { HashMap() }
|
val soundName = listOf(
|
||||||
|
"steps/steps_indoor_1.mp3",
|
||||||
|
"steps/steps_indoor_2.mp3",
|
||||||
|
"steps/steps_indoor_3.mp3",
|
||||||
|
).random()
|
||||||
|
val sound = loadEffect(soundName)
|
||||||
|
sound.play(sfxVolume)
|
||||||
|
}
|
||||||
|
|
||||||
var backgroundMusic: Music
|
"DOOR_OPEN" -> {
|
||||||
|
val sound = loadEffect("door_open.mp3")
|
||||||
|
sound.play(sfxVolume)
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
"DOOR_CLOSE" -> {
|
||||||
backgroundMusic = loadMusic("world_music.mp3")
|
val sound = loadEffect("door_close.mp3")
|
||||||
resume()
|
sound.play(sfxVolume)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun play(gameSound: GameSound, ) {
|
"TIME_TRAVEL" -> {
|
||||||
if (gameSound is GameSoundEffect) {
|
val sound = loadEffect("time_travel.mp3")
|
||||||
val sound = loadEffect(gameSound.name)
|
sound.play(sfxVolume)
|
||||||
sound.play(volumeSfx)
|
}
|
||||||
} else if (gameSound is GameMusic) {
|
|
||||||
backgroundMusic.setLooping(true)
|
"GRAB" -> {
|
||||||
resume()
|
val sound = loadEffect("grab.mp3")
|
||||||
|
sound.play(sfxVolume)
|
||||||
|
}
|
||||||
|
|
||||||
|
"BACKGROUND_MUSIC" -> {
|
||||||
|
loadMusic("world_music.mp3").play()
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
println("Could not find sound/music $gameSound")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stop() {
|
fun stop() {
|
||||||
backgroundMusic.pause()
|
loadMusic("world_music.mp3").pause()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun resume() {
|
fun resume() {
|
||||||
backgroundMusic.volume = volumeMusic
|
loadMusic("world_music.mp3").volume = musicVolume
|
||||||
if (!backgroundMusic.isPlaying) {
|
if (!loadMusic("world_music.mp3").isPlaying) {
|
||||||
backgroundMusic.play()
|
loadMusic("world_music.mp3").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().get(name)
|
return TimeTravelAssetManager.get("sounds/music/$name")
|
||||||
|
|
||||||
if (music == null) {
|
|
||||||
println("Loading sound $name")
|
|
||||||
music = Gdx.audio.newMusic(Gdx.files.internal("sounds/music/$name"))
|
|
||||||
musicTracks.get()[name] = music
|
|
||||||
}
|
|
||||||
return music!!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadSound(name: String): Sound {
|
private fun loadSound(name: String): Sound {
|
||||||
var sound = sounds.get().get(name)
|
return TimeTravelAssetManager.get("sounds/$name")
|
||||||
|
|
||||||
if (sound == null) {
|
|
||||||
println("Loading sound $name")
|
|
||||||
sound = Gdx.audio.newSound(Gdx.files.internal("sounds/$name"))
|
|
||||||
sounds.get()[name] = sound
|
|
||||||
}
|
|
||||||
|
|
||||||
return sound!!
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
package com.last.commit.config
|
|
||||||
|
|
||||||
enum class ActionCommand {
|
|
||||||
|
|
||||||
//move
|
|
||||||
UP, DOWN, LEFT, RIGHT,
|
|
||||||
|
|
||||||
//interaction
|
|
||||||
TIME_TRAVEL,
|
|
||||||
INTERACT,
|
|
||||||
|
|
||||||
//program interaction
|
|
||||||
OPEN_MENU,
|
|
||||||
|
|
||||||
JUMP
|
|
||||||
}
|
|
@ -1,91 +0,0 @@
|
|||||||
package com.last.commit.config
|
|
||||||
|
|
||||||
import com.badlogic.gdx.Input.Keys
|
|
||||||
import com.badlogic.gdx.math.MathUtils
|
|
||||||
import com.last.commit.GameState
|
|
||||||
import com.last.commit.audio.SoundEngine
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class GameSettings() {
|
|
||||||
lateinit var soundEngin : SoundEngine
|
|
||||||
|
|
||||||
private val actionKeys: EnumMap<ActionCommand, List<Int>> = EnumMap(ActionCommand::class.java)
|
|
||||||
private val actionKeysReversed: HashMap<Int, ActionCommand> = hashMapOf()
|
|
||||||
|
|
||||||
|
|
||||||
var musicEnabled: Boolean = true
|
|
||||||
set(newValue: Boolean) {
|
|
||||||
field = newValue
|
|
||||||
soundEngin?.volumeMusic = musicVolume
|
|
||||||
}
|
|
||||||
var sfxEnabled: Boolean = true
|
|
||||||
set(newValue: Boolean) {
|
|
||||||
field = newValue
|
|
||||||
soundEngin?.volumeSfx = sfxVolume
|
|
||||||
}
|
|
||||||
|
|
||||||
var musicVolume: Float = 0.5F
|
|
||||||
get() {
|
|
||||||
return if (musicEnabled) {
|
|
||||||
field
|
|
||||||
} else {
|
|
||||||
0F
|
|
||||||
}
|
|
||||||
}
|
|
||||||
set(newValue: Float) {
|
|
||||||
field = newValue
|
|
||||||
soundEngin?.volumeMusic = musicVolume
|
|
||||||
}
|
|
||||||
var sfxVolume: Float = 0.5F
|
|
||||||
get() {
|
|
||||||
return if (sfxEnabled) {
|
|
||||||
field
|
|
||||||
} else {
|
|
||||||
0F
|
|
||||||
}
|
|
||||||
}
|
|
||||||
set(newValue: Float) {
|
|
||||||
field = newValue
|
|
||||||
|
|
||||||
soundEngin?.volumeSfx = sfxVolume
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
init {
|
|
||||||
actionKeys[ActionCommand.UP] = listOf(Keys.UP, Keys.W)
|
|
||||||
actionKeys[ActionCommand.DOWN] = listOf(Keys.DOWN, Keys.S)
|
|
||||||
actionKeys[ActionCommand.LEFT] = listOf(Keys.LEFT, Keys.A)
|
|
||||||
actionKeys[ActionCommand.RIGHT] = listOf(Keys.RIGHT, Keys.D)
|
|
||||||
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)
|
|
||||||
|
|
||||||
setReversed(actionKeys)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setReversed(actionKeys: EnumMap<ActionCommand, List<Int>>) {
|
|
||||||
for (actionCode in actionKeys.keys) {
|
|
||||||
for (key in actionKeys.getValue(actionCode)) {
|
|
||||||
actionKeysReversed[key] = actionCode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getAction(keyCode: Int): ActionCommand? {
|
|
||||||
return actionKeysReversed[keyCode]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun getKeyCode(actionCommand: ActionCommand): List<Int> {
|
|
||||||
|
|
||||||
return if (Objects.nonNull(actionKeys[actionCommand])) {
|
|
||||||
actionKeys[actionCommand]!!
|
|
||||||
} else {
|
|
||||||
listOf()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package com.last.commit.config
|
|
||||||
|
|
||||||
import com.badlogic.gdx.assets.AssetManager
|
|
||||||
import com.badlogic.gdx.graphics.Texture
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Skin
|
|
||||||
|
|
||||||
class TimeTravelAssetManager : AssetManager() {
|
|
||||||
|
|
||||||
val UI_TEXTURE_PATH = "ui/uiskin.json"
|
|
||||||
|
|
||||||
init {
|
|
||||||
loadTextures()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun loadTextures() {
|
|
||||||
load(UI_TEXTURE_PATH, Skin::class.java)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getUiTexture() : Skin {
|
|
||||||
return get(UI_TEXTURE_PATH)
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,104 @@
|
|||||||
|
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.graphics.OrthographicCamera
|
||||||
|
import com.badlogic.gdx.graphics.g2d.GlyphLayout
|
||||||
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||||
|
import com.last.commit.Game
|
||||||
|
import com.last.commit.TimeTravelAssetManager
|
||||||
|
|
||||||
|
|
||||||
|
class LoadingScreen(private val assetManager: TimeTravelAssetManager, private val game: Game) : Screen {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val FONTS = 1
|
||||||
|
private const val TEXTURES = 2
|
||||||
|
private const val SKINS = 3
|
||||||
|
private const val MUSICS = 4
|
||||||
|
private const val SOUNDS = 5
|
||||||
|
private const val FINISHED = 6
|
||||||
|
}
|
||||||
|
|
||||||
|
private var currentLoadingStage = 0
|
||||||
|
private val layout = GlyphLayout()
|
||||||
|
private var currentText = ""
|
||||||
|
private val camera = OrthographicCamera()
|
||||||
|
|
||||||
|
// timer for exiting loading screen
|
||||||
|
private var countDown = 1f // 5 seconds of waiting before menu screen
|
||||||
|
|
||||||
|
private val batch = SpriteBatch()
|
||||||
|
|
||||||
|
init {
|
||||||
|
camera.setToOrtho(false, 800f, 600f)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun show() {}
|
||||||
|
override fun render(delta: Float) {
|
||||||
|
Gdx.gl.glClearColor(0f, 0f, 0.2f, 1f)
|
||||||
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
|
||||||
|
if (assetManager.update()) {
|
||||||
|
currentLoadingStage += 1
|
||||||
|
when (currentLoadingStage) {
|
||||||
|
TEXTURES -> {
|
||||||
|
currentText = "Loading textures"
|
||||||
|
assetManager.loadTextures()
|
||||||
|
}
|
||||||
|
|
||||||
|
SKINS -> {
|
||||||
|
currentText = "Loading skins"
|
||||||
|
assetManager.loadSkins()
|
||||||
|
}
|
||||||
|
|
||||||
|
MUSICS -> {
|
||||||
|
currentText = "Loading musics"
|
||||||
|
assetManager.loadMusics("sounds/music/world_music.mp3")
|
||||||
|
}
|
||||||
|
|
||||||
|
SOUNDS -> {
|
||||||
|
currentText = "Loading sounds"
|
||||||
|
assetManager.loadSounds(
|
||||||
|
"sounds/effects/steps/steps_indoor_1.mp3",
|
||||||
|
"sounds/effects/steps/steps_indoor_2.mp3",
|
||||||
|
"sounds/effects/steps/steps_indoor_3.mp3",
|
||||||
|
"sounds/effects/door_open.mp3",
|
||||||
|
"sounds/effects/door_close.mp3",
|
||||||
|
"sounds/effects/time_travel.mp3",
|
||||||
|
"sounds/effects/grab.mp3"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
FINISHED -> currentText = "Finished"
|
||||||
|
else -> currentText = "Key no found"
|
||||||
|
}
|
||||||
|
if (currentLoadingStage > 5) {
|
||||||
|
countDown -= delta // timer to stay on loading screen for short preiod once done loading
|
||||||
|
currentLoadingStage =
|
||||||
|
5 // cap loading stage to 5 as will use later to display progress bar anbd more than 5 would go off the screen
|
||||||
|
if (countDown < 0) { // countdown is complete
|
||||||
|
// game.getAudioManager().load()
|
||||||
|
game.changeScreen(Screens.MAIN_MENU)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
layout.setText(game.font, currentText)
|
||||||
|
camera.update()
|
||||||
|
batch.projectionMatrix = camera.combined
|
||||||
|
batch.begin()
|
||||||
|
game.font.draw(batch, currentText, 800 / 2 - layout.width / 2, layout.height + 10)
|
||||||
|
batch.end()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun resize(width: Int, height: Int) {}
|
||||||
|
override fun pause() {}
|
||||||
|
override fun resume() {}
|
||||||
|
override fun hide() {
|
||||||
|
assetManager.finishLoading()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun dispose() {}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
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 fun handleMouseInput(screenX: Int, screenY: Int, pointer: Int, button: Int)
|
|
||||||
|
|
||||||
abstract fun getInputProcessors(): Array<InputProcessor>
|
|
||||||
}
|
|
Loading…
Reference in New Issue