@@ -29,7 +29,7 @@ protected trait Game {
2929 def gameLoop = () => {
3030 val now = js.Date .now()
3131 val delta = now - prev
32- val updated = oldUpdated.getOrElse(new GameState (canvas, - 1 )).updateGame(delta / 1000 , keysPressed, canvas)
32+ val updated = oldUpdated.getOrElse(new GameState (canvas, - 1 , true )).updateGame(delta / 1000 , keysPressed, canvas)
3333
3434 if (oldUpdated.isEmpty || (oldUpdated.get.hero.pos != updated.hero.pos))
3535 oldUpdated = SimpleCanvasGame .render(updated)
@@ -62,8 +62,14 @@ protected trait Game {
6262 * @param hero Hero object with its position
6363 * @param monster Monster object with its position
6464 * @param monstersCaught The score
65+ * @param newGame Flags new game
6566 */
66- private case class GameState (hero : Hero [Int ], monster : Monster [Int ], monstersCaught : Int = 0 ) {
67+ private case class GameState (
68+ hero : Hero [Int ],
69+ monster : Monster [Int ],
70+ monstersCaught : Int = 0 ,
71+ newGame : Boolean
72+ ) {
6773
6874 /**
6975 * Update game objects according the pressed keys.
@@ -92,11 +98,10 @@ private case class GameState(hero: Hero[Int], monster: Monster[Int], monstersCau
9298 if (keysDown.isEmpty) this
9399 else {
94100 val newHero = new Hero (displacements.fold(hero.pos) { (z, i) => z + i * (Hero .speed * latency).toInt })
95- val correctedThis = newHero.pos + Hero .size
96101
97102 if (newHero.pos.isValidPosition(Position (canvas.width, canvas.height), Hero .size)) // Are they touching?
98103 if (newHero.pos.areTouching(monster.pos, Hero .size)) // Reset the game when the player catches a monster
99- new GameState (canvas, monstersCaught)
104+ new GameState (canvas, monstersCaught, true )
100105 else copy(hero = newHero)
101106 else this
102107 }
@@ -110,20 +115,22 @@ private case class GameState(hero: Hero[Int], monster: Monster[Int], monstersCau
110115 * @param canvas The visual html element
111116 * @param oldScore Score accumulator
112117 */
113- def this (canvas : dom.html.Canvas , oldScore : Int ) {
118+ def this (canvas : dom.html.Canvas , oldScore : Int , newGame : Boolean ) {
114119 this (
115120 Hero (canvas.width / 2 , canvas.height / 2 ),
116121 // Throw the monster somewhere on the screen randomly
117122 Monster ((math.random * (canvas.width - Hero .size)).toInt, (math.random * (canvas.height - Hero .size)).toInt),
118- oldScore + 1
123+ oldScore + 1 ,
124+ newGame
119125 )
120126 }
121127}
122128
123129/**
130+ * Monster class, holder for its coordinates, copied as extentension to the Hero class
124131 *
125- * @param pos
126- * @tparam T
132+ * @param pos Monsters' position
133+ * @tparam T Numeric generic abstraction
127134 */
128135private class Monster [T : Numeric ](val pos : Position [T ]) {
129136 override def equals (that : Any ): Boolean = that match {
@@ -132,31 +139,18 @@ private class Monster[T: Numeric](val pos: Position[T]) {
132139 }
133140
134141 override def toString = s " ${this .getClass.getSimpleName} $pos"
135- protected [simplegame] def isValidPosition (canvas : dom.html.Canvas ) = {
136- pos.isValidPosition(
137- Position (canvas.width, canvas.height).asInstanceOf [Position [T ]],
138- Hero .size.asInstanceOf [T ]
139- )
140- }
142+ protected [simplegame] def isValidPosition (canvas : dom.html.Canvas ) =
143+ pos.isValidPosition(Position (canvas.width, canvas.height).asInstanceOf [Position [T ]], Hero .size.asInstanceOf [T ])
141144}
142145
143- /**
144- *
145- */
146146private object Monster {
147147 // def apply[T: Numeric](pos: Position[T]) = new Monster(pos)
148148 def apply [T : Numeric ](x : T , y : T ) = new Monster (Position (x, y))
149149}
150150
151- /**
152- * @param pos
153- * @tparam A
154- */
155151private class Hero [A : Numeric ](override val pos : Position [A ]) extends Monster [A ](pos)
156152
157- /**
158- * Compagnion object of class Hero
159- */
153+ /** Compagnion object of class Hero */
160154private object Hero {
161155 val size = 32
162156 val speed = 256
0 commit comments