From d044df0da6f61bc74b3ee8292aafe33c2acb20b0 Mon Sep 17 00:00:00 2001 From: Manuel Andruccioli Date: Mon, 16 Oct 2023 11:33:30 +0200 Subject: [PATCH] refactor(point): rename and small changes --- .../game/components/GameMapComponent.scala | 10 +++--- .../views/game/components/MapComponent.scala | 6 ++-- .../utils/{Coordinate.scala => Point.scala} | 36 ++++++++++--------- 3 files changed, 27 insertions(+), 25 deletions(-) rename src/main/scala/scatan/views/utils/{Coordinate.scala => Point.scala} (64%) diff --git a/src/main/scala/scatan/views/game/components/GameMapComponent.scala b/src/main/scala/scatan/views/game/components/GameMapComponent.scala index 9abeb16..62033a8 100644 --- a/src/main/scala/scatan/views/game/components/GameMapComponent.scala +++ b/src/main/scala/scatan/views/game/components/GameMapComponent.scala @@ -6,8 +6,8 @@ import scatan.model.game.config.ScatanPlayer import scatan.model.game.state.ScatanState import scatan.model.map.* import scatan.views.game.components.MapComponent.{MapElement, radius, given} -import scatan.views.utils.Coordinates -import scatan.views.utils.Coordinates.* +import scatan.views.utils.Point +import scatan.views.utils.Point.* import scatan.views.utils.TypeUtils.* /** A component to display the game map. @@ -115,8 +115,8 @@ object GameMapComponent: * the svg road */ private def svgRoad(road: RoadSpot): InputSourceWithState[Element] = - val Coordinates(x1, y1) = road._1.coordinates.get - val Coordinates(x2, y2) = road._2.coordinates.get + val Point(x1, y1) = road._1.point + val Point(x2, y2) = road._2.point val player = assignmentInfoOf(road).map(_.viewPlayer) svg.g( svg.line( @@ -145,7 +145,7 @@ object GameMapComponent: * the svg spot */ private def svgSpot(structure: StructureSpot): InputSourceWithState[Element] = - val Coordinates(x, y) = structure.coordinates.get + val Point(x, y) = structure.point val player = assignmentInfoOf(structure).map(_.viewPlayer) val structureType = assignmentInfoOf(structure).map(_.viewBuildingType) svg.g( diff --git a/src/main/scala/scatan/views/game/components/MapComponent.scala b/src/main/scala/scatan/views/game/components/MapComponent.scala index bbed0f3..a7318e0 100644 --- a/src/main/scala/scatan/views/game/components/MapComponent.scala +++ b/src/main/scala/scatan/views/game/components/MapComponent.scala @@ -8,8 +8,8 @@ import scatan.model.components.ResourceType.* import scatan.model.components.{Terrain, UnproductiveTerrain} import scatan.model.components.UnproductiveTerrain.* import scatan.model.map.{GameMap, Hexagon, TileContent} -import scatan.views.utils.Coordinates -import scatan.views.utils.Coordinates.center +import scatan.views.utils.Point +import scatan.views.utils.Point.center /** A component to display the map of hexagons. */ @@ -88,7 +88,7 @@ object MapComponent: * the svg hexagon. */ private[components] def svgHexagon(hex: Hexagon, elements: LaminarElement*): MapElement = - val Coordinates(x, y) = hex.center + val Point(x, y) = hex.center svg.g( svg.transform := s"translate($x, $y)", svg.polygon( diff --git a/src/main/scala/scatan/views/utils/Coordinate.scala b/src/main/scala/scatan/views/utils/Point.scala similarity index 64% rename from src/main/scala/scatan/views/utils/Coordinate.scala rename to src/main/scala/scatan/views/utils/Point.scala index e92a588..5356dd8 100644 --- a/src/main/scala/scatan/views/utils/Coordinate.scala +++ b/src/main/scala/scatan/views/utils/Point.scala @@ -5,22 +5,24 @@ import scatan.model.map.{Hexagon, StructureSpot} /** @param value * the value of the double to wrap. */ -final case class DoubleWithPrecision(value: Double): +final case class DoubleWithPrecision(value: Double, precision: Int = 3): + private val absDiff: Double = math.pow(10, -precision) + private val roundConst = math.pow(10, precision) override def equals(x: Any): Boolean = x match case that: DoubleWithPrecision => - (this.value - that.value).abs < 0.001 + (this.value - that.value).abs < absDiff case _ => false - override def hashCode: Int = (value * 1000).round.toInt.hashCode + override def hashCode: Int = (value * roundConst).round.toInt.hashCode /** A trait to represent cartesian coordinates. */ -trait Coordinates: +trait Point: def x: DoubleWithPrecision def y: DoubleWithPrecision -object Coordinates: +object Point: /** Create a new Coordinates object. * @@ -31,7 +33,7 @@ object Coordinates: * @return * the new Coordinates object */ - def apply(x: DoubleWithPrecision, y: DoubleWithPrecision): Coordinates = CoordinatesImpl(x, y) + def apply(x: DoubleWithPrecision, y: DoubleWithPrecision): Point = PointImpl(x, y) /** Extract the x and y coordinates from a Coordinates object, unwrapping them to double. * @@ -40,16 +42,16 @@ object Coordinates: * @return * a pair of doubles. */ - def unapply(coordinates: Coordinates): (Double, Double) = - (coordinates.x.value, coordinates.y.value) + def unapply(point: Point): (Double, Double) = + (point.x.value, point.y.value) - private case class CoordinatesImpl(x: DoubleWithPrecision, y: DoubleWithPrecision) extends Coordinates + private case class PointImpl(x: DoubleWithPrecision, y: DoubleWithPrecision) extends Point /** Convert a pair of doubles to a Coordinates object. */ - given Conversion[(Double, Double), Coordinates] with - def apply(pair: (Double, Double)): Coordinates = - Coordinates(DoubleWithPrecision(pair._1), DoubleWithPrecision(pair._2)) + given Conversion[(Double, Double), Point] with + def apply(pair: (Double, Double)): Point = + Point(DoubleWithPrecision(pair._1), DoubleWithPrecision(pair._2)) /** Extension methods to handle hexagon in cartesian plane. */ @@ -59,7 +61,7 @@ object Coordinates: * @return * the center point of the hexagon */ - def center(using hexSize: Int): Coordinates = + def center(using hexSize: Int): Point = val x = hexSize * (math.sqrt(3) * hex.col + math.sqrt(3) / 2 * hex.row) val y = hexSize * (3.0 / 2 * hex.row) (x, y) @@ -69,8 +71,8 @@ object Coordinates: * @return * the points of the hexagon */ - def vertices(using hexSize: Int): Set[Coordinates] = - val Coordinates(x, y) = center + def vertices(using hexSize: Int): Set[Point] = + val Point(x, y) = center for i <- (0 to 5).toSet angle_deg = 60 * i - 30 @@ -83,5 +85,5 @@ object Coordinates: /** Extension methods to handle spot in cartesian plane. */ extension (spot: StructureSpot) - def coordinates(using hexSize: Int): Option[Coordinates] = - (spot._1.vertices & spot._2.vertices & spot._3.vertices).headOption + def point(using hexSize: Int): Point = + (spot._1.vertices & spot._2.vertices & spot._3.vertices).head