| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- import { _decorator, Component, Node, Prefab, instantiate, Vec3, Vec2, Sprite, SpriteFrame, resources, RigidBody2D, CCFloat, CCInteger } from 'cc';
- import { sp } from 'cc';
- import { ConfigManager, EnemyConfig } from '../Core/ConfigManager';
- import { EnemyComponent } from './EnemyComponent';
- const { ccclass, property } = _decorator;
- /**
- * 敌人生成器示例脚本
- * 展示如何使用ConfigManager来创建不同类型的敌人
- */
- @ccclass('EnemySpawnerExample')
- export class EnemySpawnerExample extends Component {
-
- @property({
- type: Prefab,
- tooltip: '基础敌人预制体'
- })
- public enemyPrefab: Prefab = null;
- @property({
- type: Node,
- tooltip: '敌人容器节点'
- })
- public enemyContainer: Node = null;
- @property({
- type: CCFloat,
- tooltip: '生成间隔(秒)'
- })
- public spawnInterval: number = 2.0;
- @property({
- type: CCInteger,
- tooltip: '最大敌人数量'
- })
- public maxEnemies: number = 10;
- private configManager: ConfigManager = null;
- private currentWave: number = 1;
- private currentEnemyCount: number = 0;
- private spawnTimer: number = 0;
- start() {
- // 获取配置管理器实例
- this.configManager = ConfigManager.getInstance();
-
- // 等待配置加载完成后开始生成
- this.scheduleOnce(() => {
- this.startSpawning();
- }, 1.0);
- }
- update(deltaTime: number) {
- if (!this.configManager || !this.configManager.isConfigLoaded()) {
- return;
- }
- // 更新生成计时器
- this.spawnTimer += deltaTime;
-
- if (this.spawnTimer >= this.spawnInterval && this.currentEnemyCount < this.maxEnemies) {
- this.spawnRandomEnemy();
- this.spawnTimer = 0;
- }
- }
- // 开始生成敌人
- private startSpawning() {
- if (!this.configManager || !this.configManager.isConfigLoaded()) {
- console.warn('配置管理器未准备好');
- return;
- }
- console.log('开始生成敌人');
- this.spawnTimer = 0;
- }
- // 生成随机敌人
- private spawnRandomEnemy() {
- // 根据当前波次获取合适的敌人
- const enemyConfig = this.configManager.getEnemyForWave(this.currentWave);
- if (!enemyConfig) {
- console.warn('无法获取敌人配置');
- return;
- }
- this.spawnEnemy(enemyConfig);
- }
- // 生成指定类型的敌人
- public spawnEnemyByType(enemyType: string) {
- const enemyConfig = this.configManager.getEnemyById(enemyType);
- if (!enemyConfig) {
- console.warn(`找不到类型为 ${enemyType} 的敌人配置`);
- return;
- }
- this.spawnEnemy(enemyConfig);
- }
- // 生成指定稀有度的敌人
- public spawnEnemyByRarity(rarity: string) {
- const enemyConfig = this.configManager.getRandomEnemy(rarity);
- if (!enemyConfig) {
- console.warn(`无法获取稀有度为 ${rarity} 的敌人配置`);
- return;
- }
- this.spawnEnemy(enemyConfig);
- }
- // 根据敌人配置生成敌人
- private spawnEnemy(enemyConfig: EnemyConfig) {
- if (!this.enemyPrefab) {
- console.error('敌人预制体未设置');
- return;
- }
- // 实例化敌人
- const enemyNode = instantiate(this.enemyPrefab);
- if (!enemyNode) {
- console.error('敌人实例化失败');
- return;
- }
- // 设置生成位置(右侧屏幕外)
- const spawnPosition = new Vec3(800, Math.random() * 400 - 200, 0);
- enemyNode.setPosition(spawnPosition);
- // 添加到容器
- if (this.enemyContainer) {
- this.enemyContainer.addChild(enemyNode);
- } else {
- this.node.addChild(enemyNode);
- }
- // 设置敌人配置
- this.setupEnemy(enemyNode, enemyConfig);
- this.currentEnemyCount++;
- console.log(`生成敌人: ${enemyConfig.name} (${enemyConfig.rarity})`);
- }
- // 设置敌人配置
- private setupEnemy(enemyNode: Node, enemyConfig: EnemyConfig) {
- // 添加敌人组件
- const enemyComponent = enemyNode.addComponent(EnemyComponent);
- if (enemyComponent) {
- enemyComponent.enemyConfig = enemyConfig;
- enemyComponent.spawner = this;
- }
- // 设置敌人外观
- this.setupEnemyVisual(enemyNode, enemyConfig);
- // 设置敌人物理
- this.setupEnemyPhysics(enemyNode, enemyConfig);
- // 设置敌人名称
- enemyNode.name = `Enemy_${enemyConfig.id}_${Date.now()}`;
- }
- // 设置敌人外观
- private setupEnemyVisual(enemyNode: Node, enemyConfig: EnemyConfig) {
- // 查找Spine动画组件
- const spineComponent = enemyNode.getComponent(sp.Skeleton);
- if (spineComponent) {
- // 如果有Spine组件,设置Spine动画
- this.setupEnemySpineAnimation(spineComponent, enemyConfig);
- } else {
- // 如果没有Spine组件,使用Sprite组件作为后备
- const spriteComponent = enemyNode.getComponent(Sprite);
- if (spriteComponent) {
- // 根据稀有度设置颜色
- const rarityColors = {
- 'common': { r: 200, g: 200, b: 200, a: 255 }, // 灰色
- 'uncommon': { r: 255, g: 255, b: 0, a: 255 }, // 黄色
- 'rare': { r: 255, g: 100, b: 0, a: 255 }, // 橙色
- 'boss': { r: 255, g: 0, b: 0, a: 255 } // 红色
- };
- const color = rarityColors[enemyConfig.rarity] || rarityColors['common'];
- spriteComponent.color = spriteComponent.color.set(color.r, color.g, color.b, color.a);
- }
- }
- // 设置缩放
- enemyNode.setScale(enemyConfig.visualConfig.scale, enemyConfig.visualConfig.scale, 1);
- }
- // 设置敌人Spine骨骼动画
- private setupEnemySpineAnimation(spineComponent: sp.Skeleton, enemyConfig: EnemyConfig) {
- const spineDataPath = enemyConfig.visualConfig.spritePrefab;
- if (!spineDataPath) {
- console.warn(`敌人 ${enemyConfig.name} 没有Spine动画路径`);
- return;
- }
- // 加载Spine动画数据
- resources.load(spineDataPath, sp.SkeletonData, (err, skeletonData) => {
- if (err) {
- console.warn(`加载敌人Spine动画失败: ${spineDataPath}`, err);
- return;
- }
- // 设置Spine数据
- spineComponent.skeletonData = skeletonData;
-
- // 播放默认动画(如果有的话)
- const animations = enemyConfig.visualConfig.animations;
- if (animations && animations.idle) {
- spineComponent.setAnimation(0, animations.idle, true);
- } else if (animations && animations.walk) {
- spineComponent.setAnimation(0, animations.walk, true);
- }
-
- console.log(`敌人 ${enemyConfig.name} Spine动画加载成功: ${spineDataPath}`);
- });
- }
- // 设置敌人物理
- private setupEnemyPhysics(enemyNode: Node, enemyConfig: EnemyConfig) {
- const rigidBody = enemyNode.getComponent(RigidBody2D);
- if (rigidBody) {
- // 设置线性速度(向左移动)
- const speed = enemyConfig.stats.speed;
- rigidBody.linearVelocity = new Vec2(-speed, 0);
- }
- }
- // 敌人死亡回调
- public onEnemyDeath(enemyNode: Node) {
- this.currentEnemyCount--;
-
- // 移除敌人节点
- if (enemyNode && enemyNode.isValid) {
- enemyNode.destroy();
- }
- }
- // 设置当前波次
- public setCurrentWave(wave: number) {
- this.currentWave = wave;
- console.log(`设置当前波次: ${wave}`);
- }
- // 获取当前波次
- public getCurrentWave(): number {
- return this.currentWave;
- }
- // 获取当前敌人数量
- public getCurrentEnemyCount(): number {
- return this.currentEnemyCount;
- }
- // 清除所有敌人
- public clearAllEnemies() {
- if (this.enemyContainer) {
- this.enemyContainer.destroyAllChildren();
- }
- this.currentEnemyCount = 0;
- }
- // 暂停生成
- public pauseSpawning() {
- this.spawnInterval = Number.MAX_VALUE;
- }
- // 恢复生成
- public resumeSpawning(interval: number = 2.0) {
- this.spawnInterval = interval;
- this.spawnTimer = 0;
- }
- }
|