181404010226 преди 5 месеца
родител
ревизия
2f13a748a6
променени са 2 файла, в които са добавени 38 реда и са изтрити 0 реда
  1. 31 0
      assets/scripts/CombatSystem/BulletController.ts
  2. 7 0
      assets/scripts/CombatSystem/EnemyInstance.ts

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

@@ -269,5 +269,36 @@ export class BulletController extends Component {
         if (this.lifeTimer <= 0) {
             this.node.destroy();
         }
+
+        // 检查是否飞出游戏区域
+        this.checkOutOfBounds();
+    }
+
+    /**
+     * 如果子弹飞出 GameArea 边界(留有一定安全距离)则自动销毁
+     */
+    private checkOutOfBounds() {
+        const gameArea = find('Canvas/GameLevelUI/GameArea');
+        if (!gameArea || !this.node || !this.node.isValid) return;
+
+        const uiTransform = gameArea.getComponent(UITransform);
+        if (!uiTransform) return;
+
+        const halfWidth = uiTransform.width / 2;
+        const halfHeight = uiTransform.height / 2;
+        const center = gameArea.worldPosition;
+
+        const left = center.x - halfWidth;
+        const right = center.x + halfWidth;
+        const bottom = center.y - halfHeight;
+        const top = center.y + halfHeight;
+
+        // 允许子弹稍微超出边界后再销毁,避免边缘误差
+        const margin = 100;
+
+        const pos = this.node.worldPosition;
+        if (pos.x < left - margin || pos.x > right + margin || pos.y < bottom - margin || pos.y > top + margin) {
+            this.node.destroy();
+        }
     }
 }

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

@@ -140,6 +140,13 @@ export class EnemyInstance extends Component {
         
         // 更新血量显示
         this.updateHealthDisplay();
+        
+        // 如果血量低于等于0,销毁敌人
+        if (this.health <= 0) {
+            // 移除递归调用 controller.damageEnemy,避免堆栈溢出。
+            // 如果需要让 EnemyController 统计死亡数量,可在 EnemyController.update 中自动清理无效节点,或另行实现 onEnemyDead 事件。
+            this.node.destroy();
+        }
     }
 
     update(deltaTime: number) {