- 在 MissionCardComp 中应用 RefreshDiscount 天赋以减少刷新消耗 - 在 CardComp 中应用 BuyDiscount 天赋以减少英雄购买消耗 - 在 HInfoComp 中应用 SellBonus 天赋以增加英雄出售收益 - 统一 TalentType 枚举类型,增强类型安全性 - 更新 SingletonModuleComp 中 talents 数据结构以支持类型化 - 修改 HeroAttrsComp.getTalentValue 方法参数类型为 TalentType
285 lines
11 KiB
TypeScript
285 lines
11 KiB
TypeScript
/**
|
||
* @file TalentsComp.ts
|
||
* @description 战斗结算弹窗组件(UI 视图层)
|
||
*
|
||
* 职责:
|
||
* 1. 在战斗结束时弹出,展示结算信息(得分、奖励)。
|
||
* 2. 根据传入参数判断是否可复活,切换"下一步"或"复活"按钮。
|
||
* 3. 计算单局总分并存储到 smc.vmdata.scores.score。
|
||
* 4. 提供"重新开始"和"退出"两个操作入口。
|
||
*
|
||
* 关键设计:
|
||
* - onAdded(args) 接收战斗结果参数(Talents / rewards / game_data / can_revive)。
|
||
* - calculateTotalScore() 根据 ScoreWeights 配置加权计算各项得分。
|
||
* - restart() 和 Talents_end() 通过分发 MissionEnd / MissionStart 事件驱动游戏状态切换。
|
||
*
|
||
* 依赖:
|
||
* - smc.vmdata.scores —— 全局战斗统计数据
|
||
* - ScoreWeights(ScoreSet)—— 得分权重配置
|
||
* - GameEvent.MissionEnd / MissionStart —— 游戏生命周期事件
|
||
*/
|
||
import { _decorator, Node, Label, Button, ProgressBar, instantiate, Prefab } 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 { mLogger } from "../common/Logger";
|
||
import { smc } from "../common/SingletonModuleComp";
|
||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||
import { TalentConfig, TalentInfo, TalentType } from "../common/config/TalentSet";
|
||
|
||
const { ccclass, property } = _decorator;
|
||
|
||
/**
|
||
* TalentsComp —— 天赋系统界面组件
|
||
*
|
||
* 职责:
|
||
* 1. 展示玩家等级、当前经验、进度条、可用天赋点。
|
||
* 2. 展示天赋列表及每个天赋的当前等级。
|
||
* 3. 处理天赋升级点击事件,扣除天赋点并保存。
|
||
* 4. 处理重置天赋(看广告)功能。
|
||
*/
|
||
@ccclass('TalentsComp')
|
||
@ecs.register('Talents', false)
|
||
export class TalentsComp extends CCComp {
|
||
|
||
@property({ type: Node, tooltip: "标题节点" })
|
||
title_node: Node = null!;
|
||
|
||
@property({ type: Label, tooltip: "玩家等级文本,例如 'Lv.12'" })
|
||
lbl_level: Label = null!;
|
||
|
||
@property({ type: Label, tooltip: "经验文本,例如 '150/200'" })
|
||
lbl_exp: Label = null!;
|
||
|
||
@property({ type: ProgressBar, tooltip: "经验进度条" })
|
||
pb_exp: ProgressBar = null!;
|
||
|
||
@property({ type: Label, tooltip: "当前可用天赋点数文本,例如 '4/30'" })
|
||
lbl_points: Label = null!;
|
||
|
||
@property({ type: Node, tooltip: "天赋列表容器,用于动态添加天赋项" })
|
||
talents_content: Node = null!;
|
||
|
||
@property({ type: Prefab, tooltip: "天赋项预制体\n预制体结构要求:\n- 根节点\n - lbl_name (Label): 天赋名称\n - lbl_desc (Label): 天赋描述\n - lbl_level (Label): 当前等级\n - lbl_cost (Label): 升级消耗\n - btn_upgrade (Button): 升级按钮\n - pb_level (ProgressBar或一组节点): 等级进度展示(可选)" })
|
||
prefab_talent_item: Prefab = null!;
|
||
|
||
@property({ type: Button, tooltip: "看广告重置天赋按钮" })
|
||
btn_reset: Button = null!;
|
||
|
||
@property({ type: Button, tooltip: "返回按钮" })
|
||
btn_close: Button = null!;
|
||
|
||
|
||
|
||
/** 调试日志开关 */
|
||
debugMode: boolean = false;
|
||
|
||
/** 最大玩家等级 */
|
||
private readonly MAX_PLAYER_LEVEL = 30;
|
||
|
||
protected onLoad(): void {
|
||
// 绑定按钮事件
|
||
if (this.btn_reset) {
|
||
this.btn_reset.node.on(Button.EventType.CLICK, this.onResetClicked, this);
|
||
}
|
||
if (this.btn_close) {
|
||
this.btn_close.node.on(Button.EventType.CLICK, this.onCloseClicked, this);
|
||
}
|
||
}
|
||
|
||
onAdded(args: any) {
|
||
this.refreshUI();
|
||
}
|
||
|
||
/** 刷新整体界面 */
|
||
private refreshUI() {
|
||
this.updatePlayerInfo();
|
||
this.updateTalentList();
|
||
}
|
||
|
||
/** 更新玩家等级、经验、天赋点信息 */
|
||
private updatePlayerInfo() {
|
||
const collection = smc.collection;
|
||
let level = collection.player_level || 1;
|
||
let exp = collection.player_exp || 0;
|
||
let points = collection.talent_points || 0;
|
||
|
||
// 限制最大等级
|
||
if (level > this.MAX_PLAYER_LEVEL) {
|
||
level = this.MAX_PLAYER_LEVEL;
|
||
}
|
||
|
||
if (this.lbl_level) this.lbl_level.string = `Lv.${level}`;
|
||
if (this.lbl_points) this.lbl_points.string = `天赋点: ${points}/${this.MAX_PLAYER_LEVEL}`;
|
||
|
||
// 计算当前等级升级所需经验
|
||
let expRequired = this.getExpRequirement(level);
|
||
|
||
if (level >= this.MAX_PLAYER_LEVEL) {
|
||
if (this.lbl_exp) this.lbl_exp.string = "已满级";
|
||
if (this.pb_exp) this.pb_exp.progress = 1;
|
||
} else {
|
||
if (this.lbl_exp) this.lbl_exp.string = `${exp}/${expRequired}`;
|
||
if (this.pb_exp) this.pb_exp.progress = exp / expRequired;
|
||
}
|
||
}
|
||
|
||
/** 获取对应等级的升级所需经验 */
|
||
private getExpRequirement(level: number): number {
|
||
for (let config of TalentConfig.expRequirements) {
|
||
if (level <= config.maxLevel) {
|
||
return config.expPerLevel;
|
||
}
|
||
}
|
||
return TalentConfig.expRequirements[TalentConfig.expRequirements.length - 1].expPerLevel;
|
||
}
|
||
|
||
/** 动态生成或更新天赋列表 */
|
||
private updateTalentList() {
|
||
if (!this.talents_content || !this.prefab_talent_item) return;
|
||
|
||
const collection = smc.collection;
|
||
if (!collection.talents) collection.talents = {};
|
||
|
||
// 如果内容为空,则实例化预制体
|
||
if (this.talents_content.children.length === 0) {
|
||
TalentConfig.talents.forEach(talentInfo => {
|
||
let itemNode = instantiate(this.prefab_talent_item);
|
||
this.talents_content.addChild(itemNode);
|
||
this.updateTalentItem(itemNode, talentInfo, collection.talents[talentInfo.id as TalentType] || 0);
|
||
});
|
||
} else {
|
||
// 否则直接更新现有节点
|
||
TalentConfig.talents.forEach((talentInfo, index) => {
|
||
let itemNode = this.talents_content.children[index];
|
||
if (itemNode) {
|
||
this.updateTalentItem(itemNode, talentInfo, collection.talents[talentInfo.id as TalentType] || 0);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
/** 更新单个天赋项的显示 */
|
||
private updateTalentItem(itemNode: Node, talentInfo: TalentInfo, currentLevel: number) {
|
||
let lblName = itemNode.getChildByName("lbl_name")?.getComponent(Label);
|
||
let lblDesc = itemNode.getChildByName("lbl_desc")?.getComponent(Label);
|
||
let lblLevel = itemNode.getChildByName("lbl_level")?.getComponent(Label);
|
||
let lblCost = itemNode.getChildByName("lbl_cost")?.getComponent(Label);
|
||
let btnUpgradeNode = itemNode.getChildByName("btn_upgrade");
|
||
let btnUpgrade = btnUpgradeNode?.getComponent(Button);
|
||
|
||
if (lblName) {
|
||
lblName.string = (talentInfo.icon ? `${talentInfo.icon} ` : '') + talentInfo.name;
|
||
}
|
||
|
||
if (lblDesc) {
|
||
let currentVal = currentLevel === 0 ? 0 : talentInfo.values[currentLevel - 1];
|
||
lblDesc.string = talentInfo.desc.replace('{value}', currentVal.toString());
|
||
}
|
||
|
||
if (lblLevel) lblLevel.string = `Lv.${currentLevel}`;
|
||
|
||
let isMax = currentLevel >= talentInfo.maxLevel;
|
||
let cost = isMax ? 0 : talentInfo.costs[currentLevel];
|
||
let points = smc.collection.talent_points || 0;
|
||
|
||
if (lblCost) {
|
||
lblCost.string = isMax ? "已满级" : `消耗: ${cost}点`;
|
||
}
|
||
|
||
if (btnUpgrade) {
|
||
btnUpgrade.interactable = !isMax && points >= cost;
|
||
// 清除旧的监听,避免重复绑定
|
||
btnUpgradeNode?.off(Button.EventType.CLICK);
|
||
btnUpgradeNode?.on(Button.EventType.CLICK, () => {
|
||
this.onUpgradeClicked(talentInfo.id, currentLevel, cost);
|
||
}, this);
|
||
}
|
||
}
|
||
|
||
/** 点击升级按钮 */
|
||
private onUpgradeClicked(talentId: TalentType, currentLevel: number, cost: number) {
|
||
const collection = smc.collection;
|
||
let points = collection.talent_points || 0;
|
||
|
||
if (points >= cost && currentLevel < 5) {
|
||
// 扣除天赋点
|
||
collection.talent_points -= cost;
|
||
// 增加天赋等级
|
||
collection.talents[talentId] = currentLevel + 1;
|
||
|
||
// 同步到云端
|
||
smc.updateCloudData();
|
||
|
||
// 刷新界面
|
||
this.refreshUI();
|
||
|
||
oops.gui.toast("天赋升级成功");
|
||
} else {
|
||
oops.gui.toast("天赋点不足或已满级");
|
||
}
|
||
}
|
||
|
||
/** 点击重置按钮 */
|
||
private onResetClicked() {
|
||
// 看广告回调(预留)
|
||
this.watch_ad().then(success => {
|
||
if (success) {
|
||
const collection = smc.collection;
|
||
// 计算已消耗的天赋点总和
|
||
let refundedPoints = 0;
|
||
for (let id in collection.talents) {
|
||
let talentId = Number(id) as TalentType;
|
||
let level = collection.talents[talentId] || 0;
|
||
let talentInfo = TalentConfig.talents.find(t => t.id === talentId);
|
||
if (talentInfo) {
|
||
for (let i = 0; i < level; i++) {
|
||
refundedPoints += talentInfo.costs[i];
|
||
}
|
||
}
|
||
}
|
||
|
||
// 重置天赋等级并返还点数
|
||
collection.talents = {};
|
||
collection.talent_points += refundedPoints;
|
||
|
||
// 限制不超过最大点数(30)
|
||
if (collection.talent_points > this.MAX_PLAYER_LEVEL) {
|
||
collection.talent_points = this.MAX_PLAYER_LEVEL;
|
||
}
|
||
|
||
// 同步到云端
|
||
smc.updateCloudData();
|
||
|
||
// 刷新界面
|
||
this.refreshUI();
|
||
oops.gui.toast("天赋已重置,点数已返还");
|
||
} else {
|
||
oops.gui.toast("广告观看失败,无法重置");
|
||
}
|
||
});
|
||
}
|
||
|
||
/** 模拟看广告回调,实际项目中需要替换为真实的广告SDK调用 */
|
||
private watch_ad(): Promise<boolean> {
|
||
return new Promise((resolve) => {
|
||
// 模拟广告播放延迟
|
||
setTimeout(() => {
|
||
resolve(true);
|
||
}, 500);
|
||
});
|
||
}
|
||
|
||
/** 点击返回按钮 */
|
||
private onCloseClicked() {
|
||
oops.gui.removeByNode(this.node); //
|
||
}
|
||
|
||
protected onDestroy(): void {
|
||
mLogger.log(this.debugMode, 'TalentsComp', "释放界面");
|
||
}
|
||
|
||
/** ECS 组件移除时销毁节点 */
|
||
reset() {
|
||
this.node.destroy();
|
||
}
|
||
} |