Refactored World to contain its tiles directly, instead of through a redundant container

main
Bryson Zimmerman 11 months ago
parent d6397d9fc9
commit 5a0823ef48

@ -1,10 +1,12 @@
package technology.zim package technology.zim
import technology.zim.data.Directions
import technology.zim.data.Tile import technology.zim.data.Tile
import technology.zim.data.TileProperties import technology.zim.data.TileProperties
import technology.zim.data.WorldData import technology.zim.data.WorldData
import java.util.* import java.util.*
import technology.zim.data.Directions.* import technology.zim.data.Directions.*
import technology.zim.data.TileNavigatedArray
//Singleton object containing a set of tiles //Singleton object containing a set of tiles
//Has helper functions included //Has helper functions included
@ -19,7 +21,7 @@ import technology.zim.data.Directions.*
object World { object World {
//Default size should be 10 //Default size should be 10
val tiles = WorldData(ArrayList<ArrayList<TileProperties>>()) val tiles = TileNavigatedArray<TileProperties>()
var sizeX = 10 //Default size var sizeX = 10 //Default size
var sizeY = 10 var sizeY = 10
const val ANSI_RESET = "\u001B[0m" const val ANSI_RESET = "\u001B[0m"
@ -34,11 +36,11 @@ object World {
fun update(tile: Tile, to: TileProperties) { fun update(tile: Tile, to: TileProperties) {
tiles.value[tile.x()][tile.y()] = to tiles.set(tile, to)
} }
fun get(tile: Tile): TileProperties { fun get(tile: Tile): TileProperties {
return tiles.value[tile.x()][tile.y()] return tiles.get(tile)?: TileProperties(0)
} }
//Returns a coordinate pair //Returns a coordinate pair
@ -49,20 +51,29 @@ object World {
fun setSize(x: Int, y: Int) { fun setSize(x: Int, y: Int) {
sizeX = x sizeX = x
sizeY = y sizeY = y
tiles.setSize(x, y) tiles.resize(x, y)
} }
//TODO: https://en.wikipedia.org/wiki/Box-drawing_characters //Accepts a list of directions, removes those directions from every TileProperties in WorldData
//^ make the maze look like a maze fun scrubDirections(rem: List<Directions>) {
var mask = rem.fold(0) { sum, element -> sum + element.dir}
mask = mask.inv()
tiles.forEachIndexed {
index, tile ->
tiles[index] = TileProperties((tile?:TileProperties(0)).connections and(mask))
}
}
override fun toString(): String { override fun toString(): String {
val str = StringBuilder() val str = StringBuilder()
var inPath = false var inPath = false
var checked = false var checked = false
//Reading left to right, top to bottom - can do a simple for each on the rows //Reading left to right, top to bottom - can do a simple for each on the rows
for (y in 0..tiles.value[0].size - 1) { for (y in 0..sizeY - 1) {
//Upper line: Print each tile, print right-hand connections //Upper line: Print each tile, print right-hand connections
for (x in 0..tiles.value[0].size - 1) { for (x in 0..sizeX - 1) {
if(get(Tile(x, y)).connections and(INPATH.dir) != 0) if(get(Tile(x, y)).connections and(INPATH.dir) != 0)
inPath = true inPath = true
else if (get(Tile(x, y)).connections and(FRONTIER.dir) != 0) else if (get(Tile(x, y)).connections and(FRONTIER.dir) != 0)

@ -1,23 +1,9 @@
package technology.zim.data package technology.zim.data
//Data structure wrapper for a set of tiles //Data structure wrapper for a set of tiles
//Todo: Test converting to a single row-major array, multiplying the y value by row size to find the element position //Todo: Convert to TileNavigatedArray
@JvmInline class WorldData(val value: ArrayList<ArrayList<TileProperties>>) {
value class WorldData(val value: ArrayList<ArrayList<TileProperties>>) {
//Accepts a list of directions, removes those directions from every TileProperties in WorldData
fun scrubDirections(rem: List<Directions>) {
var mask = rem.fold(0) { sum, element -> sum + element.dir}
mask = mask.inv()
value.forEachIndexed {
x, row ->
row.forEachIndexed {
y, tile ->
value[x][y] = TileProperties(tile.connections and(mask))
}
}
}
fun setSize(xmin : Int, ymin : Int) { fun setSize(xmin : Int, ymin : Int) {
val emptyTile = TileProperties(0) val emptyTile = TileProperties(0)
@ -35,7 +21,5 @@ value class WorldData(val value: ArrayList<ArrayList<TileProperties>>) {
x.add(emptyTile) x.add(emptyTile)
} }
} }
//println("WorldData now sized at: " + value.size + ", " + value[0].size)
} }
} }

Loading…
Cancel
Save