import { _decorator, Label, Node } from 'cc'; import { Singleton } from './Singleton'; import { Constants } from '../../data/Constants'; import { userIns } from '../../data/UserData'; import { Utils } from '../../utils/Utils'; const { ccclass, property } = _decorator; @ccclass('StateManager') class StateManager extends Singleton{ //存储状态字段与 UI 组件的绑定关系 private uiBindings: Map = new Map(); /** * 注册UI组件 * @param field 注册的key uiName * @param label 具体的文本 */ public registerUI(field: string, label: Label|any): void { if(!this.uiBindings.has(field)) { this.uiBindings.set(field, []); } //将UI组件添加到绑定列表 let uiArr:Array = this.uiBindings.get(field); if(!uiArr.includes(label)){ uiArr.push(label); } //初始化UI显示 this.updateSingleUI(field, label); } /** * 全局更新所有绑定该字段 * @param field 注册的key uiName */ public updateAllUI(field: string): void { let labels:Array = this.uiBindings.get(field); if (labels) { labels.forEach(label => { this.updateSingleUI(field, label); }); } } /** * 更新单个UI组件 * @param field * @param label */ private updateSingleUI(field: string, com: Label|any): void { if(field == Constants.gold){//金币数量 com.string = `${Utils.numberToString(userIns.data.gold)}`; }else if(field == Constants.diamond){//钻石数量 com.string = `${Utils.numberToString(userIns.data.diamond)}`; }else if(field == Constants.levelTask){//任务数据变化 } } } //全局单例 export const stateMgr = StateManager.ins();