feat(hero): add hero upgrade function

1. 新增英雄升级相关UI控件与事件流程
2. 实现升级消耗计算与统一扣费逻辑
3. 添加升级预览确认弹窗与属性成长展示
4. 开放tryUpgradeHeroByEid与applyHeroLevel为公共方法
This commit is contained in:
panFD
2026-08-04 22:18:20 +08:00
parent 153e0e9a1f
commit 4938e8468e
5 changed files with 153 additions and 4 deletions

View File

@@ -20,8 +20,8 @@ import { GameEvent } from "../common/config/GameEvent";
import { HeroInfo } from "../common/config/heroSet";
import { HeroAttrsComp } from "../hero/HeroAttrsComp";
import { Hero } from "../hero/Hero";
import { FacSet, getLvColor } from "../common/config/GameSet";
import { buildSkillDescRich } from "../common/config/HeroSkillDesc";
import { FacSet, FightSet, getLvColor } from "../common/config/GameSet";
import { buildSkillDesc, buildSkillDescRich, ISkillDescSource } from "../common/config/HeroSkillDesc";
import { MissionEconomy } from "./MissionEconomy";
import { UIID } from "../common/config/GameUIConfig";
@@ -60,6 +60,10 @@ export class HeroBoxComp extends CCComp {
@property(Node)
private sell_btn: Node = null;
/** 升级按钮(绑定英雄时激活,点击弹出升级预览确认框) */
@property(Node)
private upgrade_btn: Node = null;
/** 出售价格标签(按英雄等级显示当前卖价,含驻场加成) */
@property(Label)
private sell_price_label: Label = null;
@@ -167,6 +171,8 @@ export class HeroBoxComp extends CCComp {
this.noHero?.on(NodeEventType.TOUCH_END, this.onNoHeroClick, this);
// 出售按钮点击 → 出售当前绑定英雄
this.sell_btn?.on(NodeEventType.TOUCH_END, this.onSellHeroClick, this);
// 升级按钮点击 → 弹出升级预览确认框
this.upgrade_btn?.on(NodeEventType.TOUCH_END, this.onUpgradeHeroClick, this);
}
onDestroy() {
@@ -181,6 +187,96 @@ export class HeroBoxComp extends CCComp {
if (this.sell_btn && this.sell_btn.isValid) {
this.sell_btn.off(NodeEventType.TOUCH_END, this.onSellHeroClick, this);
}
if (this.upgrade_btn && this.upgrade_btn.isValid) {
this.upgrade_btn.off(NodeEventType.TOUCH_END, this.onUpgradeHeroClick, this);
}
}
/**
* 升级按钮点击:构建升级后新能力预览,弹出公共确认框。
* Why: 升级消耗金币不可逆,需展示收益并二次确认。
*/
private onUpgradeHeroClick() {
if (!this.eid || !this.model) return;
const heroLv = Math.max(1, Math.floor(this.model.lv ?? 1));
// 已满级:气泡提示,复用英雄栏 smalltip 挂载点
if (heroLv >= FightSet.HERO_MAX_LV) {
oops.message.dispatchEvent(GameEvent.ShowSmallTip, { type: "hero_full", text: "已达最高等级" });
return;
}
oops.audio.playEffect("music/button");
const heroName = this.model.hero_name ?? "";
const nextLv = heroLv + 1;
const cost = MissionEconomy.getUpgradeCost(heroLv);
const preview = this.buildUpgradePreview(nextLv);
// oops.gui 公共确认窗口(自定义 CommonPrompt内容支持富文本高亮
oops.gui.open(UIID.Window, {
title: `升级英雄`,
content: `<color=#C0392B>${heroName}</color> Lv.${heroLv} → <color=#1E8449>Lv.${nextLv}</color>\n消耗 <color=#E67E22>${cost}</color> 金币\n\n<b>升级后获得:</b>\n${preview}`,
okWord: "升级",
cancelWord: "取消",
needCancel: true,
okFunc: () => this.executeUpgradeHero()
});
}
/**
* 执行升级:校验状态后派发 HeroUpgrade 事件,由 MissionCardComp 扣费并应用等级。
* Why: 扣费/属性重算统一走 MissionCardComp与升级卡共用 tryUpgradeHeroByEid
* 本组件仅负责 UI 交互与预览,保证单一数据源。
*/
private executeUpgradeHero() {
// 确认窗口打开期间英雄可能已死亡/被移除,需二次校验
if (!this.eid || !this.model) return;
oops.message.dispatchEvent(GameEvent.HeroUpgrade, { eid: this.eid });
}
/**
* 构建升级后新能力预览富文本。
* 数据源取 HeroInfo 静态配置lv 置为目标等级),由描述标准层统一渲染,
* 与面板技能描述同一套文案规则,杜绝数值漂移。
*
* @param nextLv 目标等级
* @returns 多行富文本:属性成长 + 下一档触发技能/光环/复活/加成
*/
private buildUpgradePreview(nextLv: number): string {
const hero = HeroInfo[this.model?.hero_uuid ?? 0];
if (!hero) return "";
// ---- 属性成长(按 HERO_LV_MULTIPLIER 幂成长 + 等级额外加成) ----
const mult = Math.pow(FightSet.HERO_LV_MULTIPLIER, nextLv - 1);
const nextAp = Math.floor(hero.ap * mult + HeroBoxComp.sumBonus(hero.ap_bonus, nextLv));
const nextHp = Math.floor(hero.hp * mult + HeroBoxComp.sumBonus(hero.hp_bonus, nextLv));
const lines: string[] = [
`攻击 <color=#27AE60>${nextAp}</color> 生命 <color=#27AE60>${nextHp}</color>`
];
// ---- 新能力(触发技能 / 驻场光环 / 复活 / 额外加成,取目标等级生效档) ----
const source: ISkillDescSource = {
lv: nextLv,
call: hero.call,
dead: hero.dead,
fstart: hero.fstart,
fend: hero.fend,
atking: hero.atking,
atked: hero.atked,
field: hero.field,
revive: hero.revive,
// 预览仅展示增量,额外加成由上方属性成长体现,避免重复
};
const skillDesc = buildSkillDesc(source);
if (skillDesc) lines.push(skillDesc);
return lines.join("\n");
}
/** 累加等级额外加成(≤目标等级的所有档位) */
private static sumBonus(entries: { lv: number; value: number }[] | undefined, lv: number): number {
if (!entries) return 0;
let total = 0;
for (const e of entries) { if (e.lv <= lv) total += e.value; }
return total;
}
/**
@@ -320,6 +416,9 @@ export class HeroBoxComp extends CCComp {
if (this.sell_btn && this.sell_btn.isValid) {
this.sell_btn.active = true;
}
if (this.upgrade_btn && this.upgrade_btn.isValid) {
this.upgrade_btn.active = true;
}
this.refresh();
}
@@ -337,6 +436,9 @@ export class HeroBoxComp extends CCComp {
if (this.sell_btn && this.sell_btn.isValid) {
this.sell_btn.active = false;
}
if (this.upgrade_btn && this.upgrade_btn.isValid) {
this.upgrade_btn.active = false;
}
if (this.sell_price_label && this.sell_price_label.isValid) {
this.sell_price_label.string = "";
}