ソースを参照

触发发射子弹

181404010226 6 ヶ月 前
コミット
9179fcb755

+ 3 - 3
assets/assets/Prefabs/Ball.prefab

@@ -183,7 +183,7 @@
     "enabledContactListener": true,
     "bullet": true,
     "awakeOnLoad": true,
-    "_group": 1,
+    "_group": 2,
     "_type": 2,
     "_allowSleep": false,
     "_gravityScale": 0,
@@ -215,10 +215,10 @@
       "__id__": 11
     },
     "tag": 0,
-    "_group": 1,
+    "_group": 2,
     "_density": 1,
     "_sensor": false,
-    "_friction": 0.2,
+    "_friction": 0.8,
     "_restitution": 0,
     "_offset": {
       "__type__": "cc.Vec2",

+ 4 - 4
assets/assets/Prefabs/Block001.prefab

@@ -473,8 +473,8 @@
     "__prefab": {
       "__id__": 21
     },
-    "tag": 0,
-    "_group": 1,
+    "tag": 2,
+    "_group": 4,
     "_density": 1,
     "_sensor": false,
     "_friction": 0.2,
@@ -547,8 +547,8 @@
     "enabledContactListener": true,
     "bullet": false,
     "awakeOnLoad": true,
-    "_group": 1,
-    "_type": 0,
+    "_group": 4,
+    "_type": 1,
     "_allowSleep": true,
     "_gravityScale": 0,
     "_linearDamping": 0,

+ 4 - 4
assets/assets/Prefabs/Block002.prefab

@@ -547,8 +547,8 @@
     "__prefab": {
       "__id__": 25
     },
-    "tag": 0,
-    "_group": 1,
+    "tag": 2,
+    "_group": 4,
     "_density": 1,
     "_sensor": false,
     "_friction": 0.2,
@@ -621,8 +621,8 @@
     "enabledContactListener": true,
     "bullet": false,
     "awakeOnLoad": true,
-    "_group": 1,
-    "_type": 0,
+    "_group": 4,
+    "_type": 1,
     "_allowSleep": true,
     "_gravityScale": 0,
     "_linearDamping": 0,

+ 4 - 4
assets/assets/Prefabs/Block003.prefab

@@ -564,8 +564,8 @@
     "__prefab": {
       "__id__": 25
     },
-    "tag": 0,
-    "_group": 1,
+    "tag": 2,
+    "_group": 4,
     "_density": 1,
     "_sensor": false,
     "_friction": 0.2,
@@ -653,8 +653,8 @@
     "enabledContactListener": true,
     "bullet": false,
     "awakeOnLoad": true,
-    "_group": 1,
-    "_type": 0,
+    "_group": 4,
+    "_type": 1,
     "_allowSleep": true,
     "_gravityScale": 0,
     "_linearDamping": 0,

+ 4 - 4
assets/assets/Prefabs/Block004.prefab

@@ -655,8 +655,8 @@
     "__prefab": {
       "__id__": 29
     },
-    "tag": 0,
-    "_group": 1,
+    "tag": 2,
+    "_group": 4,
     "_density": 1,
     "_sensor": false,
     "_friction": 0.2,
@@ -759,8 +759,8 @@
     "enabledContactListener": true,
     "bullet": false,
     "awakeOnLoad": true,
-    "_group": 1,
-    "_type": 0,
+    "_group": 4,
+    "_type": 1,
     "_allowSleep": true,
     "_gravityScale": 0,
     "_linearDamping": 0,

+ 4 - 4
assets/assets/Prefabs/Block005.prefab

@@ -655,8 +655,8 @@
     "__prefab": {
       "__id__": 29
     },
-    "tag": 0,
-    "_group": 1,
+    "tag": 2,
+    "_group": 4,
     "_density": 1,
     "_sensor": false,
     "_friction": 0.2,
@@ -759,8 +759,8 @@
     "enabledContactListener": true,
     "bullet": false,
     "awakeOnLoad": true,
-    "_group": 1,
-    "_type": 0,
+    "_group": 4,
+    "_type": 1,
     "_allowSleep": true,
     "_gravityScale": 0,
     "_linearDamping": 0,

+ 54 - 36
assets/scripts/BallController.ts

@@ -44,6 +44,13 @@ export class BallController extends Component {
     start() {
         // 计算游戏边界
         this.calculateGameBounds();
+        
+        // 检查子弹预制体是否已设置
+        if (this.bulletPrefab) {
+            console.log('子弹预制体已设置:', this.bulletPrefab.name);
+        } else {
+            console.error('子弹预制体未设置!请在编辑器中设置bulletPrefab属性');
+        }
     }
 
     // 计算游戏边界(使用GameArea节点)
@@ -76,32 +83,6 @@ export class BallController extends Component {
 
         console.log('GameArea Bounds:', this.gameBounds);
         
-        // 查找并输出墙体节点信息
-        this.logWallInfo();
-    }
-    
-    // 输出墙体节点信息
-    logWallInfo() {
-        const gameArea = find('Canvas/GameLevelUI/GameArea');
-        if (!gameArea) return;
-        
-        // 查找墙体节点
-        const walls = ['TopFence', 'BottomFence', 'JiguangL', 'JiguangR'];
-        walls.forEach(wallName => {
-            const wall = gameArea.getChildByName(wallName);
-            if (wall) {
-                const transform = wall.getComponent(UITransform);
-                const collider = wall.getComponent(Collider2D);
-                console.log(`墙体节点 ${wallName} 信息:`, {
-                    position: wall.worldPosition,
-                    size: transform ? { width: transform.width, height: transform.height } : 'unknown',
-                    hasCollider: !!collider,
-                    colliderType: collider ? collider.constructor.name : 'none'
-                });
-            } else {
-                console.warn(`找不到墙体节点 ${wallName}`);
-            }
-        });
     }
 
     // 创建小球
@@ -255,6 +236,9 @@ export class BallController extends Component {
             otherPath: this.getNodePath(otherCollider.node)
         });
 
+        // Debug: 检查节点名称是否包含Block
+        console.log(`检查节点名称: "${otherCollider.node.name}",是否包含'Block': ${otherCollider.node.name.includes('Block')}`);
+
         // 获取碰撞点的世界坐标
         const worldManifold = contact.getWorldManifold();
         if (!worldManifold) {
@@ -346,7 +330,14 @@ export class BallController extends Component {
         }
         
         // 如果碰撞的是方块,发射子弹
-        if (otherCollider.node.name.includes('Block')) {
+        // 修改检测逻辑,使用更宽松的条件
+        const isBlock = 
+            nodeName.includes('Block') || 
+            nodePath.includes('Block') ||
+            (otherCollider.node.getChildByName('Weapon') !== null);
+            
+        if (isBlock) {
+            console.log(`检测到方块碰撞: ${nodeName}, 路径: ${nodePath}`);
             this.fireBullet(otherCollider.node);
         }
     }
@@ -436,6 +427,8 @@ export class BallController extends Component {
         if (!this.bulletPrefab) {
             console.error('未设置子弹预制体');
             return;
+        } else {
+            console.log('子弹预制体已设置:', this.bulletPrefab.name);
         }
         
         // 查找Weapon节点
@@ -443,6 +436,8 @@ export class BallController extends Component {
         if (!weaponNode) {
             console.warn(`方块 ${blockNode.name} 没有Weapon子节点`);
             return;
+        } else {
+            console.log(`找到方块 ${blockNode.name} 的Weapon子节点:`, weaponNode.name);
         }
         
         // 实例化子弹
@@ -518,16 +513,10 @@ export class BallController extends Component {
             // 手动检测墙体碰撞
             this.checkWallCollisions();
             
-            // 每隔一段时间输出小球状态信息
-            if (Math.random() < 0.01) { // 约每100帧输出一次
-                console.log('小球状态:', {
-                    position: this.activeBall.worldPosition,
-                    velocity: rigidBody.linearVelocity,
-                    speed: speed,
-                    direction: this.direction
-                });
-            }
         }
+
+        // 手动检测方块碰撞
+        this.checkBlockCollisions();
     }
     
     // 手动检测墙体碰撞
@@ -615,6 +604,35 @@ export class BallController extends Component {
         }
     }
 
+    // 手动检测方块碰撞
+    checkBlockCollisions() {
+        if (!this.activeBall) return;
+        
+        // 获取场景中所有方块
+        const placedBlocks = find('Canvas/PlacedBlocks');
+        if (!placedBlocks) return;
+        
+        const ballPos = this.activeBall.worldPosition;
+        const ballRadius = this.radius;
+        
+        // 检查与每个方块的碰撞
+        for (let i = 0; i < placedBlocks.children.length; i++) {
+            const block = placedBlocks.children[i];
+            if (!block.name.includes('Block')) continue;
+            
+            // 简单距离检测(可以根据实际情况优化)
+            const blockPos = block.worldPosition;
+            const distance = Vec3.distance(ballPos, blockPos);
+            
+            // 如果距离小于某个阈值,认为发生碰撞
+            if (distance < ballRadius + 50) { // 50是一个估计值,根据方块大小调整
+                console.log('手动检测到与方块碰撞:', block.name);
+                this.fireBullet(block);
+                break;
+            }
+        }
+    }
+
     // 初始化方向
     initializeDirection() {
         // 随机初始方向

+ 17 - 1
assets/scripts/BlockManager.ts

@@ -1,4 +1,4 @@
-import { _decorator, Component, Node, Prefab, instantiate, Vec3, EventTouch, Vec2, UITransform, find, Rect, Label, Color, Size } from 'cc';
+import { _decorator, Component, Node, Prefab, instantiate, Vec3, EventTouch, Vec2, UITransform, find, Rect, Label, Color, Size, Contact2DType, Collider2D, IPhysics2DContact } from 'cc';
 const { ccclass, property } = _decorator;
 
 @ccclass('BlockManager')
@@ -107,6 +107,13 @@ export class BlockManager extends Component {
         
         // 生成随机的三个块
         this.generateRandomBlocks();
+
+        // 注册碰撞回调
+        const collider = this.getComponent(Collider2D);
+        if (collider) {
+            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
+            console.log('方块碰撞监听器已注册');
+        }
     }
     
     // 确保有PlacedBlocks节点
@@ -1089,4 +1096,13 @@ export class BlockManager extends Component {
         
         console.log(`已将方块 ${block.name} 移动到PlacedBlocks节点下`);
     }
+
+    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
+        console.log('方块碰到了:', otherCollider.node.name);
+        // 如果碰到球,可以在这里发射子弹
+        if (otherCollider.node.name === 'Ball') {
+            console.log('方块被球击中!');
+            // 这里可以调用发射子弹的逻辑
+        }
+    }
 } 

+ 7 - 2
settings/v2/packages/project.json

@@ -11,11 +11,16 @@
       {
         "index": 1,
         "name": "Ball"
+      },
+      {
+        "index": 2,
+        "name": "Block"
       }
     ],
     "collisionMatrix": {
-      "0": 1,
-      "1": 0
+      "0": 3,
+      "1": 7,
+      "2": 6
     }
   }
 }