新建MissionEconomy类作为局内经济统一管理入口,整合原分散在HInfoComp、MissionComp、CardComp、MissionCardComp中的金币计算、消费、收益统计逻辑,移除各组件中重复的getMissionCoin、setMissionCoin、getRefreshCost等工具方法,统一维护评分系统的金币统计,提升代码可维护性。
113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
import { smc } from "../common/SingletonModuleComp";
|
||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||
import { GameEvent } from "../common/config/GameEvent";
|
||
import { HeroAttrsComp } from "../hero/HeroAttrsComp";
|
||
import { FieldSkillType } from "../common/config/SkillSet";
|
||
import { TalentType } from "../common/config/TalentSet";
|
||
import { FightSet } from "../common/config/GameSet";
|
||
|
||
/**
|
||
* 局内经济统一管理类
|
||
* 负责统一处理金币的增减、各种费用的计算以及战绩评分中的金币统计。
|
||
*/
|
||
export class MissionEconomy {
|
||
/** 获取当前金币 */
|
||
static getCoin(): number {
|
||
return Math.max(0, Math.floor(smc.vmdata.mission_data.coin ?? 0));
|
||
}
|
||
|
||
/** 增加金币并更新统计 */
|
||
static addCoin(amount: number) {
|
||
if (amount <= 0) return;
|
||
amount = Math.floor(amount);
|
||
smc.vmdata.mission_data.coin = this.getCoin() + amount;
|
||
smc.vmdata.scores.gold_earned += amount;
|
||
|
||
// 发送 UI 更新事件,这里我们统一使用 syncOnly 通知 UI 刷新即可
|
||
oops.message.dispatchEvent(GameEvent.CoinAdd, { delta: amount, syncOnly: true });
|
||
}
|
||
|
||
/** 消费金币并更新统计 */
|
||
static spendCoin(amount: number): boolean {
|
||
if (amount <= 0) return true;
|
||
amount = Math.floor(amount);
|
||
const current = this.getCoin();
|
||
if (current < amount) return false;
|
||
|
||
smc.vmdata.mission_data.coin = current - amount;
|
||
smc.vmdata.scores.gold_spent += amount;
|
||
|
||
// 发送 UI 更新事件
|
||
oops.message.dispatchEvent(GameEvent.CoinAdd, { delta: -amount, syncOnly: true });
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 计算卡牌刷新费用
|
||
* @param baseCost 基础费用,默认为1
|
||
*/
|
||
static getRefreshCost(baseCost: number = 1): number {
|
||
let cost = baseCost;
|
||
const discount = HeroAttrsComp.getTalentValue(TalentType.RefreshDiscount);
|
||
cost = Math.max(0, cost - discount);
|
||
return Math.floor(cost);
|
||
}
|
||
|
||
/**
|
||
* 执行刷新卡牌并扣除金币
|
||
*/
|
||
static executeRefresh(baseCost: number = 1): boolean {
|
||
const cost = this.getRefreshCost(baseCost);
|
||
const success = this.spendCoin(cost);
|
||
if (success) {
|
||
smc.vmdata.scores.refresh_count++;
|
||
}
|
||
return success;
|
||
}
|
||
|
||
/**
|
||
* 计算英雄出售金币
|
||
*/
|
||
static getSellGold(): number {
|
||
const baseSellGold = 1; // 基础卖出金币
|
||
const goldBoost = HeroAttrsComp.getFieldSkillTotalValue(FieldSkillType.SellGold);
|
||
let totalSellGold = baseSellGold + goldBoost;
|
||
// 应用天赋 SellBonus (增加数值)
|
||
const bonusGold = HeroAttrsComp.getTalentValue(TalentType.SellBonus);
|
||
if (bonusGold > 0) {
|
||
totalSellGold += bonusGold;
|
||
}
|
||
return Math.floor(totalSellGold);
|
||
}
|
||
|
||
/**
|
||
* 执行出售英雄并增加金币
|
||
*/
|
||
static executeSellHero(): number {
|
||
const gold = this.getSellGold();
|
||
this.addCoin(gold);
|
||
return gold;
|
||
}
|
||
|
||
/**
|
||
* 计算每波基础金币收益
|
||
*/
|
||
static getWaveGold(baseReward: number): number {
|
||
let reward = Math.max(0, Math.floor(baseReward));
|
||
const goldBoost = HeroAttrsComp.getFieldSkillTotalValue(FieldSkillType.WaveGold);
|
||
reward += goldBoost;
|
||
return reward;
|
||
}
|
||
|
||
/**
|
||
* 执行发放每波金币
|
||
*/
|
||
static executeWaveGold(baseReward: number): number {
|
||
const reward = this.getWaveGold(baseReward);
|
||
if (reward > 0) {
|
||
this.addCoin(reward);
|
||
}
|
||
return reward;
|
||
}
|
||
}
|