refactor(hero): 优化英雄对象池管理及视图初始化

- 在Monster类中实现多键对象池管理,提升英雄节点复用效率
- 将HeroViewComp的初始化逻辑提取到独立init方法,便于对象池复用时重置状态
- 移除HeroSpine中冗余的onDestroy方法
- 修复HeroViewComp中方向缩放计算问题,确保scale.x为正
- 优化碰撞体启用逻辑,延迟一帧确保物理系统正确注册
- 清理HeroViewComp中残留的定时器和缓动
This commit is contained in:
walkpan
2026-01-02 23:27:05 +08:00
parent 2c7a628921
commit 81f55a796d
4 changed files with 77 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
import { Vec3, _decorator , v3,Collider2D,Contact2DType,Label ,Node,Prefab,instantiate,ProgressBar, Component, Material, Sprite, math, clamp, Game, tween, Color, BoxCollider2D, UITransform} from "cc";
import { Vec3, _decorator , v3,Collider2D,Contact2DType,Label ,Node,Prefab,instantiate,ProgressBar, Component, Material, Sprite, math, clamp, Game, tween, Tween, Color, BoxCollider2D, UITransform} from "cc";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { HeroSpine } from "./HeroSpine";
@@ -88,23 +88,29 @@ export class HeroViewComp extends CCComp {
}
/** 视图层逻辑代码分离演示 */
start () {
this.init();
}
/** 初始化/重置视图状态 */
init() {
this.as.idle()
// 初始化 UI 节点
this.initUINodes();
/** 方向 */
this.node.setScale(this.scale*this.node.scale.x,1*this.node.scale.y);
this.node.setScale(this.scale*Math.abs(this.node.scale.x), 1*this.node.scale.y); // 确保 scale.x 为正后再乘方向
this.top_node.setScale(this.scale*this.top_node.scale.x,1*this.top_node.scale.y);
/* 显示角色血*/
this.top_node.getChildByName("hp").active = true;
// 🔥 怪物不显示蓝条
this.top_node.getChildByName("mp").active = this.model.fac === FacSet.HERO;
if (this.model) {
this.top_node.getChildByName("mp").active = this.model.fac === FacSet.HERO;
}
this.top_node.getChildByName("shield").active = false;
// 初始隐藏血条(有更新时才显示)
this.top_node.active = false;
}
/** 初始化 UI 节点引用 */
@@ -513,6 +519,10 @@ export class HeroViewComp extends CCComp {
}
}
reset() {
// 清理残留的定时器和缓动
this.unscheduleAllCallbacks();
Tween.stopAllByTarget(this.node);
// 清理碰撞器事件监听
const collider = this.getComponent(Collider2D);
if (collider) {
@@ -525,10 +535,10 @@ export class HeroViewComp extends CCComp {
this.damageQueue.length = 0;
this.isProcessingDamage = false;
// 延迟销毁节点
this.scheduleOnce(() => {
this.node.destroy();
}, 0.1);
// 节点生命周期由 Monster 对象池管理,此处不再销毁
// if (this.node && this.node.isValid) {
// this.node.destroy();
// }
}
}