Commits

Philipp Dörfler committed 7855a80

Changed a "by convention" 'check' into a run-time checked one

Comments (0)

Files changed (1)

src/main/scala/org/swiftsteel/model/playground/points/MapPoint.scala

  * <p>
  * It can be thought of as a point in the automap or overview map or waypoint.
  * <p>
- * By convention, adding, multiplying etc. a point with another one only works
- * when both originate from the very same map.
+ * Adding and subtracting a point with another one only works when both originate from
+ * the very same map.
  */
 case class MapPoint private(
   val map: Terrain,
 ) {
   MapPoint.checkMapBoundsViolationOf(this)
   
-  def + (that: MapPoint) = MapPoint(map, x + that.x, y + that.y)
-  def - (that: MapPoint) = MapPoint(map, x - that.x, y - that.y)
+  def + (that: MapPoint) = {
+    require(map == that.map)
+    MapPoint(map, x + that.x, y + that.y)
+  }
+  
+  def - (that: MapPoint) = {
+    require(map == that.map)
+    MapPoint(map, x - that.x, y - that.y)
+  }
+  
   def * (amount: Double) = MapPoint(map, x * amount, y * amount)
   
   def isNear (that: MapPoint) =
   
   def checkMapBoundsViolationOf(point: MapPoint) = {
     require(laysInBounds(point.map, point.x, point.y),
-        "Point (" + point.x + ", " + point.y + ") is out of bounds (" + 
+        "Point (" + point.x + ", " + point.y + ") is out of bounds (" +
           point.map.width + "x" + point.map.height + ")")
   }