mirror of
https://gitee.com/freeyz/godot-mota.git
synced 2024-12-23 07:29:23 +08:00
62 lines
1.7 KiB
GDScript
62 lines
1.7 KiB
GDScript
tool
|
|
extends TileMap
|
|
|
|
class_name NavigationTileMap
|
|
|
|
var astar : AStar2D = AStar2D.new()
|
|
|
|
# 遮挡物
|
|
export(NodePath) var collsion_tilemap_path : NodePath
|
|
|
|
# 初始化tilemap配置
|
|
func _init():
|
|
self.tile_set = load("res://images/mota_tileset.tres")
|
|
self.cell_size = Vector2(32,32)
|
|
|
|
# 初始化node和edge
|
|
func _ready():
|
|
var cells := get_cells()
|
|
init_nodes(cells)
|
|
init_edges(cells)
|
|
|
|
# 初始化nodes
|
|
func init_nodes(cells:Array):
|
|
for index in range(cells.size()):
|
|
var cell_position = cells[index] * cell_size
|
|
cell_position.x += 16
|
|
cell_position.y += 16
|
|
astar.add_point(index, cell_position)
|
|
|
|
# 初始化edge
|
|
func init_edges(cells:Array):
|
|
for index in range(cells.size()):
|
|
var cell = cells[index]
|
|
#left
|
|
var left_cell_index = cells.find(Vector2(cell.x - 1,cell.y))
|
|
if left_cell_index != -1:
|
|
astar.connect_points(index,left_cell_index)
|
|
#right
|
|
var right_cell_index = cells.find(Vector2(cell.x + 1,cell.y))
|
|
if right_cell_index != -1:
|
|
astar.connect_points(index,right_cell_index)
|
|
#up
|
|
var up_cell_index = cells.find(Vector2(cell.x,cell.y - 1))
|
|
if up_cell_index != -1:
|
|
astar.connect_points(index,up_cell_index)
|
|
#down
|
|
var down_cell_index = cells.find(Vector2(cell.x,cell.y + 1))
|
|
if down_cell_index != -1:
|
|
astar.connect_points(index,down_cell_index)
|
|
|
|
# 返回显示的道路需要去除被遮挡的道路
|
|
func get_cells() -> Array:
|
|
var cells = get_used_cells()
|
|
if not collsion_tilemap_path.is_empty():
|
|
var collsion_tilemap = get_node(collsion_tilemap_path) as TileMap
|
|
var collsion_cells = collsion_tilemap.get_used_cells()
|
|
for collsion_cell in collsion_cells:
|
|
var index = cells.find(collsion_cell)
|
|
if index != -1:
|
|
cells.remove(index)
|
|
return cells
|