Skip to content

Commit

Permalink
refactor(point): rename and small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru committed Oct 16, 2023
1 parent f08d16c commit d044df0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*/
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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

0 comments on commit d044df0

Please sign in to comment.