Skip to content

Commit

Permalink
Added a previous level button
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoiffer committed Jun 9, 2020
1 parent ed7c530 commit ef146c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/superposition/game/LevelPlaylist.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/superposition/system/LevelSystem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -25,15 +25,21 @@ 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
val playersAtExits = players.map(QuantumPosition.mapper.get(_).cell)
.forall(entityCell => multiverse.universes.map(_.state(entityCell)).forall(allExitSquares.contains))
if (input.isKeyJustPressed(N) || playersAtExits) {
levels.next()
return
}
}
}

0 comments on commit ef146c7

Please sign in to comment.