| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- import { _decorator, resources, JsonAsset } from 'cc';
- import { BaseSingleton } from './BaseSingleton';
- const { ccclass, property } = _decorator;
- // 武器配置接口
- export interface WeaponConfig {
- id: string;
- name: string;
- type: string;
- rarity: string;
- weight: number;
- stats: {
- damage: number;
- fireRate: number;
- range: number;
- bulletSpeed: number;
- penetration: number;
- accuracy: number;
- explosionRadius?: number;
- explosionDamage?: number;
- burnDuration?: number;
- burnDamage?: number;
- returnSpeed?: number;
- homingStrength?: number;
- };
- bulletConfig: {
- bulletType: string;
- bulletPrefab: string;
- hitEffect: string;
- trailEffect?: string;
- ricochetCount?: number;
- ricochetAngle?: number;
- explosionDelay?: number;
- burnEffect?: string;
- arcHeight?: number;
- returnDelay?: number;
- homingDelay?: number;
- };
- attackPattern: {
- type: string;
- projectileCount: number;
- spreadAngle: number;
- burstCount: number;
- burstDelay: number;
- };
- visualConfig: {
- weaponSprites: {
- [size: string]: string;
- };
- muzzleFlash: string;
- fireSound: string;
- };
- }
- // 敌人配置接口
- export interface EnemyConfig {
- id: string;
- name: string;
- type: string;
- rarity: string;
- weight: number;
- stats: {
- health: number;
- speed: number;
- damage: number;
- attackRange: number;
- attackSpeed: number;
- defense: number;
- coinReward: number;
- explosionDamage?: number;
- explosionRadius?: number;
- };
- movement: {
- type: string;
- pattern: string;
- speedVariation: number;
- swayAmplitude?: number;
- swayFrequency?: number;
- };
- combat: {
- attackType: string;
- attackDelay: number;
- attackCooldown: number;
- weaponType?: string;
- projectileType?: string;
- projectileSpeed?: number;
- canBlock: boolean;
- blockChance: number;
- };
- visualConfig: {
- spritePrefab: string;
- animations: {
- [state: string]: string;
- };
- scale: number;
- flipX: boolean;
- weapon?: string;
- armor?: string;
- prop?: string;
- };
- audioConfig: {
- [sound: string]: string;
- };
- specialAbilities: string[];
- stealthConfig?: {
- stealthDuration: number;
- stealthCooldown: number;
- revealOnAttack: boolean;
- visibilityAlpha: number;
- };
- armorConfig?: {
- armorHealth: number;
- armorReduction: number;
- breakThreshold: number;
- };
- explosionConfig?: {
- explosionDelay: number;
- explosionEffect: string;
- damageRadius: number;
- knockbackForce: number;
- };
- bossConfig?: {
- phases: number;
- phaseHealthThreshold: number;
- enrageBonus?: {
- speed: number;
- damage: number;
- attackSpeed: number;
- };
- summonAbility?: {
- minionType: string;
- summonCount: number;
- summonCooldown: number;
- };
- laserAbility?: {
- damage: number;
- range: number;
- chargeTime: number;
- cooldown: number;
- };
- };
- projectileConfig?: {
- bulletPrefab: string;
- hitEffect: string;
- trailEffect?: string;
- };
- }
- @ccclass('ConfigManager')
- export class ConfigManager extends BaseSingleton {
- // 仅用于类型声明,实例由 BaseSingleton 在运行时动态维护
- public static _instance: ConfigManager;
-
- private weaponsConfig: any = null;
- private enemiesConfig: any = null;
- private configLoaded: boolean = false;
-
- // 武器权重缓存
- private weaponWeightedList: WeaponConfig[] = [];
- // 敌人权重缓存
- private enemyWeightedList: EnemyConfig[] = [];
- /**
- * BaseSingleton 首次实例化回调
- */
- protected init() {
- this.loadConfigs();
- }
- // 加载所有配置文件
- private async loadConfigs() {
- this.configLoaded = false;
-
- try {
- // 加载武器配置
- await this.loadWeaponsConfig();
- // 加载敌人配置
- await this.loadEnemiesConfig();
-
- this.configLoaded = true;
- } catch (error) {
- this.configLoaded = false;
- }
- }
- // 加载武器配置
- private loadWeaponsConfig(): Promise<void> {
- return new Promise((resolve, reject) => {
- console.log('加载武器配置...');
- resources.load('data/weapons', JsonAsset, (err, asset) => {
- if (err) {
- console.error('武器配置文件加载失败:', err);
- reject(err);
- return;
- }
-
- if (!asset || !asset.json) {
- console.error('武器配置文件内容为空');
- reject(new Error('武器配置文件内容为空'));
- return;
- }
-
- this.weaponsConfig = asset.json as any;
- console.log('✅ 武器配置加载成功');
- this.buildWeaponWeightedList();
- resolve();
- });
- });
- }
- // 加载敌人配置
- private loadEnemiesConfig(): Promise<void> {
- return new Promise((resolve, reject) => {
- console.log('加载敌人配置...');
- resources.load('data/enemies', JsonAsset, (err, asset) => {
- if (err) {
- console.error('敌人配置文件加载失败:', err);
- reject(err);
- return;
- }
-
- if (!asset || !asset.json) {
- console.error('敌人配置文件内容为空');
- reject(new Error('敌人配置文件内容为空'));
- return;
- }
- this.enemiesConfig = asset.json as any;
- console.log('✅ 敌人配置加载成功');
- this.buildEnemyWeightedList();
- resolve();
- });
- });
- }
- // 构建武器权重列表
- private buildWeaponWeightedList() {
- if (!this.weaponsConfig) return;
-
- this.weaponWeightedList = [];
- this.weaponsConfig.weapons.forEach(weapon => {
- // 根据权重添加多次到列表中
- for (let i = 0; i < weapon.weight; i++) {
- this.weaponWeightedList.push(weapon);
- }
- });
-
- console.log('武器权重列表构建完成,总权重:', this.weaponWeightedList.length);
- }
- // 构建敌人权重列表
- private buildEnemyWeightedList() {
- if (!this.enemiesConfig) return;
-
- this.enemyWeightedList = [];
- this.enemiesConfig.enemies.forEach(enemy => {
- // 根据权重添加多次到列表中
- for (let i = 0; i < enemy.weight; i++) {
- this.enemyWeightedList.push(enemy);
- }
- });
-
- console.log('敌人权重列表构建完成,总权重:', this.enemyWeightedList.length);
- }
- // 随机获取武器配置
- public getRandomWeapon(rarity?: string): WeaponConfig | null {
- if (!this.weaponsConfig || this.weaponWeightedList.length === 0) {
- console.warn('武器配置未加载或为空');
- return null;
- }
- if (rarity) {
- // 按稀有度筛选
- const filteredWeapons = this.weaponsConfig.weapons.filter(weapon => weapon.rarity === rarity);
- if (filteredWeapons.length === 0) {
- console.warn(`没有找到稀有度为 ${rarity} 的武器`);
- return null;
- }
- const randomIndex = Math.floor(Math.random() * filteredWeapons.length);
- return filteredWeapons[randomIndex];
- }
- // 从权重列表中随机选择
- const randomIndex = Math.floor(Math.random() * this.weaponWeightedList.length);
- return this.weaponWeightedList[randomIndex];
- }
- // 随机获取敌人配置
- public getRandomEnemy(rarity?: string): EnemyConfig | null {
- if (!this.enemiesConfig || this.enemyWeightedList.length === 0) {
- console.warn('敌人配置未加载或为空');
- return null;
- }
- if (rarity) {
- // 按稀有度筛选
- const filteredEnemies = this.enemiesConfig.enemies.filter(enemy => enemy.rarity === rarity);
- if (filteredEnemies.length === 0) {
- console.warn(`没有找到稀有度为 ${rarity} 的敌人`);
- return null;
- }
- const randomIndex = Math.floor(Math.random() * filteredEnemies.length);
- return filteredEnemies[randomIndex];
- }
- // 从权重列表中随机选择
- const randomIndex = Math.floor(Math.random() * this.enemyWeightedList.length);
- return this.enemyWeightedList[randomIndex];
- }
- // 根据ID获取武器配置
- public getWeaponById(id: string): WeaponConfig | null {
- if (!this.weaponsConfig) return null;
- return this.weaponsConfig.weapons.find(weapon => weapon.id === id) || null;
- }
- // 根据ID获取敌人配置
- public getEnemyById(id: string): EnemyConfig | null {
- if (!this.enemiesConfig) return null;
- return this.enemiesConfig.enemies.find(enemy => enemy.id === id) || null;
- }
- /**
- * 获取敌人名称到ID的映射
- */
- public getNameToIdMapping(): { [key: string]: string } | null {
- if (!this.enemiesConfig) return null;
- return this.enemiesConfig.nameToIdMapping || null;
- }
- // 获取所有武器配置
- public getAllWeapons(): WeaponConfig[] {
- return this.weaponsConfig?.weapons || [];
- }
- // 获取所有敌人配置
- public getAllEnemies(): EnemyConfig[] {
- return this.enemiesConfig?.enemies || [];
- }
- // 根据稀有度获取武器列表
- public getWeaponsByRarity(rarity: string): WeaponConfig[] {
- if (!this.weaponsConfig) return [];
- return this.weaponsConfig.weapons.filter(weapon => weapon.rarity === rarity);
- }
- // 根据稀有度获取敌人列表
- public getEnemiesByRarity(rarity: string): EnemyConfig[] {
- if (!this.enemiesConfig) return [];
- return this.enemiesConfig.enemies.filter(enemy => enemy.rarity === rarity);
- }
- // 获取方块尺寸列表(已更新为形状ID)
- public getBlockSizes(): string[] {
- if (!this.weaponsConfig || !this.weaponsConfig.blockSizes) {
- return ['I', 'H-I', 'L', 'S', 'D-T'];
- }
- // 从blockSizes配置中提取形状ID
- return this.weaponsConfig.blockSizes.map((shape: any) => shape.id);
- }
-
- // 获取方块形状配置列表
- public getBlockShapes(): any[] {
- if (!this.weaponsConfig || !this.weaponsConfig.blockSizes) {
- console.warn('武器配置或方块形状配置未加载');
- return [];
- }
- return this.weaponsConfig.blockSizes;
- }
- // 获取波次进展配置
- public getWaveProgression(): any {
- return this.enemiesConfig?.waveProgression || {};
- }
- // 根据波次获取合适的敌人
- public getEnemyForWave(waveNumber: number): EnemyConfig | null {
- if (!this.enemiesConfig) return null;
-
- const progression = this.enemiesConfig.waveProgression;
- let enemyPool: string[] = [];
-
- if (waveNumber <= 5) {
- enemyPool = progression.earlyWaves || [];
- } else if (waveNumber <= 15) {
- enemyPool = [...(progression.earlyWaves || []), ...(progression.midWaves || [])];
- } else if (waveNumber <= 25) {
- enemyPool = [...(progression.midWaves || []), ...(progression.lateWaves || [])];
- } else {
- enemyPool = [...(progression.lateWaves || []), ...(progression.bossWaves || [])];
- }
-
- if (enemyPool.length === 0) {
- return this.getRandomEnemy();
- }
-
- const randomId = enemyPool[Math.floor(Math.random() * enemyPool.length)];
- return this.getEnemyById(randomId);
- }
- // 检查配置是否已加载
- public isConfigLoaded(): boolean {
- return this.configLoaded;
- }
- }
|