65 lines
1.6 KiB
GDScript
65 lines
1.6 KiB
GDScript
# Level.gd
|
|
extends Node2D
|
|
|
|
# 定义一个信号来通知血迹精灵更新
|
|
signal mask_tiles_updated
|
|
|
|
@onready var canvas_group = $Tile_Group
|
|
|
|
var tilemaplayers: Array[TileMapLayer] = []
|
|
var mask_tiles: Array[Vector2i] = []
|
|
|
|
func _ready():
|
|
# 获取所有的 TileMapLayer
|
|
collect_tilemaplayers()
|
|
update_mask_tiles()
|
|
# 为每个 TileMapLayer 订阅变化信号
|
|
for layer in tilemaplayers:
|
|
layer.changed.connect(_on_tilemap_changed)
|
|
|
|
|
|
#region 收集所有的 TileMapLayer
|
|
func collect_tilemaplayers() -> void:
|
|
tilemaplayers.clear()
|
|
for child in canvas_group.get_children():
|
|
if child is TileMapLayer:
|
|
tilemaplayers.append(child)
|
|
|
|
func update_mask_tiles() -> void:
|
|
mask_tiles.clear()
|
|
|
|
# 添加安全检查
|
|
if not is_instance_valid(canvas_group):
|
|
return
|
|
|
|
# 遍历每个 TileMapLayer
|
|
for layer in tilemaplayers:
|
|
# 添加安全检查
|
|
if not is_instance_valid(layer):
|
|
continue
|
|
|
|
var used_cells = layer.get_used_cells()
|
|
for coords in used_cells:
|
|
if not is_instance_valid(layer): # 再次检查,以防在循环中被释放
|
|
break
|
|
var tile_data = layer.get_cell_tile_data(coords)
|
|
if tile_data and tile_data.get_custom_data("mask_area") == 1:
|
|
if not coords in mask_tiles:
|
|
mask_tiles.append(coords)
|
|
|
|
func _on_tilemap_changed():
|
|
update_mask_tiles()
|
|
# 发送信号通知所有血迹更新
|
|
mask_tiles_updated.emit()
|
|
|
|
func cleanup():
|
|
for layer in tilemaplayers:
|
|
if is_instance_valid(layer) and layer.changed.is_connected(_on_tilemap_changed):
|
|
layer.changed.disconnect(_on_tilemap_changed)
|
|
tilemaplayers.clear()
|
|
mask_tiles.clear()
|
|
|
|
func _exit_tree():
|
|
cleanup()
|
|
#endregion
|