我正在开发一个基于瓷砖的游戏,试图突出所有与玩家在 R 个单位内的瓷砖。

为了实现这一点,我从一个包含 0,0 坐标的数组开始,重复 R 次:{将坐标添加到每个坐标的北、南、东、西。}

所以在第一次循环后,数组从 [[0,0]] 变为 [[0,0],[0,1],[1,0],[0,-1],[-1,0]]

这会向外传播以获取我想要的所有瓷砖... 但也会重复瓷砖。

在这个过程之后,我想使用 array_unique(list_of_coords) 来获取坐标列表的唯一列表...

但它并没有工作😞

我希望我可以说更多,但是似乎真的就是这样了。它只是返回相同的数组,没有做出任何改变。我也无法使用 array_contains() 来检查它是否已经包含了我要添加的值,并且这也没有工作。

是否有任何已知陷阱我可能会遇到,或者为什么这可能不会工作?感谢提前。


关键代码:

list_of_tiles = array_unique(list_of_tiles)
array_foreach(list_of_tiles, draw_tiles)

完整代码:

function show_manhattan_range(absolute_x, absolute_y, tile_r){
// 绘制半透明的彩色瓷砖以显示在一定范围内可以影响的所有空间。
_dx = absolute_x
_dy = absolute_y

list_of_tiles = [[0,0]]

for (i = tile_r; i > 0; i-=1 ) {
// 获取绘制方块的坐标列表
propogate_tiles = function(element, index)
{
// element 是一个坐标数组,如 0,0。这个函数在每个方向上添加 +1 坐标
new_coords = []
_north = [element[0], element[1]-1]
_east = [element[0] + 1, element[1]]
_south = [element[0], element[1]+1]
_west = [element[0] - 1, element[1]]
if !array_contains(list_of_tiles,_north)
{array_push(new_coords,_north)}
if !array_contains(list_of_tiles,_east)
{array_push(new_coords,_east)}
if !array_contains(list_of_tiles,_south)
{array_push(new_coords,_south)}
if !array_contains(list_of_tiles,_west)
{array_push(new_coords,_west)}
list_of_tiles = array_concat(list_of_tiles, new_coords)
}
array_foreach(list_of_tiles,propogate_tiles)
// 这个过程重复 R 次。之后,我们会得到绘制的所有瓷砖的列表。
list_of_tiles = array_unique(list_of_tiles)
// 我自己尝试使用一个巨大的 if !(array_contains) 等等,但这并没有工作。
}


draw_tiles = function(coord)
{
// 获取一个 [x,y] 坐标数组并绘制一个相对于绝对坐标的瓷砖。
_ddx = _dx + (coord[0] * global.tile_size)
_ddy = _dy+ (coord[1] * global.tile_size)

draw_sprite_ext(Spr_floor_tile, 0, _ddx, _ddy, 0.9, 0.9, 0, c_maroon, 0.1)
}

show_debug_message($"show_manhattan_range: {list_of_tiles}")

list_of_tiles = array_unique(list_of_tiles)
array_foreach(list_of_tiles, draw_tiles)

show_debug_message($"show_manhattan_range: {list_of_tiles}")
} // end