소스 검색

多发实现

181404010226 5 달 전
부모
커밋
017757275e
2개의 변경된 파일94개의 추가작업 그리고 64개의 파일을 삭제
  1. 31 22
      assets/scripts/CombatSystem/BallController.ts
  2. 63 42
      assets/scripts/CombatSystem/WeaponBullet.ts

+ 31 - 22
assets/scripts/CombatSystem/BallController.ts

@@ -681,34 +681,43 @@ export class BallController extends Component {
                 return;
             }
             
-            // 创建子弹实例
-            const bullet = instantiate(this.bulletPrefab);
-            if (!bullet) {
-                console.error('❌ 子弹实例创建失败');
-                return;
-            }
-            
-            // 查找GameArea
+            // 判断是否为多发子弹(散射/连发等)
+            const countCfg = finalConfig.bulletConfig.count;
+            const isMultiShot = countCfg && countCfg.type !== 'single' && countCfg.amount > 1;
+
+            // 查找GameArea(子弹统一添加到此节点)
             const gameArea = find('Canvas/GameLevelUI/GameArea');
             if (!gameArea) {
                 console.error('❌ 找不到GameArea节点');
-                bullet.destroy();
                 return;
             }
-            
-            // 添加到场景
-            gameArea.addChild(bullet);
-            
-            // 添加或获取WeaponBullet组件
-            let weaponBullet = bullet.getComponent(WeaponBullet);
-            if (!weaponBullet) {
-                weaponBullet = bullet.addComponent(WeaponBullet);
+
+            if (isMultiShot) {
+                // 使用批量创建逻辑
+                const bullets = WeaponBullet.createBullets(initData, this.bulletPrefab as any);
+                bullets.forEach(b => {
+                    gameArea.addChild(b);
+                });
+                console.log(`✅ 已创建多发子弹 (${bullets.length} 发)`);
+            } else {
+                // 单发逻辑(沿用原流程)
+                const bullet = instantiate(this.bulletPrefab);
+                if (!bullet) {
+                    console.error('❌ 子弹实例创建失败');
+                    return;
+                }
+
+                gameArea.addChild(bullet);
+
+                let weaponBullet = bullet.getComponent(WeaponBullet);
+                if (!weaponBullet) {
+                    weaponBullet = bullet.addComponent(WeaponBullet);
+                }
+
+                weaponBullet.init(initData);
+                console.log('✅ 新子弹系统创建完成 (单发)');
             }
-            
-            // 初始化子弹
-            weaponBullet.init(initData);
-            
-            console.log('✅ 新子弹系统创建完成');
+
             console.log('🔫 === 子弹创建完成 ===');
             
         }).catch(error => {

+ 63 - 42
assets/scripts/CombatSystem/WeaponBullet.ts

@@ -123,8 +123,38 @@ export class WeaponBullet extends Component {
             return [];
         }
         
-        // 计算子弹生成信息
-        const direction = initData.direction || new Vec3(1, 0, 0);
+        // === 计算基础发射方向 ===
+        let direction: Vec3;
+        if (initData.direction) {
+            direction = initData.direction.clone();
+        } else if (initData.autoTarget) {
+            // 复制 calculateDirection 中的最近敌人算法,以避免新增方法
+            const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
+            if (enemyContainer) {
+                const enemies = enemyContainer.children.filter(child => child.active && !!child.name);
+                let nearestEnemy: Node = null;
+                let nearestDistance = Infinity;
+                for (const enemy of enemies) {
+                    const dist = Vec3.distance(initData.firePosition, enemy.worldPosition);
+                    if (dist < nearestDistance) {
+                        nearestDistance = dist;
+                        nearestEnemy = enemy;
+                    }
+                }
+                if (nearestEnemy) {
+                    direction = nearestEnemy.worldPosition.clone().subtract(initData.firePosition).normalize();
+                }
+            }
+
+            // 如果没有敌人或计算失败,则回退为随机方向
+            if (!direction) {
+                const angleRand = Math.random() * Math.PI * 2;
+                direction = new Vec3(Math.cos(angleRand), Math.sin(angleRand), 0);
+            }
+        } else {
+            direction = new Vec3(1, 0, 0);
+        }
+        
         const spawnInfos = BulletCount.calculateBulletSpawns(
             config.bulletConfig.count,
             initData.firePosition,
@@ -293,8 +323,8 @@ export class WeaponBullet extends Component {
             return new Vec3(Math.cos(angle), Math.sin(angle), 0);
         }
         
-        // 寻找最近敌人
-        const nearestEnemy = this.findNearestEnemy();
+        // 寻找最近敌人(使用静态工具函数)
+        const nearestEnemy = WeaponBullet.findNearestEnemyGlobal(this.node.worldPosition);
         if (nearestEnemy) {
             const direction = nearestEnemy.worldPosition.clone()
                 .subtract(this.node.worldPosition)
@@ -308,44 +338,6 @@ export class WeaponBullet extends Component {
         return new Vec3(Math.cos(angle), Math.sin(angle), 0);
     }
     
-    /**
-     * 寻找最近敌人
-     */
-    private findNearestEnemy(): Node | null {
-        const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
-        if (!enemyContainer) return null;
-        
-        const enemies = enemyContainer.children.filter(child => 
-            child.active && this.isEnemyNode(child)
-        );
-        
-        if (enemies.length === 0) return null;
-        
-        let nearestEnemy: Node = null;
-        let nearestDistance = Infinity;
-        const bulletPos = this.node.worldPosition;
-        
-        for (const enemy of enemies) {
-            const distance = Vec3.distance(bulletPos, enemy.worldPosition);
-            if (distance < nearestDistance) {
-                nearestDistance = distance;
-                nearestEnemy = enemy;
-            }
-        }
-        
-        return nearestEnemy;
-    }
-    
-    /**
-     * 判断是否为敌人节点
-     */
-    private isEnemyNode(node: Node): boolean {
-        const name = node.name.toLowerCase();
-        return name.includes('enemy') || 
-               name.includes('敌人') ||
-               node.getComponent('EnemyInstance') !== null;
-    }
-    
     /**
      * 设置碰撞监听
      */
@@ -510,4 +502,33 @@ export class WeaponBullet extends Component {
             }
         });
     }
+    
+    /**
+     * 静态:根据给定世界坐标,寻找最近的敌人节点
+     */
+    private static findNearestEnemyGlobal(worldPos: Vec3): Node | null {
+        const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
+        if (!enemyContainer) return null;
+
+        const enemies = enemyContainer.children.filter(child => {
+            if (!child.active) return false;
+            const nameLower = child.name.toLowerCase();
+            if (nameLower.includes('enemy') || nameLower.includes('敌人')) return true;
+            if (child.getComponent('EnemyInstance')) return true;
+            return false;
+        });
+
+        if (enemies.length === 0) return null;
+
+        let nearest: Node = null;
+        let nearestDist = Infinity;
+        for (const enemy of enemies) {
+            const dist = Vec3.distance(worldPos, enemy.worldPosition);
+            if (dist < nearestDist) {
+                nearestDist = dist;
+                nearest = enemy;
+            }
+        }
+        return nearest;
+    }
 }