rp_11001/assets/Loading/scripts/manager/GameDataManager.ts
2025-12-18 15:27:34 +08:00

246 lines
9.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
if (name == '') {
return null
}
return this._frameTypeCache.get(name);
}
/**
* 预加载框架类型缓存
* @returns Promise 在加载完成时解析
*/
public preloadFrameTypeCache(): Promise<void> {
// 定义帧类型名称
const frameNames = [
'13_2',
'13_3',
'13_4',
];
const blurFrameNames = [
'13_2_mh',
];
// 返回Promise让调用者可以等待加载完成
return new Promise<void>((resolve, reject) => {
// 加载Game bundle
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 errors: string[] = [];
// 需要完成的加载任务数量
let tasksToComplete = 2; // 两个图集
// 检查是否所有任务都完成
const checkAllTasksCompleted = () => {
tasksToComplete--;
if (tasksToComplete <= 0) {
if (errors.length > 0) {
// 有错误发生拒绝Promise
reject(new Error('Frame type loading failed: ' + errors.join('; ')));
} else {
// 所有加载成功解析Promise
resolve();
}
}
};
// 加载普通图集
const path = 'rp_11001/symbol/symbol';
bundle.load(path, SpriteAtlas, (atlasErr, atlas: SpriteAtlas) => {
if (atlasErr) {
const errorMsg = `Failed to load normal frame atlas: ${atlasErr.message}`;
console.error('[GameDataManager]', errorMsg);
errors.push(errorMsg);
checkAllTasksCompleted();
return;
}
if (!atlas) {
const errorMsg = 'Normal frame atlas is null';
console.error('[GameDataManager]', errorMsg);
errors.push(errorMsg);
checkAllTasksCompleted();
return;
}
// 检查是否所有需要的帧都存在
const missingFrames: string[] = [];
// 遍历普通帧并缓存直接使用名称作为key
for (const name of frameNames) {
const spriteFrame = atlas.getSpriteFrame(name);
if (spriteFrame) {
this._frameTypeCache.set(name, spriteFrame);
} else {
missingFrames.push(name);
}
}
if (missingFrames.length > 0) {
const errorMsg = `Missing normal frames: ${missingFrames.join(', ')}`;
console.warn('[GameDataManager]', errorMsg);
errors.push(errorMsg);
}
checkAllTasksCompleted();
});
// 加载模糊图集
const blurPath = 'rp_11001/symbol_mh/symbol';
bundle.load(blurPath, SpriteAtlas, (blurAtlasErr, blurAtlas: SpriteAtlas) => {
if (blurAtlasErr) {
const errorMsg = `Failed to load blur frame atlas: ${blurAtlasErr.message}`;
console.error('[GameDataManager]', errorMsg);
errors.push(errorMsg);
checkAllTasksCompleted();
return;
}
if (!blurAtlas) {
const errorMsg = 'Blur frame atlas is null';
console.error('[GameDataManager]', errorMsg);
errors.push(errorMsg);
checkAllTasksCompleted();
return;
}
// 检查是否所有需要的模糊帧都存在
const missingBlurFrames: string[] = [];
// 遍历模糊帧并缓存直接使用名称作为key
for (const name of blurFrameNames) {
const spriteFrame = blurAtlas.getSpriteFrame(name);
if (spriteFrame) {
this._frameTypeCache.set(name, spriteFrame);
} else {
missingBlurFrames.push(name);
}
}
if (missingBlurFrames.length > 0) {
const errorMsg = `Missing blur frames: ${missingBlurFrames.join(', ')}`;
console.warn('[GameDataManager]', errorMsg);
errors.push(errorMsg);
}
checkAllTasksCompleted();
});
});
});
}
// 添加图标缓存
private _symbolCache: Map<string, SpriteFrame> = new Map();
getSymbolCache(name: string): SpriteFrame {
return this._symbolCache.get(name);
}
/**
* 预加载图标缓存
*/
public preloadSymbolCache(): Promise<void> {
// 直接定义图标名称数组
const symbolNames = [
'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '10'
];
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 = 'rp_11001/symbol/symbol';
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];
}
}