Skip to content

Commit e088a80

Browse files
chore: Tidy ups
1 parent 85f946a commit e088a80

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ private case class GameState(hero: Hero[Int], monster: Monster[Int], monstersCau
9494
val newHero = new Hero(displacements.fold(hero.pos) { (z, i) => z + i * (Hero.speed * latency).toInt })
9595
val correctedThis = newHero.pos + Hero.size
9696

97-
if (newHero.pos.isValidPosition(Position(canvas.width, canvas.height), correctedThis)) // Are they touching?
98-
if (newHero.pos.areTouching(monster.pos, correctedThis, monster.pos + Hero.size)) // Reset the game when the player catches a monster
97+
if (newHero.pos.isValidPosition(Position(canvas.width, canvas.height), Hero.size)) // Are they touching?
98+
if (newHero.pos.areTouching(monster.pos, Hero.size)) // Reset the game when the player catches a monster
9999
new GameState(canvas, monstersCaught)
100100
else copy(hero = newHero)
101101
else this
@@ -135,7 +135,7 @@ private class Monster[T: Numeric](val pos: Position[T]) {
135135
protected[simplegame] def isValidPosition(canvas: dom.html.Canvas) = {
136136
pos.isValidPosition(
137137
Position(canvas.width, canvas.height).asInstanceOf[Position[T]],
138-
pos + Hero.size.asInstanceOf[T]
138+
Hero.size.asInstanceOf[T]
139139
)
140140
}
141141
}

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package nl.amsscala
22

3-
import org.scalajs.dom
4-
53
/**
64
* Provides generic classes and operators for dealing with 2D positions.
75
*
@@ -25,17 +23,27 @@ package object simplegame {
2523

2624
def *(factor: P) = Position(x * factor, y * factor)
2725

28-
@inline def intersectsWith(a0: P, b0: P, a1: P, b1: P) = a0 <= b1 && a1 <= b0
29-
def isValidPosition(canvasPos: Position[P], correctedThis: Position[P]): Boolean = {
30-
// println(s"Testing: $x, $y")
26+
private def interSectsArea[P: Numeric](p0: Position[P], p1: Position[P], p2: Position[P], p3: Position[P]) = {
27+
@inline def intersectsWith(a0: P, b0: P, a1: P, b1: P) = a0 <= b1 && a1 <= b0
3128

32-
intersectsWith(0.asInstanceOf[P], canvasPos.x, correctedThis.x, x) &&
33-
intersectsWith(0.asInstanceOf[P], canvasPos.y, correctedThis.y, y)
29+
intersectsWith(p0.x, p1.x, p2.x, p3.x) &&
30+
intersectsWith(p0.y, p1.y, p2.y, p3.y)
3431
}
3532

36-
def areTouching(posB: Position[P], correctedThis: Position[P], correctedPosB: Position[P]): Boolean = {
37-
intersectsWith(x, correctedThis.x, posB.x, correctedPosB.x) &&
38-
intersectsWith(y, correctedThis.y, posB.y, correctedPosB.y)
33+
def isValidPosition(canvasPos: Position[P], size: P): Boolean = {
34+
// println(s"Testing: $x, $y")
35+
36+
interSectsArea(Position(0, 0).asInstanceOf[Position[P]], canvasPos, this + size, this)
3937
}
38+
39+
/**
40+
* Checks that two squares intersects
41+
*
42+
* @param posB Position of the second square
43+
* @param side size of both two squares
44+
* @return True if a intersection occurs
45+
*/
46+
def areTouching(posB: Position[P], side: P): Boolean = interSectsArea(this, this + side, posB, posB + side)
4047
}
48+
4149
}

0 commit comments

Comments
 (0)