xyz Hey, thanks for that! Also, that makes sense with assert, I'll work on making a boundry checker to my current code. I feel like maybe I'm going over complicated with what I am doing, maybe I'm missing something. I have still some bugs I'm ironing out, here is what I have so far, it is more to try and judge if there are any critical flaws in my thinking as I am implementing this data structure and converting the visual to be translated into the data structure.
extends GridMap
class Cell:
var height: int
var walkable: bool
func _init(iheight: int, iwalkable: int) -> void:
height = iheight
walkable = iwalkable
# map size (cell number)
var _size: Vector2i
# map (array of cells)
var _cells: Array[Cell]
# cell size in world units (meter)
var _cell_size_physical: int = 1
var _cell_height_physical: int = 1
# sets up array size holding cells
func initialize(size: Vector2i) -> void:
_size = size
_cells.resize(_size.x * _size.y)
func _get_array_index(pos: Vector2i) -> int:
assert(pos.x >= 0 and pos.x <= _size.x and pos.y >= 0 and pos.y <= _size.y)
return pos.x + pos.y * _size.y
func get_cell(pos: Vector2i) -> Cell:
return _cells[_get_array_index(pos)]
func get_cell_pos(pos: Vector2i) -> int:
return _get_array_index(pos)
func set_cell(pos: Vector2i,data: Cell) -> void:
print(get_cell_pos(pos), data)
_cells[get_cell_pos(pos)] = data
#var cell = _cells.insert(get_cell(pos),data)
#cell.height = data.height
#cell.walkable = data.walkable
# list of all tiles
var used_cells: Array[Vector3i]
# saves the tile type of each tile in used
var cell_item: Array
# translates the used_cells coords to Vector2i
var cell_coords: Array[Vector2i]
func _ready() -> void:
used_cells = get_used_cells()
for cell in used_cells:
cell_item.append(get_cell_item(cell))
for pos in used_cells:
cell_coords.append(Vector2i(pos.x,pos.z))
cell_coords.sort_custom(_sort_descending)
initialize(_get_size(cell_coords[0],cell_coords[cell_coords.size() - 1]))
setup_cell_grid()
func setup_cell_grid() -> void:
cell_coords.sort_custom(func(a,b): return a.x + a.y < b.x + b.y)
# var count : int
var grid_count: = Vector2i(0,0)
for cell in cell_coords:
for used in used_cells:
if cell == Vector2i(used.x,used.z):
set_cell(grid_count,Cell.new(0,true))
if grid_count.x < _size.x:
grid_count.x += 1
elif grid_count.y < _size.y:
grid_count.x = 0
grid_count.y += 1
else:
print("what happened?")
# count += 1
# print(count)
func _sort_descending(a,b) -> bool:
if a.x + a.y > b.x + b.y:
return true
return false
func _get_size(num1: Vector2i, num2: Vector2i) -> Vector2i:
var val_x: int
var val_y: int
if num1.x <= 0:
val_x = abs(num2.x) + num1.x + 1
elif num2.x < 0:
val_x = abs(num2.x) + num1.x + 1
else:
val_x = num1.x - num2.x + 1
if num1.y <= 0:
val_y = abs(num2.y) + num1.y + 1
elif num2.y < 0:
val_y = abs(num2.y) + num1.y + 1
else:
val_y = num1.y - num2.y + 1
return Vector2i(val_x,val_y)