From f7b5a506fd59c36c60533ace1fbcc00a22e7aca1 Mon Sep 17 00:00:00 2001 From: r3versi Date: Fri, 3 Jan 2020 09:33:01 +0100 Subject: [PATCH] HexMath is a singleton (#1603) --- core/src/com/unciv/logic/GameStarter.kt | 2 +- core/src/com/unciv/logic/HexMath.kt | 17 +++++++++-------- core/src/com/unciv/logic/map/MapGenerator.kt | 8 ++++---- core/src/com/unciv/logic/map/TileMap.kt | 6 +++--- core/src/com/unciv/ui/EmpireOverviewScreen.kt | 2 +- core/src/com/unciv/ui/cityscreen/CityScreen.kt | 2 +- core/src/com/unciv/ui/map/TileGroupMap.kt | 2 +- .../com/unciv/ui/mapeditor/EditorMapHolder.kt | 4 ++-- core/src/com/unciv/ui/tilegroups/TileGroup.kt | 4 ++-- core/src/com/unciv/ui/worldscreen/Minimap.kt | 2 +- 10 files changed, 25 insertions(+), 24 deletions(-) diff --git a/core/src/com/unciv/logic/GameStarter.kt b/core/src/com/unciv/logic/GameStarter.kt index 092f6730..b41fdbcf 100644 --- a/core/src/com/unciv/logic/GameStarter.kt +++ b/core/src/com/unciv/logic/GameStarter.kt @@ -191,7 +191,7 @@ class GameStarter{ // edge is checking the distance to the CENTER POINT // Can't believe we used a dumb way of calculating this before! val hexagonalRadius = -tileMap.leftX - val distanceFromCenter = HexMath().getDistance(vector, Vector2.Zero) + val distanceFromCenter = HexMath.getDistance(vector, Vector2.Zero) return hexagonalRadius-distanceFromCenter >= n } } \ No newline at end of file diff --git a/core/src/com/unciv/logic/HexMath.kt b/core/src/com/unciv/logic/HexMath.kt index 6b30b702..e0ec2002 100644 --- a/core/src/com/unciv/logic/HexMath.kt +++ b/core/src/com/unciv/logic/HexMath.kt @@ -4,7 +4,7 @@ import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector3 import kotlin.math.* -class HexMath { +object HexMath { fun getVectorForAngle(angle: Float): Vector2 { return Vector2(Math.sin(angle.toDouble()).toFloat(), Math.cos(angle.toDouble()).toFloat()) @@ -15,13 +15,14 @@ class HexMath { } fun getAdjacentVectors(origin: Vector2): ArrayList { - val vectors = ArrayList() - vectors += Vector2(1f, 0f) - vectors += Vector2(1f, 1f) - vectors += Vector2(0f, 1f) - vectors += Vector2(-1f, 0f) - vectors += Vector2(-1f, -1f) - vectors += Vector2(0f, -1f) + val vectors = arrayListOf( + Vector2(1f, 0f), + Vector2(1f, 1f), + Vector2(0f, 1f), + Vector2(-1f, 0f), + Vector2(-1f, -1f), + Vector2(0f, -1f) + ) for (vector in vectors) vector.add(origin) return vectors } diff --git a/core/src/com/unciv/logic/map/MapGenerator.kt b/core/src/com/unciv/logic/map/MapGenerator.kt index 5f1af4dc..8dacbaba 100644 --- a/core/src/com/unciv/logic/map/MapGenerator.kt +++ b/core/src/com/unciv/logic/map/MapGenerator.kt @@ -623,7 +623,7 @@ class MapLandmassGenerator { for (loop in 0..numSmooth) { for (tileInfo in tileMap.values) { - if (HexMath().getDistance(Vector2.Zero, tileInfo.position) < mapRadius) { + if (HexMath.getDistance(Vector2.Zero, tileInfo.position) < mapRadius) { val numberOfLandNeighbors = tileInfo.neighbors.count { it.baseTerrain == grassland } if (tileInfo.baseTerrain == grassland) { // land tile if (numberOfLandNeighbors < 3) @@ -652,7 +652,7 @@ class MapLandmassGenerator { val landProbability = 0.55f if (mapType == MapType.pangaea) { - val distanceFactor = (HexMath().getDistance(Vector2.Zero, tileInfo.position) * 1.8 / mapRadius).toFloat() + val distanceFactor = (HexMath.getDistance(Vector2.Zero, tileInfo.position) * 1.8 / mapRadius).toFloat() if (Random().nextDouble() < landProbability.pow(distanceFactor)) return TerrainType.Land else return TerrainType.Water } @@ -666,10 +666,10 @@ class MapLandmassGenerator { } // default - if (HexMath().getDistance(Vector2.Zero, tileInfo.position) > 0.9f * mapRadius) { + if (HexMath.getDistance(Vector2.Zero, tileInfo.position) > 0.9f * mapRadius) { if (Random().nextDouble() < 0.1) return TerrainType.Land else return TerrainType.Water } - if (HexMath().getDistance(Vector2.Zero, tileInfo.position) > 0.85f * mapRadius) { + if (HexMath.getDistance(Vector2.Zero, tileInfo.position) > 0.85f * mapRadius) { if (Random().nextDouble() < 0.2) return TerrainType.Land else return TerrainType.Water } if (Random().nextDouble() < landProbability) return TerrainType.Land else return TerrainType.Water diff --git a/core/src/com/unciv/logic/map/TileMap.kt b/core/src/com/unciv/logic/map/TileMap.kt index a3997e2a..e8542034 100644 --- a/core/src/com/unciv/logic/map/TileMap.kt +++ b/core/src/com/unciv/logic/map/TileMap.kt @@ -35,7 +35,7 @@ class TileMap { constructor(radius:Int, ruleset: Ruleset){ - for(vector in HexMath().getVectorsInDistance(Vector2.Zero, radius)) + for(vector in HexMath.getVectorsInDistance(Vector2.Zero, radius)) tileList.add(TileInfo().apply { position = vector; baseTerrain= Constants.grassland }) setTransients(ruleset) } @@ -56,12 +56,12 @@ class TileMap { } fun getTilesInDistance(origin: Vector2, distance: Int): List { - return HexMath().getVectorsInDistance(origin, distance).asSequence() + return HexMath.getVectorsInDistance(origin, distance).asSequence() .filter {contains(it)}.map { get(it) }.toList() } fun getTilesAtDistance(origin: Vector2, distance: Int): List { - return HexMath().getVectorsAtDistance(origin, distance).asSequence() + return HexMath.getVectorsAtDistance(origin, distance).asSequence() .filter {contains(it)}.map { get(it) }.toList() } diff --git a/core/src/com/unciv/ui/EmpireOverviewScreen.kt b/core/src/com/unciv/ui/EmpireOverviewScreen.kt index bd149ffa..00699bb5 100644 --- a/core/src/com/unciv/ui/EmpireOverviewScreen.kt +++ b/core/src/com/unciv/ui/EmpireOverviewScreen.kt @@ -340,7 +340,7 @@ class EmpireOverviewScreen(val viewingPlayer:CivilizationInfo) : CameraStageBase val civGroup = getCivGroup(civ, "", viewingPlayer) - val vector = HexMath().getVectorForAngle(2 * Math.PI.toFloat() *i / relevantCivs.size) + val vector = HexMath.getVectorForAngle(2 * Math.PI.toFloat() *i / relevantCivs.size) civGroup.center(group) civGroup.moveBy(vector.x*groupSize/3, vector.y*groupSize/3) diff --git a/core/src/com/unciv/ui/cityscreen/CityScreen.kt b/core/src/com/unciv/ui/cityscreen/CityScreen.kt index d9dc1014..4cc2c7be 100644 --- a/core/src/com/unciv/ui/cityscreen/CityScreen.kt +++ b/core/src/com/unciv/ui/cityscreen/CityScreen.kt @@ -228,7 +228,7 @@ class CityScreen(internal val city: CityInfo) : CameraStageBaseScreen() { tileGroups.add(tileGroup) - val positionalVector = HexMath().hex2WorldCoords(tileInfo.position.cpy().sub(cityInfo.location)) + val positionalVector = HexMath.hex2WorldCoords(tileInfo.position.cpy().sub(cityInfo.location)) val groupSize = 50 tileGroup.setPosition(stage.width / 2 + positionalVector.x * 0.8f * groupSize.toFloat(), stage.height / 2 + positionalVector.y * 0.8f * groupSize.toFloat()) diff --git a/core/src/com/unciv/ui/map/TileGroupMap.kt b/core/src/com/unciv/ui/map/TileGroupMap.kt index 6d27d31f..1d138455 100644 --- a/core/src/com/unciv/ui/map/TileGroupMap.kt +++ b/core/src/com/unciv/ui/map/TileGroupMap.kt @@ -13,7 +13,7 @@ class TileGroupMap(val tileGroups: Collection, val padding: Flo val groupSize = 50 init{ for(tileGroup in tileGroups){ - val positionalVector = HexMath().hex2WorldCoords(tileGroup.tileInfo.position) + val positionalVector = HexMath.hex2WorldCoords(tileGroup.tileInfo.position) tileGroup.setPosition(positionalVector.x * 0.8f * groupSize.toFloat(), positionalVector.y * 0.8f * groupSize.toFloat()) diff --git a/core/src/com/unciv/ui/mapeditor/EditorMapHolder.kt b/core/src/com/unciv/ui/mapeditor/EditorMapHolder.kt index dc0f875b..ddf50209 100644 --- a/core/src/com/unciv/ui/mapeditor/EditorMapHolder.kt +++ b/core/src/com/unciv/ui/mapeditor/EditorMapHolder.kt @@ -62,8 +62,8 @@ class EditorMapHolder(internal val mapEditorScreen: MapEditorScreen, internal va fun getClosestTileTo(stageCoords: Vector2): TileInfo? { val positionalCoords = tileGroupMap.getPositionalVector(stageCoords) - val hexPosition = HexMath().world2HexCoords(positionalCoords) - val rounded = HexMath().roundHexCoords(hexPosition) + val hexPosition = HexMath.world2HexCoords(positionalCoords) + val rounded = HexMath.roundHexCoords(hexPosition) if (tileMap.contains(rounded)) return tileMap.get(rounded) diff --git a/core/src/com/unciv/ui/tilegroups/TileGroup.kt b/core/src/com/unciv/ui/tilegroups/TileGroup.kt index 966fcd8e..4dc1bda6 100644 --- a/core/src/com/unciv/ui/tilegroups/TileGroup.kt +++ b/core/src/com/unciv/ui/tilegroups/TileGroup.kt @@ -366,7 +366,7 @@ open class TileGroup(var tileInfo: TileInfo, var tileSetStrings:TileSetStrings) if (neighborOwner != tileOwner && !borderImages.containsKey(neighbor)) { // there should be a border here but there isn't val relativeHexPosition = tileInfo.position.cpy().sub(neighbor.position) - val relativeWorldPosition = HexMath().hex2WorldCoords(relativeHexPosition) + val relativeWorldPosition = HexMath.hex2WorldCoords(relativeHexPosition) // This is some crazy voodoo magic so I'll explain. val images = mutableListOf() @@ -421,7 +421,7 @@ open class TileGroup(var tileInfo: TileInfo, var tileSetStrings:TileSetStrings) roadImage.image = image val relativeHexPosition = tileInfo.position.cpy().sub(neighbor.position) - val relativeWorldPosition = HexMath().hex2WorldCoords(relativeHexPosition) + val relativeWorldPosition = HexMath.hex2WorldCoords(relativeHexPosition) // This is some crazy voodoo magic so I'll explain. image.moveBy(25f, 25f) // Move road to center of tile diff --git a/core/src/com/unciv/ui/worldscreen/Minimap.kt b/core/src/com/unciv/ui/worldscreen/Minimap.kt index 2770adcb..be3a626e 100644 --- a/core/src/com/unciv/ui/worldscreen/Minimap.kt +++ b/core/src/com/unciv/ui/worldscreen/Minimap.kt @@ -35,7 +35,7 @@ class Minimap(val mapHolder: WorldMapHolder) : ScrollPane(null){ for (tileInfo in mapHolder.tileMap.values) { val hex = ImageGetter.getImage("OtherIcons/Hexagon") - val positionalVector = HexMath().hex2WorldCoords(tileInfo.position) + val positionalVector = HexMath.hex2WorldCoords(tileInfo.position) val groupSize = 10f hex.setSize(groupSize,groupSize) hex.setPosition(positionalVector.x * 0.5f * groupSize,