Files
heros/assets/script/game/hero/MonMove.ts
walkpan 2a309a14d0 feat(渲染): 实现基于线路和生成顺序的层级管理系统
添加IndexSet枚举定义基础层级和增量
修改怪物生成逻辑以支持线路(lane)和生成顺序(spawnOrder)
重构MonMoveSystem中的渲染层级更新逻辑
优化HeroViewComp中血条显示逻辑
调整怪物位置配置以支持双线路布局
2025-11-03 06:38:06 +08:00

194 lines
7.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { HeroViewComp } from "./HeroViewComp";
import { HeroAttrsComp } from "./HeroAttrsComp";
import { smc } from "../common/SingletonModuleComp";
import { FacSet, IndexSet } from "../common/config/GameSet";
import { Attrs } from "../common/config/HeroAttrs";
/** 怪物移动组件 */
@ecs.register('MonMove')
export class MonMoveComp extends ecs.Comp {
/** 移动方向1向右-1向左 */
direction: number = 1;
/** 目标x坐标 */
targetX: number = 0;
/** 是否处于移动状态 */
moving: boolean = true;
/** 线路标识0=一线(y=120)1=二线(y=80) */
lane: number = 0;
/** 生成顺序:用于同线路内的层级排序,数值越大越晚生成,层级越前 */
spawnOrder: number = 0;
reset() {
this.direction = 1;
this.targetX = 0;
this.moving = true;
this.lane = 0;
this.spawnOrder = 0;
}
}
/** 怪物移动系统 - 专门处理怪物的移动逻辑 */
@ecs.register('MonMoveSystem')
export class MonMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
filter(): ecs.IMatcher {
return ecs.allOf(MonMoveComp, HeroViewComp, HeroAttrsComp);
}
update(e: ecs.Entity) {
if (!smc.mission.play || smc.mission.pause) return;
const move = e.get(MonMoveComp);
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
// 只处理怪物
if (model.fac !== FacSet.MON) return;
if (!move.moving) return;
const shouldStop = this.checkEnemiesInFace(e);
model.is_atking = this.checkEnemiesInRange(e, model.Attrs[Attrs.DIS]);
// 更新渲染层级
this.updateRenderOrder(e);
if (!shouldStop) {
if (model.is_stop || model.is_dead || model.isStun() || model.isFrost()) {
view.status_change("idle");
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 newX = view.node.position.x + delta;
// 限制移动范围
if (this.validatePosition(newX, move)) {
view.status_change("move");
view.node.setPosition(newX, view.node.position.y, 0);
} else {
// 当达到目标位置边界时也切换为idle状态
view.status_change("idle");
// 达到边界是永久停止设置moving为false
move.moving = false;
}
} else {
view.status_change("idle");
// 因为敌人在面前而暂时停止不设置moving为false保持检查状态
}
}
/** 验证目标位置有效性 */
private validatePosition(newX: number, move: MonMoveComp): boolean {
// 我方不能超过右边界,敌方不能超过左边界
return move.direction === 1 ?
newX <= move.targetX :
newX >= move.targetX;
}
/** 检测攻击范围内敌人 */
private checkEnemiesInRange(entity: ecs.Entity, range: number): boolean {
const currentView = entity.get(HeroViewComp);
if (!currentView || !currentView.node) return false;
const currentPos = currentView.node.position;
const team = entity.get(HeroAttrsComp).fac;
let found = false;
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).some(e => {
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (!view || !view.node) return false;
const distance = Math.abs(currentPos.x - view.node.position.x);
if (model.fac !== team && !model.is_dead) {
if (distance <= range) {
found = true;
return true;
}
}
});
return found;
}
/** 检测面前是否有敌人 */
private checkEnemiesInFace(entity: ecs.Entity): boolean {
const currentView = entity.get(HeroViewComp);
if (!currentView || !currentView.node) return false;
const currentPos = currentView.node.position;
const team = entity.get(HeroAttrsComp).fac;
let found = false;
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).some(e => {
const model = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (!view || !view.node) return false;
const distance = Math.abs(currentPos.x - view.node.position.x);
if (model.fac !== team && !model.is_dead) {
if (distance <= 75) {
found = true;
return true;
}
}
});
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);
});
}
}