refactor(渲染): 重构实体层级管理方式

- 移除通过 setSiblingIndex 手动设置层级的方式
- 新增 HERO、LINE1、LINE2、SKILL 等容器节点自动管理层级
- 调整英雄、怪物、技能等实体的父节点到对应容器
- 优化提示信息的位置偏移量
This commit is contained in:
2025-11-04 14:23:07 +08:00
parent 1e0537b63d
commit 087f4010be
7 changed files with 419 additions and 59 deletions

View File

@@ -46,15 +46,14 @@ export class Hero extends ecs.Entity {
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
var scene = smc.map.MapView.scene;
node.parent = scene.entityLayer!.node!
node.parent = scene.entityLayer!.node!.getChildByName("HERO")!;
const collider = node.getComponent(BoxCollider2D);
if (collider) collider.enabled = false; // 先禁用
node.setScale(size*node.scale.x,size*node.scale.y);
node.setPosition(pos)
// 🔥 设置初始 SiblingIndex - 英雄基础层级 + 位置偏移
const initialSiblingIndex = IndexSet.HERO;
node.setSiblingIndex(initialSiblingIndex);
console.log("hero",node.getSiblingIndex());
// console.log("hero load",pos)

View File

@@ -293,10 +293,10 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
});
// 设置渲染顺序x坐标越大的显示在上层index越大层级越高
sortedUnits.forEach((unit, index) => {
const model = unit.get(HeroViewComp);
model.node.setSiblingIndex(index); // 直接使用indexx坐标大的index大层级高
});
// sortedUnits.forEach((unit, index) => {
// const model = unit.get(HeroViewComp);
// model.node.setSiblingIndex(index); // 直接使用indexx坐标大的index大层级高
// });
}
/** 检查指定位置是否已被占用 */

View File

@@ -99,7 +99,7 @@ export class HeroViewComp extends CCComp {
/** 初始化 UI 节点引用 */
private initUINodes() {
this.top_node = this.node.getChildByName("top");
let hp_y = this.node.getComponent(UITransform).height;
let hp_y = this.node.getComponent(UITransform).height+10;
this.top_node.setPosition(0, hp_y, 0);
}
@@ -245,7 +245,7 @@ export class HeroViewComp extends CCComp {
}
/** 技能提示 */
private tooltip(type: number = 1, value: string = "", s_uuid: number = 1001, y: number = 90) {
private tooltip(type: number = 1, value: string = "", s_uuid: number = 1001, y: number = 120) {
let tip = ecs.getEntity<Tooltip>(Tooltip);
let pos = v3(0, 60);
pos.y = pos.y + y;
@@ -253,7 +253,7 @@ export class HeroViewComp extends CCComp {
}
/** 血量提示(伤害数字) */
private hp_tip(type: number = 1, value: string = "", s_uuid: number = 1001, y: number = 100) {
private hp_tip(type: number = 1, value: string = "", s_uuid: number = 1001, y: number = 120) {
let tip = ecs.getEntity<Tooltip>(Tooltip);
let x = this.node.position.x;
let ny = this.node.getComponent(UITransform).height + y;

View File

@@ -42,20 +42,15 @@ export class Monster extends ecs.Entity {
var path = "game/heros/"+HeroInfo[uuid].path;
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
node.parent = scene.entityLayer!.node!
let LINE1=scene.entityLayer!.node!.getChildByName("LINE1")!;
let LINE2=scene.entityLayer!.node!.getChildByName("LINE2")!;
// 🔥 设置初始 SiblingIndex - 防止溢出
const baseLane = lane === 0 ? LINE1 : LINE2;
node.parent = baseLane
const collider = node.getComponent(BoxCollider2D);
if (collider) collider.enabled = false; // 先禁用 // 延迟一帧启用碰撞体
node.setScale(size*node.scale.x,size*node.scale.y);
node.setPosition(pos)
// 🔥 设置初始 SiblingIndex - 防止溢出
const baseLane = lane === 0 ? IndexSet.MON1 : IndexSet.MON2;
// 使用模运算防止索引溢出,确保在安全范围内循环
const safeSpawnOrder = spawnOrder % 999; // 限制在999以内避免溢出到其他层级
const initialSiblingIndex = baseLane + safeSpawnOrder;
node.setSiblingIndex(initialSiblingIndex);
console.log("mon",node.getSiblingIndex());
var view = node.getComponent(HeroViewComp)!;
const model = this.get(HeroAttrsComp);
const skillsComp = this.get(HeroSkillsComp);
@@ -71,11 +66,10 @@ export class Monster extends ecs.Entity {
model.type = hero.type;
model.fac = FacSet.MON;
model.is_boss = monType == MonType.BOSS;
if(model.is_boss) node.setSiblingIndex(IndexSet.BOSS);
if(model.is_boss) node.parent = scene.entityLayer!.node!.getChildByName("HERO")!;
if(!model.is_boss){
model.is_kalami = true;
}
// 根据等级和类型获取怪物属性
const {hp, mp, ap, map, def, mdef} = getMonAttr(lv, uuid, monType);
// 初始化属性数组