feat: 提交资源
This commit is contained in:
64
assets/games/scripts/currency/CurrencyManager.ts
Normal file
64
assets/games/scripts/currency/CurrencyManager.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { _decorator } from "cc";
|
||||
|
||||
import { EventManager } from "@max-studio/core/event/EventManager";
|
||||
import { Singleton } from "@max-studio/core/Singleton";
|
||||
import LogUtils from "@max-studio/core/utils/LogUtils";
|
||||
|
||||
import { CurrencyEventMessage, CurrencyType } from "./Types";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("CurrencyManager")
|
||||
export class CurrencyManager extends Singleton {
|
||||
private currencyValues: Record<CurrencyType, number> = {} as Record<CurrencyType, number>;
|
||||
|
||||
/** 获取货币值 */
|
||||
public getCurrencyValue(type: CurrencyType): number {
|
||||
return this.currencyValues[type] || 0;
|
||||
}
|
||||
|
||||
/** 设置货币值 */
|
||||
public setCurrencyValue(type: CurrencyType, value: number, isNotify: boolean = true) {
|
||||
this.currencyValues[type] = value;
|
||||
if (isNotify) {
|
||||
EventManager.getInstance().emit(CurrencyEventMessage.CURRENCY_VALUE_CHANGED, type, value);
|
||||
}
|
||||
}
|
||||
|
||||
/** 格式化货币显示 */
|
||||
public formatCurrencyDisplay(type: CurrencyType, value?: number): string {
|
||||
const amount = value ?? this.getCurrencyValue(type);
|
||||
|
||||
// 根据货币类型返回不同的格式
|
||||
if (amount >= 1000000) {
|
||||
return `${(amount / 1000000).toFixed(1)}M`;
|
||||
} else if (amount >= 1000) {
|
||||
return `${(amount / 1000).toFixed(1)}K`;
|
||||
}
|
||||
|
||||
return amount.toString();
|
||||
}
|
||||
|
||||
/** 检查货币是否足够 */
|
||||
public isCurrencyEnough(type: CurrencyType, amount: number): boolean {
|
||||
return this.getCurrencyValue(type) >= amount;
|
||||
}
|
||||
|
||||
/** 消耗货币 */
|
||||
public consumeCurrency(type: CurrencyType, amount: number, isNotify: boolean = true): boolean {
|
||||
if (amount <= 0) {
|
||||
LogUtils.warn(`[CurrencyManager] 消耗货币数量必须大于0: ${amount}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.isCurrencyEnough(type, amount)) {
|
||||
LogUtils.warn(`[CurrencyManager] 货币不足: ${type}, 需要: ${amount}, 当前: ${this.getCurrencyValue(type)}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentValue = this.getCurrencyValue(type);
|
||||
const newValue = currentValue - amount;
|
||||
this.setCurrencyValue(type, newValue, isNotify);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
9
assets/games/scripts/currency/CurrencyManager.ts.meta
Normal file
9
assets/games/scripts/currency/CurrencyManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0474457b-c003-444f-b68b-9279920ba444",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
9
assets/games/scripts/currency/Types.ts
Normal file
9
assets/games/scripts/currency/Types.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export enum CurrencyType {
|
||||
ENERGY = "energy",
|
||||
GEM = "gem",
|
||||
GOLD = "gold",
|
||||
}
|
||||
|
||||
export enum CurrencyEventMessage {
|
||||
CURRENCY_VALUE_CHANGED = 'currency_value_changed',
|
||||
}
|
||||
9
assets/games/scripts/currency/Types.ts.meta
Normal file
9
assets/games/scripts/currency/Types.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "4eb1626b-f4b8-4566-b197-30c135c7e3a8",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
9
assets/games/scripts/currency/components.meta
Normal file
9
assets/games/scripts/currency/components.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "ea4b3885-21a2-4076-8162-00bd5ee2c090",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
46
assets/games/scripts/currency/components/CurrencyItem.ts
Normal file
46
assets/games/scripts/currency/components/CurrencyItem.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { _decorator, Component, Enum, Label } from "cc";
|
||||
|
||||
import { EventManager } from "@max-studio/core/event/EventManager";
|
||||
import UIManager from "@max-studio/core/ui/UIManager";
|
||||
import LogUtils from "@max-studio/core/utils/LogUtils";
|
||||
|
||||
import { ShopUI } from "../../uis/ShopUI";
|
||||
import { CurrencyManager } from "../CurrencyManager";
|
||||
import { CurrencyEventMessage, CurrencyType } from "../Types";
|
||||
import { onEvent } from "@max-studio/core/decorators";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("CurrencyItem")
|
||||
export class CurrencyItem extends Component {
|
||||
@property({ type: Enum(CurrencyType) })
|
||||
public type: CurrencyType = CurrencyType.ENERGY;
|
||||
|
||||
@property(Label)
|
||||
public label: Label = null!;
|
||||
|
||||
protected onLoad(): void {
|
||||
this.node.onClick(this.onGotoCurrencyShop, this);
|
||||
}
|
||||
|
||||
private onGotoCurrencyShop() {
|
||||
LogUtils.log("点击了货币商店:", this.type);
|
||||
void UIManager.getInstance().openUI(ShopUI, this.type);
|
||||
}
|
||||
|
||||
@onEvent(CurrencyEventMessage.CURRENCY_VALUE_CHANGED)
|
||||
private onCurrencyValueChanged(type: CurrencyType, value: number) {
|
||||
if (type !== this.type) {
|
||||
return;
|
||||
}
|
||||
this.label.string = CurrencyManager.getInstance().formatCurrencyDisplay(this.type, value);
|
||||
}
|
||||
|
||||
protected onDestroy(): void {
|
||||
EventManager.getInstance().off(CurrencyEventMessage.CURRENCY_VALUE_CHANGED, this.onCurrencyValueChanged, this);
|
||||
}
|
||||
|
||||
protected start(): void {
|
||||
this.label.string = CurrencyManager.getInstance().formatCurrencyDisplay(this.type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "8840c2d8-d822-482c-b68f-2a88cd9c8e72",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user