浏览代码

子弹避免重复

181404010226 5 月之前
父节点
当前提交
1eed9ece7d

+ 1 - 1
assets/Scenes/GameLevel.scene

@@ -19787,7 +19787,7 @@
     },
     "_enabled": true,
     "__prefab": null,
-    "bulletSpeed": 100,
+    "bulletSpeed": 10,
     "_id": "35PM+091VFSJdCE9CR6t/B"
   },
   {

+ 12 - 47
assets/scripts/CombatSystem/BallController.ts

@@ -57,6 +57,10 @@ export class BallController extends Component {
     // 小球是否已开始运动
     private ballStarted: boolean = false;
 
+    // 在类字段区添加
+    private blockFireCooldown: Map<string, number> = new Map();
+    private FIRE_COOLDOWN = 0.05;
+
     start() {
         // 如果没有指定placedBlocksContainer,尝试找到它
         if (!this.placedBlocksContainer) {
@@ -543,8 +547,12 @@ export class BallController extends Component {
                 contactPos = blockNode.worldPosition.clone();
             }
 
-            this.fireBulletAt(blockNode, contactPos);
-            console.log('✅ 子弹发射完成');
+            const now = performance.now();
+            const lastTime = this.blockFireCooldown.get(blockNode.uuid) || 0;
+            if (now - lastTime > this.FIRE_COOLDOWN * 1000) {
+                this.blockFireCooldown.set(blockNode.uuid, now);
+                this.fireBulletAt(blockNode, contactPos);
+            }
         }
     }
 
@@ -716,13 +724,6 @@ export class BallController extends Component {
         }
         console.log('✅ 获取到子弹控制器组件');
         
-        // 设置全局子弹速度(如果 WeaponBlockExample 提供)
-        const weaponExampleInstance = WeaponBlockExample.getInstance();
-        if (weaponExampleInstance) {
-            bulletController.speed = weaponExampleInstance.getCurrentBulletSpeed();
-            console.log('⚙️ 应用全局子弹速度:', bulletController.speed);
-        }
-        
         // 设置发射位置
         bulletController.setFirePosition(firePosition);
         
@@ -1072,44 +1073,8 @@ export class BallController extends Component {
             return;
         }        
         
-        // 查找方块中的Weapon节点
-        console.log('🔍 开始查找方块中的Weapon节点...');
-        const weaponNode = this.findWeaponNode(blockNode);
-        if (!weaponNode) {
-            console.error('❌ 在方块中找不到Weapon节点:', blockNode.name);
-            console.log('方块结构:');
-            this.logNodeStructure(blockNode, 0);
-            
-            // 如果找不到Weapon节点,使用方块本身的位置作为发射位置
-            console.log('🔧 使用方块本身作为发射位置');
-            console.log('方块世界坐标:', fireWorldPos);
-            
-            // 直接创建子弹并设置位置
-            this.createAndFireBullet(fireWorldPos);
-            
-            console.log('🔫 === 方块武器发射子弹流程结束 ===');
-            return;
-        }
-        
-        console.log('✅ 找到Weapon节点:', weaponNode.name);
-        console.log('Weapon节点路径:', this.getNodePath(weaponNode));
-        
-        // 获取武器的世界坐标作为发射位置
-        let firePosition: Vec3;
-        try {
-            firePosition = weaponNode.worldPosition;
-            console.log('武器世界坐标:', firePosition);
-        } catch (error) {
-            console.error('❌ 获取武器坐标失败:', error);
-            // 备用方案:使用方块坐标
-            firePosition = fireWorldPos;
-            console.log('使用方块坐标作为备用:', firePosition);
-        }
-        
-        // 创建并发射子弹
-        console.log('🚀 创建子弹...');
-        this.createAndFireBullet(firePosition);
-        
+        // 直接使用碰撞世界坐标作为发射点
+        this.createAndFireBullet(fireWorldPos);
         console.log('🔫 === 方块武器发射子弹流程结束 ===');
     }
 } 

+ 7 - 0
assets/scripts/CombatSystem/BulletController.ts

@@ -1,4 +1,5 @@
 import { _decorator, Component, Node, Vec2, Vec3, find, RigidBody2D, Collider2D, Contact2DType, IPhysics2DContact, instantiate, Prefab, UITransform } from 'cc';
+import { WeaponBlockExample } from './WeaponBlockExample';
 const { ccclass, property } = _decorator;
 
 /**
@@ -192,6 +193,12 @@ export class BulletController extends Component {
             return;
         }
         
+        // 覆盖速度值(全局控制)
+        const weaponGlobal = WeaponBlockExample.getInstance && WeaponBlockExample.getInstance();
+        if (weaponGlobal) {
+            this.speed = weaponGlobal.getCurrentBulletSpeed();
+        }
+        
         const velocity = new Vec2(
             this.direction.x * this.speed,
             this.direction.y * this.speed