HexMath is a singleton (#1603)

This commit is contained in:
r3versi 2020-01-03 09:33:01 +01:00 committed by Yair Morgenstern
parent a1b0c1dcd4
commit f7b5a506fd
10 changed files with 25 additions and 24 deletions

View file

@ -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
}
}

View file

@ -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<Vector2> {
val vectors = ArrayList<Vector2>()
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
}

View file

@ -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

View file

@ -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<TileInfo> {
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<TileInfo> {
return HexMath().getVectorsAtDistance(origin, distance).asSequence()
return HexMath.getVectorsAtDistance(origin, distance).asSequence()
.filter {contains(it)}.map { get(it) }.toList()
}

View file

@ -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)

View file

@ -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())

View file

@ -13,7 +13,7 @@ class TileGroupMap<T: TileGroup>(val tileGroups: Collection<T>, 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())

View file

@ -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)

View file

@ -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<Image>()
@ -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

View file

@ -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,