feat(talent): 引入天赋碎片系统替换通用天赋点
- 新增 TalentFragmentType 枚举和 TalentFragmentInfo 接口定义碎片类型 - 在 SingletonModuleComp 中新增 talent_fragments 字段存储碎片库存,talent_points 改为可选字段以兼容旧存档 - 为每个天赋配置 fragmentType 指定升级所需的具体碎片类型 - 修改 TalentsComp 升级逻辑,从消耗天赋点改为扣除对应类型的碎片 - 重置天赋功能现在返还已消耗的碎片而非天赋点 - 更新界面显示,展示碎片库存摘要和具体消耗
This commit is contained in:
@@ -24,7 +24,7 @@ import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/modu
|
||||
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";
|
||||
import { TalentConfig, TalentFragmentInfo, TalentFragmentType, TalentInfo, TalentType } from "../common/config/TalentSet";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@@ -32,9 +32,9 @@ const { ccclass, property } = _decorator;
|
||||
* TalentsComp —— 天赋系统界面组件
|
||||
*
|
||||
* 职责:
|
||||
* 1. 展示玩家等级、当前经验、进度条、可用天赋点。
|
||||
* 1. 展示玩家等级、当前经验、进度条、碎片库存。
|
||||
* 2. 展示天赋列表及每个天赋的当前等级。
|
||||
* 3. 处理天赋升级点击事件,扣除天赋点并保存。
|
||||
* 3. 处理天赋升级点击事件,扣除碎片并保存。
|
||||
* 4. 处理重置天赋(看广告)功能。
|
||||
*/
|
||||
@ccclass('TalentsComp')
|
||||
@@ -53,7 +53,7 @@ export class TalentsComp extends CCComp {
|
||||
@property({ type: ProgressBar, tooltip: "经验进度条" })
|
||||
pb_exp: ProgressBar = null!;
|
||||
|
||||
@property({ type: Label, tooltip: "当前可用天赋点数文本,例如 '4/30'" })
|
||||
@property({ type: Label, tooltip: "当前碎片库存摘要文本" })
|
||||
lbl_points: Label = null!;
|
||||
|
||||
@property({ type: Node, tooltip: "天赋列表容器,用于动态添加天赋项" })
|
||||
@@ -96,12 +96,12 @@ export class TalentsComp extends CCComp {
|
||||
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;
|
||||
const fragmentSummary = this.getFragmentSummaryText();
|
||||
|
||||
// 限制最大等级
|
||||
if (level > this.MAX_PLAYER_LEVEL) {
|
||||
@@ -109,7 +109,7 @@ export class TalentsComp extends CCComp {
|
||||
}
|
||||
|
||||
if (this.lbl_level) this.lbl_level.string = `Lv.${level}`;
|
||||
if (this.lbl_points) this.lbl_points.string = `天赋点: ${points}/${this.MAX_PLAYER_LEVEL}`;
|
||||
if (this.lbl_points) this.lbl_points.string = fragmentSummary;
|
||||
|
||||
// 计算当前等级升级所需经验
|
||||
let expRequired = this.getExpRequirement(level);
|
||||
@@ -179,31 +179,46 @@ export class TalentsComp extends CCComp {
|
||||
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;
|
||||
const levelCost = isMax ? null : this.getLevelFragmentCost(talentInfo, currentLevel);
|
||||
const canUpgrade = !!levelCost && this.hasEnoughFragments(levelCost);
|
||||
|
||||
if (lblCost) {
|
||||
lblCost.string = isMax ? "已满级" : `消耗: ${cost}点`;
|
||||
lblCost.string = isMax ? "已满级" : `消耗: ${this.buildFragmentCostText(levelCost!)}`;
|
||||
}
|
||||
|
||||
if (btnUpgrade) {
|
||||
btnUpgrade.interactable = !isMax && points >= cost;
|
||||
btnUpgrade.interactable = !isMax && canUpgrade;
|
||||
// 清除旧的监听,避免重复绑定
|
||||
btnUpgradeNode?.off(Button.EventType.CLICK);
|
||||
btnUpgradeNode?.on(Button.EventType.CLICK, () => {
|
||||
this.onUpgradeClicked(talentInfo.id, currentLevel, cost);
|
||||
this.onUpgradeClicked(talentInfo.id, currentLevel);
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
|
||||
/** 点击升级按钮 */
|
||||
private onUpgradeClicked(talentId: TalentType, currentLevel: number, cost: number) {
|
||||
private onUpgradeClicked(talentId: TalentType, currentLevel: number) {
|
||||
const collection = smc.collection;
|
||||
let points = collection.talent_points || 0;
|
||||
const talentInfo = TalentConfig.talents.find(t => t.id === talentId);
|
||||
if (!talentInfo) {
|
||||
oops.gui.toast("天赋配置不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
if (points >= cost && currentLevel < 5) {
|
||||
// 1. 扣除消耗
|
||||
collection.talent_points -= cost;
|
||||
if (currentLevel >= talentInfo.maxLevel) {
|
||||
oops.gui.toast("该天赋已满级");
|
||||
return;
|
||||
}
|
||||
|
||||
const levelCost = this.getLevelFragmentCost(talentInfo, currentLevel);
|
||||
if (!levelCost) {
|
||||
oops.gui.toast("天赋消耗配置异常");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.hasEnoughFragments(levelCost)) {
|
||||
// 1. 扣除单碎片消耗
|
||||
this.consumeFragments(levelCost);
|
||||
// 2. 更新等级
|
||||
collection.talents[talentId] = currentLevel + 1;
|
||||
|
||||
@@ -215,7 +230,7 @@ export class TalentsComp extends CCComp {
|
||||
|
||||
oops.gui.toast("天赋升级成功");
|
||||
} else {
|
||||
oops.gui.toast("天赋点不足或已满级");
|
||||
oops.gui.toast("碎片不足或已满级");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,26 +240,32 @@ export class TalentsComp extends CCComp {
|
||||
this.watch_ad().then(success => {
|
||||
if (success) {
|
||||
const collection = smc.collection;
|
||||
// 计算已消耗的天赋点总和
|
||||
let refundedPoints = 0;
|
||||
// 计算已消耗碎片并返还
|
||||
const refundedFragments: Partial<Record<TalentFragmentType, number>> = {};
|
||||
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];
|
||||
const levelCost = this.getLevelFragmentCost(talentInfo, i);
|
||||
if (!levelCost) continue;
|
||||
refundedFragments[levelCost.type] = (refundedFragments[levelCost.type] || 0) + levelCost.amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 重置天赋等级并返还点数
|
||||
// 重置天赋等级并返还碎片
|
||||
collection.talents = {};
|
||||
collection.talent_points += refundedPoints;
|
||||
|
||||
// 限制不超过最大点数(30)
|
||||
if (collection.talent_points > this.MAX_PLAYER_LEVEL) {
|
||||
collection.talent_points = this.MAX_PLAYER_LEVEL;
|
||||
if (!collection.talent_fragments) {
|
||||
collection.talent_fragments = {};
|
||||
}
|
||||
for (const fragmentType in refundedFragments) {
|
||||
const key = Number(fragmentType) as TalentFragmentType;
|
||||
const amount = refundedFragments[key] || 0;
|
||||
if (amount > 0) {
|
||||
collection.talent_fragments[key] = (collection.talent_fragments[key] || 0) + amount;
|
||||
}
|
||||
}
|
||||
|
||||
// 同步到云端
|
||||
@@ -252,13 +273,59 @@ export class TalentsComp extends CCComp {
|
||||
|
||||
// 刷新界面
|
||||
this.refreshUI();
|
||||
oops.gui.toast("天赋已重置,点数已返还");
|
||||
oops.gui.toast("天赋已重置,碎片已返还");
|
||||
} else {
|
||||
oops.gui.toast("广告观看失败,无法重置");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** 获取升级到下一等级所需的单碎片消耗 */
|
||||
private getLevelFragmentCost(talentInfo: TalentInfo, currentLevel: number): { type: TalentFragmentType; amount: number } | null {
|
||||
if (talentInfo.fragmentType === undefined) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
type: talentInfo.fragmentType,
|
||||
amount: talentInfo.costs[currentLevel] ?? 0
|
||||
};
|
||||
}
|
||||
|
||||
/** 判断当前库存是否满足一次升级 */
|
||||
private hasEnoughFragments(cost: { type: TalentFragmentType; amount: number }): boolean {
|
||||
const bag = smc.collection.talent_fragments || {};
|
||||
return (bag[cost.type] || 0) >= cost.amount;
|
||||
}
|
||||
|
||||
/** 执行碎片扣除 */
|
||||
private consumeFragments(cost: { type: TalentFragmentType; amount: number }) {
|
||||
const bag = smc.collection.talent_fragments || (smc.collection.talent_fragments = {});
|
||||
bag[cost.type] = Math.max(0, (bag[cost.type] || 0) - cost.amount);
|
||||
}
|
||||
|
||||
/** 生成单次升级消耗文案 */
|
||||
private buildFragmentCostText(cost: { type: TalentFragmentType; amount: number }): string {
|
||||
const info = this.getFragmentInfo(cost.type);
|
||||
const own = smc.collection.talent_fragments?.[cost.type] || 0;
|
||||
const fragmentName = info ? info.name : `碎片${cost.type}`;
|
||||
return `${fragmentName} ${own}/${cost.amount}`;
|
||||
}
|
||||
|
||||
/** 顶部碎片库存摘要 */
|
||||
private getFragmentSummaryText(): string {
|
||||
const bag = smc.collection.talent_fragments || {};
|
||||
const summary = TalentConfig.fragments
|
||||
.filter(fragment => (bag[fragment.id] || 0) > 0)
|
||||
.map(fragment => `${fragment.name}:${bag[fragment.id] || 0}`)
|
||||
.join(" ");
|
||||
return summary ? `碎片: ${summary}` : "碎片: 暂无";
|
||||
}
|
||||
|
||||
/** 按碎片类型查找碎片配置 */
|
||||
private getFragmentInfo(type: TalentFragmentType): TalentFragmentInfo | undefined {
|
||||
return TalentConfig.fragments.find(fragment => fragment.id === type);
|
||||
}
|
||||
|
||||
/** 模拟看广告回调,实际项目中需要替换为真实的广告SDK调用 */
|
||||
private watch_ad(): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
@@ -282,4 +349,4 @@ export class TalentsComp extends CCComp {
|
||||
reset() {
|
||||
this.node.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user