Fixed Tile data structure conversions and supporting functions

main
Bryson Zimmerman 12 months ago
parent c07952bec1
commit 4eb1ad3ad8

@ -5,12 +5,18 @@ enum class Directions(val dir: Int) {
UP(1),
DOWN(2),
LEFT(4),
RIGHT(8),
VISITED(16)
;
RIGHT(8);
companion object {
fun opposite(dir: Directions): Directions {
return when(dir) {
UP -> DOWN
DOWN -> UP
LEFT -> RIGHT
RIGHT -> LEFT
else -> NONE
}
}
fun getModifier(dir: Directions): Long {
return when(dir) {
UP -> NORTH
@ -42,10 +48,4 @@ enum class Directions(val dir: Int) {
fun inv(): Int {
return dir.inv()
}
fun opposite(dir: Long): Long {
val x = (dir shr 32).toInt() * -1
val y = dir.toInt() * -1
return x.toLong() shl 32 + y
}
}

@ -9,13 +9,8 @@ value class Tile(val value: Long) {
}
fun connect(candidateTile: Tile) {
val dir = Directions.convertModifier(this.value - candidateTile.value)
if(candidateTile.isInBounds()) {
World.update(this, getProperties().add(dir))
}
else {
throw(ArrayIndexOutOfBoundsException("Cannot connect to an out of bounds tile"))
}
val dir = Directions.convertModifier(candidateTile.value - this.value)
connect(dir)
}
//Connect two tiles together.
//Calls utility function on the connected cell
@ -23,10 +18,11 @@ value class Tile(val value: Long) {
val candidateTile = this+dir
//Ensure that the tile is within bounds
if(candidateTile.isInBounds())
if(candidateTile.isInBounds() && this.isInBounds())
{
World.tiles.value[x()][y()] = getProperties().add(dir)
World.tiles.value[candidateTile.x()][candidateTile.y()] = candidateTile.getProperties().add(dir)
World.tiles.value[candidateTile.x()][candidateTile.y()] = candidateTile.getProperties().add(Directions.opposite(dir))
}
else {
//Shouldn't matter whether we skip connecting an out-of-bounds item
@ -78,26 +74,30 @@ value class Tile(val value: Long) {
//Get tile at given direction
operator fun plus(dir: Directions): Tile {
return Tile(x() + x(dir), y() + y(dir))
return this + Directions.getModifier(dir)
}
//Get tile at direction opposite of given direction
operator fun minus(dir: Long): Tile {
return Tile(x() - x(dir), y() - y(dir))
operator fun plus(mod: Long): Tile {
return Tile(this.value + mod)
}
operator fun plus(tile: Tile): Tile {
return Tile(x() + tile.x(), y() + tile.y())
}
fun x(): Int {
return (value shr 32).toInt()
}
//Gets the x value of the given coordinate form
fun x(coord: Long):Int {
return (coord shr 32).toInt()
fun x(coord: Tile):Int {
return coord.x()
}
//Gets the x value of the coordinate form of the given direction
fun x(dir: Directions):Int {
return (Directions.getModifier(dir) shr 32).toInt()
return (this + dir).x()
}
fun y():Int {
@ -105,13 +105,13 @@ value class Tile(val value: Long) {
}
//Gets the y value of the given coordinate form
fun y(coord: Long):Int {
return coord.toInt()
fun y(coord: Tile):Int {
return coord.y()
}
//Gets the y value of the coordinate form of the given direction
fun y(dir: Directions):Int {
return y() + Directions.getModifier(dir).toInt()
return (this + dir).y()
}
override fun toString():String {

@ -48,6 +48,9 @@ class TileTest {
someTile.connect(UP)
val northTile = someTile + UP
assert(someTile.getAdjacentTiles(true).contains(northTile))
assert(someTile.getProperties().isNorth())
assertFalse(someTile.getProperties().isSouth())
assertFalse(someTile.getProperties().isWest())
@ -57,6 +60,7 @@ class TileTest {
assertFalse(northTile.getProperties().isNorth())
assertFalse(northTile.getProperties().isWest())
assertFalse(northTile.getProperties().isEast())
assert(northTile.getAdjacentTiles(true).contains(someTile))
}
@Test
@ -67,13 +71,10 @@ class TileTest {
val westTile = someTile + LEFT
val eastTile = someTile + RIGHT
//Generating raw tiles without built-in bounds checking, do it here
if(northTile.isInBounds())
northTile.getProperties().visited()
if(southTile.isInBounds())
southTile.getProperties().visited()
someTile.connect(northTile)
someTile.connect(DOWN)
val world = World.tiles
val adjacent = someTile.getAdjacentTiles(false)
assertFalse(adjacent.contains(northTile))
assertFalse(adjacent.contains(southTile))

Loading…
Cancel
Save