diff --git a/.gitignore b/.gitignore index 9e1eae7..09e4ecf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ .gradle /build/ -!gradle/wrapper/gradle-wrapper.jar out/ ### STS ### diff --git a/build.gradle.kts b/build.gradle.kts index 1163b7f..34bff44 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,11 +2,15 @@ allprojects { repositories { mavenLocal() mavenCentral() - jcenter() jitpack() } } subprojects { apply() +} + +buildscript { + dependencies { + } } \ No newline at end of file diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 2a58952..1f1963b 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -7,5 +7,5 @@ repositories { } dependencies { - implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20-M1") + implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0") } \ No newline at end of file diff --git a/buildSrc/gradle/wrapper/gradle-wrapper.jar b/buildSrc/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..5c2d1cf Binary files /dev/null and b/buildSrc/gradle/wrapper/gradle-wrapper.jar differ diff --git a/buildSrc/gradle/wrapper/gradle-wrapper.properties b/buildSrc/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..8cdd65c --- /dev/null +++ b/buildSrc/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Thu Jan 16 13:42:40 CET 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt index 67a3d3c..55db1c3 100644 --- a/buildSrc/src/main/kotlin/Projects.kt +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -3,6 +3,6 @@ import org.gradle.kotlin.dsl.project object Projects { - inline val DependencyHandlerScope.mixiteCore get() = project(":mixite.core") + inline val DependencyHandlerScope.mixiteCore get() = project(":mixite2.core") } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index eab6abd..96e4cbd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ -group=org.hexworks.mixite -version=2020.1.0 +group=org.hexworks.mixite2 +version=0.1.1 org.gradle.parallel=true org.gradle.daemon=true \ No newline at end of file diff --git a/mixite.benchmarks/build.gradle b/mixite.benchmarks/build.gradle deleted file mode 100644 index 410bfdd..0000000 --- a/mixite.benchmarks/build.gradle +++ /dev/null @@ -1,44 +0,0 @@ -plugins { - id "kotlin-multiplatform" - id "kotlinx.benchmark" version "0.2.0-dev-6" - id "org.jetbrains.kotlin.plugin.allopen" version "1.3.50" -} - -allOpen { - annotation("org.openjdk.jmh.annotations.State") -} - -kotlin { - - jvm { - compilations.all { - kotlinOptions.jvmTarget = '1.8' - } - } - - sourceSets { - commonMain { - dependencies { - implementation "org.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-6" // MPP fail, import manually - implementation project(":mixite.core") - } - } - } - -} - -benchmark { - configurations { - main { - iterationTime = 1 - iterationTimeUnit = "s" - include(jmhIncludes) - } - } - - targets { - register("jvm") { - jmhVersion = "1.22" - } - } -} diff --git a/mixite.benchmarks/gradle.properties b/mixite.benchmarks/gradle.properties deleted file mode 100644 index 9cc20de..0000000 --- a/mixite.benchmarks/gradle.properties +++ /dev/null @@ -1 +0,0 @@ -jmhIncludes=.* diff --git a/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonPerfTest.kt b/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonPerfTest.kt deleted file mode 100644 index 2421c94..0000000 --- a/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonPerfTest.kt +++ /dev/null @@ -1,37 +0,0 @@ -package org.hexworks.mixite.core.api - -import kotlinx.benchmark.* -import org.hexworks.mixite.core.api.contract.HexagonDataStorage -import org.hexworks.mixite.core.api.defaults.DefaultHexagonDataStorage -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData -import org.hexworks.mixite.core.internal.GridData - -@State(Scope.Benchmark) -@Warmup(iterations = 5) -@Measurement(iterations = 5) -@Suppress("Unused") -open class HexagonPerfTest { - - private lateinit var gridData: GridData - private lateinit var cc: CubeCoordinate - private lateinit var store: HexagonDataStorage - - @Setup - fun setUp() { - gridData = HexagonalGridBuilder() - .setGridLayout(HexagonalGridLayout.RECTANGULAR) - .setGridHeight(1) - .setGridWidth(1) - .setRadius(10.0) - .build() - .gridData - cc = CubeCoordinate.fromCoordinates(-49, 0) - store = DefaultHexagonDataStorage() - } - - - // @Benchmark - fun constructHexagon(bh: Blackhole) { -// bh.consume(HexagonImpl(gridData, cc, store)) - } -} diff --git a/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilderPerfTest.kt b/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilderPerfTest.kt deleted file mode 100644 index 45ca34c..0000000 --- a/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilderPerfTest.kt +++ /dev/null @@ -1,39 +0,0 @@ -package org.hexworks.mixite.core.api - -import kotlinx.benchmark.* -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData - -@State(Scope.Benchmark) -@Warmup(iterations = 5) -@Measurement(iterations = 5) -open class HexagonalGridBuilderPerfTest { - - @Param("RECTANGULAR", "HEXAGONAL", "TRIANGULAR", "TRAPEZOID") - lateinit var layout: HexagonalGridLayout - - lateinit var builder: HexagonalGridBuilder - - @Setup - fun setUp() { - builder = HexagonalGridBuilder() - .setRadius(100.0) - .apply { gridLayoutWithConsistentSize(layout, this) } - } - - private fun gridLayoutWithConsistentSize(layout: HexagonalGridLayout, builder: HexagonalGridBuilder<*>) { - builder.setGridLayout(layout).apply { - when (layout) { - // Each of these is about 10000 in size. Resulting ops/sec should be consistent. - HexagonalGridLayout.RECTANGULAR -> setGridHeight(100).setGridWidth(100) - HexagonalGridLayout.TRAPEZOID -> setGridHeight(100).setGridWidth(100) - HexagonalGridLayout.TRIANGULAR -> setGridHeight(141).setGridWidth(141) - HexagonalGridLayout.HEXAGONAL -> setGridHeight(115).setGridWidth(115) - } - } - } - - @Suppress("Unused") - @Benchmark - fun buildTime() = builder.build() - -} diff --git a/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridCalculatorPerfTest.kt b/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridCalculatorPerfTest.kt deleted file mode 100644 index d8ecf78..0000000 --- a/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridCalculatorPerfTest.kt +++ /dev/null @@ -1,55 +0,0 @@ -package org.hexworks.mixite.core.api - -import kotlinx.benchmark.* -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData -import org.hexworks.mixite.core.internal.impl.HexagonalGridCalculatorImpl - -@State(Scope.Benchmark) -@Warmup(iterations = 5) -@Measurement(iterations = 5) -@Suppress("Unused") -open class HexagonalGridCalculatorPerfTest { - - private lateinit var calc: HexagonalGridCalculatorImpl - private lateinit var hexFrom: Hexagon - private lateinit var hexTo: Hexagon - private lateinit var grid: HexagonalGrid - - @Setup - fun setUp() { - grid = HexagonalGridBuilder() - .setGridLayout(HexagonalGridLayout.RECTANGULAR) - .setGridHeight(100) - .setGridWidth(100) - .setRadius(10.0) - .build() - calc = HexagonalGridCalculatorImpl(grid) - hexFrom = grid.getByCubeCoordinate(CubeCoordinate.fromCoordinates(2, 2)).get() - hexTo = grid.getByCubeCoordinate(CubeCoordinate.fromCoordinates(7, 9)).get() - } - - @Benchmark - fun calculateRingFrom(bh: Blackhole) { - val hexs = calc.calculateRingFrom(hexFrom, 2) - for (hexagon in hexs) { - bh.consume(hexagon) - } - } - - @Benchmark - fun calculateMovementRangeFrom(bh: Blackhole) { - val hexs = calc.calculateMovementRangeFrom(hexFrom, 2) - for (hexagon in hexs) { - bh.consume(hexagon) - } - } - - @Benchmark - fun drawLine(bh: Blackhole) { - val hexs = calc.drawLine(hexFrom, hexTo) - for (hexagon in hexs) { - bh.consume(hexagon) - } - } - -} diff --git a/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridPerfTest.kt b/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridPerfTest.kt deleted file mode 100644 index 5a23570..0000000 --- a/mixite.benchmarks/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridPerfTest.kt +++ /dev/null @@ -1,113 +0,0 @@ -package org.hexworks.mixite.core.api - -import kotlinx.benchmark.* -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData -import kotlin.math.PI -import kotlin.math.cos -import kotlin.math.sin - -@State(Scope.Benchmark) -@Warmup(iterations = 5) -@Measurement(iterations = 5) -@Suppress("Unused") -open class HexagonalGridPerfTest { - - private lateinit var hexRandom: Hexagon - private lateinit var ccTo: CubeCoordinate - private lateinit var ccFrom: CubeCoordinate - private lateinit var grid: HexagonalGrid - private lateinit var spiralPath: List - private lateinit var circlePath: List - - @Setup - fun setUp() { - grid = HexagonalGridBuilder() - .setGridLayout(HexagonalGridLayout.RECTANGULAR) - .setGridHeight(100) - .setGridWidth(100) - .setRadius(10.0) - .build() - ccFrom = CubeCoordinate.fromCoordinates(-49, 0) - ccTo = CubeCoordinate.fromCoordinates(99, 99) - hexRandom = grid.getByCubeCoordinate(CubeCoordinate.fromCoordinates(1, 1)).get() - } - - @Setup - fun createSpiralMousePath() { - val steps = 24 // 24 points around a circle - val turns = 10 - val points = ArrayList() - for (p in 0..steps * turns) { - val angle = (steps * 2 * PI) / (p % steps) - val distance = 2000.0 * p / (steps * turns) - points += Point.fromPosition( - distance * cos(angle) + 1000, - distance * sin(angle) + 500) - } - spiralPath = points - } - - @Setup - fun createCircularMousePath() { - val steps = 24 // 24 points around a circle - val turns = 10 - val points = ArrayList() - for (p in 0..steps * turns) { - val angle = (steps * 2 * PI) / (p % steps) - val distance = 300 - points += Point.fromPosition( - distance * cos(angle) + 1000, - distance * sin(angle) + 500) - } - circlePath = points - } - - @Benchmark - fun iterateHexagons(bh: Blackhole) { - val hexs = grid.hexagons - for (hexagon in hexs) { - bh.consume(hexagon) - } - } - - @Benchmark - fun getHexagonsByOffsetRange(bh: Blackhole) { - // Gets all hexs so the ops/sec is comparable to iterateHexagons - val hexs = grid.getHexagonsByOffsetRange(0, 100, 0, 100) - for (hexagon in hexs) { - bh.consume(hexagon) - } - } - - @Benchmark - fun getHexagonsByCubeRange(bh: Blackhole) { - // Gets all hexs so the ops/sec is comparable to iterateHexagons - // Note % of CC are not in the grid - val hexs = grid.getHexagonsByCubeRange(ccFrom, ccTo) - for (hexagon in hexs) { - bh.consume(hexagon) - } - } - - @Benchmark - fun getByPixelCoordinate50pct(bh: Blackhole) { - // ~ 50% hit ratio - for ((x, y) in spiralPath) { - bh.consume(grid.getByPixelCoordinate(x, y)) - } - } - - @Benchmark - fun getByPixelCoordinate100pct(bh: Blackhole) { - // ~ 100% hit ratio - for ((x, y) in circlePath) { - bh.consume(grid.getByPixelCoordinate(x, y)) - } - } - - @Benchmark - fun getNeighborsOf(bh: Blackhole) { - bh.consume(grid.getNeighborsOf(hexRandom)) - } - -} diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/RotationDirection.kt b/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/RotationDirection.kt deleted file mode 100644 index dceca70..0000000 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/RotationDirection.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.hexworks.mixite.core.api - -/** - * Represents a right or left angle (60°) of a Hexagon rotation. - * See: http://www.redblobgames.com/grids/hexagons/#rotation - */ -enum class RotationDirection constructor(private val rotationCalculator: RotationCalculator) { - - RIGHT(object : RotationCalculator { - override fun calculate(coord: CubeCoordinate): CubeCoordinate { - return CubeCoordinate.fromCoordinates(-coord.gridZ, -coord.gridY) - } - }), - LEFT(object : RotationCalculator { - override fun calculate(coord: CubeCoordinate): CubeCoordinate { - return CubeCoordinate.fromCoordinates(-coord.gridY, -coord.gridX) - } - }); - - /** - * Calculates a rotation (right or left) of `coord`. - * - * @param coord coordinate to rotate - * - * @return rotated coordinate - */ - fun calculateRotation(coord: CubeCoordinate): CubeCoordinate { - return rotationCalculator.calculate(coord) - } - - internal interface RotationCalculator { - fun calculate(coord: CubeCoordinate): CubeCoordinate - } -} diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/defaults/DefaultHexagonDataStorage.kt b/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/defaults/DefaultHexagonDataStorage.kt deleted file mode 100644 index ffb71cd..0000000 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/defaults/DefaultHexagonDataStorage.kt +++ /dev/null @@ -1,44 +0,0 @@ -package org.hexworks.mixite.core.api.defaults - -import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.contract.HexagonDataStorage -import org.hexworks.mixite.core.api.contract.SatelliteData - -class DefaultHexagonDataStorage : HexagonDataStorage { - - private val storage = LinkedHashMap>() - - override val coordinates: Iterable - get() = storage.keys - - override fun addCoordinate(cubeCoordinate: CubeCoordinate) { - storage[cubeCoordinate] = Maybe.empty() - } - - override fun addCoordinate(cubeCoordinate: CubeCoordinate, satelliteData: T): Boolean { - val previous = storage.put(cubeCoordinate, Maybe.of(satelliteData)) - return previous != null - } - - override fun getSatelliteDataBy(cubeCoordinate: CubeCoordinate): Maybe { - return if (storage.containsKey(cubeCoordinate)) storage[cubeCoordinate]!! else Maybe.empty() - } - - override fun containsCoordinate(cubeCoordinate: CubeCoordinate): Boolean { - return storage.containsKey(cubeCoordinate) - } - - override fun hasDataFor(cubeCoordinate: CubeCoordinate): Boolean { - return storage.containsKey(cubeCoordinate) && storage[cubeCoordinate]!!.isPresent - } - - override fun clearDataFor(cubeCoordinate: CubeCoordinate): Boolean { - var result = false - if (hasDataFor(cubeCoordinate)) { - result = true - } - storage[cubeCoordinate] = Maybe.empty() - return result - } -} diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategy.kt b/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategy.kt deleted file mode 100644 index 73e9f4c..0000000 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategy.kt +++ /dev/null @@ -1,29 +0,0 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy - -import org.hexworks.mixite.core.api.CoordinateConverter -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData - -class RectangularGridLayoutStrategy : GridLayoutStrategy() { - - override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { - val coords = ArrayList( builder.getGridHeight() * builder.getGridWidth()) - for (y in 0 until builder.getGridHeight()) { - for (x in 0 until builder.getGridWidth()) { - val gridX = CoordinateConverter.convertOffsetCoordinatesToCubeX(x, y, builder.getOrientation()) - val gridZ = CoordinateConverter.convertOffsetCoordinatesToCubeZ(x, y, builder.getOrientation()) - coords.add(CubeCoordinate.fromCoordinates(gridX, gridZ)) - } - } - return coords - } - - override fun checkParameters(gridHeight: Int, gridWidth: Int): Boolean { - return checkCommonCase(gridHeight, gridWidth) - } - - override fun getName(): String { - return "RECTANGULAR" - } -} diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategy.kt b/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategy.kt deleted file mode 100644 index 03aaf04..0000000 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategy.kt +++ /dev/null @@ -1,26 +0,0 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy - -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData - -class TrapezoidGridLayoutStrategy : GridLayoutStrategy() { - - override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { - val coords = ArrayList(builder.getGridHeight() * builder.getGridWidth()) - for (gridZ in 0 until builder.getGridHeight()) { - for (gridX in 0 until builder.getGridWidth()) { - coords.add(CubeCoordinate.fromCoordinates(gridX, gridZ)) - } - } - return coords - } - - override fun checkParameters(gridHeight: Int, gridWidth: Int): Boolean { - return checkCommonCase(gridHeight, gridWidth) - } - - override fun getName(): String { - return "TRAPEZOID" - } -} diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/CustomGridLayoutStrategy.kt b/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/CustomGridLayoutStrategy.kt deleted file mode 100644 index bfa9ed2..0000000 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/CustomGridLayoutStrategy.kt +++ /dev/null @@ -1,33 +0,0 @@ -package org.hexworks.mixite.core.internal.impl - -import org.hexworks.mixite.core.api.CoordinateConverter -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData -import org.hexworks.mixite.core.internal.impl.layoutstrategy.GridLayoutStrategy - -class CustomGridLayoutStrategy : GridLayoutStrategy() { - - override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { - val coords = ArrayList( builder.getGridHeight() * builder.getGridWidth()) - for (y in 0 until builder.getGridHeight()) { - val xStart = if (y == 0 || y == builder.getGridHeight() - 1) 1 else 0 - val xEnd = if (y == 0 || y == builder.getGridHeight() - 1) builder.getGridWidth() - 1 - else builder.getGridWidth() - for (x in xStart until xEnd) { - val gridX = CoordinateConverter.convertOffsetCoordinatesToCubeX(x, y, builder.getOrientation()) - val gridZ = CoordinateConverter.convertOffsetCoordinatesToCubeZ(x, y, builder.getOrientation()) - coords.add(CubeCoordinate.fromCoordinates(gridX, gridZ)) - } - } - return coords - } - - override fun checkParameters(gridHeight: Int, gridWidth: Int): Boolean { - return true - } - - override fun getName(): String { - return "CUSTOM" - } -} diff --git a/mixite.core/build.gradle.kts b/mixite2.core/build.gradle.kts similarity index 72% rename from mixite.core/build.gradle.kts rename to mixite2.core/build.gradle.kts index 3fd9345..07f8dd6 100644 --- a/mixite.core/build.gradle.kts +++ b/mixite2.core/build.gradle.kts @@ -4,7 +4,7 @@ plugins { kotlinMultiplatform } -group = "org.hexworks.mixite" +group = "org.hexworks.mixite2" kotlin { sourceSets { @@ -42,4 +42,16 @@ kotlin { jsTestApi(kotlinTestJs) } } +} + +publishing { + publications { + create("maven") { + groupId = "org.hexworks.mixite2" + artifactId = "mixite2.core-jvm" + version = "0.1.1" + + from(components["kotlin"]) + } + } } \ No newline at end of file diff --git a/mixite2.core/gradle/wrapper/gradle-wrapper.jar b/mixite2.core/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..5c2d1cf Binary files /dev/null and b/mixite2.core/gradle/wrapper/gradle-wrapper.jar differ diff --git a/mixite2.core/gradle/wrapper/gradle-wrapper.properties b/mixite2.core/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..8cdd65c --- /dev/null +++ b/mixite2.core/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Thu Jan 16 13:42:40 CET 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/CoordinateConverter.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/CoordinateConverter.kt similarity index 65% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/CoordinateConverter.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/CoordinateConverter.kt index 127f1a1..208939f 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/CoordinateConverter.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/CoordinateConverter.kt @@ -1,4 +1,4 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api import kotlin.jvm.JvmStatic @@ -23,8 +23,8 @@ class CoordinateConverter { * @return cube x */ @JvmStatic - fun convertOffsetCoordinatesToCubeX(offsetX: Int, offsetY: Int, orientation: HexagonOrientation): Int { - return if (HexagonOrientation.FLAT_TOP.equals(orientation)) offsetX else offsetX - offsetY / 2 + fun convertOffsetCoordinatesToCubeX(offsetX: Int, offsetY: Int, orientation: org.hexworks.mixite2.core.api.HexagonOrientation): Int { + return if (org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP.equals(orientation)) offsetX else offsetX - offsetY / 2 } /** @@ -37,8 +37,8 @@ class CoordinateConverter { * @return cube z */ @JvmStatic - fun convertOffsetCoordinatesToCubeZ(offsetX: Int, offsetY: Int, orientation: HexagonOrientation): Int { - return if (HexagonOrientation.FLAT_TOP.equals(orientation)) offsetY - offsetX / 2 else offsetY + fun convertOffsetCoordinatesToCubeZ(offsetX: Int, offsetY: Int, orientation: org.hexworks.mixite2.core.api.HexagonOrientation): Int { + return if (org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP.equals(orientation)) offsetY - offsetX / 2 else offsetY } /** @@ -50,8 +50,8 @@ class CoordinateConverter { * @return offset row or y-value */ @JvmStatic - fun convertCubeCoordinateToOffsetRow(coordinate: CubeCoordinate, orientation: HexagonOrientation): Int { - return if(HexagonOrientation.FLAT_TOP.equals(orientation)) { + fun convertCubeCoordinateToOffsetRow(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate, orientation: org.hexworks.mixite2.core.api.HexagonOrientation): Int { + return if(org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP.equals(orientation)) { coordinate.gridZ + (coordinate.gridX - (coordinate.gridX and 1)) / 2 } else { coordinate.gridZ @@ -67,8 +67,8 @@ class CoordinateConverter { * @return offset column or x-value */ @JvmStatic - fun convertCubeCoordinateToOffsetColumn(coordinate: CubeCoordinate, orientation: HexagonOrientation): Int { - return if(HexagonOrientation.FLAT_TOP.equals(orientation)) { + fun convertCubeCoordinateToOffsetColumn(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate, orientation: org.hexworks.mixite2.core.api.HexagonOrientation): Int { + return if(org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP.equals(orientation)) { coordinate.gridX } else { coordinate.gridX + (coordinate.gridZ - (coordinate.gridZ and 1)) / 2 diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/CubeCoordinate.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/CubeCoordinate.kt similarity index 62% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/CubeCoordinate.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/CubeCoordinate.kt index 5a0dabd..0f1a992 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/CubeCoordinate.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/CubeCoordinate.kt @@ -1,4 +1,4 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api import kotlin.jvm.JvmStatic @@ -21,7 +21,7 @@ data class CubeCoordinate private constructor(val gridX: Int, val gridZ: Int) { * @return key */ fun toAxialKey(): String { - return gridX.toString() + SEP + gridZ + return gridX.toString() + org.hexworks.mixite2.core.api.CubeCoordinate.Companion.SEP + gridZ } companion object { @@ -37,11 +37,14 @@ data class CubeCoordinate private constructor(val gridX: Int, val gridZ: Int) { * @return coord */ @JvmStatic - fun fromAxialKey(axialKey: String): CubeCoordinate { - val result: CubeCoordinate + fun fromAxialKey(axialKey: String): org.hexworks.mixite2.core.api.CubeCoordinate { + val result: org.hexworks.mixite2.core.api.CubeCoordinate try { - val coords = axialKey.split(SEP.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - result = fromCoordinates(coords[0].toInt(), coords[1].toInt()) + val coords = axialKey.split(org.hexworks.mixite2.core.api.CubeCoordinate.Companion.SEP.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + result = org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates( + coords[0].toInt(), + coords[1].toInt() + ) } catch (e: Exception) { throw IllegalArgumentException("Failed to create CubeCoordinate from key: $axialKey", e) } @@ -58,8 +61,8 @@ data class CubeCoordinate private constructor(val gridX: Int, val gridZ: Int) { * @return coord */ @JvmStatic - fun fromCoordinates(gridX: Int, gridZ: Int): CubeCoordinate { - return CubeCoordinate(gridX, gridZ) + fun fromCoordinates(gridX: Int, gridZ: Int): org.hexworks.mixite2.core.api.CubeCoordinate { + return org.hexworks.mixite2.core.api.CubeCoordinate(gridX, gridZ) } } } diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Hexagon.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Hexagon.kt similarity index 81% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Hexagon.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Hexagon.kt index e95c3ca..03fe693 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Hexagon.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Hexagon.kt @@ -1,7 +1,7 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.contract.SatelliteData /** * Represents a Hexagon. @@ -39,7 +39,7 @@ interface Hexagon { /** * Returns the [CubeCoordinate] of this [Hexagon]. */ - val cubeCoordinate: CubeCoordinate + val cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate /** * Returns this [Hexagon]'s **x** (cube) coordinate on the [HexagonalGrid]. @@ -63,20 +63,6 @@ interface Hexagon { */ val center: Point - /** - * Returns the center **x** (pixel) coordinate of this [Hexagon]. - */ - @Deprecated("use center") - val centerX: Double - - /** - * Returns the center **y** (pixel) coordinate of this [Hexagon]. - * - * @return center y - */ - @Deprecated("use center") - val centerY: Double - /** * Returns this [Hexagon]'s satellite data. */ diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonOrientation.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonOrientation.kt similarity index 84% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonOrientation.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonOrientation.kt index 49773ef..70fd1ff 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonOrientation.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonOrientation.kt @@ -1,4 +1,4 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api /** * Enum representing the possible orientations of a [Hexagon]. The names diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGrid.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGrid.kt similarity index 82% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGrid.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGrid.kt index 0897cb6..f7f1fba 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGrid.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGrid.kt @@ -1,8 +1,8 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.contract.SatelliteData -import org.hexworks.mixite.core.internal.GridData +import org.hexworks.mixite2.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.internal.GridData /** * @@ -43,7 +43,7 @@ interface HexagonalGrid { * * @return [Hexagon]s in the given range. */ - fun getHexagonsByCubeRange(from: CubeCoordinate, to: CubeCoordinate): Iterable> + fun getHexagonsByCubeRange(from: org.hexworks.mixite2.core.api.CubeCoordinate, to: org.hexworks.mixite2.core.api.CubeCoordinate): Iterable> /** * Returns all [Hexagon]s contained in the given offset coordinate range. @@ -66,7 +66,7 @@ interface HexagonalGrid { * * @return is it on the grid? */ - fun containsCubeCoordinate(coordinate: CubeCoordinate): Boolean + fun containsCubeCoordinate(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Boolean /** * Returns a [Hexagon] by its cube coordinate. @@ -75,7 +75,7 @@ interface HexagonalGrid { * * @return Maybe with a Hexagon if it is present */ - fun getByCubeCoordinate(coordinate: CubeCoordinate): Maybe> + fun getByCubeCoordinate(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Maybe> /** * Returns a [Hexagon] by a pixel coordinate. @@ -94,7 +94,7 @@ interface HexagonalGrid { * * @return CubeCoordinate */ - fun getNeighborCoordinateByIndex(coordinate: CubeCoordinate, index: Int): CubeCoordinate + fun getNeighborCoordinateByIndex(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate, index: Int): org.hexworks.mixite2.core.api.CubeCoordinate /** * Returns a neighbor of a Hexagon by its neighbor index. diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilder.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridBuilder.kt similarity index 87% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilder.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridBuilder.kt index 5e113db..98ccb5d 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilder.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridBuilder.kt @@ -1,14 +1,14 @@ -package org.hexworks.mixite.core.api - -import org.hexworks.mixite.core.api.HexagonOrientation.POINTY_TOP -import org.hexworks.mixite.core.api.contract.HexagonDataStorage -import org.hexworks.mixite.core.api.contract.SatelliteData -import org.hexworks.mixite.core.api.defaults.DefaultHexagonDataStorage -import org.hexworks.mixite.core.internal.GridData -import org.hexworks.mixite.core.internal.impl.HexagonalGridCalculatorImpl -import org.hexworks.mixite.core.internal.impl.HexagonalGridImpl -import org.hexworks.mixite.core.internal.impl.layoutstrategy.GridLayoutStrategy -import org.hexworks.mixite.core.internal.impl.layoutstrategy.RectangularGridLayoutStrategy +package org.hexworks.mixite2.core.api + +import org.hexworks.mixite2.core.api.HexagonOrientation.POINTY_TOP +import org.hexworks.mixite2.core.api.contract.HexagonDataStorage +import org.hexworks.mixite2.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.defaults.DefaultHexagonDataStorage +import org.hexworks.mixite2.core.internal.GridData +import org.hexworks.mixite2.core.internal.impl.HexagonalGridCalculatorImpl +import org.hexworks.mixite2.core.internal.impl.HexagonalGridImpl +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.GridLayoutStrategy +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.RectangularGridLayoutStrategy /** * diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridCalculator.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridCalculator.kt similarity index 96% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridCalculator.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridCalculator.kt index d2a062e..8b6e923 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridCalculator.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridCalculator.kt @@ -1,7 +1,7 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.contract.SatelliteData /** * Supports advanced operations on a [HexagonalGrid]. diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridLayout.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridLayout.kt similarity index 91% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridLayout.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridLayout.kt index 057e613..a6821e9 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/HexagonalGridLayout.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/HexagonalGridLayout.kt @@ -1,6 +1,6 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api -import org.hexworks.mixite.core.internal.impl.layoutstrategy.* +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.* /** * This enum represents the possible shapes a [HexagonalGrid] can have. diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Point.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Point.kt similarity index 96% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Point.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Point.kt index 02232ab..42177e7 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Point.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Point.kt @@ -1,4 +1,4 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api import kotlin.jvm.JvmStatic import kotlin.math.sqrt diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Rectangle.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Rectangle.kt similarity index 98% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Rectangle.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Rectangle.kt index b309ec8..b914fcf 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/Rectangle.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/Rectangle.kt @@ -1,4 +1,4 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api class Rectangle { var x: Double = 0.0 diff --git a/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/RotationDirection.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/RotationDirection.kt new file mode 100644 index 0000000..c742ee8 --- /dev/null +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/RotationDirection.kt @@ -0,0 +1,34 @@ +package org.hexworks.mixite2.core.api + +/** + * Represents a right or left angle (60°) of a Hexagon rotation. + * See: http://www.redblobgames.com/grids/hexagons/#rotation + */ +enum class RotationDirection constructor(private val rotationCalculator: RotationCalculator) { + + RIGHT(object : RotationCalculator { + override fun calculate(coord: org.hexworks.mixite2.core.api.CubeCoordinate): org.hexworks.mixite2.core.api.CubeCoordinate { + return org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(-coord.gridZ, -coord.gridY) + } + }), + LEFT(object : RotationCalculator { + override fun calculate(coord: org.hexworks.mixite2.core.api.CubeCoordinate): org.hexworks.mixite2.core.api.CubeCoordinate { + return org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(-coord.gridY, -coord.gridX) + } + }); + + /** + * Calculates a rotation (right or left) of `coord`. + * + * @param coord coordinate to rotate + * + * @return rotated coordinate + */ + fun calculateRotation(coord: org.hexworks.mixite2.core.api.CubeCoordinate): org.hexworks.mixite2.core.api.CubeCoordinate { + return rotationCalculator.calculate(coord) + } + + internal interface RotationCalculator { + fun calculate(coord: org.hexworks.mixite2.core.api.CubeCoordinate): org.hexworks.mixite2.core.api.CubeCoordinate + } +} diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/contract/HexagonDataStorage.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/contract/HexagonDataStorage.kt similarity index 64% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/contract/HexagonDataStorage.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/contract/HexagonDataStorage.kt index eb6f65a..ec4ad14 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/contract/HexagonDataStorage.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/contract/HexagonDataStorage.kt @@ -1,7 +1,7 @@ -package org.hexworks.mixite.core.api.contract +package org.hexworks.mixite2.core.api.contract import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.CubeCoordinate /** * This interface represents all storage operations which are needed for a working @@ -13,44 +13,44 @@ interface HexagonDataStorage { /** * Returns all coordinates which are stored in this object. */ - val coordinates: Iterable + val coordinates: Iterable /** * Adds a [CubeCoordinate] for this grid without any [SatelliteData]. * Does not overwrite the coordinate if it is already present. */ - fun addCoordinate(cubeCoordinate: CubeCoordinate) + fun addCoordinate(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate) /** * Adds a [CubeCoordinate] for this grid with [SatelliteData]. * Overwrites previous [SatelliteData] if it was present. * @return true if overwrote data false otherwise. */ - fun addCoordinate(cubeCoordinate: CubeCoordinate, satelliteData: T): Boolean + fun addCoordinate(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate, satelliteData: T): Boolean /** * Gets the [SatelliteData] stored on a [CubeCoordinate] if present. * Also returns empty [Maybe] when `cubeCoordinate` is not present. * @return optional [SatelliteData]. */ - fun getSatelliteDataBy(cubeCoordinate: CubeCoordinate): Maybe + fun getSatelliteDataBy(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Maybe /** * Tells whether there is a [Hexagon] on the given [CubeCoordinate] or not. * @return true if present false if not */ - fun containsCoordinate(cubeCoordinate: CubeCoordinate): Boolean + fun containsCoordinate(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Boolean /** * Tells whether there is [SatelliteData] stored for a [CubeCoordinate] or not. * Also returns false if `cubeCoordinate` is not present in the storage. */ - fun hasDataFor(cubeCoordinate: CubeCoordinate): Boolean + fun hasDataFor(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Boolean /** * Clears the [SatelliteData] for the given [CubeCoordinate]. * @return true if the storage was changed false otherwise. */ - fun clearDataFor(cubeCoordinate: CubeCoordinate): Boolean + fun clearDataFor(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Boolean } diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/contract/SatelliteData.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/contract/SatelliteData.kt similarity index 95% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/contract/SatelliteData.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/contract/SatelliteData.kt index 13bb40e..1abe5ab 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/contract/SatelliteData.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/contract/SatelliteData.kt @@ -1,4 +1,4 @@ -package org.hexworks.mixite.core.api.contract +package org.hexworks.mixite2.core.api.contract /** * Represents arbitrary data which can be attached to a Hexagon. diff --git a/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultHexagonDataStorage.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultHexagonDataStorage.kt new file mode 100644 index 0000000..8dfab01 --- /dev/null +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultHexagonDataStorage.kt @@ -0,0 +1,44 @@ +package org.hexworks.mixite2.core.api.defaults + +import org.hexworks.cobalt.datatypes.Maybe +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.contract.HexagonDataStorage +import org.hexworks.mixite2.core.api.contract.SatelliteData + +class DefaultHexagonDataStorage : HexagonDataStorage { + + private val storage = LinkedHashMap>() + + override val coordinates: Iterable + get() = storage.keys + + override fun addCoordinate(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate) { + storage[cubeCoordinate] = Maybe.empty() + } + + override fun addCoordinate(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate, satelliteData: T): Boolean { + val previous = storage.put(cubeCoordinate, Maybe.of(satelliteData)) + return previous != null + } + + override fun getSatelliteDataBy(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Maybe { + return if (storage.containsKey(cubeCoordinate)) storage[cubeCoordinate]!! else Maybe.empty() + } + + override fun containsCoordinate(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Boolean { + return storage.containsKey(cubeCoordinate) + } + + override fun hasDataFor(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Boolean { + return storage.containsKey(cubeCoordinate) && storage[cubeCoordinate]!!.isPresent + } + + override fun clearDataFor(cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Boolean { + var result = false + if (hasDataFor(cubeCoordinate)) { + result = true + } + storage[cubeCoordinate] = Maybe.empty() + return result + } +} diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/defaults/DefaultSatelliteData.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultSatelliteData.kt similarity index 71% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/defaults/DefaultSatelliteData.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultSatelliteData.kt index 4e59826..be386a0 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/api/defaults/DefaultSatelliteData.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultSatelliteData.kt @@ -1,6 +1,6 @@ -package org.hexworks.mixite.core.api.defaults +package org.hexworks.mixite2.core.api.defaults -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.contract.SatelliteData /** * Convenience class implementing SatelliteData. diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/GridData.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/GridData.kt similarity index 83% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/GridData.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/GridData.kt index 065b475..2eb7852 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/GridData.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/GridData.kt @@ -1,8 +1,8 @@ -package org.hexworks.mixite.core.internal +package org.hexworks.mixite2.core.internal -import org.hexworks.mixite.core.api.HexagonOrientation -import org.hexworks.mixite.core.api.HexagonalGridLayout -import org.hexworks.mixite.core.internal.impl.layoutstrategy.GridLayoutStrategy +import org.hexworks.mixite2.core.api.HexagonOrientation +import org.hexworks.mixite2.core.api.HexagonalGridLayout +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.GridLayoutStrategy import kotlin.math.sqrt /** diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonImpl.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonImpl.kt similarity index 86% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonImpl.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonImpl.kt index 8ba9c72..e38be80 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonImpl.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonImpl.kt @@ -1,10 +1,10 @@ -package org.hexworks.mixite.core.internal.impl +package org.hexworks.mixite2.core.internal.impl import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.* -import org.hexworks.mixite.core.api.contract.HexagonDataStorage -import org.hexworks.mixite.core.api.contract.SatelliteData -import org.hexworks.mixite.core.internal.GridData +import org.hexworks.mixite2.core.api.* +import org.hexworks.mixite2.core.api.contract.HexagonDataStorage +import org.hexworks.mixite2.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.internal.GridData import kotlin.math.PI import kotlin.math.cos import kotlin.math.sin @@ -13,9 +13,9 @@ import kotlin.math.sin * Default implementation of the [Hexagon] interface. */ class HexagonImpl internal constructor( - private val sharedData: GridData, - override val cubeCoordinate: CubeCoordinate, - private val hexagonDataStorage: HexagonDataStorage) : Hexagon { + private val sharedData: GridData, + override val cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate, + private val hexagonDataStorage: HexagonDataStorage) : Hexagon { override val vertices: List override val points: List @@ -35,10 +35,6 @@ class HexagonImpl internal constructor( override val gridZ: Int get() = cubeCoordinate.gridZ - override val centerX = center.coordinateX - - override val centerY = center.coordinateY - override val satelliteData: Maybe get() = hexagonDataStorage.getSatelliteDataBy(cubeCoordinate) diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridCalculatorImpl.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridCalculatorImpl.kt similarity index 81% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridCalculatorImpl.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridCalculatorImpl.kt index eaf9126..4f15ea5 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridCalculatorImpl.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridCalculatorImpl.kt @@ -1,8 +1,8 @@ -package org.hexworks.mixite.core.internal.impl +package org.hexworks.mixite2.core.internal.impl import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.* -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.* +import org.hexworks.mixite2.core.api.contract.SatelliteData import kotlin.math.abs import kotlin.math.max import kotlin.math.min @@ -22,7 +22,7 @@ class HexagonalGridCalculatorImpl(override val hexagonalGrid: for (x in -distance..distance) { for (y in max(-distance, -x - distance)..min(distance, -x + distance)) { val z = -x - y - val tempCoordinate = CubeCoordinate.fromCoordinates(hexagon.gridX + x, hexagon.gridZ + z) + val tempCoordinate = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(hexagon.gridX + x, hexagon.gridZ + z) hexagonalGrid.getByCubeCoordinate(tempCoordinate).ifPresent { ret += it } } } @@ -32,9 +32,9 @@ class HexagonalGridCalculatorImpl(override val hexagonalGrid: override fun rotateHexagon(originalHex: Hexagon, targetHex: Hexagon, rotationDirection: RotationDirection): Maybe> { val diffX = targetHex.gridX - originalHex.gridX val diffZ = targetHex.gridZ - originalHex.gridZ - val diffCoord = CubeCoordinate.fromCoordinates(diffX, diffZ) + val diffCoord = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(diffX, diffZ) val rotatedCoord = rotationDirection.calculateRotation(diffCoord) - val resultCoord = CubeCoordinate.fromCoordinates( + val resultCoord = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates( originalHex.gridX + rotatedCoord.gridX, originalHex.gridZ + rotatedCoord.gridZ) // 0, x, return hexagonalGrid.getByCubeCoordinate(resultCoord) @@ -43,7 +43,7 @@ class HexagonalGridCalculatorImpl(override val hexagonalGrid: override fun calculateRingFrom(centerHexagon: Hexagon, radius: Int): Set> { val result = HashSet>() - var currentCoordinate = CubeCoordinate.fromCoordinates( + var currentCoordinate = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates( centerHexagon.gridX - radius, centerHexagon.gridZ + radius ) @@ -88,7 +88,7 @@ class HexagonalGridCalculatorImpl(override val hexagonalGrid: return true } - private fun cubeLinearInterpolate(from: CubeCoordinate, to: CubeCoordinate, sample: Double): CubeCoordinate { + private fun cubeLinearInterpolate(from: org.hexworks.mixite2.core.api.CubeCoordinate, to: org.hexworks.mixite2.core.api.CubeCoordinate, sample: Double): org.hexworks.mixite2.core.api.CubeCoordinate { return roundToCubeCoordinate(linearInterpolate(from.gridX, to.gridX, sample), linearInterpolate(from.gridY, to.gridY, sample), linearInterpolate(from.gridZ, to.gridZ, sample)) @@ -98,7 +98,7 @@ class HexagonalGridCalculatorImpl(override val hexagonalGrid: return from + (to - from) * sample } - private fun roundToCubeCoordinate(gridX: Double, gridY: Double, gridZ: Double): CubeCoordinate { + private fun roundToCubeCoordinate(gridX: Double, gridY: Double, gridZ: Double): org.hexworks.mixite2.core.api.CubeCoordinate { var rx = round(gridX).toInt() val ry = round(gridY).toInt() var rz = round(gridZ).toInt() @@ -112,6 +112,6 @@ class HexagonalGridCalculatorImpl(override val hexagonalGrid: } else if (differenceY <= differenceZ) { rz = -rx - ry } - return CubeCoordinate.fromCoordinates(rx, rz) + return org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(rx, rz) } } diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridImpl.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridImpl.kt similarity index 73% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridImpl.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridImpl.kt index 52ee87b..164849a 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridImpl.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridImpl.kt @@ -1,10 +1,10 @@ -package org.hexworks.mixite.core.internal.impl +package org.hexworks.mixite2.core.internal.impl import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.* -import org.hexworks.mixite.core.api.contract.HexagonDataStorage -import org.hexworks.mixite.core.api.contract.SatelliteData -import org.hexworks.mixite.core.internal.GridData +import org.hexworks.mixite2.core.api.* +import org.hexworks.mixite2.core.api.contract.HexagonDataStorage +import org.hexworks.mixite2.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.internal.GridData import kotlin.math.abs class HexagonalGridImpl(builder: HexagonalGridBuilder) : HexagonalGrid { @@ -37,16 +37,16 @@ class HexagonalGridImpl(builder: HexagonalGridBuilder) : H } } - private fun hexagon(coordinate: CubeCoordinate): HexagonImpl { + private fun hexagon(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate): HexagonImpl { return HexagonImpl(gridData, coordinate, hexagonDataStorage) } - override fun getHexagonsByCubeRange(from: CubeCoordinate, to: CubeCoordinate): Iterable> { - val coordinates = ArrayList(abs(from.gridZ-to.gridZ) + abs(from.gridX-to.gridX)) + override fun getHexagonsByCubeRange(from: org.hexworks.mixite2.core.api.CubeCoordinate, to: org.hexworks.mixite2.core.api.CubeCoordinate): Iterable> { + val coordinates = ArrayList(abs(from.gridZ-to.gridZ) + abs(from.gridX-to.gridX)) for (gridZ in from.gridZ..to.gridZ) { for (gridX in from.gridX..to.gridX) { - val coord = CubeCoordinate.fromCoordinates(gridX, gridZ) + val coord = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(gridX, gridZ) if (containsCubeCoordinate(coord)) { coordinates.add(coord) } @@ -71,13 +71,13 @@ class HexagonalGridImpl(builder: HexagonalGridBuilder) : H } override fun getHexagonsByOffsetRange(gridXFrom: Int, gridXTo: Int, gridYFrom: Int, gridYTo: Int): Iterable> { - val coords = ArrayList() + val coords = ArrayList() for (gridX in gridXFrom..gridXTo) { for (gridY in gridYFrom..gridYTo) { - val cubeX = CoordinateConverter.convertOffsetCoordinatesToCubeX(gridX, gridY, gridData.orientation) - val cubeZ = CoordinateConverter.convertOffsetCoordinatesToCubeZ(gridX, gridY, gridData.orientation) - val coord = CubeCoordinate.fromCoordinates(cubeX, cubeZ) + val cubeX = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeX(gridX, gridY, gridData.orientation) + val cubeZ = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeZ(gridX, gridY, gridData.orientation) + val coord = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(cubeX, cubeZ) if (containsCubeCoordinate(coord)) { coords.add(coord) } @@ -101,24 +101,24 @@ class HexagonalGridImpl(builder: HexagonalGridBuilder) : H } } - override fun containsCubeCoordinate(coordinate: CubeCoordinate): Boolean { + override fun containsCubeCoordinate(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Boolean { return this.hexagonDataStorage.containsCoordinate(coordinate) } - private fun _getByCubeCoordinate(coordinate: CubeCoordinate) = + private fun _getByCubeCoordinate(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate) = if (containsCubeCoordinate(coordinate)) hexagon(coordinate) else null - override fun getByCubeCoordinate(coordinate: CubeCoordinate): Maybe> = + override fun getByCubeCoordinate(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate): Maybe> = Maybe.ofNullable(_getByCubeCoordinate(coordinate)) override fun getByPixelCoordinate(coordinateX: Double, coordinateY: Double): Maybe> { var estimatedGridX = (coordinateX / gridData.hexagonWidth).toInt() var estimatedGridZ = (coordinateY / gridData.hexagonHeight).toInt() - estimatedGridX = CoordinateConverter.convertOffsetCoordinatesToCubeX(estimatedGridX, estimatedGridZ, gridData.orientation) - estimatedGridZ = CoordinateConverter.convertOffsetCoordinatesToCubeZ(estimatedGridX, estimatedGridZ, gridData.orientation) + estimatedGridX = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeX(estimatedGridX, estimatedGridZ, gridData.orientation) + estimatedGridZ = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeZ(estimatedGridX, estimatedGridZ, gridData.orientation) // it is possible that the estimated coordinates are off-grid so we // create a virtual hexagon - val estimatedCoordinate = CubeCoordinate.fromCoordinates(estimatedGridX, estimatedGridZ) + val estimatedCoordinate = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(estimatedGridX, estimatedGridZ) val centerHex = hexagon(estimatedCoordinate) val nearestHex = nearestHexagonToPoint(centerHex, Point.fromPosition(coordinateX, coordinateY)) @@ -130,13 +130,13 @@ class HexagonalGridImpl(builder: HexagonalGridBuilder) : H } private fun _getNeighborByIndex(hexagon: Hexagon, index: Int) = - CubeCoordinate.fromCoordinates( + org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates( hexagon.gridX + NEIGHBORS[index][NEIGHBOR_X_INDEX], hexagon.gridZ + NEIGHBORS[index][NEIGHBOR_Z_INDEX] ) - override fun getNeighborCoordinateByIndex(coordinate: CubeCoordinate, index: Int) = - CubeCoordinate.fromCoordinates( + override fun getNeighborCoordinateByIndex(coordinate: org.hexworks.mixite2.core.api.CubeCoordinate, index: Int) = + org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates( coordinate.gridX + NEIGHBORS[index][NEIGHBOR_X_INDEX], coordinate.gridZ + NEIGHBORS[index][NEIGHBOR_Z_INDEX] ) diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/GridLayoutStrategy.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/GridLayoutStrategy.kt similarity index 77% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/GridLayoutStrategy.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/GridLayoutStrategy.kt index 147a830..2fd4f63 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/GridLayoutStrategy.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/GridLayoutStrategy.kt @@ -1,8 +1,8 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy +package org.hexworks.mixite2.core.internal.impl.layoutstrategy -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData /** * Represents the method of creating a [org.hexworks.mixite.core.api.HexagonalGrid] corresponding to a given shape. @@ -17,7 +17,7 @@ abstract class GridLayoutStrategy { * * @return observable */ - abstract fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable + abstract fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable /** * Checks whether the supplied parameters are valid for the given strategy. diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategy.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategy.kt similarity index 67% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategy.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategy.kt index b178fee..8e1f02b 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategy.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategy.kt @@ -1,9 +1,9 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy +package org.hexworks.mixite2.core.internal.impl.layoutstrategy -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.HexagonOrientation -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.HexagonOrientation +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData import kotlin.math.abs import kotlin.math.floor import kotlin.math.max @@ -11,9 +11,9 @@ import kotlin.math.round class HexagonalGridLayoutStrategy : GridLayoutStrategy() { - override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { + override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { val gridSize = builder.getGridHeight() - val coords = ArrayList(1 + (gridSize * gridSize * 6 - 6) / 8) // TODO cell count owned by the builder + val coords = ArrayList(1 + (gridSize * gridSize * 6 - 6) / 8) // TODO cell count owned by the builder var startX = if (HexagonOrientation.FLAT_TOP.equals(builder.getOrientation())) floor(gridSize / 2.0).toInt() else round(gridSize / 4.0).toInt() val hexRadius = floor(gridSize / 2.0).toInt() val minX = startX - hexRadius @@ -22,7 +22,7 @@ class HexagonalGridLayoutStrategy : GridLayoutStrategy() { val distanceFromMid = abs(hexRadius - y) for (x in max(startX, minX)..max(startX, minX) + hexRadius + hexRadius - distanceFromMid) { val gridZ = if (HexagonOrientation.FLAT_TOP.equals(builder.getOrientation())) y - floor(gridSize / 4.0).toInt() else y - coords.add(CubeCoordinate.fromCoordinates(x, gridZ)) + coords.add(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(x, gridZ)) } startX-- y++ diff --git a/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategy.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategy.kt new file mode 100644 index 0000000..70cbe9f --- /dev/null +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategy.kt @@ -0,0 +1,29 @@ +package org.hexworks.mixite2.core.internal.impl.layoutstrategy + +import org.hexworks.mixite2.core.api.CoordinateConverter +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData + +class RectangularGridLayoutStrategy : GridLayoutStrategy() { + + override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { + val coords = ArrayList( builder.getGridHeight() * builder.getGridWidth()) + for (y in 0 until builder.getGridHeight()) { + for (x in 0 until builder.getGridWidth()) { + val gridX = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeX(x, y, builder.getOrientation()) + val gridZ = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeZ(x, y, builder.getOrientation()) + coords.add(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(gridX, gridZ)) + } + } + return coords + } + + override fun checkParameters(gridHeight: Int, gridWidth: Int): Boolean { + return checkCommonCase(gridHeight, gridWidth) + } + + override fun getName(): String { + return "RECTANGULAR" + } +} diff --git a/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategy.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategy.kt new file mode 100644 index 0000000..521ff25 --- /dev/null +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategy.kt @@ -0,0 +1,26 @@ +package org.hexworks.mixite2.core.internal.impl.layoutstrategy + +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData + +class TrapezoidGridLayoutStrategy : GridLayoutStrategy() { + + override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { + val coords = ArrayList(builder.getGridHeight() * builder.getGridWidth()) + for (gridZ in 0 until builder.getGridHeight()) { + for (gridX in 0 until builder.getGridWidth()) { + coords.add(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(gridX, gridZ)) + } + } + return coords + } + + override fun checkParameters(gridHeight: Int, gridWidth: Int): Boolean { + return checkCommonCase(gridHeight, gridWidth) + } + + override fun getName(): String { + return "TRAPEZOID" + } +} diff --git a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategy.kt b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategy.kt similarity index 55% rename from mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategy.kt rename to mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategy.kt index c23e69d..cafef07 100644 --- a/mixite.core/src/commonMain/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategy.kt +++ b/mixite2.core/src/commonMain/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategy.kt @@ -1,18 +1,18 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy +package org.hexworks.mixite2.core.internal.impl.layoutstrategy -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData class TriangularGridLayoutStrategy : GridLayoutStrategy() { - override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { + override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { val gridSize = builder.getGridHeight() - val coords = ArrayList(gridSize * (gridSize + 1) / 2) + val coords = ArrayList(gridSize * (gridSize + 1) / 2) for (gridZ in 0 until gridSize) { val endX = gridSize - gridZ for (gridX in 0 until endX) { - coords.add(CubeCoordinate.fromCoordinates(gridX, gridZ)) + coords.add(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(gridX, gridZ)) } } return coords diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/GridLayoutStrategyUtil.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/GridLayoutStrategyUtil.kt similarity index 71% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/GridLayoutStrategyUtil.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/GridLayoutStrategyUtil.kt index ce93e5c..edc35f1 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/GridLayoutStrategyUtil.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/GridLayoutStrategyUtil.kt @@ -1,8 +1,8 @@ -package org.hexworks.mixite.core +package org.hexworks.mixite2.core -import org.hexworks.mixite.core.api.HexagonOrientation -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.HexagonOrientation +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData object GridLayoutStrategyTestUtil { diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/HexagonStub.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/HexagonStub.kt similarity index 71% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/HexagonStub.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/HexagonStub.kt index e552004..2661d00 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/HexagonStub.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/HexagonStub.kt @@ -1,24 +1,22 @@ -package org.hexworks.mixite.core +package org.hexworks.mixite2.core import org.hexworks.cobalt.datatypes.Maybe -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.Hexagon -import org.hexworks.mixite.core.api.Point -import org.hexworks.mixite.core.api.Rectangle -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.Hexagon +import org.hexworks.mixite2.core.api.Point +import org.hexworks.mixite2.core.api.Rectangle +import org.hexworks.mixite2.core.api.contract.SatelliteData class HexagonStub(override val id: String = "", override val points: List = listOf(), override val vertices: List = listOf(), override val externalBoundingBox: Rectangle = Rectangle(0.0, 0.0, 0.0, 0.0), override val internalBoundingBox: Rectangle = Rectangle(0.0, 0.0, 0.0, 0.0), - override val cubeCoordinate: CubeCoordinate = CubeCoordinate.fromCoordinates(0, 0), + override val cubeCoordinate: org.hexworks.mixite2.core.api.CubeCoordinate = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(0, 0), override val gridX: Int = 0, override val gridY: Int = 0, override val gridZ: Int = 0, override val center: Point = Point.fromPosition(0.0, 0.0), - override val centerX: Double = center.coordinateY, - override val centerY: Double = center.coordinateY, override val satelliteData: Maybe = Maybe.empty()) : Hexagon { override fun setSatelliteData(data: T) { diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/CoordinateConverterTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/CoordinateConverterTest.kt similarity index 63% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/CoordinateConverterTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/CoordinateConverterTest.kt index eaadcf0..5c419ae 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/CoordinateConverterTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/CoordinateConverterTest.kt @@ -1,7 +1,7 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromAxialKey -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromAxialKey +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -11,38 +11,38 @@ class CoordinateConverterTest { @Test fun shouldThrowExceptionWhenInstantiated() { assertFailsWith { - CoordinateConverter() + org.hexworks.mixite2.core.api.CoordinateConverter() } } @Test fun shouldConvertOffsetCoordinatesToAxialXWithPointy() { - val result = CoordinateConverter.convertOffsetCoordinatesToCubeX(TEST_X, TEST_Y, HexagonOrientation.POINTY_TOP) + val result = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeX(TEST_X, TEST_Y, HexagonOrientation.POINTY_TOP) assertEquals(EXPECTED_AXIAL_X_WITH_POINTY, result) } @Test fun shouldConvertOffsetCoordinatesToAxialXWithFlat() { - val result = CoordinateConverter.convertOffsetCoordinatesToCubeX(TEST_X, TEST_Y, HexagonOrientation.FLAT_TOP) + val result = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeX(TEST_X, TEST_Y, HexagonOrientation.FLAT_TOP) assertEquals(EXPECTED_AXIAL_X_WITH_FLAT, result) } @Test fun shouldConvertOffsetCoordinatesToAxialZWithPointy() { - val result = CoordinateConverter.convertOffsetCoordinatesToCubeZ(TEST_X, TEST_Y, HexagonOrientation.POINTY_TOP) + val result = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeZ(TEST_X, TEST_Y, HexagonOrientation.POINTY_TOP) assertEquals(EXPECTED_AXIAL_Z_WITH_POINTY, result) } @Test fun shouldConvertOffsetCoordinatesToAxialZWithFlat() { - val result = CoordinateConverter.convertOffsetCoordinatesToCubeZ(TEST_X, TEST_Y, HexagonOrientation.FLAT_TOP) + val result = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeZ(TEST_X, TEST_Y, HexagonOrientation.FLAT_TOP) assertEquals(EXPECTED_AXIAL_Z_WITH_FLAT, result) } @Test fun shouldConvertCubeCoordinatesToOffsetRowWithFlat() { for((i, cube) in testCubes.withIndex()) { - val result = CoordinateConverter.convertCubeCoordinateToOffsetRow(cube, HexagonOrientation.FLAT_TOP) + val result = org.hexworks.mixite2.core.api.CoordinateConverter.convertCubeCoordinateToOffsetRow(cube, HexagonOrientation.FLAT_TOP) assertEquals(expectedOffsetRowWithFlat[i], result, "(" + cube.gridX + "," + cube.gridZ + ") to " + result) } } @@ -50,7 +50,7 @@ class CoordinateConverterTest { @Test fun shouldConvertCubeCoordinatesToOffsetColWithFlat() { for((i, cube) in testCubes.withIndex()) { - val result = CoordinateConverter.convertCubeCoordinateToOffsetColumn(cube, HexagonOrientation.FLAT_TOP) + val result = org.hexworks.mixite2.core.api.CoordinateConverter.convertCubeCoordinateToOffsetColumn(cube, HexagonOrientation.FLAT_TOP) assertEquals(expectedOffsetColWithFlat[i], result, "(" + cube.gridX + "," + cube.gridZ + ") to " + result) } } @@ -58,7 +58,7 @@ class CoordinateConverterTest { @Test fun shouldConvertCubeCoordinatesToOffsetRowWithPointy() { for((i, cube) in testCubes.withIndex()) { - val result = CoordinateConverter.convertCubeCoordinateToOffsetRow(cube, HexagonOrientation.POINTY_TOP) + val result = org.hexworks.mixite2.core.api.CoordinateConverter.convertCubeCoordinateToOffsetRow(cube, HexagonOrientation.POINTY_TOP) assertEquals(expectedOffsetRowWithPointy[i], result, "(" + cube.gridX + "," + cube.gridZ + ") to " + result) } } @@ -66,7 +66,7 @@ class CoordinateConverterTest { @Test fun shouldConvertCubeCoordinatesToOffsetColWithPointy() { for((i, cube) in testCubes.withIndex()) { - val result = CoordinateConverter.convertCubeCoordinateToOffsetColumn(cube, HexagonOrientation.POINTY_TOP) + val result = org.hexworks.mixite2.core.api.CoordinateConverter.convertCubeCoordinateToOffsetColumn(cube, HexagonOrientation.POINTY_TOP) assertEquals(expectedOffsetColWithPointy[i], result, "(" + cube.gridX + "," + cube.gridZ + ") to " + result) } } @@ -97,9 +97,9 @@ class CoordinateConverterTest { private const val EXPECTED_AXIAL_Z_WITH_FLAT = 3 private val testCubes = arrayOf( - CubeCoordinate.fromCoordinates(-1, -2), - CubeCoordinate.fromCoordinates( 2, -3), - CubeCoordinate.fromCoordinates( 7, 8) + org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(-1, -2), + org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates( 2, -3), + org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates( 7, 8) ) private val expectedOffsetColWithFlat = intArrayOf(-1, 2, 7) diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/CubeCoordinateTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/CubeCoordinateTest.kt similarity index 78% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/CubeCoordinateTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/CubeCoordinateTest.kt index 5c4b8d0..171ccf9 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/CubeCoordinateTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/CubeCoordinateTest.kt @@ -1,7 +1,7 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromAxialKey -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromAxialKey +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals @@ -9,7 +9,7 @@ import kotlin.test.assertFailsWith class CubeCoordinateTest { - lateinit var target: CubeCoordinate + lateinit var target: org.hexworks.mixite2.core.api.CubeCoordinate @BeforeTest fun setUp() { @@ -46,7 +46,7 @@ class CubeCoordinateTest { @Test fun shouldFailToCreateCoordinateFromMalformedKey() { assertFailsWith { - CubeCoordinate.fromAxialKey("asdf") + org.hexworks.mixite2.core.api.CubeCoordinate.fromAxialKey("asdf") } } diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/DefaultSatelliteDataTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/DefaultSatelliteDataTest.kt similarity index 90% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/DefaultSatelliteDataTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/DefaultSatelliteDataTest.kt index a0b83a1..dd5c2e2 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/DefaultSatelliteDataTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/DefaultSatelliteDataTest.kt @@ -1,6 +1,6 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData +import org.hexworks.mixite2.core.api.defaults.DefaultSatelliteData import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonOrientationTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonOrientationTest.kt similarity index 80% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonOrientationTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonOrientationTest.kt index 9a83eef..cee2346 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonOrientationTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonOrientationTest.kt @@ -1,6 +1,6 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api -import org.hexworks.mixite.core.api.HexagonOrientation.* +import org.hexworks.mixite2.core.api.HexagonOrientation.* import kotlin.test.Test import kotlin.test.assertEquals diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilderTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonalGridBuilderTest.kt similarity index 85% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilderTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonalGridBuilderTest.kt index 225d60b..b772003 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonalGridBuilderTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonalGridBuilderTest.kt @@ -1,9 +1,9 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api -import org.hexworks.mixite.core.api.HexagonOrientation.FLAT_TOP -import org.hexworks.mixite.core.api.HexagonalGridLayout.RECTANGULAR -import org.hexworks.mixite.core.api.HexagonalGridLayout.TRIANGULAR -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP +import org.hexworks.mixite2.core.api.HexagonalGridLayout.RECTANGULAR +import org.hexworks.mixite2.core.api.HexagonalGridLayout.TRIANGULAR +import org.hexworks.mixite2.core.api.contract.SatelliteData import kotlin.test.* class HexagonalGridBuilderTest { diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonalGridLayoutTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonalGridLayoutTest.kt similarity index 64% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonalGridLayoutTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonalGridLayoutTest.kt index c2b23e8..ec12028 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/HexagonalGridLayoutTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/HexagonalGridLayoutTest.kt @@ -1,10 +1,10 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api -import org.hexworks.mixite.core.api.HexagonalGridLayout.* -import org.hexworks.mixite.core.internal.impl.layoutstrategy.HexagonalGridLayoutStrategy -import org.hexworks.mixite.core.internal.impl.layoutstrategy.RectangularGridLayoutStrategy -import org.hexworks.mixite.core.internal.impl.layoutstrategy.TrapezoidGridLayoutStrategy -import org.hexworks.mixite.core.internal.impl.layoutstrategy.TriangularGridLayoutStrategy +import org.hexworks.mixite2.core.api.HexagonalGridLayout.* +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.HexagonalGridLayoutStrategy +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.RectangularGridLayoutStrategy +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.TrapezoidGridLayoutStrategy +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.TriangularGridLayoutStrategy import kotlin.test.Test import kotlin.test.assertTrue diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/PointTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/PointTest.kt similarity index 95% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/PointTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/PointTest.kt index 6f8f217..8a24e2c 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/PointTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/PointTest.kt @@ -1,4 +1,4 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api import kotlin.math.sqrt import kotlin.test.Test diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/RectangleTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/RectangleTest.kt similarity index 97% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/RectangleTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/RectangleTest.kt index f996ceb..37a9d2e 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/RectangleTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/RectangleTest.kt @@ -1,4 +1,4 @@ -package org.hexworks.mixite.core.api +package org.hexworks.mixite2.core.api import kotlin.test.Test import kotlin.test.assertEquals diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/defaults/DefaultHexagonDataStorageTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultHexagonDataStorageTest.kt similarity index 92% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/defaults/DefaultHexagonDataStorageTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultHexagonDataStorageTest.kt index f1ad5b8..fb88a5f 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/api/defaults/DefaultHexagonDataStorageTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/api/defaults/DefaultHexagonDataStorageTest.kt @@ -1,6 +1,6 @@ -package org.hexworks.mixite.core.api.defaults +package org.hexworks.mixite2.core.api.defaults -import org.hexworks.mixite.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.CubeCoordinate import kotlin.test.* class DefaultHexagonDataStorageTest { @@ -133,8 +133,8 @@ class DefaultHexagonDataStorageTest { companion object { - private val TEST_CUBE_COORDINATE = CubeCoordinate.fromCoordinates(1, 1) - private val ANOTHER_TEST_CUBE_COORDINATE = CubeCoordinate.fromCoordinates(2, 1) + private val TEST_CUBE_COORDINATE = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(1, 1) + private val ANOTHER_TEST_CUBE_COORDINATE = org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(2, 1) } } diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/GridDataTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/GridDataTest.kt similarity index 90% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/GridDataTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/GridDataTest.kt index e0542ed..0e97532 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/GridDataTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/GridDataTest.kt @@ -1,8 +1,8 @@ -package org.hexworks.mixite.core.internal +package org.hexworks.mixite2.core.internal -import org.hexworks.mixite.core.api.HexagonOrientation.FLAT_TOP -import org.hexworks.mixite.core.api.HexagonOrientation.POINTY_TOP -import org.hexworks.mixite.core.api.HexagonalGridLayout.RECTANGULAR +import org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP +import org.hexworks.mixite2.core.api.HexagonOrientation.POINTY_TOP +import org.hexworks.mixite2.core.api.HexagonalGridLayout.RECTANGULAR import kotlin.math.sqrt import kotlin.test.Test import kotlin.test.assertEquals diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/CustomGridLayoutHexagonImplTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/CustomGridLayoutHexagonImplTest.kt similarity index 64% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/CustomGridLayoutHexagonImplTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/CustomGridLayoutHexagonImplTest.kt index c47f16e..1e227c6 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/CustomGridLayoutHexagonImplTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/CustomGridLayoutHexagonImplTest.kt @@ -1,57 +1,57 @@ -package org.hexworks.mixite.core.internal.impl - -import org.hexworks.mixite.core.api.CoordinateConverter -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.Hexagon -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -class CustomGridLayoutHexagonImplTest : HexagonalGridImplTest() { - - override fun getBuilder(): HexagonalGridBuilder { - return HexagonalGridBuilder() - .setGridLayout(CustomGridLayoutStrategy()) - .setGridHeight(GRID_HEIGHT) - .setGridWidth(GRID_WIDTH) - .setRadius(RADIUS.toDouble()) - .setOrientation(ORIENTATION) - } - - override fun shouldReturnProperHexagonsWhenEachHexagonIsTestedInAGivenGrid() { - val hexagons = target.hexagons - val expectedCoordinates = HashSet() - for (x in 0 until GRID_WIDTH) { - for (y in 0 until GRID_HEIGHT) { - if (y == 0 && x == 0 || y == 0 && x == GRID_WIDTH - 1 || y == GRID_HEIGHT - 1 && x == 0 || - y == GRID_HEIGHT - 1 && x == GRID_WIDTH - 1) { - continue - } - val gridX = CoordinateConverter.convertOffsetCoordinatesToCubeX(x, y, ORIENTATION) - val gridZ = CoordinateConverter.convertOffsetCoordinatesToCubeZ(x, y, ORIENTATION) - expectedCoordinates.add("$gridX,$gridZ") - } - } - var count = 0 - for (hexagon in hexagons) { - expectedCoordinates.remove("${hexagon.gridX},${hexagon.gridZ}") - count++ - } - assertEquals(96, count) - assertTrue(expectedCoordinates.isEmpty()) - } - - override fun shouldReturnProperNeighborsOfHexagonWhenHexIsOnTheEdge() { - val hex = target.getByCubeCoordinate(CubeCoordinate.fromCoordinates(-4, 8)).get() - val expected = HashSet>() - expected.add(target.getByCubeCoordinate(CubeCoordinate.fromCoordinates(-3, 7)).get()) - expected.add(target.getByCubeCoordinate(CubeCoordinate.fromCoordinates(-3, 8)).get()) - val actual = target.getNeighborsOf(hex) - assertEquals(expected, actual) - } - - override fun shouldProperlyReturnGridLayoutWhenGetGridLayoutIsCalled() { - assertTrue(target.gridData.gridLayout is CustomGridLayoutStrategy) - } +package org.hexworks.mixite2.core.internal.impl + +import org.hexworks.mixite2.core.api.CoordinateConverter +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.Hexagon +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.defaults.DefaultSatelliteData +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class CustomGridLayoutHexagonImplTest : HexagonalGridImplTest() { + + override fun getBuilder(): HexagonalGridBuilder { + return HexagonalGridBuilder() + .setGridLayout(CustomGridLayoutStrategy()) + .setGridHeight(GRID_HEIGHT) + .setGridWidth(GRID_WIDTH) + .setRadius(RADIUS.toDouble()) + .setOrientation(ORIENTATION) + } + + override fun shouldReturnProperHexagonsWhenEachHexagonIsTestedInAGivenGrid() { + val hexagons = target.hexagons + val expectedCoordinates = HashSet() + for (x in 0 until GRID_WIDTH) { + for (y in 0 until GRID_HEIGHT) { + if (y == 0 && x == 0 || y == 0 && x == GRID_WIDTH - 1 || y == GRID_HEIGHT - 1 && x == 0 || + y == GRID_HEIGHT - 1 && x == GRID_WIDTH - 1) { + continue + } + val gridX = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeX(x, y, ORIENTATION) + val gridZ = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeZ(x, y, ORIENTATION) + expectedCoordinates.add("$gridX,$gridZ") + } + } + var count = 0 + for (hexagon in hexagons) { + expectedCoordinates.remove("${hexagon.gridX},${hexagon.gridZ}") + count++ + } + assertEquals(96, count) + assertTrue(expectedCoordinates.isEmpty()) + } + + override fun shouldReturnProperNeighborsOfHexagonWhenHexIsOnTheEdge() { + val hex = target.getByCubeCoordinate(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(-4, 8)).get() + val expected = HashSet>() + expected.add(target.getByCubeCoordinate(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(-3, 7)).get()) + expected.add(target.getByCubeCoordinate(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(-3, 8)).get()) + val actual = target.getNeighborsOf(hex) + assertEquals(expected, actual) + } + + override fun shouldProperlyReturnGridLayoutWhenGetGridLayoutIsCalled() { + assertTrue(target.gridData.gridLayout is CustomGridLayoutStrategy) + } } \ No newline at end of file diff --git a/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/CustomGridLayoutStrategy.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/CustomGridLayoutStrategy.kt new file mode 100644 index 0000000..bc40bd7 --- /dev/null +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/CustomGridLayoutStrategy.kt @@ -0,0 +1,33 @@ +package org.hexworks.mixite2.core.internal.impl + +import org.hexworks.mixite2.core.api.CoordinateConverter +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.GridLayoutStrategy + +class CustomGridLayoutStrategy : GridLayoutStrategy() { + + override fun fetchGridCoordinates(builder: HexagonalGridBuilder): Iterable { + val coords = ArrayList( builder.getGridHeight() * builder.getGridWidth()) + for (y in 0 until builder.getGridHeight()) { + val xStart = if (y == 0 || y == builder.getGridHeight() - 1) 1 else 0 + val xEnd = if (y == 0 || y == builder.getGridHeight() - 1) builder.getGridWidth() - 1 + else builder.getGridWidth() + for (x in xStart until xEnd) { + val gridX = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeX(x, y, builder.getOrientation()) + val gridZ = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeZ(x, y, builder.getOrientation()) + coords.add(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(gridX, gridZ)) + } + } + return coords + } + + override fun checkParameters(gridHeight: Int, gridWidth: Int): Boolean { + return true + } + + override fun getName(): String { + return "CUSTOM" + } +} diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonImplTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonImplTest.kt similarity index 88% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonImplTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonImplTest.kt index c170185..2b6df40 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonImplTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonImplTest.kt @@ -1,14 +1,14 @@ -package org.hexworks.mixite.core.internal.impl - -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates -import org.hexworks.mixite.core.api.Hexagon -import org.hexworks.mixite.core.api.HexagonOrientation.FLAT_TOP -import org.hexworks.mixite.core.api.HexagonOrientation.POINTY_TOP -import org.hexworks.mixite.core.api.HexagonalGridLayout.RECTANGULAR -import org.hexworks.mixite.core.api.Point.Companion.fromPosition -import org.hexworks.mixite.core.api.defaults.DefaultHexagonDataStorage -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData -import org.hexworks.mixite.core.internal.GridData +package org.hexworks.mixite2.core.internal.impl + +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.Hexagon +import org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP +import org.hexworks.mixite2.core.api.HexagonOrientation.POINTY_TOP +import org.hexworks.mixite2.core.api.HexagonalGridLayout.RECTANGULAR +import org.hexworks.mixite2.core.api.Point.Companion.fromPosition +import org.hexworks.mixite2.core.api.defaults.DefaultHexagonDataStorage +import org.hexworks.mixite2.core.api.defaults.DefaultSatelliteData +import org.hexworks.mixite2.core.internal.GridData import kotlin.math.round import kotlin.test.BeforeTest import kotlin.test.Test diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridCalculatorImplTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridCalculatorImplTest.kt similarity index 85% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridCalculatorImplTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridCalculatorImplTest.kt index b404780..40fca06 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridCalculatorImplTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridCalculatorImplTest.kt @@ -1,11 +1,11 @@ -package org.hexworks.mixite.core.internal.impl - -import org.hexworks.mixite.core.HexagonStub -import org.hexworks.mixite.core.api.* -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates -import org.hexworks.mixite.core.api.RotationDirection.LEFT -import org.hexworks.mixite.core.api.RotationDirection.RIGHT -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData +package org.hexworks.mixite2.core.internal.impl + +import org.hexworks.mixite2.core.HexagonStub +import org.hexworks.mixite2.core.api.* +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.RotationDirection.LEFT +import org.hexworks.mixite2.core.api.RotationDirection.RIGHT +import org.hexworks.mixite2.core.api.defaults.DefaultSatelliteData import kotlin.test.* class HexagonalGridCalculatorImplTest { @@ -100,6 +100,35 @@ class HexagonalGridCalculatorImplTest { } } + /** + * This test specifically targets a bug that was found in the + * Python port. + */ + @Test + fun shouldProperlyCalculateLineWithFlatTop() { + + val localBuilder = HexagonalGridBuilder() + .setGridHeight(10) + .setGridWidth(10) + .setGridLayout(HexagonalGridLayout.RECTANGULAR) + .setOrientation(HexagonOrientation.FLAT_TOP) + .setRadius(35.0) + val localGrid = localBuilder.build() + val localTarget = localBuilder.buildCalculatorFor(localGrid) + + val actual = localTarget.drawLine(localGrid.getByCubeCoordinate(fromCoordinates(6, 4)).get(), + localGrid.getByCubeCoordinate(fromCoordinates(7, -2)).get()) + assertEquals(expected = listOf( + localGrid.getByCubeCoordinate(fromCoordinates(6, 4)).get(), + localGrid.getByCubeCoordinate(fromCoordinates(6, 3)).get(), + localGrid.getByCubeCoordinate(fromCoordinates(6, 2)).get(), + localGrid.getByCubeCoordinate(fromCoordinates(6, 1)).get(), + localGrid.getByCubeCoordinate(fromCoordinates(7, 0)).get(), + localGrid.getByCubeCoordinate(fromCoordinates(7, -1)).get(), + localGrid.getByCubeCoordinate(fromCoordinates(7, -2)).get()), + actual = actual) + } + @Test fun shouldCheckForVisibilityCorrectly() { diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridImplTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridImplTest.kt similarity index 91% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridImplTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridImplTest.kt index 0906cb1..27b6d92 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/HexagonalGridImplTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/HexagonalGridImplTest.kt @@ -1,12 +1,18 @@ -package org.hexworks.mixite.core.internal.impl - -import org.hexworks.mixite.core.api.* -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates -import org.hexworks.mixite.core.api.HexagonalGridLayout.RECTANGULAR -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData -import org.hexworks.mixite.core.internal.impl.layoutstrategy.RectangularGridLayoutStrategy -import kotlin.test.* - +package org.hexworks.mixite2.core.internal.impl + +import org.hexworks.mixite2.core.api.* +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.defaults.DefaultSatelliteData +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.assertFalse + +/** + * This class does not actually run its own tests! This is a base class, + * and the subclasses each run the inherited tests. + */ abstract class HexagonalGridImplTest { internal lateinit var target: HexagonalGrid @@ -22,8 +28,8 @@ abstract class HexagonalGridImplTest { @Test fun shouldReturnHexagonsInProperIterationOrderWhenGetHexagonsIsCalled() { - val expectedCoordinates = ArrayList() - val actualCoordinates = ArrayList() + val expectedCoordinates = ArrayList() + val actualCoordinates = ArrayList() for (cubeCoordinate in builder.gridLayoutStrategy.fetchGridCoordinates(builder)) { expectedCoordinates.add(cubeCoordinate) diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/RectangularGridLayoutHexagonImplTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/RectangularGridLayoutHexagonImplTest.kt similarity index 58% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/RectangularGridLayoutHexagonImplTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/RectangularGridLayoutHexagonImplTest.kt index 838d339..42a2d8d 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/RectangularGridLayoutHexagonImplTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/RectangularGridLayoutHexagonImplTest.kt @@ -1,53 +1,53 @@ -package org.hexworks.mixite.core.internal.impl - -import org.hexworks.mixite.core.api.CoordinateConverter -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.Hexagon -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.defaults.DefaultSatelliteData -import org.hexworks.mixite.core.internal.impl.layoutstrategy.RectangularGridLayoutStrategy -import kotlin.test.assertEquals -import kotlin.test.assertTrue - -class RectangularGridLayoutHexagonImplTest : HexagonalGridImplTest() { - - override fun getBuilder(): HexagonalGridBuilder { - return HexagonalGridBuilder() - .setGridHeight(GRID_HEIGHT) - .setGridWidth(GRID_WIDTH) - .setRadius(RADIUS.toDouble()) - .setOrientation(ORIENTATION) - } - - override fun shouldReturnProperHexagonsWhenEachHexagonIsTestedInAGivenGrid() { - val hexagons = target.hexagons - val expectedCoordinates = HashSet() - for (x in 0 until GRID_WIDTH) { - for (y in 0 until GRID_HEIGHT) { - val gridX = CoordinateConverter.convertOffsetCoordinatesToCubeX(x, y, ORIENTATION) - val gridZ = CoordinateConverter.convertOffsetCoordinatesToCubeZ(x, y, ORIENTATION) - expectedCoordinates.add("$gridX,$gridZ") - } - } - var count = 0 - for (hexagon in hexagons) { - expectedCoordinates.remove("${hexagon.gridX},${hexagon.gridZ}") - count++ - } - assertEquals(100, count) - assertTrue(expectedCoordinates.isEmpty()) - } - - override fun shouldReturnProperNeighborsOfHexagonWhenHexIsOnTheEdge() { - val hex = target.getByCubeCoordinate(CubeCoordinate.fromCoordinates(5, 9)).get() - val expected = HashSet>() - expected.add(target.getByCubeCoordinate(CubeCoordinate.fromCoordinates(5, 8)).get()) - expected.add(target.getByCubeCoordinate(CubeCoordinate.fromCoordinates(4, 9)).get()) - val actual = target.getNeighborsOf(hex) - assertEquals(expected, actual) - } - - override fun shouldProperlyReturnGridLayoutWhenGetGridLayoutIsCalled() { - assertTrue(target.gridData.gridLayout is RectangularGridLayoutStrategy) - } +package org.hexworks.mixite2.core.internal.impl + +import org.hexworks.mixite2.core.api.CoordinateConverter +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.Hexagon +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.defaults.DefaultSatelliteData +import org.hexworks.mixite2.core.internal.impl.layoutstrategy.RectangularGridLayoutStrategy +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class RectangularGridLayoutHexagonImplTest : HexagonalGridImplTest() { + + override fun getBuilder(): HexagonalGridBuilder { + return HexagonalGridBuilder() + .setGridHeight(GRID_HEIGHT) + .setGridWidth(GRID_WIDTH) + .setRadius(RADIUS.toDouble()) + .setOrientation(ORIENTATION) + } + + override fun shouldReturnProperHexagonsWhenEachHexagonIsTestedInAGivenGrid() { + val hexagons = target.hexagons + val expectedCoordinates = HashSet() + for (x in 0 until GRID_WIDTH) { + for (y in 0 until GRID_HEIGHT) { + val gridX = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeX(x, y, ORIENTATION) + val gridZ = org.hexworks.mixite2.core.api.CoordinateConverter.convertOffsetCoordinatesToCubeZ(x, y, ORIENTATION) + expectedCoordinates.add("$gridX,$gridZ") + } + } + var count = 0 + for (hexagon in hexagons) { + expectedCoordinates.remove("${hexagon.gridX},${hexagon.gridZ}") + count++ + } + assertEquals(100, count) + assertTrue(expectedCoordinates.isEmpty()) + } + + override fun shouldReturnProperNeighborsOfHexagonWhenHexIsOnTheEdge() { + val hex = target.getByCubeCoordinate(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(5, 9)).get() + val expected = HashSet>() + expected.add(target.getByCubeCoordinate(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(5, 8)).get()) + expected.add(target.getByCubeCoordinate(org.hexworks.mixite2.core.api.CubeCoordinate.fromCoordinates(4, 9)).get()) + val actual = target.getNeighborsOf(hex) + assertEquals(expected, actual) + } + + override fun shouldProperlyReturnGridLayoutWhenGetGridLayoutIsCalled() { + assertTrue(target.gridData.gridLayout is RectangularGridLayoutStrategy) + } } \ No newline at end of file diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategyTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategyTest.kt similarity index 91% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategyTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategyTest.kt index 8e96ce6..434edc4 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategyTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/HexagonalGridLayoutStrategyTest.kt @@ -1,11 +1,11 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy - -import org.hexworks.mixite.core.GridLayoutStrategyTestUtil.fetchDefaultBuilder -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates -import org.hexworks.mixite.core.api.HexagonOrientation.FLAT_TOP -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData +package org.hexworks.mixite2.core.internal.impl.layoutstrategy + +import org.hexworks.mixite2.core.GridLayoutStrategyTestUtil.fetchDefaultBuilder +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertFalse @@ -26,7 +26,7 @@ class HexagonalGridLayoutStrategyTest { fun shouldProperlyCreateHexagonsWithPointyOrientationWhenCreateHexagonsIsCalled() { val coordIter = target.fetchGridCoordinates(builder).iterator() - val coords = ArrayList() + val coords = ArrayList() while (coordIter.hasNext()) { coords.add(coordIter.next()) } @@ -57,7 +57,7 @@ class HexagonalGridLayoutStrategyTest { builder.setGridHeight(5).setGridWidth(5) val coordIter = target.fetchGridCoordinates(builder).iterator() - val coords = ArrayList() + val coords = ArrayList() while (coordIter.hasNext()) { coords.add(coordIter.next()) } @@ -100,7 +100,7 @@ class HexagonalGridLayoutStrategyTest { builder.setOrientation(FLAT_TOP) val coordIter = target.fetchGridCoordinates(builder).iterator() - val coords = ArrayList() + val coords = ArrayList() while (coordIter.hasNext()) { coords.add(coordIter.next()) } @@ -132,7 +132,7 @@ class HexagonalGridLayoutStrategyTest { builder.setGridHeight(5).setGridWidth(5).setOrientation(FLAT_TOP) val coordIter = target.fetchGridCoordinates(builder).iterator() - val coords = ArrayList() + val coords = ArrayList() while (coordIter.hasNext()) { coords.add(coordIter.next()) } diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategyTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategyTest.kt similarity index 88% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategyTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategyTest.kt index cf10106..0fec2af 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategyTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/RectangularGridLayoutStrategyTest.kt @@ -1,11 +1,11 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy - -import org.hexworks.mixite.core.GridLayoutStrategyTestUtil -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates -import org.hexworks.mixite.core.api.HexagonOrientation.FLAT_TOP -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData +package org.hexworks.mixite2.core.internal.impl.layoutstrategy + +import org.hexworks.mixite2.core.GridLayoutStrategyTestUtil +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertFalse @@ -26,7 +26,7 @@ class RectangularGridLayoutStrategyTest { fun shouldProperlyCreateHexagonsWithPointyOrientationWhenCreateHexagonsIsCalled() { val coordIter = target.fetchGridCoordinates(builder).iterator() - val coords = ArrayList() + val coords = ArrayList() while (coordIter.hasNext()) { coords.add(coordIter.next()) } @@ -62,7 +62,7 @@ class RectangularGridLayoutStrategyTest { builder.setOrientation(FLAT_TOP) val coordIter = target.fetchGridCoordinates(builder).iterator() - val coords = ArrayList() + val coords = ArrayList() while (coordIter.hasNext()) { coords.add(coordIter.next()) } diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategyTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategyTest.kt similarity index 78% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategyTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategyTest.kt index 5cb0805..7eab411 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategyTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TrapezoidGridLayoutStrategyTest.kt @@ -1,11 +1,11 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy +package org.hexworks.mixite2.core.internal.impl.layoutstrategy -import org.hexworks.mixite.core.GridLayoutStrategyTestUtil -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates -import org.hexworks.mixite.core.api.HexagonOrientation.FLAT_TOP -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.GridLayoutStrategyTestUtil +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertTrue @@ -26,8 +26,8 @@ class TrapezoidGridLayoutStrategyTest { testCoordinates(target.fetchGridCoordinates(builder).iterator()) } - private fun testCoordinates(coordIter: Iterator) { - val coords = ArrayList() + private fun testCoordinates(coordIter: Iterator) { + val coords = ArrayList() while (coordIter.hasNext()) { coords.add(coordIter.next()) } diff --git a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategyTest.kt b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategyTest.kt similarity index 81% rename from mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategyTest.kt rename to mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategyTest.kt index f58af5a..32f5f13 100644 --- a/mixite.core/src/commonTest/kotlin/org/hexworks/mixite/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategyTest.kt +++ b/mixite2.core/src/commonTest/kotlin/org/hexworks/mixite2/core/internal/impl/layoutstrategy/TriangularGridLayoutStrategyTest.kt @@ -1,11 +1,11 @@ -package org.hexworks.mixite.core.internal.impl.layoutstrategy +package org.hexworks.mixite2.core.internal.impl.layoutstrategy -import org.hexworks.mixite.core.GridLayoutStrategyTestUtil.fetchDefaultBuilder -import org.hexworks.mixite.core.api.CubeCoordinate -import org.hexworks.mixite.core.api.CubeCoordinate.Companion.fromCoordinates -import org.hexworks.mixite.core.api.HexagonOrientation.FLAT_TOP -import org.hexworks.mixite.core.api.HexagonalGridBuilder -import org.hexworks.mixite.core.api.contract.SatelliteData +import org.hexworks.mixite2.core.GridLayoutStrategyTestUtil.fetchDefaultBuilder +import org.hexworks.mixite2.core.api.CubeCoordinate +import org.hexworks.mixite2.core.api.CubeCoordinate.Companion.fromCoordinates +import org.hexworks.mixite2.core.api.HexagonOrientation.FLAT_TOP +import org.hexworks.mixite2.core.api.HexagonalGridBuilder +import org.hexworks.mixite2.core.api.contract.SatelliteData import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertFalse @@ -57,9 +57,9 @@ class TriangularGridLayoutStrategyTest { assertFalse(result) } - private fun testCoordinates(coordIter: Iterator) { + private fun testCoordinates(coordIter: Iterator) { - val coords = ArrayList() + val coords = ArrayList() while (coordIter.hasNext()) { coords.add(coordIter.next()) } diff --git a/settings.gradle.kts b/settings.gradle.kts index 9ef0f08..8a55872 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,4 +1,4 @@ -rootProject.name = "mixite" +rootProject.name = "mixite2" pluginManagement { repositories { @@ -6,5 +6,4 @@ pluginManagement { } } -include(":mixite.core") -//include(":mixite.benchmarks") \ No newline at end of file +include(":mixite2.core") \ No newline at end of file