diff --git a/src/superposition/game/LevelPlaylist.scala b/src/superposition/game/LevelPlaylist.scala index 396fe82..8394e0e 100644 --- a/src/superposition/game/LevelPlaylist.scala +++ b/src/superposition/game/LevelPlaylist.scala @@ -15,6 +15,9 @@ final class LevelPlaylist(engine: Engine) { /** The list of level factories. */ private var factories: Seq[LevelFactory] = Nil + /** The index of the current level. */ + private var currentIndex: Int = 0 + /** The current level. */ private var _current: Option[Level] = None @@ -31,17 +34,24 @@ final class LevelPlaylist(engine: Engine) { /** Advances to the next level in the playlist. */ def next(): Unit = { - factories = factories match { - case Nil => Nil - case _ :: next => next + if (currentIndex < factories.size - 1) { + currentIndex += 1 + play() + } + } + + /** Retreats to the previous level in the playlist. */ + def prev(): Unit = { + if (currentIndex > 0) { + currentIndex -= 1 + play() } - play() } /** Plays the current level or resets the current level if it is already playing. */ def play(): Unit = { current.foreach(removeLevel(engine)) - _current = factories.headOption map (_ ()) + _current = Some(factories(currentIndex)()) current.foreach(addLevel(engine)) } } diff --git a/src/superposition/system/LevelSystem.scala b/src/superposition/system/LevelSystem.scala index 182ccc7..dad5bf9 100644 --- a/src/superposition/system/LevelSystem.scala +++ b/src/superposition/system/LevelSystem.scala @@ -2,7 +2,7 @@ package superposition.system import com.badlogic.ashley.core.{Engine, Entity, EntitySystem, Family} import com.badlogic.gdx.Gdx.input -import com.badlogic.gdx.Input.Keys.{N, R} +import com.badlogic.gdx.Input.Keys._ import superposition.component._ import superposition.game.LevelPlaylist @@ -25,8 +25,13 @@ final class LevelSystem(levels: LevelPlaylist) extends EntitySystem { } override def update(deltaTime: Float): Unit = { + if (input.isKeyJustPressed(P)) { + levels.prev() + return + } if (input.isKeyJustPressed(R)) { levels.play() + return } val multiverse = levels.current.get.multiverse val allExitSquares = exits.flatMap(ClassicalPosition.mapper.get(_).cells).toSet @@ -34,6 +39,7 @@ final class LevelSystem(levels: LevelPlaylist) extends EntitySystem { .forall(entityCell => multiverse.universes.map(_.state(entityCell)).forall(allExitSquares.contains)) if (input.isKeyJustPressed(N) || playersAtExits) { levels.next() + return } } }