Question
This program is long, but the problem lies in the changeLayer() function. I’m trying to set the player’s layer so that objects below the player are rendered after the player, and objects above the player are rendered before the player. However, the player keeps jumping between being above and below other objects, even though I’m not moving it.
extends Node2D
@onready var 高度层: TileMapLayer = $"input/高度层"
@onready var 结构层: TileMapLayer = $"input/结构层"
@onready var 细节层: TileMapLayer = $"input/细节层"
@export var player: CharacterBody2D
@export var 水格选项 = [
{"source":3, "pos":Vector2i(11,13), "alt":0},
{"source":3, "pos":Vector2i(12,13), "alt":0},
{"source":3, "pos":Vector2i(13,13), "alt":0},
{"source":3, "pos":Vector2i(12,14), "alt":0},
{"source":3, "pos":Vector2i(13,14), "alt":0}
]
@export var 草格选项 = [
{"source":3, "pos":Vector2i(0,1), "alt":0},
{"source":3, "pos":Vector2i(0,2), "alt":0},
{"source":3, "pos":Vector2i(0,3), "alt":0},
{"source":3, "pos":Vector2i(0,4), "alt":0},
{"source":3, "pos":Vector2i(0,5), "alt":0},
]
@export var 花格选项 = [
{"source":3, "pos":Vector2i(0,6), "alt":0},
{"source":3, "pos":Vector2i(0,7), "alt":0},
{"source":3, "pos":Vector2i(1,6), "alt":0},
{"source":3, "pos":Vector2i(1,7), "alt":0},
{"source":3, "pos":Vector2i(2,6), "alt":0},
{"source":3, "pos":Vector2i(2,7), "alt":0},
{"source":3, "pos":Vector2i(3,6), "alt":0},
{"source":3, "pos":Vector2i(3,7), "alt":0},
]
@export var 路格选项 = [
{"source":3, "pos":Vector2i(4,0), "alt":0},
{"source":3, "pos":Vector2i(4,1), "alt":0},
{"source":3, "pos":Vector2i(4,2), "alt":0},
{"source":3, "pos":Vector2i(4,3), "alt":0},
{"source":3, "pos":Vector2i(4,4), "alt":0},
]
@export var 石格选项 = [
{"source":3, "pos":Vector2i(4,6), "alt":0},
{"source":3, "pos":Vector2i(4,7), "alt":0},
{"source":3, "pos":Vector2i(4,8), "alt":0},
{"source":3, "pos":Vector2i(4,9), "alt":0},
]
var fx4 = [0, -1, 1, 0] # 上 左 右 下
var fy4 = [-1, 0, 0, 1]
var fx9 = [-1, 0, 1, -1, 0, 1, -1, 0, 1] # 3x3
var fy9 = [-1, -1, -1, 0, 0, 0, 1, 1, 1]
var 地层数量 : int = 0;
var 已处理: Dictionary = {}
var children
func _ready() -> void:
var 水层 = TileMapLayer.new();
水层.name = "水层"
var tileset = preload("res://TileSet/simile.tres")
水层.tile_set = tileset
$"output".add_child(水层)
var 山层 = TileMapLayer.new();
山层.name = "山层"
山层.tile_set = tileset
$"output".add_child(山层)
for cell in 高度层.get_used_cells():
var 随机格子 = 水格选项[randi() % 水格选项.size()]
水层.set_cell(cell, 随机格子.source, 随机格子.pos, 随机格子.alt)
for cell in 高度层.get_used_cells():
if 已处理.has(cell):
continue
var source = 高度层.get_cell_source_id(cell)
var atlas = 高度层.get_cell_atlas_coords(cell)
var alt = 高度层.get_cell_alternative_tile(cell)
if source == 2 and atlas.x >= 0 and atlas.x <= 3 and atlas.y == 0:
var 地层 = TileMapLayer.new();
地层.tile_set = tileset
地层数量 = 地层数量 + 1
地层.name = "地层"+str(cell.y);
$"output".add_child(地层)
var 同层: Array = get_connected_cells(cell,高度层)
for cellhigh in 同层:
已处理[cellhigh] = true
var 周围: Array = []
周围.resize(9)
for i in range(9):
var neighbor_pos = cellhigh + Vector2i(fx9[i], fy9[i])
var neighbor_source = 高度层.get_cell_source_id(neighbor_pos)
var neighbor_alt = 高度层.get_cell_alternative_tile(neighbor_pos)
var neighbor_atlas = 高度层.get_cell_atlas_coords(neighbor_pos)
周围[i] = {"pos": neighbor_pos, "source": neighbor_source, "atlas": neighbor_atlas, "alt": neighbor_alt}
# 条件判断
var 中 = 周围[4]
var 下 = 周围[7]
var 可能: Array=[]
for i in range(9):
if 周围[i].source==中.source and 周围[i].atlas==中.atlas and 周围[i].alt==中.alt:
可能.append(1);
else:
可能.append(0);
if 下.source == 2 and 下.atlas == Vector2i(0,1) and 下.alt == 0:
for i in range(中.atlas.x * 2 + 1):
if i>=0 and i<中.atlas.x * 2 + 1-2:
if 可能[3]==1 and 可能[5]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(8,5),0)
elif 可能[0]==1 and 可能[5]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(6,5),0)
elif 可能[2]==1 and 可能[3]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(9,5),0)
elif 可能[2]==0 and 可能[5]==0:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(10,4),0)
for shan in range(中.atlas.x * 2 + 1):
if 高度层.get_cell_source_id(中.pos+Vector2i(0,i+1)+Vector2i(1,-shan-1))==2 and 高度层.get_cell_atlas_coords(中.pos+Vector2i(0,i+1)+Vector2i(1,-shan-1)).y==0 and 高度层.get_cell_atlas_coords(中.pos+Vector2i(0,i+1)+Vector2i(1,-shan-1)).x==中.atlas.x:
山层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(13,15),0)
break
elif 可能[0]==0 and 可能[3]==0:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(5,4),0)
for shan in range(中.atlas.x * 2 + 1):
if 高度层.get_cell_source_id(中.pos+Vector2i(0,i+1)+Vector2i(-1,-shan-1))==2 and 高度层.get_cell_atlas_coords(中.pos+Vector2i(0,i+1)+Vector2i(-1,-shan-1)).y==0 and 高度层.get_cell_atlas_coords(中.pos+Vector2i(0,i+1)+Vector2i(-1,-shan-1)).x==中.atlas.x:
山层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(13,15),0)
break
elif i==中.atlas.x * 2 + 1-2:
if 可能[3]==1 and 可能[5]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(8,11),0)
elif 可能[0]==1 and 可能[5]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(6,11),0)
elif 可能[2]==1 and 可能[3]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(9,11),0)
elif 可能[2]==0 and 可能[5]==0:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(10,4),0)
for shan in range(中.atlas.x * 2 + 1):
if 高度层.get_cell_source_id(中.pos+Vector2i(0,i+1)+Vector2i(1,-shan-1))==2 and 高度层.get_cell_atlas_coords(中.pos+Vector2i(0,i+1)+Vector2i(1,-shan-1)).y==0 and 高度层.get_cell_atlas_coords(中.pos+Vector2i(0,i+1)+Vector2i(1,-shan-1)).x==中.atlas.x:
山层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(13,15),0)
break
elif 可能[0]==0 and 可能[3]==0:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(5,4),0)
for shan in range(中.atlas.x * 2 + 1):
if 高度层.get_cell_source_id(中.pos+Vector2i(0,i+1)+Vector2i(-1,-shan-1))==2 and 高度层.get_cell_atlas_coords(中.pos+Vector2i(0,i+1)+Vector2i(-1,-shan-1)).y==0 and 高度层.get_cell_atlas_coords(中.pos+Vector2i(0,i+1)+Vector2i(-1,-shan-1)).x==中.atlas.x:
山层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(13,15),0)
break
else:
if 可能[3]==1 and 可能[5]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(8,12),0)
elif 可能[0]==1 and 可能[5]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(6,12),0)
elif 可能[2]==1 and 可能[3]==1:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(9,12),0)
elif 可能[2]==0 and 可能[5]==0:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(10,11),0)
elif 可能[0]==0 and 可能[3]==0:
地层.set_cell(中.pos+Vector2i(0,i+1), 3, Vector2i(5,11),0)
if 中.atlas.x!=0:
if 可能[3]==1 and 可能[5]==1:
地层.set_cell(中.pos, 3, Vector2i(8,4),0)
elif 可能[0]==1 and 可能[5]==1:
地层.set_cell(中.pos, 3, Vector2i(6,4),0)
elif 可能[2]==1 and 可能[3]==1:
地层.set_cell(中.pos, 3, Vector2i(9,4),0)
elif 可能[2]==0 and 可能[5]==0:
地层.set_cell(中.pos, 3, Vector2i(10,3),0)
elif 可能[0]==0 and 可能[3]==0:
地层.set_cell(中.pos, 3, Vector2i(5,3),0)
else:
if 可能[3]==1 and 可能[5]==1:
地层.set_cell(中.pos, 3, Vector2i(8,14),0)
elif 可能[0]==1 and 可能[5]==1:
地层.set_cell(中.pos, 3, Vector2i(6,14),0)
elif 可能[2]==1 and 可能[3]==1:
地层.set_cell(中.pos, 3, Vector2i(9,14),0)
elif 可能[2]==0 and 可能[5]==0:
地层.set_cell(中.pos, 3, Vector2i(10,13),0)
elif 可能[0]==0 and 可能[3]==0:
地层.set_cell(中.pos, 3, Vector2i(5,13),0)
else:
if 可能[0]==1 and 可能[1]==1 and 可能[2]==1 and 可能[3]==1 and 可能[5]==1 and 可能[6]==0 and 可能[7]==1 and 可能[8]==1:
if 中.atlas.x!=0:
地层.set_cell(中.pos, 3, Vector2i(6,13),0)
else:
地层.set_cell(中.pos, 3, Vector2i(6,3),0)
elif 可能[0]==1 and 可能[1]==1 and 可能[2]==1 and 可能[3]==1 and 可能[5]==1 and 可能[6]==1 and 可能[7]==1 and 可能[8]==0:
if 中.atlas.x!=0:
地层.set_cell(中.pos, 3, Vector2i(9,13),0)
else:
地层.set_cell(中.pos, 3, Vector2i(9,3),0)
elif 可能[1]==1 and 可能[2]==1 and 可能[3]==0 and 可能[5]==1 and 可能[7]==1 and 可能[8]==1:
地层.set_cell(中.pos, 3, Vector2i(5,2),0)
elif 可能[1]==1 and 可能[0]==1 and 可能[3]==1 and 可能[5]==0 and 可能[6]==1 and 可能[7]==1:
地层.set_cell(中.pos, 3, Vector2i(10,2),0)
elif 可能[1]==0 and 可能[3]==1 and 可能[5]==1 and 可能[7]==1 and not 结构层.get_cell_atlas_coords(周围[1].pos)==Vector2i(1,2):
#print(周围[1].atlas)
地层.set_cell(中.pos, 3, Vector2i(7,0),0)
elif 可能[1]==0 and 可能[3]==1 and 可能[5]==1 and 可能[7]==1 and 结构层.get_cell_atlas_coords(周围[1].pos)==Vector2i(1,2):
var 随机格子 = 草格选项[randi() % 草格选项.size()]
地层.set_cell(中.pos, 随机格子.source, 随机格子.pos, 随机格子.alt)
elif 可能[0]==0 and 可能[1]==0 and 可能[2]==0 and 可能[3]==0 and 可能[5]==1 and 可能[6]==1 and 可能[7]==1:
地层.set_cell(中.pos, 3, Vector2i(6,0),0)
elif 可能[0]==0 and 可能[1]==0 and 可能[2]==1 and 可能[3]==0 and 可能[5]==1 and 可能[6]==1 and 可能[7]==1:
地层.set_cell(中.pos, 3, Vector2i(6,0),0)
elif 可能[0]==0 and 可能[1]==0 and 可能[2]==0 and 可能[3]==0 and 可能[5]==1 and 可能[6]==0 and 可能[7]==1 and 可能[8]==1:
地层.set_cell(中.pos, 3, Vector2i(6,0),0)
elif 可能[0]==0 and 可能[1]==0 and 可能[2]==1 and 可能[3]==0 and 可能[5]==1 and 可能[6]==0 and 可能[7]==1:
地层.set_cell(中.pos, 3, Vector2i(5,1),0)
elif 可能[0]==0 and 可能[1]==0 and 可能[2]==0 and 可能[3]==1 and 可能[5]==0 and 可能[8]==1 and 可能[7]==1:
地层.set_cell(中.pos, 3, Vector2i(9,0),0)
elif 可能[0]==1 and 可能[1]==0 and 可能[2]==0 and 可能[3]==1 and 可能[5]==0 and 可能[8]==1 and 可能[7]==1:
地层.set_cell(中.pos, 3, Vector2i(9,0),0)
elif 可能[0]==0 and 可能[1]==0 and 可能[2]==0 and 可能[3]==1 and 可能[5]==0 and 可能[6]==1 and 可能[7]==1 and 可能[8]==0:
地层.set_cell(中.pos, 3, Vector2i(9,0),0)
elif 可能[0]==1 and 可能[1]==0 and 可能[2]==0 and 可能[3]==1 and 可能[5]==0 and 可能[8]==0 and 可能[7]==1:
地层.set_cell(中.pos, 3, Vector2i(10,1),0)
elif 可能[0]==0 and 可能[1]==1 and 可能[2]==1 and 可能[3]==1 and 可能[5]==1 and 可能[6]==1 and 可能[7]==1 and 可能[8]==1:
地层.set_cell(中.pos, 3, Vector2i(6,1),0)
elif 可能[0]==1 and 可能[1]==1 and 可能[2]==0 and 可能[3]==1 and 可能[5]==1 and 可能[6]==1 and 可能[7]==1 and 可能[8]==1:
地层.set_cell(中.pos, 3, Vector2i(9,1),0)
elif 可能[0]==1 and 可能[1]==1 and 可能[2]==1 and 可能[3]==1 and 可能[5]==1 and 可能[6]==1 and 可能[7]==1 and 可能[8]==1:
var 周围结构: Array = []
周围结构.resize(9)
for i in range(9):
var neighbor_pos = 中.pos + Vector2i(fx9[i], fy9[i])
var neighbor_source = 结构层.get_cell_source_id(neighbor_pos)
var neighbor_alt = 结构层.get_cell_alternative_tile(neighbor_pos)
var neighbor_atlas = 结构层.get_cell_atlas_coords(neighbor_pos)
周围结构[i] = {"pos": neighbor_pos, "source": neighbor_source, "atlas": neighbor_atlas, "alt": neighbor_alt}
var 可能结构: Array=[]
for i in range(9):
if 周围结构[i].source==2 and 周围结构[i].atlas==Vector2i(2,1) and 周围结构[i].alt==0:
可能结构.append(0);
else:
可能结构.append(1);
if 可能结构[4] == 1:
var 随机格子 = 草格选项[randi() % 草格选项.size()]
地层.set_cell(中.pos, 随机格子.source, 随机格子.pos, 随机格子.alt)
elif match_pattern(可能结构, [0,0,null,0,0,1,null,1,1]):
地层.set_cell(中.pos, 3, Vector2i(3,2), 0)
elif match_pattern(可能结构, [null,0,0,1,0,0,1,1,null]):
地层.set_cell(中.pos, 3, Vector2i(1,2), 0)
elif match_pattern(可能结构, [null,1,1,0,0,1,0,0,null]):
地层.set_cell(中.pos, 3, Vector2i(3,0), 0)
elif match_pattern(可能结构, [1,1,null,1,0,0,null,0,0]):
地层.set_cell(中.pos, 3, Vector2i(1,0), 0)
elif match_pattern(可能结构, [0,0,0,null,0,null,null,1,null]):
地层.set_cell(中.pos, 3, Vector2i(2,2), 0)
elif match_pattern(可能结构, [null,1,null,null,0,null,0,0,0]):
地层.set_cell(中.pos, 3, Vector2i(2,0), 0)
elif match_pattern(可能结构, [0,null,null,0,0,1,0,null,null]):
地层.set_cell(中.pos, 3, Vector2i(3,1), 0)
elif match_pattern(可能结构, [null,null,0,1,0,0,null,null,0]):
地层.set_cell(中.pos, 3, Vector2i(1,1), 0)
elif match_pattern(可能结构, [1,0,0,0,0,0,0,0,0]):
地层.set_cell(中.pos, 3, Vector2i(2,4), 0)
elif match_pattern(可能结构, [0,0,1,0,0,0,0,0,0]):
地层.set_cell(中.pos, 3, Vector2i(1,4), 0)
elif match_pattern(可能结构, [0,0,0,0,0,0,1,0,0]):
地层.set_cell(中.pos, 3, Vector2i(2,3), 0)
elif match_pattern(可能结构, [0,0,0,0,0,0,0,0,1]):
地层.set_cell(中.pos, 3, Vector2i(1,3), 0)
elif match_pattern(可能结构, [1,0,0,0,0,0,0,0,1]):
地层.set_cell(中.pos, 3, Vector2i(3,3), 0)
elif match_pattern(可能结构, [0,0,1,0,0,0,1,0,0]):
地层.set_cell(中.pos, 3, Vector2i(3,4), 0)
elif match_pattern(可能结构, [0,0,0,0,0,0,0,0,0]):
var 随机格子 = 路格选项[randi() % 路格选项.size()]
地层.set_cell(中.pos, 随机格子.source, 随机格子.pos, 随机格子.alt)
已处理 = {}
for cell in 结构层.get_used_cells():
if 已处理.has(cell):
continue
if 结构层.get_cell_source_id(cell)==2 and 结构层.get_cell_atlas_coords(cell)==Vector2i(1,2) and 结构层.get_cell_alternative_tile(cell)==0:
var 楼梯层 = TileMapLayer.new();
楼梯层.tile_set = tileset
楼梯层.name = "楼梯层"+str(cell.y);
$"output".add_child(楼梯层)
var 同层 : Array = get_connected_cells(cell,结构层)
for cell2 in 同层:
已处理[cell2] = true
var 周围: Array = []
周围.resize(9)
for i in range(9):
var neighbor_pos = cell2 + Vector2i(fx9[i], fy9[i])
var neighbor_source = 结构层.get_cell_source_id(neighbor_pos)
var neighbor_alt = 结构层.get_cell_alternative_tile(neighbor_pos)
var neighbor_atlas = 结构层.get_cell_atlas_coords(neighbor_pos)
周围[i] = {"pos": neighbor_pos, "source": neighbor_source, "atlas": neighbor_atlas, "alt": neighbor_alt}
var 可能结构上楼梯: Array=[]
for i in range(9):
#print(周围[i].pos," ",周围[i].source," ",周围[i].atlas," ",周围[i].alt)
if 周围[i].source==2 and 周围[i].atlas==Vector2i(1,2) and 周围[i].alt==0:
可能结构上楼梯.append(1);
else:
可能结构上楼梯.append(0);
#print(可能结构上楼梯," ",match_pattern(可能结构上楼梯,[0,0,0,1,1,1,1,1,1]))
if match_pattern(可能结构上楼梯,[0,0,0,1,1,1,1,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(3,10), 0)
elif match_pattern(可能结构上楼梯,[0,0,0,0,1,1,0,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(2,10), 0)
elif match_pattern(可能结构上楼梯,[0,0,0,1,1,0,1,1,0]):
楼梯层.set_cell(cell2, 3, Vector2i(4,10), 0)
elif match_pattern(可能结构上楼梯,[1,1,1,1,1,1,1,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(3,11), 0)
elif match_pattern(可能结构上楼梯,[0,1,1,0,1,1,0,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(2,11), 0)
elif match_pattern(可能结构上楼梯,[1,1,0,1,1,0,1,1,0]):
楼梯层.set_cell(cell2, 3, Vector2i(4,11), 0)
elif match_pattern(可能结构上楼梯,[1,1,1,1,1,1,0,0,0]):
楼梯层.set_cell(cell2, 3, Vector2i(3,12), 0)
elif match_pattern(可能结构上楼梯,[0,1,1,0,1,1,0,0,0]):
楼梯层.set_cell(cell2, 3, Vector2i(2,12), 0)
elif match_pattern(可能结构上楼梯,[1,1,0,1,1,0,0,0,0]):
楼梯层.set_cell(cell2, 3, Vector2i(4,12), 0)
elif 结构层.get_cell_source_id(cell)==2 and 结构层.get_cell_atlas_coords(cell)==Vector2i(3,1) and 结构层.get_cell_alternative_tile(cell)==0:
var 楼梯层 = TileMapLayer.new();
楼梯层.tile_set = tileset
楼梯层.name = "楼梯层"+str(cell.y);
$"output".add_child(楼梯层)
var 同层 : Array = get_connected_cells(cell,结构层)
for cell2 in 同层:
已处理[cell2] = true
var 周围: Array = []
周围.resize(9)
for i in range(9):
var neighbor_pos = cell2 + Vector2i(fx9[i], fy9[i])
var neighbor_source = 结构层.get_cell_source_id(neighbor_pos)
var neighbor_alt = 结构层.get_cell_alternative_tile(neighbor_pos)
var neighbor_atlas = 结构层.get_cell_atlas_coords(neighbor_pos)
周围[i] = {"pos": neighbor_pos, "source": neighbor_source, "atlas": neighbor_atlas, "alt": neighbor_alt}
var 可能结构上楼梯: Array=[]
for i in range(9):
print(周围[i].pos," ",周围[i].source," ",周围[i].atlas," ",周围[i].alt)
if 周围[i].source==2 and 周围[i].atlas==Vector2i(3,1) and 周围[i].alt==0:
可能结构上楼梯.append(1);
else:
可能结构上楼梯.append(0);
if match_pattern(可能结构上楼梯,[0,0,0,0,1,0,0,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(0,11), 0)
elif match_pattern(可能结构上楼梯,[0,1,0,0,1,1,0,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(0,12), 0)
elif match_pattern(可能结构上楼梯,[0,1,1,0,1,1,0,0,1]):
楼梯层.set_cell(cell2, 3, Vector2i(0,13), 0)
elif match_pattern(可能结构上楼梯,[1,0,0,1,1,0,1,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(1,12), 0)
elif match_pattern(可能结构上楼梯,[1,1,0,1,1,1,0,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(1,13), 0)
elif match_pattern(可能结构上楼梯,[1,1,1,0,1,1,0,0,1]):
楼梯层.set_cell(cell2, 3, Vector2i(1,14), 0)
楼梯层.set_cell(cell2+Vector2i(0,1), 3, Vector2i(15,10), 0)
elif match_pattern(可能结构上楼梯,[1,0,0,1,1,0,1,1,0]):
楼梯层.set_cell(cell2, 3, Vector2i(2,13), 0)
elif match_pattern(可能结构上楼梯,[1,1,0,1,1,0,0,1,0]):
楼梯层.set_cell(cell2, 3, Vector2i(2,14), 0)
elif match_pattern(可能结构上楼梯,[1,1,0,0,1,0,0,0,0]):
楼梯层.set_cell(cell2, 3, Vector2i(2,15), 0)
elif 结构层.get_cell_source_id(cell)==2 and 结构层.get_cell_atlas_coords(cell)==Vector2i(0,2) and 结构层.get_cell_alternative_tile(cell)==0:
var 楼梯层 = TileMapLayer.new();
楼梯层.tile_set = tileset
楼梯层.name = "楼梯层"+str(cell.y);
$"output".add_child(楼梯层)
var 同层 : Array = get_connected_cells(cell,结构层)
for cell2 in 同层:
已处理[cell2] = true
var 周围: Array = []
周围.resize(9)
for i in range(9):
var neighbor_pos = cell2 + Vector2i(fx9[i], fy9[i])
var neighbor_source = 结构层.get_cell_source_id(neighbor_pos)
var neighbor_alt = 结构层.get_cell_alternative_tile(neighbor_pos)
var neighbor_atlas = 结构层.get_cell_atlas_coords(neighbor_pos)
周围[i] = {"pos": neighbor_pos, "source": neighbor_source, "atlas": neighbor_atlas, "alt": neighbor_alt}
var 可能结构上楼梯: Array=[]
for i in range(9):
print(周围[i].pos," ",周围[i].source," ",周围[i].atlas," ",周围[i].alt)
if 周围[i].source==2 and 周围[i].atlas==Vector2i(0,2) and 周围[i].alt==0:
可能结构上楼梯.append(1);
else:
可能结构上楼梯.append(0);
if match_pattern(可能结构上楼梯,[0,0,0,0,1,0,1,1,0]):
楼梯层.set_cell(cell2, 3, Vector2i(0,11), 1)
elif match_pattern(可能结构上楼梯,[0,1,0,1,1,0,1,1,0]):
楼梯层.set_cell(cell2, 3, Vector2i(0,12), 1)
elif match_pattern(可能结构上楼梯,[1,1,0,1,1,0,1,0,0]):
楼梯层.set_cell(cell2, 3, Vector2i(0,13), 1)
elif match_pattern(可能结构上楼梯,[0,0,1,0,1,1,1,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(1,12), 1)
elif match_pattern(可能结构上楼梯,[0,1,1,1,1,1,1,1,0]):
楼梯层.set_cell(cell2, 3, Vector2i(1,13), 1)
elif match_pattern(可能结构上楼梯,[1,1,1,1,1,0,1,0,0]):
楼梯层.set_cell(cell2, 3, Vector2i(1,14), 1)
楼梯层.set_cell(cell2+Vector2i(0,1), 3, Vector2i(15,10), 1)
elif match_pattern(可能结构上楼梯,[0,0,1,0,1,1,0,1,1]):
楼梯层.set_cell(cell2, 3, Vector2i(2,13), 1)
elif match_pattern(可能结构上楼梯,[0,1,1,0,1,1,0,1,0]):
楼梯层.set_cell(cell2, 3, Vector2i(2,14), 1)
elif match_pattern(可能结构上楼梯,[0,1,1,0,1,0,0,0,0]):
楼梯层.set_cell(cell2, 3, Vector2i(2,15), 1)
var 花石层 = TileMapLayer.new();
花石层.tile_set = tileset
花石层.name = "花石层";
$"output".add_child(花石层)
for cell in 细节层.get_used_cells():
if 细节层.get_cell_source_id(cell)==2 and 细节层.get_cell_atlas_coords(cell)==Vector2i(3,2) and 细节层.get_cell_alternative_tile(cell)==0:
var 随机格子 = 花格选项[randi() % 花格选项.size()]
花石层.set_cell(cell, 随机格子.source, 随机格子.pos, 随机格子.alt)
if 细节层.get_cell_source_id(cell)==2 and 细节层.get_cell_atlas_coords(cell)==Vector2i(1,3) and 细节层.get_cell_alternative_tile(cell)==0:
var 随机格子 = 石格选项[randi() % 石格选项.size()]
花石层.set_cell(cell, 随机格子.source, 随机格子.pos, 随机格子.alt)
if 细节层.get_cell_source_id(cell)==2 and 细节层.get_cell_atlas_coords(cell)==Vector2i(0,3) and 细节层.get_cell_alternative_tile(cell)==0:
var 丛层 = TileMapLayer.new();
丛层.tile_set = tileset
丛层.name = "丛层("+str(cell.x*16)+","+str(cell.y*16+4)+")";
$"output".add_child(丛层)
丛层.set_cell(cell, 3, Vector2i(1,9), 0)
丛层.set_cell(cell+Vector2i(0,-1), 3, Vector2i(1,8), 0)
if 细节层.get_cell_source_id(cell)==2 and 细节层.get_cell_atlas_coords(cell)==Vector2i(2,3) and 细节层.get_cell_alternative_tile(cell)==0:
var 树层 = TileMapLayer.new();
树层.tile_set = tileset
树层.name = "树层("+str(cell.x*16)+","+str(cell.y*16-8)+")";
$"output".add_child(树层)
for i in range(11, 16): # 11 ~ 15
for j in range(0, 7): # 0 ~ 6
树层.set_cell(cell + Vector2i(i-13, j-6), 3, Vector2i(i, j), 0)
var parent := $"output"
children = parent.get_children()
# 收集需要排序的“地层X”节点
var layer_nodes: Array[Node] = []
for c in children:
if c.name.begins_with("地层"):
var suffix :String = c.name.substr(2)
if suffix.is_valid_int():
layer_nodes.append(c)
# 按数字排序
layer_nodes.sort_custom(func(a, b):
return int(a.name.substr(2)) < int(b.name.substr(2))
)
# 按原始索引把排序后的节点放回去
var indices := []
for i in range(children.size()):
if children[i] in layer_nodes:
indices.append(i)
for i in range(indices.size()):
parent.move_child(layer_nodes[i], indices[i])
parent = $"output"
children = parent.get_children()
layer_nodes = []
for c in children:
if c.name.begins_with("丛层")||c.name.begins_with("树层"):
layer_nodes.append(c)
# 按 y 排序
layer_nodes.sort_custom(func(a, b):
return get_cong_layer_y(a.name) < get_cong_layer_y(b.name)
)
# 保持原有层级位置,只调整相对顺序
indices = []
for i in range(children.size()):
if children[i] in layer_nodes:
indices.append(i)
for i in range(indices.size()):
parent.move_child(layer_nodes[i], indices[i])
children = parent.get_children()
changeLayer()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func changeLayer() -> void:
var parent = $"output"
var player_y = player.transform.origin.y
var target_index = children.size() # 默认在最上层
parent.move_child(player, 0)
children=parent.get_children()
for i in range(children.size()):
var c = children[i]
if c.name.begins_with("树层") or c.name.begins_with("丛层"):
if abs(player_y-get_cong_layer_y(c.name))<=3 or player_y<get_cong_layer_y(c.name):
target_index = i
print(str(player_y)+" "+str(get_cong_layer_y(c.name))+" "+str(target_index))
break
parent.move_child(player, target_index)
# 返回与 start_cell 上下左右连通且 tile 相同的所有格子
func get_connected_cells(start_cell: Vector2i,层:TileMapLayer) -> Array:
var 同层: Array = [] # 存放结果
var visited: Dictionary = {} # 记录已经访问过的格子
var queue: Array = [start_cell] # BFS 队列
# 获取起始格子 tile 信息
var source = 层.get_cell_source_id(start_cell)
var atlas = 层.get_cell_atlas_coords(start_cell)
var alt = 层.get_cell_alternative_tile(start_cell)
# 上下左右偏移
var fx = [0, -1, 1, 0]
var fy = [-1, 0, 0, 1]
while queue.size() > 0:
var current = queue.pop_front() # BFS 用 pop_front, DFS 用 pop_back
if visited.has(current):
continue
visited[current] = true
# 获取当前格子 tile 信息
var c_source = 层.get_cell_source_id(current)
var c_atlas = 层.get_cell_atlas_coords(current)
var c_alt = 层.get_cell_alternative_tile(current)
if c_source == source and c_atlas == atlas and c_alt == alt:
同层.append(current)
# 遍历上下左右邻居
for i in range(4):
var neighbor = current + Vector2i(fx[i], fy[i])
if not visited.has(neighbor):
queue.append(neighbor)
return 同层
func match_pattern(arr: Array, pattern: Array) -> bool:
if arr == null or pattern == null:
return false
if arr.size() < pattern.size():
return false
for i in range(pattern.size()):
if pattern[i] == null:
continue
if arr[i] != pattern[i]:
return false
return true
func get_cong_layer_y(layer_name: String) -> int:
var l := layer_name.find("(")
var comma := layer_name.find(",")
var r := layer_name.find(")")
if l == -1 or comma == -1 or r == -1:
return (int)(-INF)
# 提取 y 字符串
var y_str := layer_name.substr(comma + 1, r - comma - 1).strip_edges()
# 把 "_" 替换成 "."
y_str = y_str.replace("_", ".")
# 转换为 float
var y_val = y_str.to_float()
return y_val