Skip to content

Commit 214ac4b

Browse files
chore: Tidy ups 01
1 parent e088a80 commit 214ac4b

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

src/main/scala-2.11/nl/amsscala/simplegame/game.scala

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
128135
private 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-
*/
146146
private 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-
*/
155151
private 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 */
160154
private object Hero {
161155
val size = 32
162156
val speed = 256

src/main/scala-2.11/nl/amsscala/simplegame/package.scala

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
package nl.amsscala
22

33
/**
4-
* Provides generic classes and operators for dealing with 2D positions.
5-
*
4+
* Provides generic class and operators for dealing with 2D positions.
5+
* As well dealing with 2D areas.
66
*/
77
package object simplegame {
88
// Experimental timestamp and position, displacement is a function of time
99
type keysBufferType = scala.collection.mutable.Map[Int, (Double, Position[Int])]
1010

11+
/**
12+
* Generic base class Position, holding the two coordinates
13+
*
14+
* @param x
15+
* @param y
16+
* @tparam P
17+
*/
1118
case class Position[P: Numeric](x: P, y: P) {
1219

1320
import Numeric.Implicits.infixNumericOps
1421
import Ordering.Implicits.infixOrderingOps
1522

23+
/** Binaire sum operator for two coordinates */
1624
def +(p: Position[P]) = Position(x + p.x, y + p.y)
1725

26+
/** Binaire sum operator e,g. (a, b) + n => (a + n, b +n) */
1827
def +(term: P) = Position(x + term, y + term)
1928

29+
/** Binaire subtract operator for the difference of two coordinates */
2030
def -(p: Position[P]) = Position(x - p.x, y - p.y)
2131

32+
/** Binaire multiply operator for two coordinates, multplies each of the ordinate */
2233
def *(p: Position[P]) = Position(x * p.x, y * p.y)
2334

35+
/** Binaire multiply operator e,g. (a, b) * n=> (a * n, b * n) */
2436
def *(factor: P) = Position(x * factor, y * factor)
2537

2638
private def interSectsArea[P: Numeric](p0: Position[P], p1: Position[P], p2: Position[P], p3: Position[P]) = {
@@ -30,17 +42,24 @@ package object simplegame {
3042
intersectsWith(p0.y, p1.y, p2.y, p3.y)
3143
}
3244

33-
def isValidPosition(canvasPos: Position[P], size: P): Boolean = {
45+
/**
46+
* Check if the square area is within the rectangle area
47+
*
48+
* @param canvasPos
49+
* @param side
50+
* @return
51+
*/
52+
def isValidPosition(canvasPos: Position[P], side: P): Boolean = {
3453
// println(s"Testing: $x, $y")
3554

36-
interSectsArea(Position(0, 0).asInstanceOf[Position[P]], canvasPos, this + size, this)
55+
interSectsArea(Position(0, 0).asInstanceOf[Position[P]], canvasPos, this + side, this)
3756
}
3857

3958
/**
4059
* Checks that two squares intersects
4160
*
4261
* @param posB Position of the second square
43-
* @param side size of both two squares
62+
* @param side side of both two squares
4463
* @return True if a intersection occurs
4564
*/
4665
def areTouching(posB: Position[P], side: P): Boolean = interSectsArea(this, this + side, posB, posB + side)

src/test/scala-2.11/nl/amsscala/simplegame/GameSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class GameSuite extends SuiteSpec {
8181
game.updateGame(1D, mutable.Map(0 -> dummyTimeStamp), canvas) shouldBe game
8282
}
8383
it("bad path") { // No move due a of out canvas limit case
84-
game.updateGame(1.48828125, mutable.Map(Right -> dummyTimeStamp, Down -> dummyTimeStamp), canvas) shouldBe game
84+
game.updateGame(1.48828125D, mutable.Map(Right -> dummyTimeStamp, Down -> dummyTimeStamp), canvas) shouldBe game
8585
}
8686
it("experiment") {
8787

0 commit comments

Comments
 (0)