Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
import { _decorator, assetManager, Game, SpriteAtlas, SpriteFrame } from 'cc';
|
|
|
|
export class GameDataManager {
|
|
private static _instance: GameDataManager = null;
|
|
static get instance(): GameDataManager {
|
|
if (!this._instance) {
|
|
this._instance = new GameDataManager();
|
|
}
|
|
return this._instance;
|
|
}
|
|
|
|
|
|
// 游戏信息
|
|
private _gameInfo: any = null;
|
|
set gameInfo(info: any) { this._gameInfo = info; this._frb = info.Frb; }
|
|
get gameInfo(): any { return this._gameInfo; }
|
|
|
|
|
|
private _frb: any = null;
|
|
set frb(info: any) { this._frb = info; }
|
|
get frb(): any { return this._frb; }
|
|
|
|
// 自动次数
|
|
private _autoCount: number = 0;
|
|
set autoCount(count: number) { this._autoCount = count; }
|
|
get autoCount(): number { return this._autoCount; }
|
|
|
|
|
|
// 缓存图标框
|
|
private _frameTypeCache: Map<string, SpriteFrame> = new Map();
|
|
|
|
getFrameTypeCache(name: string): SpriteFrame {
|
|
return this._frameTypeCache.get(name);
|
|
}
|
|
// 添加图标缓存
|
|
private _symbolCache: Map<string, SpriteFrame> = new Map();
|
|
|
|
getSymbolCache(name: string): SpriteFrame {
|
|
return this._symbolCache.get(name);
|
|
}
|
|
|
|
/**
|
|
* 预加载图标缓存
|
|
*/
|
|
// public preloadSymbolCache(): Promise<void> {
|
|
// // 直接定义图标名称数组
|
|
// const symbolNames = [
|
|
// 's03', 's04', 's05', 's06', 's07',
|
|
// 's08', 's09', 's10', 's11', 's12', 's13'
|
|
// ];
|
|
|
|
// return new Promise<void>((resolve, reject) => {
|
|
// assetManager.loadBundle('Game', (bundleErr, bundle) => {
|
|
// if (bundleErr) {
|
|
// console.error('[GameDataManager] Failed to load Game bundle:', bundleErr);
|
|
// reject(new Error('Failed to load Game bundle: ' + bundleErr.message));
|
|
// return;
|
|
// }
|
|
|
|
// const path = 'textures/Images/icons/symbols_01';
|
|
// bundle.load(path, SpriteAtlas, (atlasErr, atlas: SpriteAtlas) => {
|
|
// if (atlasErr) {
|
|
// const errorMsg = `Failed to load symbols atlas: ${atlasErr.message}`;
|
|
// console.error('[GameDataManager]', errorMsg);
|
|
// reject(new Error(errorMsg));
|
|
// return;
|
|
// }
|
|
|
|
// if (!atlas) {
|
|
// const errorMsg = 'Symbols atlas is null';
|
|
// console.error('[GameDataManager]', errorMsg);
|
|
// reject(new Error(errorMsg));
|
|
// return;
|
|
// }
|
|
|
|
// // 检查是否所有需要的图标都存在
|
|
// const missingSymbols: string[] = [];
|
|
|
|
// // 遍历图标并缓存
|
|
// for (const name of symbolNames) {
|
|
// const spriteFrame = atlas.getSpriteFrame(name);
|
|
// if (spriteFrame) {
|
|
// this._symbolCache.set(name, spriteFrame);
|
|
// } else {
|
|
// missingSymbols.push(name);
|
|
// }
|
|
// }
|
|
|
|
// if (missingSymbols.length > 0) {
|
|
// const errorMsg = `Missing symbols: ${missingSymbols.join(', ')}`;
|
|
// console.warn('[GameDataManager]', errorMsg);
|
|
// reject(new Error(errorMsg));
|
|
// return;
|
|
// }
|
|
|
|
// resolve();
|
|
// });
|
|
// });
|
|
// });
|
|
// }
|
|
|
|
// 获取随机图标
|
|
getRandomSymbol(): SpriteFrame {
|
|
const symbols = Array.from(this._symbolCache.values());
|
|
if (symbols.length === 0) return null;
|
|
|
|
const randomIndex = Math.floor(Math.random() * symbols.length);
|
|
return symbols[randomIndex];
|
|
}
|
|
} |