11package 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