浏览代码

优化文档7-15

181404010226 5 月之前
父节点
当前提交
ea1bc70f79

文件差异内容过多而无法显示
+ 334 - 222
assets/Scenes/GameLevel.scene


+ 8 - 1
assets/scripts/CombatSystem/BallController.ts

@@ -1,6 +1,7 @@
 import { _decorator, Component, Node, Vec2, Vec3, UITransform, Collider2D, Contact2DType, IPhysics2DContact, RigidBody2D, Prefab, instantiate, find, CircleCollider2D } from 'cc';
 import { PhysicsManager } from '../Core/PhysicsManager';
 import { WeaponBullet, BulletInitData, WeaponConfig } from './WeaponBullet';
+import { EnemyController } from './EnemyController';
 const { ccclass, property } = _decorator;
 
 @ccclass('BallController')
@@ -21,7 +22,7 @@ export class BallController extends Component {
 
     // 球的移动速度
     @property
-    public speed: number = 10;
+    public speed: number = 60;
 
     // 反弹随机偏移最大角度(弧度)
     @property
@@ -406,6 +407,12 @@ export class BallController extends Component {
     onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
         // Debug logs removed
         
+        // 判断是否有敌人,若无则不产生子弹
+        const enemyController = EnemyController.getInstance();
+        if (!enemyController || !enemyController.hasActiveEnemies()) {
+            return;
+        }
+        
         // 判断哪个是小球,哪个是方块
         let ballNode: Node = null;
         let blockNode: Node = null;

+ 16 - 7
assets/scripts/CombatSystem/BlockManager.ts

@@ -1,4 +1,4 @@
-import { _decorator, Component, Node, Prefab, instantiate, Vec3, EventTouch, Vec2, UITransform, find, Rect, Label, Color, Size, Sprite, SpriteFrame, resources, Button } from 'cc';
+import { _decorator, Component, Node, Prefab, instantiate, Vec3, EventTouch, Vec2, UITransform, find, Rect, Label, Color, Size, Sprite, SpriteFrame, resources, Button, Collider2D } from 'cc';
 import { ConfigManager, WeaponConfig } from '../Core/ConfigManager';
 import { SaveDataManager } from '../LevelSystem/SaveDataManager';
 import { LevelSessionManager } from '../Core/LevelSessionManager';
@@ -433,6 +433,9 @@ export class BlockManager extends Component {
             
             block.setSiblingIndex(block.parent.children.length - 1);
             this.tempStoreBlockOccupiedGrids(block);
+            // 拖拽开始时禁用碰撞体
+            const collider = block.getComponent(Collider2D);
+            if (collider) collider.enabled = false;
         }, this);
         
         block.on(Node.EventType.TOUCH_MOVE, (event: EventTouch) => {
@@ -467,6 +470,9 @@ export class BlockManager extends Component {
                 
           
                 this.currentDragBlock = null;
+                // 拖拽结束时恢复碰撞体
+                const collider = block.getComponent(Collider2D);
+                if (collider) collider.enabled = true;
             }
         }, this);
         
@@ -474,6 +480,9 @@ export class BlockManager extends Component {
             if (this.currentDragBlock) {
                 this.returnBlockToOriginalPosition();
                 this.currentDragBlock = null;
+                // 拖拽取消时恢复碰撞体
+                const collider = block.getComponent(Collider2D);
+                if (collider) collider.enabled = true;
             }
         }, this);
     }
@@ -553,9 +562,9 @@ export class BlockManager extends Component {
             if (this.gameStarted) {
                 this.addLockedVisualHint(this.currentDragBlock);
             }
-+
-+            // 检查并执行合成
-+            this.tryMergeBlock(this.currentDragBlock);
+
+            // 检查并执行合成
+            this.tryMergeBlock(this.currentDragBlock);
         } else {
             if (this.deductPlayerCoins(price)) {
                 this.clearTempStoredOccupiedGrids(this.currentDragBlock);
@@ -576,9 +585,9 @@ export class BlockManager extends Component {
                 if (this.gameStarted) {
                     this.addLockedVisualHint(this.currentDragBlock);
                 }
-+
-+                // 检查并执行合成
-+                this.tryMergeBlock(this.currentDragBlock);
+
+                // 检查并执行合成
+                this.tryMergeBlock(this.currentDragBlock);
             } else {
                 this.returnBlockToOriginalPosition();
             }

+ 24 - 0
assets/scripts/CombatSystem/EnemyController.ts

@@ -384,6 +384,30 @@ export class EnemyController extends BaseSingleton {
         return this.getActiveEnemies().length;
     }
 
+    // 获取最近的敌人节点
+    public getNearestEnemy(fromPosition: Vec3): Node | null {
+        const enemies = this.getActiveEnemies();
+        if (enemies.length === 0) return null;
+
+        let nearestEnemy: Node = null;
+        let nearestDistance = Infinity;
+
+        for (const enemy of enemies) {
+            const distance = Vec3.distance(fromPosition, enemy.worldPosition);
+            if (distance < nearestDistance) {
+                nearestDistance = distance;
+                nearestEnemy = enemy;
+            }
+        }
+
+        return nearestEnemy;
+    }
+
+    // 检查是否有活跃敌人
+    public hasActiveEnemies(): boolean {
+        return this.getActiveEnemies().length > 0;
+    }
+
     // 获取游戏是否已开始状态
     public isGameStarted(): boolean {
         return this.gameStarted;

+ 8 - 0
assets/scripts/CombatSystem/SkillSelection/SkillButtonAnimator.ts

@@ -41,6 +41,14 @@ export class SkillButtonAnimator extends Component {
             .start();
         this.node.setPosition(this._origPos);
     }
+
+    public resetState() {
+        // Reset scale, position, opacity, etc. as needed
+        this.node.setScale(Vec3.ONE);
+        this.node.active = true;
+        // this.node.setPosition(this._initPos);
+        // this.node.opacity = 255;
+    }
 }
 
 export default SkillButtonAnimator; 

+ 20 - 0
assets/scripts/CombatSystem/SkillSelection/SkillSelectionController.ts

@@ -72,6 +72,8 @@ export class SkillSelectionController extends Component {
                         gm.resetEnergy();
                         gm.resumeGame();
                     }
+                    // 关闭后立刻重置UI
+                    this.resetUI();
                 });
             }
         };
@@ -84,6 +86,24 @@ export class SkillSelectionController extends Component {
         });
     }
 
+    private resetUI() {
+        this._clicked = false;
+        this.skillButtons.forEach(btn => {
+            // 恢复交互
+            const b = btn.getComponent(Button);
+            if (b) b.interactable = true;
+            // 恢复缩放
+            btn.setScale(Vec3.ONE);
+            // 恢复位置(如果有初始位置记录,建议用初始位置)
+            // btn.setPosition(初始位置);
+            // 恢复透明度等
+            // btn.opacity = 255;
+            // 还原动画状态
+            const anim = btn.getComponent(SkillButtonAnimator);
+            anim?.resetState?.();
+        });
+    }
+
     private _gameManager: GameManager = null;
     private getGameManager(): GameManager {
         if (this._gameManager && this._gameManager.isValid) return this._gameManager;

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

@@ -9,10 +9,6 @@ const { ccclass, property } = _decorator;
 export class WeaponBlockExample extends Component {
     private static _instance: WeaponBlockExample = null;
 
-    // 全局子弹速度(BulletController 会读取)
-    @property({ tooltip: '全局子弹速度(覆盖子弹预制体上的默认值)' })
-    public bulletSpeed: number = 300;
-
     onLoad() {
         WeaponBlockExample._instance = this;
     }
@@ -27,7 +23,4 @@ export class WeaponBlockExample extends Component {
         return WeaponBlockExample._instance;
     }
 
-    public getCurrentBulletSpeed(): number {
-        return this.bulletSpeed;
-    }
 } 

+ 4 - 13
assets/scripts/CombatSystem/WeaponBullet.ts

@@ -133,19 +133,10 @@ export class WeaponBullet extends Component {
         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;
-                    }
-                }
+            // 使用 EnemyController 单例获取最近敌人,避免频繁 find
+            const enemyController = (globalThis as any).EnemyController?.getInstance?.();
+            if (enemyController) {
+                const nearestEnemy = enemyController.getNearestEnemy(initData.firePosition);
                 if (nearestEnemy) {
                     direction = nearestEnemy.worldPosition.clone().subtract(initData.firePosition).normalize();
                 }

部分文件因为文件数量过多而无法显示