0224995 1 день назад
Родитель
Сommit
56b5b14f13

+ 6 - 1
assets/Script/Game/MyApp/GameScene/EnemyMgr.ts

@@ -9,6 +9,7 @@ import { GameMgr } from '../../GameFrameWork/GameMgr';
 import { TouchGame } from '../TouchGame';
 import { Tools } from '../../Tools/Tools';
 import { EnemyTower } from './EnemyTower';
+import { PoolManager } from '../../GameFrameWork/NodePool/PoolManager';
 const { ccclass, property } = _decorator;
 
 @ccclass('EnemyMgr')
@@ -34,6 +35,9 @@ export class EnemyMgr extends ModulerBase {
         this._enemyTower = this.node.parent.getChildByName("EnemyTower");
     }
     init() {
+        //创建多个敌人预制体
+        PoolManager.instance.registerPool(resMgr.getPrefab("Enemy"),6);
+
         this._roleDatas = dataMgr.getAllDataByName("RoleCardData");
         //第几关的数据
         this._lvDt = this._lvDts[GameInfo.Instance.getCurlv() - 1];
@@ -48,7 +52,8 @@ export class EnemyMgr extends ModulerBase {
     }
 
     private _createEnemy() {
-        const enemy: Node = instantiate(resMgr.getPrefab("Enemy"));
+        //const enemy: Node = instantiate(resMgr.getPrefab("Enemy"));
+        const enemy: Node = PoolManager.instance.get(resMgr.getPrefab("Enemy"));
         enemy.parent = this.node;
         const enemyTS = enemy.getComponent(Enemy);
         const imgNameArray = this._lvDt.imgName;

+ 1 - 1
assets/Script/Game/MyApp/GameScene/MyRole.ts

@@ -9,7 +9,7 @@ const { ccclass, property } = _decorator;
 @ccclass('MyRole')
 export class MyRole extends Role {
     private _lifeBar: LifeBar = null;
-
+    
     protected onLoad(): void {
         this._lifeBar = this.node.getComponent(LifeBar);
     }

+ 55 - 7
assets/Script/Game/MyApp/Role.ts

@@ -53,7 +53,15 @@ export class Role extends Component {
 
     //状态管理
     _state: RoleState = null;
-    
+    //被回收时,重置属性
+    protected onDisable(): void {
+        this._clearData();
+    }
+    //重新激活时
+    protected onEnable(): void {
+
+    }
+
     init(name: string, pos: Vec3, roleDatas: RoleData[], dir?: number) {
         this._bulletPool = this.node.getComponent(BulletPool);
         this._bulletPool.init();
@@ -97,7 +105,6 @@ export class Role extends Component {
         //位置
         this.node.setWorldPosition(pos);
         this.bulletLayer = find("Canvas/GameRoot/BulletLayer");
-
     }
 
     private _setRoleData(roleData: RoleData) {
@@ -125,14 +132,14 @@ export class Role extends Component {
     }
 
     update(deltaTime: number) {
-        
+
         if (this._state === RoleState.Die) return;
         //只有游戏结束才能执行后续操作
-        if(GameInfo.Instance.getIsGameOver()) return;
-        
+        if (GameInfo.Instance.getIsGameOver()) return;
+
         if (!this.currentTarget) {
             this._handleMovement(deltaTime);
-            
+
             this._detectEnemies();
         } else {
             this._handleAttack(deltaTime);
@@ -156,7 +163,7 @@ export class Role extends Component {
         //节点不可用,不寻敌
         if (!this.node.isValid) return;
         //游戏结束,不寻敌
-        if(GameInfo.Instance.getIsGameOver()) return;
+        if (GameInfo.Instance.getIsGameOver()) return;
         //const startPos = new Vec2(this.node.position.x, this.node.position.y);
         const startPos = this.node.position.clone();
         const endPos = new Vec2((this.direction * this.atkLength) + this.node.position.x, this.node.position.y);
@@ -312,6 +319,47 @@ export class Role extends Component {
         })
     }
 
+    private _clearData() {
+        // 重置基础数据
+        this.hp = null;
+        this.atk = null;
+        this.atkLength = null;
+        this.moveSpeed = null;
+
+        // 重置方向和状态
+        this.direction = 1;
+        this.isStop = false;
+
+        // 清除战斗系统状态
+        this._attackTimer = 0;
+        this.targetNode = null;
+        this.currentTarget = null;
+
+        // 释放子弹资源
+        if (this._bullet) {
+            // 如果使用了对象池,将子弹放回池
+            if (this._bulletPool) {
+                const isEnemy = this.direction === -1;
+                this._bulletPool.recycle(this._bullet, isEnemy);
+            } else {
+                // 否则直接销毁
+                this._bullet.destroy();
+            }
+            this._bullet = null;
+        }
+
+        // 重置动画状态
+        if (this._animation) {
+            this._animation.stop();
+        }
+
+        // 重置状态机
+        this._state = null;
+
+        // 清除节点引用
+        //this.bulletLayer = null;
+    }
+
     //要求子类实现的碰撞分组和阵营判断方法,确保不同阵营角色可以正确交互
     protected _getCollisionGroup(): number {
         throw new Error("Method not implemented");

+ 13 - 6
assets/Script/Game/MyApp/TouchGame.ts

@@ -1,4 +1,4 @@
-import { _decorator,  Node, EventTouch, Rect, UITransform, Sprite, SpriteFrame, Vec2, instantiate, Vec3, Component } from 'cc';
+import { _decorator, Node, EventTouch, Rect, UITransform, Sprite, SpriteFrame, Vec2, instantiate, Vec3, Component } from 'cc';
 import { resMgr } from '../../Frames/ResourcesMgr';
 import { Role } from './Role';
 import { dataMgr } from '../../Frames/DataManager';
@@ -7,6 +7,7 @@ import { ModulerBase } from '../GameFrameWork/ModulerBase';
 import { MyRole } from './GameScene/MyRole';
 import { Card } from './Card';
 import { Bottom } from './GameScene/Bottom';
+import { PoolManager } from '../GameFrameWork/NodePool/PoolManager';
 const { ccclass, property } = _decorator;
 
 @ccclass('TouchGame')
@@ -36,6 +37,9 @@ export class TouchGame extends ModulerBase {
     }
 
     protected start(): void {
+        //创建6个预制体
+        PoolManager.instance.registerPool(resMgr.getPrefab("Role"), 6);
+
         this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
         this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
         this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
@@ -51,7 +55,7 @@ export class TouchGame extends ModulerBase {
             if (box.contains(pos)) {
                 this._clickedNode = element;
                 this._clickedNodeCom = this._clickedNode.getComponent(Card);
-                if(!this._clickedNodeCom.disabled && this._clickedNodeCom.clickable){
+                if (!this._clickedNodeCom.disabled && this._clickedNodeCom.clickable) {
                     const img: SpriteFrame = element.getChildByName("Sprite").getComponent(Sprite).spriteFrame;
                     if (!img) {
                         this._isDragging = false;
@@ -65,7 +69,7 @@ export class TouchGame extends ModulerBase {
                         this.setDragNodePosition(pos);
                     }
                     break; // 找到一个就停止遍历 
-                } 
+                }
             }
         }
     }
@@ -105,7 +109,7 @@ export class TouchGame extends ModulerBase {
         for (const element of this._load.children) {
             if (element.name !== "Roles") {
                 const box = element.getComponent(UITransform).getBoundingBoxToWorld();
-                if (box.contains(new Vec2(pos.x,pos.y))) {
+                if (box.contains(new Vec2(pos.x, pos.y))) {
                     this._highLightIdx = element.getSiblingIndex();
                     if (this._highLightIdx >= 6) {
                         this._hideNode();
@@ -115,7 +119,10 @@ export class TouchGame extends ModulerBase {
                 }
             }
         }
-        const role: Node = instantiate(resMgr.getPrefab("Role"));
+        
+        //从NodePool里获取一个Node
+        const role: Node = PoolManager.instance.get(resMgr.getPrefab("Role"));
+        //const role: Node = instantiate(resMgr.getPrefab("Role"));
         role.parent = this._load.getChildByName("Roles");
 
         const roleTS: Role = role.getComponent(MyRole);
@@ -130,7 +137,7 @@ export class TouchGame extends ModulerBase {
         this._clickedNode.getComponent(Card).aniPlay();
         this._hideNode();
     }
-    
+
     private _hideNode() {
         this._dragNode.active = false;
         this._dragNode.getComponent(Sprite).spriteFrame = null;