refactor(map): 重构局内经济系统,统一封装金币操作逻辑
新建MissionEconomy类作为局内经济统一管理入口,整合原分散在HInfoComp、MissionComp、CardComp、MissionCardComp中的金币计算、消费、收益统计逻辑,移除各组件中重复的getMissionCoin、setMissionCoin、getRefreshCost等工具方法,统一维护评分系统的金币统计,提升代码可维护性。
This commit is contained in:
@@ -33,6 +33,7 @@ import { smc } from "../common/SingletonModuleComp";
|
||||
import { UIID } from "../common/config/GameUIConfig";
|
||||
import { HeroAttrsComp } from "../hero/HeroAttrsComp";
|
||||
import { TalentType } from "../common/config/TalentSet";
|
||||
import { MissionEconomy } from "./MissionEconomy";
|
||||
|
||||
|
||||
|
||||
@@ -291,19 +292,7 @@ export class CardComp extends CCComp {
|
||||
useCard(): CardConfig | null {
|
||||
if (!this.cardData || this.isUsing) return null;
|
||||
const cardCost = this.card_cost;
|
||||
const currentCoin = this.getMissionCoin();
|
||||
// 金币不足 → 提示并回弹
|
||||
if (currentCoin < cardCost) {
|
||||
oops.gui.toast(`金币不足,召唤需要${cardCost}`);
|
||||
this.playReboundAnim();
|
||||
mLogger.log(this.debugMode, "CardComp", "use card coin not enough", {
|
||||
uuid: this.cardData.uuid,
|
||||
type: this.cardData.type,
|
||||
cardCost,
|
||||
currentCoin
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
// 英雄卡特殊校验:通过 guard 对象实现"可取消"模式
|
||||
if (this.cardData.type === CardType.Hero) {
|
||||
const guard = {
|
||||
@@ -319,16 +308,24 @@ export class CardComp extends CCComp {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// 扣除金币
|
||||
this.setMissionCoin(currentCoin - cardCost);
|
||||
// 【评分系统 - 效率分】记录购卡消耗的金币,以及刷新后的选中卡次数(命中率分子)
|
||||
smc.vmdata.scores.gold_spent += cardCost;
|
||||
|
||||
// 使用统一经济管理入口消费金币
|
||||
const success = MissionEconomy.spendCoin(cardCost);
|
||||
if (!success) {
|
||||
oops.gui.toast(`金币不足,召唤需要${cardCost}`);
|
||||
this.playReboundAnim();
|
||||
mLogger.log(this.debugMode, "CardComp", "use card coin not enough", {
|
||||
uuid: this.cardData.uuid,
|
||||
type: this.cardData.type,
|
||||
cardCost,
|
||||
currentCoin: MissionEconomy.getCoin()
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
// 【评分系统 - 效率分】记录刷新后的选中卡次数(命中率分子)
|
||||
smc.vmdata.scores.refresh_hit_count++;
|
||||
|
||||
oops.message.dispatchEvent(GameEvent.CoinAdd, {
|
||||
syncOnly: true,
|
||||
delta: -cardCost
|
||||
});
|
||||
// 标记使用中,阻止并发操作
|
||||
this.isUsing = true;
|
||||
const used = this.cardData;
|
||||
@@ -336,7 +333,7 @@ export class CardComp extends CCComp {
|
||||
uuid: used.uuid,
|
||||
type: used.type,
|
||||
cost: cardCost,
|
||||
leftCoin: this.getMissionCoin()
|
||||
leftCoin: MissionEconomy.getCoin()
|
||||
});
|
||||
// 播放消失动画 → 动画结束后清槽并分发效果
|
||||
this.playUseDisappearAnim(() => {
|
||||
@@ -1194,18 +1191,7 @@ export class CardComp extends CCComp {
|
||||
|
||||
// ======================== 数据访问 ========================
|
||||
|
||||
/** 从全局单例获取当前局内金币数量 */
|
||||
private getMissionCoin(): number {
|
||||
const missionData = smc?.vmdata?.mission_data;
|
||||
return Math.max(0, Math.floor(missionData?.coin ?? 0));
|
||||
}
|
||||
|
||||
/** 设置当前局内金币数量(自动向下取整并 clamp 至 >= 0) */
|
||||
private setMissionCoin(value: number) {
|
||||
const missionData = smc?.vmdata?.mission_data;
|
||||
if (!missionData) return;
|
||||
missionData.coin = Math.max(0, Math.floor(value));
|
||||
}
|
||||
|
||||
/** ECS 组件移除时的释放钩子:销毁节点 */
|
||||
reset() {
|
||||
|
||||
Reference in New Issue
Block a user