refactor(hero): 优化技能初始化逻辑并添加技能卡选择功能

- 移除initSkills和addSkill方法中多余的entity参数,改为使用组件内ent属性
- 在HeroSkillsComp中添加技能卡选择事件监听和处理
- 在MissionCardComp中实现技能卡选择界面和事件分发
This commit is contained in:
panw
2026-01-05 14:45:39 +08:00
parent 167297820e
commit 45508abca4
4 changed files with 56 additions and 11 deletions

View File

@@ -93,7 +93,7 @@ export class Hero extends ecs.Entity {
} }
// ✅ 初始化技能数据(迁移到 HeroSkillsComp // ✅ 初始化技能数据(迁移到 HeroSkillsComp
skillsComp.initSkills(hero.skills, uuid, this); skillsComp.initSkills(hero.skills, uuid);
// 设置基础属性 // 设置基础属性
model.base_ap = hero.ap; model.base_ap = hero.ap;

View File

@@ -1,4 +1,6 @@
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS"; import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { GameEvent } from "../common/config/GameEvent";
import { Attrs } from "../common/config/HeroAttrs"; import { Attrs } from "../common/config/HeroAttrs";
import { HeroInfo } from "../common/config/heroSet"; import { HeroInfo } from "../common/config/heroSet";
import { HSSet, SkillSet } from "../common/config/SkillSet"; import { HSSet, SkillSet } from "../common/config/SkillSet";
@@ -42,6 +44,23 @@ export class HeroSkillsComp extends ecs.Comp {
/** AI 检测计时器 */ /** AI 检测计时器 */
ai_timer: number = 0; ai_timer: number = 0;
onLoad() {
oops.message.on(GameEvent.UseSkillCard, this.onUseSkillCard, this);
}
onDestroy() {
oops.message.off(GameEvent.UseSkillCard, this.onUseSkillCard, this);
}
private onUseSkillCard(event: string, args: any) {
const attrsComp = this.ent.get(HeroAttrsComp);
if (!attrsComp || !attrsComp.is_master) return;
const s_uuid = args as number;
console.log(`[HeroSkills] 收到技能选择事件,添加技能 ID: ${s_uuid}`);
this.addSkill(s_uuid,HSSet.max);
}
// ==================== 辅助方法 ==================== // ==================== 辅助方法 ====================
/** /**
@@ -50,7 +69,7 @@ export class HeroSkillsComp extends ecs.Comp {
* @param uuid 英雄UUID * @param uuid 英雄UUID
* @param entity 实体对象(用于更新技能距离缓存) * @param entity 实体对象(用于更新技能距离缓存)
*/ */
initSkills(sUuids: number[], uuid: number, entity?: ecs.Entity) { initSkills(sUuids: number[], uuid: number) {
this.skills = []; this.skills = [];
for (let i = 0; i < sUuids.length; i++) { for (let i = 0; i < sUuids.length; i++) {
const s_uuid = sUuids[i]; const s_uuid = sUuids[i];
@@ -75,8 +94,8 @@ export class HeroSkillsComp extends ecs.Comp {
} }
// 更新技能距离缓存 // 更新技能距离缓存
if (entity) { if (this.ent) {
const attrsComp = entity.get(HeroAttrsComp); const attrsComp = this.ent.get(HeroAttrsComp);
if (attrsComp) { if (attrsComp) {
attrsComp.updateSkillDistanceCache(this); attrsComp.updateSkillDistanceCache(this);
} }
@@ -86,9 +105,9 @@ export class HeroSkillsComp extends ecs.Comp {
/** /**
* 添加单个技能 * 添加单个技能
* @param s_uuid 技能配置ID * @param s_uuid 技能配置ID
* @param entity 实体对象(用于更新技能距离缓存) * @param hset 技能类型
*/ */
addSkill(s_uuid: number, entity?: ecs.Entity, hset: HSSet=HSSet.skill) { addSkill(s_uuid: number, hset: HSSet=HSSet.skill) {
const config = SkillSet[s_uuid]; const config = SkillSet[s_uuid];
if (!config) { if (!config) {
console.warn(`[HeroSkills] 技能配置不存在: ${s_uuid}`); console.warn(`[HeroSkills] 技能配置不存在: ${s_uuid}`);
@@ -104,8 +123,8 @@ export class HeroSkillsComp extends ecs.Comp {
}; };
// 更新技能距离缓存 // 更新技能距离缓存
if (entity) { if (this.ent) {
const attrsComp = entity.get(HeroAttrsComp); const attrsComp = this.ent.get(HeroAttrsComp);
if (attrsComp) { if (attrsComp) {
attrsComp.updateSkillDistanceCache(this); attrsComp.updateSkillDistanceCache(this);
} }
@@ -261,6 +280,7 @@ export class HeroSkillsComp extends ecs.Comp {
} }
reset() { reset() {
oops.message.off(GameEvent.UseSkillCard, this.onUseSkillCard, this);
this.skills = {}; this.skills = {};
} }
setMaxAuto(on: boolean) { setMaxAuto(on: boolean) {

View File

@@ -130,7 +130,7 @@ export class Monster extends ecs.Entity {
model.Attrs[Attrs.BACK_CHANCE]=15 model.Attrs[Attrs.BACK_CHANCE]=15
model.Attrs[Attrs.CON_RES]=10 model.Attrs[Attrs.CON_RES]=10
// ✅ 初始化技能数据(迁移到 HeroSkillsComp // ✅ 初始化技能数据(迁移到 HeroSkillsComp
skillsComp.initSkills(hero.skills, uuid, this); skillsComp.initSkills(hero.skills, uuid);
this.add(view); this.add(view);
// 重置视图状态(对象池复用时必须) // 重置视图状态(对象池复用时必须)

View File

@@ -4,6 +4,7 @@ import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/modu
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops"; import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { GameEvent } from "../common/config/GameEvent"; import { GameEvent } from "../common/config/GameEvent";
import { talConf, ItalConf } from "../common/config/TalSet"; import { talConf, ItalConf } from "../common/config/TalSet";
import { SkillSet } from "../common/config/SkillSet";
import { ItemSet } from "../common/config/ItemSet"; import { ItemSet } from "../common/config/ItemSet";
import { smc } from "../common/SingletonModuleComp"; import { smc } from "../common/SingletonModuleComp";
@@ -48,6 +49,7 @@ export class MissionCardComp extends CCComp {
onLoad() { onLoad() {
oops.message.on(GameEvent.TalentSelect, this.onTalentSelect, this); oops.message.on(GameEvent.TalentSelect, this.onTalentSelect, this);
oops.message.on(GameEvent.HeroSkillSelect, this.onHeroSkillSelect, this);
oops.message.on(GameEvent.ShopOpen, this.onShopOpen, this); oops.message.on(GameEvent.ShopOpen, this.onShopOpen, this);
oops.message.on(GameEvent.MissionStart, this.init, this); oops.message.on(GameEvent.MissionStart, this.init, this);
@@ -55,6 +57,7 @@ export class MissionCardComp extends CCComp {
onDestroy() { onDestroy() {
oops.message.off(GameEvent.TalentSelect, this.onTalentSelect, this); oops.message.off(GameEvent.TalentSelect, this.onTalentSelect, this);
oops.message.off(GameEvent.HeroSkillSelect, this.onHeroSkillSelect, this);
oops.message.off(GameEvent.ShopOpen, this.onShopOpen, this); oops.message.off(GameEvent.ShopOpen, this.onShopOpen, this);
this.ent.destroy(); this.ent.destroy();
} }
@@ -104,6 +107,15 @@ export class MissionCardComp extends CCComp {
this.playShowAnimation(); this.playShowAnimation();
} }
private onHeroSkillSelect(event: string, args: any) {
this.node.active = true;
this.hasSelected = false;
this.curCardType = CardType.Skill;
this.resetCardStates();
this.refCards();
this.playShowAnimation();
}
private playShowAnimation() { private playShowAnimation() {
const cards = [this.card1, this.card2, this.card3, this.card4]; const cards = [this.card1, this.card2, this.card3, this.card4];
cards.forEach((card, index) => { cards.forEach((card, index) => {
@@ -123,7 +135,10 @@ export class MissionCardComp extends CCComp {
if (this.curCardType === CardType.Talent) { if (this.curCardType === CardType.Talent) {
allData = Object.values(talConf); allData = Object.values(talConf);
} } else if (this.curCardType === CardType.Skill) {
// 过滤掉怪物技能 (uuid >= 6200)
allData = Object.values(SkillSet).filter((s:any) => s.uuid < 6200);
}
// 后续扩展其他类型 // 后续扩展其他类型
// else if (this.curCardType === CardType.Skill) { ... } // else if (this.curCardType === CardType.Skill) { ... }
@@ -158,7 +173,15 @@ export class MissionCardComp extends CCComp {
let info = card.getChildByName("info")?.getChildByName("Label") let info = card.getChildByName("info")?.getChildByName("Label")
if(info){ if(info){
// 根据类型显示不同描述目前天赋用desc // 根据类型显示不同描述目前天赋用desc
let desc = data.desc || ""; let desc = "";
if (this.curCardType === CardType.Talent) {
desc = data.desc || "";
} else if (this.curCardType === CardType.Skill) {
desc = data.info || "";
} else {
desc = data.desc || "";
}
// 如果是物品,显示价格 // 如果是物品,显示价格
if (this.curCardType === CardType.Potion && data.price) { if (this.curCardType === CardType.Potion && data.price) {
desc += `\n价格: ${data.price}`; desc += `\n价格: ${data.price}`;
@@ -286,6 +309,8 @@ export class MissionCardComp extends CCComp {
// 根据类型发送不同事件 // 根据类型发送不同事件
if (this.curCardType === CardType.Talent) { if (this.curCardType === CardType.Talent) {
oops.message.dispatchEvent(GameEvent.UseTalentCard, selectedData.uuid); oops.message.dispatchEvent(GameEvent.UseTalentCard, selectedData.uuid);
} else if (this.curCardType === CardType.Skill) {
oops.message.dispatchEvent(GameEvent.UseSkillCard, selectedData.uuid);
} }
// 后续扩展其他类型事件 // 后续扩展其他类型事件
this.node.active = false; this.node.active = false;