xyz So, here is what I've wrote, I tried to make it before then looking at your demo code so, I wouldn't be tempted to imitate it first. I noticed that your code seemed like it would just grab the top tile, regardless if there was a tile directly under it. So, what I tried to do was write something that would sort the top tiles, and also find if there were any tiles that were underneath with a gap between them.
I am still trying to make it fully accurate, it will get me the tiles that I want, but also there are certain cases where another tiles slips by that isn't actually not in the stack.
I'll keep working on it tomorrow, but thought I'd post it here.
extends GridMap
class CellData:
var height: int
var has_below: bool
var is_below: CellData
func _init(iheight: int, ihas_below: bool, iis_below: CellData) -> void:
height = iheight
has_below = ihas_below
is_below = iis_below
var map_cells: Dictionary
func _ready() -> void:
_get_map_cells()
func _get_map_cells() -> void:
var sort_cells: Array[Array] = _sort_cells()
for primary in sort_cells[0]:
var add: = Vector2i(primary.x,primary.z)
for secondary in sort_cells[1]:
if not primary.x == secondary.x:
continue
if not primary.z == secondary.z:
continue
map_cells[add] = CellData.new(primary.y,true,CellData.new(secondary.y,false,null))
if not map_cells.get(add):
map_cells[add] = CellData.new(primary.y,false,null)
for cell in map_cells:
if map_cells[cell].has_below:
print(map_cells[cell].is_below.height,map_cells[cell].height)
func _sort_cells() -> Array[Array]:
var used_cells = get_used_cells()
var sorted: Array[Vector3i]
var below: Array[Vector3i]
#finds the top tiles of the gridmap, and potential tiles below others
for first_cell in used_cells:
var cell_test: Vector3i = first_cell
for second_cell in used_cells:
if first_cell == second_cell:
continue
if not first_cell.x == second_cell.x:
continue
if not first_cell.z == second_cell.z:
continue
if not first_cell.y < second_cell.y:
continue
if abs(first_cell.y - second_cell.y) > 1:
if not below.has(first_cell):
below.append(first_cell)
cell_test = second_cell
if not sorted.has(cell_test):
sorted.append(cell_test)
#finds only the cells in the below array that is below another tile, and not stacked
for cell2 in below:
var cell_below: = Vector3i.ZERO
for cell1 in sorted:
if not cell1.x == cell2.x:
continue
if not cell1.z == cell2.z:
continue
if not abs(cell1.y - cell2.y) > 1:
continue
cell_below = cell2
if cell_below:
below.pop_at(below.find(cell_below))
print(sorted,below)
return [sorted,below]