Spring774
2025-11-08 feb3ecaea68da0d3cfa9750856cef357acd78d0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { ccclass, property } = cc._decorator;
 
export enum PoolType {
    BALL = 'ball',
    BRICK = 'brick',
    PROP_SHOOT = 'shoot',
    PROP_TIMES = 'times',
    PROP_WALL = 'wall'
}
 
@ccclass
export class PoolManager extends cc.Component {
    private static instance: PoolManager;
 
    @property(cc.Prefab)
    ballPrefab: cc.Prefab = null;
    @property(cc.Prefab)
    brickPrefab: cc.Prefab = null;
    @property(cc.Prefab)
    propShootPrefab: cc.Prefab = null;
    @property(cc.Prefab)
    propTimesPrefab: cc.Prefab = null;
    @property(cc.Prefab)
    propWallPrefab: cc.Prefab = null;
 
    public static get Ins() {
        return PoolManager.instance;
    }
 
    private readonly ballReadyCount = 1000;
    private readonly brickReadyCount = 1000;
    private readonly shootReadyCount = 20;
    private readonly timesReadyCount = 20;
    private readonly wallReadyCount = 5;
    private pools = {};
    private createCounter = {};
    onLoad() {
        PoolManager.instance = this;
        cc.game.addPersistRootNode(this.node);
 
        this.pools[PoolType.BALL] = new cc.NodePool('Ball');
        this.pools[PoolType.BRICK] = new cc.NodePool('Brick');
        this.pools[PoolType.PROP_SHOOT] = new cc.NodePool('Prop');
        this.pools[PoolType.PROP_TIMES] = new cc.NodePool('Prop');
        this.pools[PoolType.PROP_WALL] = new cc.NodePool('Prop');
    }
 
    start() {
        this.schedule(() => {
            this.tryLoad();
        }, 0.033);
    }
 
    private tryLoad() {
        let counter = this.createCounter[PoolType.BALL] || 0;
        let fullFlag = 0;
        if (counter < this.ballReadyCount) {
            for (let i = 0; i < 10; ++i) {
                this._create(PoolType.BALL);
            }
        } else {
            fullFlag++;
        }
 
        counter = this.createCounter[PoolType.BRICK] || 0;
        if (counter < this.brickReadyCount) {
            for (let i = 0; i < 10; ++i) {
                this._create(PoolType.BRICK);
            }
        } else {
            fullFlag++;
        }
 
        counter = this.createCounter[PoolType.PROP_SHOOT] || 0;
        if (counter < this.shootReadyCount) {
            this._create(PoolType.PROP_SHOOT);
        } else {
            fullFlag++;
        }
 
        counter = this.createCounter[PoolType.PROP_TIMES] || 0;
        if (counter < this.timesReadyCount) {
            this._create(PoolType.PROP_TIMES);
        } else {
            fullFlag++;
        }
 
        counter = this.createCounter[PoolType.PROP_WALL] || 0;
        if (counter < this.wallReadyCount) {
            this._create(PoolType.PROP_WALL);
        } else {
            fullFlag++;
        }
 
        if (fullFlag >= 5) {
            this.unscheduleAllCallbacks();
            console.log('poolmanager all reloaded.');
        }
    }
 
    private _create(poolType: PoolType) {
        let counter = this.createCounter[poolType] || 0;
        let _node = null;
        if (poolType == PoolType.BALL) {
            _node = cc.instantiate(this.ballPrefab);
        } else if (poolType == PoolType.BRICK) {
            _node = cc.instantiate(this.brickPrefab);
        } else if (poolType == PoolType.PROP_SHOOT) {
            _node = cc.instantiate(this.propShootPrefab);
        } else if (poolType == PoolType.PROP_TIMES) {
            _node = cc.instantiate(this.propTimesPrefab);
        } else if (poolType == PoolType.PROP_WALL) {
            _node = cc.instantiate(this.propWallPrefab);
        } else {
            cc.error('PoolManager create null');
        }
 
        counter++;
        this.createCounter[poolType] = counter;
 
        let pool: cc.NodePool = this.pools[poolType];
        pool.put(_node);
    }
 
    get(poolType: PoolType, manager: any) {
        let _node = null;
        let pool: cc.NodePool = this.pools[poolType];
        if (pool.size() > 0) {
            _node = pool.get(manager);
        } else {
            this._create(poolType);
            _node = pool.get(manager);
        }
 
        return _node;
    }
 
    put(poolType: PoolType, node: cc.Node) {
        let pool: cc.NodePool = this.pools[poolType];
        pool.put(node);
    }
}