| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import { JsonAsset, resources } from 'cc';
- import { WeaponConfig } from './BulletTypes';
- /**
- * 轻量配置加载器:管理 weapons.json 数据
- * - 可通过 setWeaponsData 直接注入(推荐迁移时使用)
- * - 可从 resources 目录加载(路径示例:data/weapons)
- */
- export class ConfigLoader {
- private static weaponsData: { weapons: WeaponConfig[] } | null = null;
- public static setWeaponsData(data: { weapons: WeaponConfig[] } | null) {
- this.weaponsData = data;
- }
- public static async loadWeaponsFromResources(path: string): Promise<void> {
- return new Promise((resolve, reject) => {
- resources.load(path, JsonAsset, (err, asset) => {
- if (err) {
- reject(err);
- return;
- }
- try {
- const json = asset.json as any;
- if (!json || !json.weapons) {
- throw new Error('Invalid weapons.json structure');
- }
- this.weaponsData = json as { weapons: WeaponConfig[] };
- resolve();
- } catch (e) {
- reject(e);
- }
- });
- });
- }
- public static getWeaponConfig(id: string): WeaponConfig | null {
- if (!this.weaponsData || !this.weaponsData.weapons) return null;
- const w = this.weaponsData.weapons.find(x => x.id === id);
- return w ?? null;
- }
- public static clearCache() {
- this.weaponsData = null;
- }
- }
|