refactor: 移除全局主角引用,改用ECS查询定位主角实体

- 移除 SingletonModuleComp 中的 role 字段及相关设置
- 在 MissionComp 中移除重置 role 的代码
- 修改 Hero 类的销毁方法,不再清理 role 引用
- 在 MissionCardComp 中通过 HeroMasterComp 查询来定位主角实体
- 增加详细调试日志以追踪天赋、技能等组件的添加过程
This commit is contained in:
panw
2026-02-03 15:56:22 +08:00
parent 63dd22fb88
commit 147131d3c2
4 changed files with 25 additions and 38 deletions

View File

@@ -157,7 +157,7 @@ export class SingletonModuleComp extends ecs.Comp {
};
/** 主角实体引用 */
role: ecs.Entity | null = null;
// role: ecs.Entity | null = null;
/**
* 记录天赋获取

View File

@@ -33,12 +33,6 @@ export class Hero extends ecs.Entity {
}
destroy(): void {
// 如果是主角,清理全局引用
if (smc.role === this) {
console.log(`[Hero] 主角销毁,清除 smc.role`);
smc.role = null;
}
// 销毁节点,防止视觉残留
const view = this.get(HeroViewComp);
if (view && view.node && view.node.isValid) {
@@ -49,6 +43,7 @@ export class Hero extends ecs.Entity {
this.remove(HeroAttrsComp);
this.remove(HeroSkillsComp);
this.remove(TalComp);
this.remove(HeroMasterComp)
super.destroy();
}
@@ -94,8 +89,7 @@ export class Hero extends ecs.Entity {
model.rangeType = hero.rangeType;
// 只有主角才挂载天赋组件
if (is_master) {
smc.role = this; // 记录主角实体引用
console.log(`[Hero] 主角创建,设置 smc.role, uuid=${uuid}`);
console.log(`[Hero] 主角创建, uuid=${uuid}`);
this.add(TalComp);
this.add(HeroMasterComp)
const talComp = this.get(TalComp);

View File

@@ -14,6 +14,8 @@ import { BuffConf } from "../common/config/SkillSet";
import { BType } from "../common/config/HeroAttrs";
import { AttrCards, PotionCards } from "../common/config/AttrSet";
import { HeroMasterComp } from "../hero/HeroMasterComp";
const { ccclass, property } = _decorator;
interface ICardEvent {
@@ -26,7 +28,7 @@ interface ICardEvent {
@ecs.register('MissionCard', false)
export class MissionCardComp extends CCComp {
@property({ tooltip: "是否启用调试日志" })
private debugMode: boolean = false;
private debugMode: boolean = true;
/** 视图层逻辑代码分离演示 */
@property(Node)
@@ -259,7 +261,7 @@ export class MissionCardComp extends CCComp {
// 使用 CardSet 的 getCardOptions 获取卡牌
// 这里我们要获取 4 张卡牌
const options = getCardOptions(level, 4, [], forcedType);
console.log("[MissionCard]获取到的卡牌选项:", options);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] 获取到的卡牌选项: ${JSON.stringify(options)}`);
// 更新卡片数据
if (options.length > 0) this.updateCardData(1, options[0]);
if (options.length > 1) this.updateCardData(2, options[1]);
@@ -434,24 +436,15 @@ export class MissionCardComp extends CCComp {
.to(0.1, { scale: new Vec3(1, 1, 1) })
.delay(0.5)
.call(() => {
// 根据类型直接操作 smc.role (如果是主角)
// 确保只影响主角,避免广播事件导致所有实体生效
let role = smc.role;
// 使用 HeroMasterComp 查找主角实体
// @ts-ignore
const entities = ecs.query(ecs.allOf(HeroMasterComp));
let role = entities.length > 0 ? entities[0] : null;
// 容错处理:如果 smc.role 为空,尝试通过 ECS 查询查找主角
if (!role) {
console.warn("[MissionCard] smc.role 为空,尝试查找主角实体...");
// @ts-ignore
const entities = ecs.query(ecs.allOf(HeroAttrsComp));
for (const e of entities) {
const attrs = e.get(HeroAttrsComp);
if (attrs && attrs.is_master) {
role = e;
smc.role = e; // 修复 smc.role 引用
console.log("[MissionCard] 成功找回主角实体并修复 smc.role");
break;
}
}
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] 未找到挂载 HeroMasterComp 的主角实体`);
} else {
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] 成功定位主角实体: ${role.eid}`);
}
if (role) {
@@ -462,10 +455,10 @@ export class MissionCardComp extends CCComp {
const talComp = role.get(TalComp);
if (talComp) {
const beforeCount = Object.keys(talComp.Tals).length;
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Talent Before: Count=${beforeCount}`);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Talent Before: Count=${beforeCount}, Tals=${JSON.stringify(talComp.Tals)}`);
talComp.addTal(selectedData.uuid);
const afterCount = Object.keys(talComp.Tals).length;
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Talent After: Count=${afterCount}, Added=${selectedData.uuid}`);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Talent After: Count=${afterCount}, Added=${selectedData.uuid}, Tals=${JSON.stringify(talComp.Tals)}`);
}
break;
case CardType.Skill:
@@ -474,10 +467,10 @@ export class MissionCardComp extends CCComp {
const skillComp = role.get(HeroSkillsComp);
if (skillComp) {
const beforeCount = Object.keys(skillComp.skills).length;
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Skill Before: Count=${beforeCount}`);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Skill Before: Count=${beforeCount}, Skills=${JSON.stringify(skillComp.skills)}`);
skillComp.addSkill(selectedData.uuid);
const afterCount = Object.keys(skillComp.skills).length;
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Skill After: Count=${afterCount}, Added=${selectedData.uuid}`);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Skill After: Count=${afterCount}, Added=${selectedData.uuid}, Skills=${JSON.stringify(skillComp.skills)}`);
}
break;
case CardType.Partner:
@@ -491,7 +484,7 @@ export class MissionCardComp extends CCComp {
const potion = PotionCards[selectedData.uuid];
if (potion) {
const beforeVal = attrsComp.Attrs[potion.attr] || 0;
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Potion Before: Attr[${potion.attr}]=${beforeVal}`);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Potion Before: Attr[${potion.attr}]=${beforeVal}, Attrs=${JSON.stringify(attrsComp.Attrs)}`);
const buffConf: BuffConf = {
buff: potion.attr,
@@ -503,7 +496,7 @@ export class MissionCardComp extends CCComp {
attrsComp.addBuff(buffConf);
smc.updateHeroInfo(attrsComp);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Potion Applied: ${potion.desc}, Value=${potion.value}`);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Potion Applied: ${potion.desc}, Value=${potion.value}, Attrs=${JSON.stringify(attrsComp.Attrs)}`);
oops.gui.toast(potion.desc);
}
}
@@ -521,7 +514,7 @@ export class MissionCardComp extends CCComp {
if (roleAttrs) {
roleBefore = roleAttrs.Attrs[attrCard.attr] || 0;
}
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Attr Before: Global=${globalBefore}, Hero=${roleBefore}`);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Attr Before: Global=${globalBefore}, Hero=${roleBefore}, Attrs=${JSON.stringify(roleAttrs.Attrs)}`);
if (!smc.global_attrs[attrCard.attr]) {
smc.global_attrs[attrCard.attr] = [0, 0];
@@ -537,14 +530,14 @@ export class MissionCardComp extends CCComp {
smc.updateHeroInfo(attrsComp);
const roleAfter = attrsComp.Attrs[attrCard.attr] || 0;
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Attr After: Global=${current[0]}, Hero=${roleAfter}`);
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] Attr After: Global=${current[0]}, Hero=${roleAfter}, Attrs=${JSON.stringify(attrsComp.Attrs)}`);
}
oops.gui.toast(attrCard.desc);
}
break;
}
} else {
console.warn("[MissionCard] 主角实体无效,无法应用卡牌效果");
mLogger.log(this.debugMode, 'MissionCard', `[MissionCard] 主角实体无效,无法应用卡牌效果`);
}
// 记录已获取的卡牌

View File

@@ -277,8 +277,8 @@ do_ad(){
this.spawnedSpecialIndices.clear(); // 重置特殊刷怪记录
// 重置全局属性加成和主角引用 (确保新一局数据干净)
console.log(`[MissionComp] data_init 重置 smc.role 为 null`);
smc.role = null;
// console.log(`[MissionComp] data_init 重置 smc.role 为 null`);
// smc.role = null;
// 重置英雄数据,确保新一局是初始状态
smc.vmdata.hero = {