-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip(map-pick): add map picker in setup view
- Loading branch information
Showing
4 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/scala/scatan/views/game/components/map/MapComponent.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package scatan.views.game.components.map | ||
|
||
import scatan.model.GameMap | ||
import com.raquo.laminar.api.L.* | ||
import scatan.model.map.Hexagon | ||
import scatan.views.utils.Coordinates.center | ||
import scatan.views.utils.Coordinates | ||
import scatan.views.game.components.ContextMap | ||
import scatan.views.game.components.ContextMap.toImgId | ||
import scatan.model.map.TileContent | ||
import scatan.model.components.UnproductiveTerrain | ||
|
||
object MapComponent: | ||
|
||
private given hexSize: Int = 100 | ||
private val radius = hexSize / 4 | ||
private val svgCornersPoints: String = | ||
(for | ||
i <- 0 to 5 | ||
angleDeg = 60 * i + 30 | ||
angleRad = Math.PI / 180 * angleDeg | ||
x = hexSize * math.cos(angleRad) | ||
y = hexSize * math.sin(angleRad) | ||
yield s"$x,$y").mkString(" ") | ||
private val layersToCanvasSize: Int => Int = x => (2 * x * hexSize) + 50 | ||
|
||
def map: GameMap ?=> Element = | ||
val gameMap = summon[GameMap] | ||
val canvasSize = layersToCanvasSize(gameMap.totalLayers) | ||
svg.svg( | ||
svgImages, | ||
svg.viewBox := s"-${canvasSize} -${canvasSize} ${2 * canvasSize} ${2 * canvasSize}", | ||
for | ||
hex <- gameMap.tiles.toList | ||
content = gameMap.toContent(hex) | ||
yield svgHexagon(hex, content) | ||
) | ||
|
||
private def svgHexagon(hex: Hexagon, content: TileContent): Element = | ||
val Coordinates(x, y) = hex.center | ||
svg.g( | ||
svg.transform := s"translate($x, $y)", | ||
svg.polygon( | ||
svg.points := svgCornersPoints, | ||
svg.cls := "hexagon", | ||
svg.fill := s"url(#${content.terrain.toImgId})" | ||
), | ||
content.terrain match | ||
case UnproductiveTerrain.Sea => "" | ||
case _ => circularNumber(hex, content) | ||
) | ||
|
||
def circularNumber(hex: Hexagon, content: TileContent): Element = | ||
svg.g( | ||
svg.circle( | ||
svg.cx := "0", | ||
svg.cy := "0", | ||
svg.r := s"$radius", | ||
svg.className := "hexagon-center-circle" | ||
), | ||
svg.text( | ||
svg.x := "0", | ||
svg.y := "0", | ||
svg.fontSize := s"$radius", | ||
svg.className := "hexagon-center-number", | ||
content.number.map(_.toString).getOrElse("") | ||
) | ||
) | ||
|
||
private val svgImages: Element = | ||
svg.svg( | ||
svg.defs( | ||
for (terrain, path) <- ContextMap.resources.toList | ||
yield svg.pattern( | ||
svg.idAttr := terrain.toImgId, | ||
svg.width := "100%", | ||
svg.height := "100%", | ||
svg.patternContentUnits := "objectBoundingBox", | ||
svg.image( | ||
svg.href := path, | ||
svg.width := "1", | ||
svg.height := "1", | ||
svg.preserveAspectRatio := "none" | ||
) | ||
) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters