Sfoglia il codice sorgente

墙体血量本地读取

181404010226 5 mesi fa
parent
commit
c0f0b4e652

+ 3 - 1
assets/Scenes/GameLevel.scene

@@ -15573,7 +15573,9 @@
       "__uuid__": "70f7651c-5d17-45aa-8079-b58c5b191125",
       "__expectedType__": "cc.Prefab"
     },
-    "wallHealth": 1200,
+    "wallHealthNode": {
+      "__id__": 286
+    },
     "enemyCountLabelNode": {
       "__id__": 46
     },

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

@@ -57,6 +57,12 @@ export class BallController extends Component {
     // 小球是否已开始运动
     private ballStarted: boolean = false;
 
+    // 小球暂停时记录的速度
+    private pausedVelocity: Vec2 = new Vec2();
+
+    // 标记是否处于暂停状态
+    private isPaused: boolean = false;
+
     // 在类字段区添加
     private blockFireCooldown: Map<string, number> = new Map();
     private FIRE_COOLDOWN = 0.05;
@@ -839,6 +845,47 @@ export class BallController extends Component {
         this.ballStarted = true;
     }
 
+    /**
+     * 暂停小球运动:记录当前速度并停止刚体
+     */
+    public pauseBall() {
+        if (this.isPaused) return;
+        this.isPaused = true;
+        this.ballStarted = false;
+
+        if (this.activeBall && this.activeBall.isValid) {
+            const rb = this.activeBall.getComponent(RigidBody2D);
+            if (rb) {
+                this.pausedVelocity = rb.linearVelocity.clone();
+                rb.linearVelocity = new Vec2(0, 0);
+                rb.sleep();
+            }
+        }
+    }
+
+    /**
+     * 恢复小球运动:恢复暂停前的速度
+     */
+    public resumeBall() {
+        if (!this.isPaused) return;
+        this.isPaused = false;
+        this.ballStarted = true;
+        console.log('恢复小球运动');
+        if (this.activeBall && this.activeBall.isValid) {
+            const rb = this.activeBall.getComponent(RigidBody2D);
+            if (rb) {
+                rb.wakeUp();
+                const hasPrevVelocity = this.pausedVelocity && (this.pausedVelocity.x !== 0 || this.pausedVelocity.y !== 0);
+                if (hasPrevVelocity) {
+                    rb.linearVelocity = this.pausedVelocity.clone();
+                } else {
+                    // 若没有记录速度,则重新初始化方向
+                    this.initializeDirection();
+                }
+            }
+        }
+    }
+
     // 输出碰撞矩阵调试信息
     private logCollisionMatrix() {
         // 根据项目配置检查碰撞矩阵

+ 12 - 16
assets/scripts/CombatSystem/EnemyController.ts

@@ -54,13 +54,12 @@ export class EnemyController extends BaseSingleton {
     private defaultHealth: number = 30;
 
     // 墙体属性
-    @property({
-        tooltip: '墙体初始血量'
-    })
-    public wallHealth: number = 1200;
+    // 最终数值将在 init() 中从存档或 GameManager 注入
+    public wallHealth: number = 0;
 
-    // 墙体血量显示节点(运行时动态查找)
-    private wallHealthNode: Node = null;
+    // 墙体血量显示节点(Inspector 拖拽 HeartLabel 节点到此)
+    @property({ type: Node, tooltip: '墙体血量 Label 节点 (HeartLabel)' })
+    public wallHealthNode: Node = null;
 
     // 游戏区域边界 - 改为public,让敌人实例可以访问
     public gameBounds = {
@@ -197,26 +196,23 @@ export class EnemyController extends BaseSingleton {
 
     // 初始化墙体血量显示
     initWallHealthDisplay() {
-        if (!this.wallHealthNode) {
-            // 尝试查找墙体血量显示节点
-            this.wallHealthNode = find('Canvas/GameLevelUI/HeartNode');
-        }
-
         if (this.wallHealthNode) {
-            // 更新墙体血量显示
             this.updateWallHealthDisplay();
         } else {
-            console.warn('未设置墙体血量显示节点');
+            console.warn('EnemyController 未绑定 HeartLabel 节点,请在 Inspector 中拖拽 Canvas/GameLevelUI/HeartNode/HeartLabel');
         }
     }
 
     // 更新墙体血量显示
     public updateWallHealthDisplay() {
-        if (!this.wallHealthNode) {
-            return;
+        if (!this.wallHealthNode) return;
+
+        // 直接在当前节点或其子节点寻找 Label
+        let heartLabel = this.wallHealthNode.getComponent(Label);
+        if (!heartLabel) {
+            heartLabel = this.wallHealthNode.getComponentInChildren(Label);
         }
 
-        const heartLabel = this.wallHealthNode.getComponent(Label);
         if (heartLabel) {
             heartLabel.string = this.wallHealth.toString();
         }

+ 7 - 5
assets/scripts/CombatSystem/SkillSelection/SkillSelectionController.ts

@@ -1,4 +1,4 @@
-import { _decorator, Component, Node, Button, Vec3, find } from 'cc';
+import { _decorator, Component, Node, Button, Vec3, find} from 'cc';
 import SkillButtonAnimator from './SkillButtonAnimator';
 import { GameManager } from '../../LevelSystem/GameManager';
 const { ccclass, property } = _decorator;
@@ -87,11 +87,13 @@ export class SkillSelectionController extends Component {
     private _gameManager: GameManager = null;
     private getGameManager(): GameManager {
         if (this._gameManager && this._gameManager.isValid) return this._gameManager;
-        const gmNode = find('GameManager');
-        if (gmNode) {
-            this._gameManager = gmNode.getComponent(GameManager);
+
+        // 尝试在 Canvas 下递归查找
+        const canvas = find('Canvas');
+        if (canvas) {
+            this._gameManager = canvas.getComponentInChildren(GameManager);
+            if (this._gameManager) return this._gameManager;
         }
-        return this._gameManager;
     }
 }
 

+ 18 - 1
assets/scripts/LevelSystem/GameManager.ts

@@ -359,8 +359,11 @@ export class GameManager extends Component {
         
         if (!this.enemyController) {
             this.enemyController = this.enemyManager.addComponent(EnemyController);
-            this.enemyController.wallHealth = this.wallHealth;
         }
+
+        // 无论 EnemyController 是否新建,都注入墙体血量
+        this.enemyController.wallHealth = this.wallHealth;
+        this.enemyController.updateWallHealthDisplay?.();
     }
 
     // === 游戏状态检查 ===
@@ -475,11 +478,19 @@ export class GameManager extends Component {
 
     // === 暂停游戏 ===
     private pauseGame() {
+        // 设置状态为暂停
+        this.currentState = GameState.PAUSED;
         this.gameStarted = false;
         
         if (this.enemyController && this.enemyController.pauseSpawning) {
             this.enemyController.pauseSpawning();
         }
+
+        // 暂停小球
+        if (this.ballController) {
+            const bc = this.ballController.getComponent(BallController);
+            bc?.pauseBall?.();
+        }
     }
 
     // === 恢复游戏 ===
@@ -491,6 +502,12 @@ export class GameManager extends Component {
             this.enemyController.resumeSpawning();
         }
 
+        // 恢复小球
+        if (this.ballController) {
+            const bc = this.ballController.getComponent(BallController);
+            bc?.resumeBall?.();
+        }
+
         if (this.gameSuccessUI) {
             this.gameSuccessUI.active = false;
         }