Files
heros/assets/script/game/hero/Mon.ts
panw 087f4010be refactor(渲染): 重构实体层级管理方式
- 移除通过 setSiblingIndex 手动设置层级的方式
- 新增 HERO、LINE1、LINE2、SKILL 等容器节点自动管理层级
- 调整英雄、怪物、技能等实体的父节点到对应容器
- 优化提示信息的位置偏移量
2025-11-04 14:23:07 +08:00

135 lines
5.2 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 { instantiate, Node, Prefab, Vec3 ,v3,resources,SpriteFrame,Sprite,SpriteAtlas, BoxCollider2D} from "cc";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { smc } from "../common/SingletonModuleComp";
import { BoxSet, FacSet, IndexSet } from "../common/config/GameSet";
import { HeroInfo } from "../common/config/heroSet";
import { HeroAttrsComp } from "./HeroAttrsComp";
import { BuffConf, SkillSet } from "../common/config/SkillSet";
import { getNeAttrs, getAttrs ,Attrs} from "../common/config/HeroAttrs";
import { getMonAttr, MonType } from "../map/RogueConfig";
import { HeroViewComp } from "./HeroViewComp";
import { HeroSkillsComp } from "./HeroSkills";
import { MonMoveComp } from "./MonMove";
/** 角色实体 */
@ecs.register(`Monster`)
export class Monster extends ecs.Entity {
HeroModel!: HeroAttrsComp;
HeroSkills!: HeroSkillsComp;
HeroView!: HeroViewComp;
MonMove!: MonMoveComp;
protected init() {
this.addComponents<ecs.Comp>(
MonMoveComp,
HeroAttrsComp,
HeroSkillsComp,
);
}
destroy(): void {
this.remove(HeroViewComp);
this.remove(HeroAttrsComp);
this.remove(HeroSkillsComp);
super.destroy();
}
/** 加载角色 */
load(pos: Vec3 = Vec3.ZERO,scale:number = 1,uuid:number=1001,lv:number=1,monType:MonType=MonType.NORMAL, buffs: BuffConf[] = [],is_call=false, lane: number = 0, spawnOrder: number = 0) {
scale=-1
let size=1
var scene = smc.map.MapView.scene;
var path = "game/heros/"+HeroInfo[uuid].path;
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
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)
var view = node.getComponent(HeroViewComp)!;
const model = this.get(HeroAttrsComp);
const skillsComp = this.get(HeroSkillsComp);
let hero = HeroInfo[uuid]; // 共用英雄数据
// 设置 View 层属性(表现相关)
view.scale = scale;
view.box_group = BoxSet.MONSTER;
// 设置 Model 层属性(数据相关)
model.hero_uuid = uuid;
model.hero_name = hero.name;
model.lv = lv;
model.type = hero.type;
model.fac = FacSet.MON;
model.is_boss = monType == MonType.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);
// 初始化属性数组
model.Attrs = getAttrs();
model.hp = model.Attrs[Attrs.HP_MAX] = hp;
model.mp = model.Attrs[Attrs.MP_MAX] = mp;
model.Attrs[Attrs.DEF] = def;
model.Attrs[Attrs.MDEF] = mdef;
model.Attrs[Attrs.AP] = ap;
model.Attrs[Attrs.MAP] = map;
model.Attrs[Attrs.SPEED] = hero.speed;
model.Attrs[Attrs.DIS] = hero.dis;
// ✅ 初始化技能数据(迁移到 HeroSkillsComp
skillsComp.initSkills(hero.skills, uuid, this);
this.add(view);
oops.message.dispatchEvent("monster_load",this)
// 初始化移动参数,包括线路和生成顺序
const move = this.get(MonMoveComp);
move.direction = -1; // 向左移动
move.targetX = -800; // 左边界
move.lane = lane; // 设置线路标识
move.spawnOrder = spawnOrder; // 设置生成顺序
smc.vmdata.mission_data.mon_num++
}
reset() {
// 注: 自定义释放逻辑,视图层实现 ecs.IComp 接口的 ecs 组件需要手动释放
super.destroy();
}
}
@ecs.register('MonLifecycleSystem')
export class MonLifecycleSystem extends ecs.ComblockSystem
implements ecs.IEntityEnterSystem, ecs.IEntityRemoveSystem {
filter() {
return ecs.allOf(MonMoveComp);
}
entityEnter(e: ecs.Entity): void {
// 怪物实体创建时的特殊处理
const heroAttrs = e.get(HeroAttrsComp);
if (heroAttrs) {
console.log(`怪物进入世界: ${heroAttrs.hero_name}`);
} else {
console.log(`怪物进入世界: 实体ID ${e.eid}`);
}
}
entityRemove(e: ecs.Entity): void {
// 怪物实体销毁时的清理工作
const heroAttrs = e.get(HeroAttrsComp);
if (heroAttrs) {
console.log(`怪物离开世界: ${heroAttrs.hero_name}`);
} else {
console.log(`怪物离开世界: 实体ID ${e.eid}`);
}
}
}