0224995 6 saat önce
ebeveyn
işleme
b96d121c6e

+ 9 - 0
assets/Script/Game/GameFrameWork/NodePool.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "e990dc8e-61b6-4ce9-a341-a814d42f20a9",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 36 - 0
assets/Script/Game/GameFrameWork/NodePool/GameObjectPool.ts

@@ -0,0 +1,36 @@
+import { _decorator, Component, instantiate, Node, NodePool, Prefab } from 'cc';
+
+export class GameObjectPool {
+    private _pool: NodePool;
+    private _prefab: Prefab;
+
+    constructor(prefab: Prefab, initialSize: number){
+        this._prefab = prefab;
+        this._pool = new NodePool();
+        this.preLoad(initialSize);
+    }
+
+    private preLoad(count: number){
+        for(let i = 0; i < count; i++){
+            const node = instantiate(this._prefab);
+            this._pool.put(node);
+        }
+    }
+
+    //获取节点
+    public get(): Node{
+        let node: Node;
+        if(this._pool.size() > 0){
+            node = this._pool.get();
+        } else {
+            node = instantiate(this._prefab)
+        }
+        return node;
+    }
+
+    public put(node: Node){
+        this._pool.put(node);
+    }
+}
+
+

+ 9 - 0
assets/Script/Game/GameFrameWork/NodePool/GameObjectPool.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "28035b97-b930-4c6d-8a18-a726bd8c6620",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 43 - 0
assets/Script/Game/GameFrameWork/NodePool/PoolManager.ts

@@ -0,0 +1,43 @@
+import { _decorator, Component, Node, Prefab } from 'cc';
+import { GameObjectPool } from './GameObjectPool';
+
+export class PoolManager {
+    private static _instance: PoolManager;
+    private _pools: Map<string, GameObjectPool> = new Map();
+
+    public static get instance(){
+        if(!this._instance){
+            this._instance = new PoolManager();
+        }
+        return this._instance;
+    }
+
+    public registerPool(prefab: Prefab, initialSize: number = 10){
+        const key = prefab.name;
+        if(!this._pools.has(key)){
+            this._pools.set(key, new GameObjectPool(prefab, initialSize));
+        }
+    }
+
+    //获取节点实例
+    public get(prefab: Prefab): Node{
+        const key = prefab.name;
+        if(!this._pools.has(key)){
+            this.registerPool(prefab);
+        }
+        return this._pools.get(key)!.get();
+    }
+
+    //回收节点
+    public put(node: Node){
+        const key = node.name;
+        if(this._pools.has(key)){
+            this._pools.get(key)!.put(node);
+        } else {
+            Error("PoolManager destroy")
+            node.destroy();
+        }
+    }
+}
+
+

+ 9 - 0
assets/Script/Game/GameFrameWork/NodePool/PoolManager.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "de7a9498-81fb-4d22-b5d4-a57ac00d7172",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 5 - 5
assets/Script/Game/MyApp/GameScene/BulletPool.ts

@@ -3,7 +3,7 @@ import { resMgr } from '../../../Frames/ResourcesMgr';
 const { ccclass, property } = _decorator;
 
 @ccclass('BulletPool')
-export class BulletPool extends Component{
+export class BulletPool extends Component {
     private _bulletPrefab: Prefab = null;
     private _enemyBulletPrefab: Prefab = null;
 
@@ -25,18 +25,18 @@ export class BulletPool extends Component{
     }
     getBullet(isEnemy: boolean): Node {
         const pool = isEnemy ? this._enemyBulletPool : this._bulletPool;
-        if(pool.size()>0){
+        if (pool.size() > 0) {
             return pool.get()!;
-        }else{
+        } else {
             //动态扩容
-            const prefab = isEnemy ? this._enemyBulletPrefab: this._bulletPrefab;
+            const prefab = isEnemy ? this._enemyBulletPrefab : this._bulletPrefab;
             return instantiate(prefab);
         }
     }
 
     recycle(bullet: Node, isEnemy: boolean) {
         const pool = isEnemy ? this._enemyBulletPool : this._bulletPool;
-        if(!pool) return;
+        if (!pool) return;
         pool.put(bullet);
     }
 }

+ 1 - 1
assets/Script/Game/MyApp/Role.ts

@@ -53,7 +53,7 @@ export class Role extends Component {
 
     //状态管理
     _state: RoleState = null;
-
+    
     init(name: string, pos: Vec3, roleDatas: RoleData[], dir?: number) {
         this._bulletPool = this.node.getComponent(BulletPool);
         this._bulletPool.init();