Commits

Philipp Dörfler  committed 5dff2be

Tried base both Point and MapPoint on a common abstract point class

  • Participants
  • Parent commits ef3f7dc

Comments (0)

Files changed (4)

File src/main/scala/org/swiftsteel/model/playground/points/AbstractPoint.scala

+/*******************************************************************************
+ * Copyright (c) 2009-2011 Philipp Dörfler
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ * 
+ * 3. Neither the name of the copyright holders nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ ******************************************************************************/
+
+package org.swiftsteel.model.playground.points
+
+import org.swiftsteel.model.playground.Terrain
+
+abstract class AbstractPoint[P <: AbstractPoint[P]] {
+  protected val map: Terrain
+  
+  def +(other: P): P = {
+    mustBeOnSameMapAs(other)
+    +☞(other)
+  }
+  
+  def -(other: P): P = {
+    mustBeOnSameMapAs(other)
+    -☞(other)
+  }
+  
+  protected def +☞(other: P): P
+  protected def -☞(other: P): P
+  
+  def *(amount: Double): P
+  
+  protected def mustBeOnSameMapAs(other: P) =
+    require(map == other.map, "terrain of both points differs: %s != %s".format(map, other.map))
+}

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

   val map: Terrain,
   val x: Double,
   val y: Double
-) {
+) extends AbstractPoint[MapPoint] {
   MapPoint checkMapBoundsViolationOf this
   
-  def + (other: MapPoint) = {
-    mustBeOnSameMapAs(other)
-    MapPoint(map, x + other.x, y + other.y)
-  }
-  
-  def - (other: MapPoint) = {
-    mustBeOnSameMapAs(other)
-    MapPoint(map, x - other.x, y - other.y)
-  }
-  
+  protected def +☞(other: MapPoint) = MapPoint(map, x + other.x, y + other.y)
+  protected def -☞(other: MapPoint) = MapPoint(map, x - other.x, y - other.y)
   def * (amount: Double) = MapPoint(map, x * amount, y * amount)
   
   def isNear (other: MapPoint) = {
   }
     
   lazy val to3D = Point(map, x, y, map.heightAt(this))
-  
-  private def mustBeOnSameMapAs(other: MapPoint) =
-    require(map == other.map, "terrain of both points differs: %s != %s".format(map, other.map))
 }
 
 object MapPoint {

File src/main/scala/org/swiftsteel/model/playground/points/Point.scala

   x: Double,
   y: Double,
   height: Double
-) {
+) extends AbstractPoint[Point] {
   MapPoint.checkMapBoundsViolationOf(to2D)
   
   private def to2D = MapPoint.of(map, x, y)
   
-  def + (that: Point) = Point(map, x + that.x, y + that.y, height + that.height)
-  def - (that: Point) = Point(map, x - that.x, y - that.y, height - that.height)
+  protected def +☞(other: Point) = Point(map, x + other.x, y + other.y, height + other.height)
+  protected def -☞(other: Point) = Point(map, x - other.x, y - other.y, height - other.height)
   def * (amount: Double) = Point(map, x * amount, y * amount, height * amount)
 }

File src/test/scala/org/swiftsteel/model/playground/TerrainPaddingSpec.scala

 
 import org.swiftsteel.specs._
 import org.swiftsteel.model.math.NextPowerOfTwo
-import org.swiftsteel.model.util.TwoDimensionalArray
 
 object TerrainPaddingSpec extends SwiftSteelSpec {