feat(渲染): 重构渲染层级管理并添加BOSS层级

移除动态渲染层级更新逻辑,改为在实体加载时设置固定层级
- 在Hero和Monster的load方法中设置初始siblingIndex
- 添加BOSS枚举值到IndexSet
- 为Monster添加溢出保护,防止层级索引过大
- 删除墓地位置判断等不再需要的逻辑
This commit is contained in:
2025-11-03 13:36:33 +08:00
parent 96952ef493
commit 9fcb6d3121
6 changed files with 20 additions and 71 deletions

View File

@@ -140,7 +140,8 @@ export enum IndexSet {
MON2 = 3000, MON2 = 3000,
/** 每个怪物的层级增量,确保后生成的在前面 */ /** 每个怪物的层级增量,确保后生成的在前面 */
MON_INCREMENT = 1, MON_INCREMENT = 1,
SLILL=4000 SLILL=4000,
BOSS=2100,
} }
export const TooltipTypes = { export const TooltipTypes = {
life:1, life:1,

View File

@@ -4,7 +4,7 @@ import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ec
import { smc } from "../common/SingletonModuleComp"; import { smc } from "../common/SingletonModuleComp";
import { HeroAttrsComp } from "./HeroAttrsComp"; import { HeroAttrsComp } from "./HeroAttrsComp";
import { HeroViewComp } from "./HeroViewComp"; import { HeroViewComp } from "./HeroViewComp";
import { BoxSet, FacSet } from "../common/config/GameSet"; import { BoxSet, FacSet, IndexSet } from "../common/config/GameSet";
import { HeroInfo, HeroPos, HType } from "../common/config/heroSet"; import { HeroInfo, HeroPos, HType } from "../common/config/heroSet";
import { GameEvent } from "../common/config/GameEvent"; import { GameEvent } from "../common/config/GameEvent";
import { SkillSet } from "../common/config/SkillSet"; import { SkillSet } from "../common/config/SkillSet";
@@ -50,6 +50,11 @@ export class Hero extends ecs.Entity {
const collider = node.getComponent(BoxCollider2D); const collider = node.getComponent(BoxCollider2D);
if (collider) collider.enabled = false; // 先禁用 if (collider) collider.enabled = false; // 先禁用
node.setPosition(pos) node.setPosition(pos)
// 🔥 设置初始 SiblingIndex - 英雄基础层级 + 位置偏移
const initialSiblingIndex = IndexSet.HERO;
node.setSiblingIndex(initialSiblingIndex);
// console.log("hero load",pos) // console.log("hero load",pos)
var hv = node.getComponent(HeroViewComp)!; var hv = node.getComponent(HeroViewComp)!;
const model = this.get(HeroAttrsComp); const model = this.get(HeroAttrsComp);

View File

@@ -56,11 +56,7 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
return; return;
} }
// 新增墓地位置判断,如果已经在墓地则不再移动
if (view.node.position.x === -1000 || view.node.position.x === 1000) {
view.status_change("idle");
return;
}
// 英雄阵营特殊逻辑:根据职业区分行为 // 英雄阵营特殊逻辑:根据职业区分行为
const hasEnemies = this.checkEnemiesExist(e); const hasEnemies = this.checkEnemiesExist(e);

View File

@@ -2,7 +2,7 @@ import { instantiate, Node, Prefab, Vec3 ,v3,resources,SpriteFrame,Sprite,Sprite
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops"; import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS"; import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { smc } from "../common/SingletonModuleComp"; import { smc } from "../common/SingletonModuleComp";
import { BoxSet, FacSet } from "../common/config/GameSet"; import { BoxSet, FacSet, IndexSet } from "../common/config/GameSet";
import { HeroInfo } from "../common/config/heroSet"; import { HeroInfo } from "../common/config/heroSet";
import { HeroAttrsComp } from "./HeroAttrsComp"; import { HeroAttrsComp } from "./HeroAttrsComp";
import { BuffConf, SkillSet } from "../common/config/SkillSet"; import { BuffConf, SkillSet } from "../common/config/SkillSet";
@@ -47,6 +47,12 @@ export class Monster extends ecs.Entity {
if (collider) collider.enabled = false; // 先禁用 // 延迟一帧启用碰撞体 if (collider) collider.enabled = false; // 先禁用 // 延迟一帧启用碰撞体
node.setPosition(pos) node.setPosition(pos)
// 🔥 设置初始 SiblingIndex - 防止溢出
const baseLane = lane === 0 ? IndexSet.MON1 : IndexSet.MON2;
// 使用模运算防止索引溢出,确保在安全范围内循环
const safeSpawnOrder = spawnOrder % 999; // 限制在999以内避免溢出到其他层级
const initialSiblingIndex = baseLane + safeSpawnOrder;
node.setSiblingIndex(initialSiblingIndex);
var view = node.getComponent(HeroViewComp)!; var view = node.getComponent(HeroViewComp)!;
const model = this.get(HeroAttrsComp); const model = this.get(HeroAttrsComp);

View File

@@ -49,8 +49,7 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
const shouldStop = this.checkEnemiesInFace(e); const shouldStop = this.checkEnemiesInFace(e);
model.is_atking = this.checkEnemiesInRange(e, model.Attrs[Attrs.DIS]); model.is_atking = this.checkEnemiesInRange(e, model.Attrs[Attrs.DIS]);
// 更新渲染层级 // 🔥 移除渲染层级更新:各线路固定,后召唤的天然层级更高,无需动态调整
this.updateRenderOrder(e);
if (!shouldStop) { if (!shouldStop) {
if (model.is_stop || model.is_dead || model.isStun() || model.isFrost()) { if (model.is_stop || model.is_dead || model.isStun() || model.isFrost()) {
@@ -58,11 +57,7 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
return; return;
} }
// 新增墓地位置判断,如果已经在墓地则不再移动
if (view.node.position.x === -1000 || view.node.position.x === 1000) {
view.status_change("idle");
return;
}
// 怪物简单移动逻辑:向目标方向移动 // 怪物简单移动逻辑:向目标方向移动
const delta = (model.Attrs[Attrs.SPEED]/3) * this.dt * move.direction; const delta = (model.Attrs[Attrs.SPEED]/3) * this.dt * move.direction;
@@ -137,58 +132,4 @@ export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpda
}); });
return found; return found;
} }
/** 更新渲染层级 */
private updateRenderOrder(entity: ecs.Entity) {
// 直接调用全局更新方法,避免重复计算
this.updateAllUnitsRenderOrder();
}
/** 更新所有单位的渲染层级 */
private updateAllUnitsRenderOrder() {
// 获取所有单位
const allUnits = ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp));
// 创建单位数组,包含层级信息
const unitsWithOrder: Array<{
entity: ecs.Entity,
view: HeroViewComp,
order: number
}> = [];
allUnits.forEach(e => {
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (!view || !view.node || !model) return;
let order = 0;
if (model.fac === FacSet.HERO) {
// 英雄层级:基础层级 + y轴位置影响
order = IndexSet.HERO + Math.floor(view.node.position.y);
} else if (model.fac === FacSet.MON) {
const move = e.get(MonMoveComp);
if (move) {
// 怪物层级:基于线路和生成顺序
const baseLane = move.lane === 0 ? IndexSet.MON1 : IndexSet.MON2;
order = baseLane + (move.spawnOrder * IndexSet.MON_INCREMENT);
}
}
unitsWithOrder.push({
entity: e,
view: view,
order: order
});
});
// 按层级排序order值小的在后面siblingIndex小order值大的在前面siblingIndex大
unitsWithOrder.sort((a, b) => a.order - b.order);
// 设置 siblingIndex
unitsWithOrder.forEach((unit, index) => {
unit.view.node.setSiblingIndex(index);
});
}
} }

View File

@@ -193,8 +193,8 @@ export class MissionMonCompComp extends CCComp {
// 根据位置判断线路y=110为一线(lane=0)y=80为二线(lane=1) // 根据位置判断线路y=110为一线(lane=0)y=80为二线(lane=1)
const lane = pos.y === 110 ? 0 : 1; const lane = pos.y === 110 ? 0 : 1;
// 递增全局生成顺序 // 递增全局生成顺序 - 🔥 添加溢出保护
this.globalSpawnOrder++; this.globalSpawnOrder = (this.globalSpawnOrder + 1) % 999; // 防止无限增长在999处循环重置
// 生成怪物,传递线路和生成顺序 // 生成怪物,传递线路和生成顺序
mon.load(pos, scale, uuid, lv, monType, buffs, false, lane, this.globalSpawnOrder); mon.load(pos, scale, uuid, lv, monType, buffs, false, lane, this.globalSpawnOrder);