| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- import { _decorator, Component, Node, Prefab, Vec3, input, Input, EventKeyboard, KeyCode } from 'cc';
- import { EnemySpawnerExample } from './EnemySpawnerExample';
- import { ConfigManager } from './ConfigManager';
- const { ccclass, property } = _decorator;
- /**
- * 敌人生成器测试场景
- * 用于测试敌人配置系统的完整示例
- */
- @ccclass('EnemySpawnerTestScene')
- export class EnemySpawnerTestScene extends Component {
- @property({
- type: EnemySpawnerExample,
- tooltip: '敌人生成器组件'
- })
- public enemySpawner: EnemySpawnerExample = null;
- start() {
- console.log('=== 敌人生成器测试场景启动 ===');
- this.setupTestScene();
- this.setupKeyboardControls();
- }
- private async setupTestScene() {
- // 等待配置管理器加载完成
- const configManager = ConfigManager.getInstance();
-
- // 等待配置加载
- await new Promise<void>((resolve) => {
- const checkConfig = () => {
- if (configManager.isConfigLoaded()) {
- resolve();
- } else {
- this.scheduleOnce(checkConfig, 0.1);
- }
- };
- checkConfig();
- });
- console.log('✅ 配置管理器已加载');
-
- // 显示可用敌人信息
- this.displayEnemyInfo();
-
- // 创建测试说明
- this.createTestInstructions();
- }
- private displayEnemyInfo() {
- const configManager = ConfigManager.getInstance();
- const allEnemies = configManager.getAllEnemies();
-
- console.log('👹 可用敌人列表:');
- allEnemies.forEach(enemy => {
- console.log(` - ${enemy.name} (${enemy.id}) - ${enemy.rarity} - HP:${enemy.stats.health}`);
- });
- // 按稀有度统计
- const rarityCount = {
- common: configManager.getEnemiesByRarity('common').length,
- uncommon: configManager.getEnemiesByRarity('uncommon').length,
- rare: configManager.getEnemiesByRarity('rare').length,
- boss: configManager.getEnemiesByRarity('boss').length
- };
- console.log('📊 敌人稀有度统计:');
- console.log(` 普通: ${rarityCount.common}个`);
- console.log(` 稀有: ${rarityCount.uncommon}个`);
- console.log(` 史诗: ${rarityCount.rare}个`);
- console.log(` BOSS: ${rarityCount.boss}个`);
- }
- private createTestInstructions() {
- console.log('🎯 敌人生成器测试说明:');
- console.log('键盘控制:');
- console.log(' 1 - 生成普通敌人');
- console.log(' 2 - 生成稀有敌人');
- console.log(' 3 - 生成史诗敌人');
- console.log(' B - 生成BOSS');
- console.log(' R - 生成随机敌人');
- console.log(' C - 清除所有敌人');
- console.log(' P - 暂停/恢复生成');
- console.log(' W - 下一波次');
- console.log(' S - 显示状态信息');
- }
- private setupKeyboardControls() {
- input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
- }
- private onKeyDown(event: EventKeyboard) {
- if (!this.enemySpawner) {
- console.warn('敌人生成器未设置');
- return;
- }
- switch (event.keyCode) {
- case KeyCode.DIGIT_1:
- this.enemySpawner.spawnEnemyByRarity('common');
- console.log('🟢 生成普通敌人');
- break;
- case KeyCode.DIGIT_2:
- this.enemySpawner.spawnEnemyByRarity('uncommon');
- console.log('🟡 生成稀有敌人');
- break;
- case KeyCode.DIGIT_3:
- this.enemySpawner.spawnEnemyByRarity('rare');
- console.log('🔵 生成史诗敌人');
- break;
- case KeyCode.KEY_B:
- this.enemySpawner.spawnEnemyByRarity('boss');
- console.log('🔴 生成BOSS');
- break;
- case KeyCode.KEY_R:
- this.spawnRandomEnemy();
- console.log('🎲 生成随机敌人');
- break;
- case KeyCode.KEY_C:
- this.enemySpawner.clearAllEnemies();
- console.log('🧹 清除所有敌人');
- break;
- case KeyCode.KEY_P:
- this.toggleSpawning();
- break;
- case KeyCode.KEY_W:
- this.nextWave();
- break;
- case KeyCode.KEY_S:
- this.showStatus();
- break;
- }
- }
- private spawnRandomEnemy() {
- // 生成随机敌人类型
- const enemyTypes = [
- 'normal_zombie', 'roadblock_zombie', 'wandering_zombie',
- 'mage_zombie', 'archer_zombie', 'stealth_zombie',
- 'bucket_zombie', 'barrel_zombie'
- ];
-
- const randomType = enemyTypes[Math.floor(Math.random() * enemyTypes.length)];
- this.enemySpawner.spawnEnemyByType(randomType);
- }
- private isPaused = false;
- private toggleSpawning() {
- if (this.isPaused) {
- this.enemySpawner.resumeSpawning(2.0);
- console.log('▶️ 恢复敌人生成');
- } else {
- this.enemySpawner.pauseSpawning();
- console.log('⏸️ 暂停敌人生成');
- }
- this.isPaused = !this.isPaused;
- }
- private nextWave() {
- const currentWave = this.enemySpawner.getCurrentWave();
- const nextWave = currentWave + 1;
- this.enemySpawner.setCurrentWave(nextWave);
- console.log(`🌊 切换到第 ${nextWave} 波`);
- }
- private showStatus() {
- const currentWave = this.enemySpawner.getCurrentWave();
- const enemyCount = this.enemySpawner.getCurrentEnemyCount();
-
- console.log('📋 当前状态:');
- console.log(` 当前波次: ${currentWave}`);
- console.log(` 敌人数量: ${enemyCount}`);
- console.log(` 生成状态: ${this.isPaused ? '暂停' : '进行中'}`);
- }
- onDestroy() {
- input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
- }
- }
- /**
- * 敌人生成器设置指南
- */
- export class EnemySpawnerSetupGuide {
-
- /**
- * 在Cocos Creator编辑器中的设置步骤:
- *
- * 1. 创建场景节点结构:
- * Canvas
- * ├── ConfigManager (挂载 ConfigManager.ts)
- * ├── TestScene (挂载 EnemySpawnerTestScene.ts)
- * └── EnemyTest
- * ├── EnemySpawner (挂载 EnemySpawnerExample.ts)
- * └── EnemyContainer (空节点,用于容纳生成的敌人)
- *
- * 2. 创建敌人预制体:
- * - 创建一个Node,命名为 Enemy
- * - 添加Sprite组件 (用于显示敌人图标)
- * - 添加RigidBody2D组件 (用于物理移动)
- * - 添加Collider2D组件 (用于碰撞检测)
- * - 设置合适的尺寸和物理属性
- * - 保存为预制体
- *
- * 3. 配置EnemySpawnerExample组件:
- * - Enemy Prefab: 拖入刚创建的敌人预制体
- * - Enemy Container: 拖入EnemyContainer节点
- * - Spawn Interval: 设置生成间隔 (默认2秒)
- * - Max Enemies: 设置最大敌人数量 (默认10个)
- *
- * 4. 配置EnemySpawnerTestScene组件:
- * - Enemy Spawner: 拖入EnemySpawner节点的EnemySpawnerExample组件
- *
- * 5. 运行测试:
- * - 播放场景
- * - 使用键盘控制生成敌人
- * - 查看控制台输出
- * - 观察敌人移动和行为
- */
- }
|