refactor(天赋系统): 将天赋和碎片集合改为完整记录并初始化默认值
- 将 `talents` 和 `talent_fragments` 的类型从 `Partial<Record>` 改为 `Record`,确保所有键始终存在 - 初始化集合对象时,为所有天赋类型和碎片类型设置默认值 0,避免后续的空值检查 - 更新数据恢复逻辑,使用 `Object.assign` 合并云端数据,保留本地初始化的完整结构 - 简化相关业务逻辑代码,移除不必要的空值检查和默认值回退
This commit is contained in:
@@ -138,21 +138,20 @@ export class TalentsComp extends CCComp {
|
||||
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);
|
||||
this.updateTalentItem(itemNode, talentInfo, collection.talents[talentInfo.id as TalentType]);
|
||||
});
|
||||
} 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);
|
||||
this.updateTalentItem(itemNode, talentInfo, collection.talents[talentInfo.id as TalentType]);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -244,7 +243,7 @@ export class TalentsComp extends CCComp {
|
||||
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 level = collection.talents[talentId];
|
||||
let talentInfo = TalentConfig.talents.find(t => t.id === talentId);
|
||||
if (talentInfo) {
|
||||
for (let i = 0; i < level; i++) {
|
||||
@@ -256,15 +255,14 @@ export class TalentsComp extends CCComp {
|
||||
}
|
||||
|
||||
// 重置天赋等级并返还碎片
|
||||
collection.talents = {};
|
||||
if (!collection.talent_fragments) {
|
||||
collection.talent_fragments = {};
|
||||
for (let k in collection.talents) {
|
||||
collection.talents[k as any as TalentType] = 0;
|
||||
}
|
||||
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;
|
||||
collection.talent_fragments[key] += amount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,30 +291,30 @@ export class TalentsComp extends CCComp {
|
||||
|
||||
/** 判断当前库存是否满足一次升级 */
|
||||
private hasEnoughFragments(cost: { type: TalentFragmentType; amount: number }): boolean {
|
||||
const bag = smc.collection.talent_fragments || {};
|
||||
return (bag[cost.type] || 0) >= cost.amount;
|
||||
const bag = smc.collection.talent_fragments;
|
||||
return bag[cost.type] >= 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);
|
||||
const bag = smc.collection.talent_fragments;
|
||||
bag[cost.type] = Math.max(0, bag[cost.type] - 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 own = smc.collection.talent_fragments[cost.type];
|
||||
const fragmentName = info ? info.name : `碎片${cost.type}`;
|
||||
return `${fragmentName} ${own}/${cost.amount}`;
|
||||
}
|
||||
|
||||
/** 顶部碎片库存摘要 */
|
||||
private getFragmentSummaryText(): string {
|
||||
const bag = smc.collection.talent_fragments || {};
|
||||
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}`)
|
||||
.filter(fragment => bag[fragment.id] > 0)
|
||||
.map(fragment => `${fragment.name}:${bag[fragment.id]}`)
|
||||
.join(" ");
|
||||
return summary ? `碎片: ${summary}` : "碎片: 暂无";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user