-
Notifications
You must be signed in to change notification settings - Fork 3
/
particles-pool.js
171 lines (144 loc) · 3.21 KB
/
particles-pool.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { WORLD_MAP_BLOCK_SIZE } from "../../settings";
import { ParticlesBucket } from "./particles-bucket";
import { CubeParticle } from "./cube-particle";
const PARTICLES_DEFAULT_POOL_SIZE = 1e4;
export class ParticlesPool {
/**
* @type {ParticlesPool}
* @private
*/
static _instance = null;
/**
* @returns {ParticlesPool}
*/
static getPool() {
if (!this._instance) {
return ( this._instance = new ParticlesPool() );
}
return this._instance;
}
/**
* @type {number}
* @private
*/
_poolSize = PARTICLES_DEFAULT_POOL_SIZE;
/**
* @type {Array<Particle>}
* @private
*/
_pool = [];
/**
* @type {boolean}
* @private
*/
_poolReady = false;
/**
* @type {Array<ParticlesBucket>}
* @private
*/
_buckets = [];
/**
* @param {Particle} sourceObject
*/
createPool (sourceObject = null) {
if (this._poolReady) {
console.warn('[Warn] Particles Pool already initialized');
}
this._pool = [];
sourceObject = sourceObject || this._createSourceObject();
for (let i = 0; i < this._poolSize; ++i) {
this._cloneParticle( sourceObject );
}
this._poolReady = true;
}
/**
* @param {number} particlesNumber
* @returns {ParticlesBucket}
*/
take (particlesNumber = 0) {
particlesNumber = Math.min( Math.max( particlesNumber, 0 ), this.poolFreeSize );
if (!this._poolReady) {
particlesNumber = 0;
}
// console.info(`[ParticlesPool] got ${particlesNumber} particles from the pool`);
let particles = this._pool.splice( 0, particlesNumber );
let bucket = new ParticlesBucket( particles );
this._buckets.push( bucket );
return bucket;
}
/**
* @param {ParticlesBucket} particlesBucket
*/
transfer (particlesBucket) {
if (!particlesBucket.isReleased) {
return particlesBucket.release();
}
let bucketIndex = this._findBucketIndex( particlesBucket );
if (bucketIndex >= 0) {
this._buckets.splice( bucketIndex, 1 );
}
let particles = particlesBucket.particles || [];
this._pool = this._pool.concat( particles );
}
/**
* @returns {number}
*/
get poolMaxSize () {
return this._poolSize;
}
/**
* @returns {number}
*/
get poolFreeSize () {
return this._pool.length;
}
/**
* @returns {Array<*>}
*/
get buckets () {
return this._buckets;
}
/**
* @returns {boolean}
*/
get poolReady () {
return this._poolReady;
}
/**
* @param {*} options
* @private
*/
_initOptions (options = {}) {
let {
poolSize = PARTICLES_DEFAULT_POOL_SIZE
} = options;
this._poolSize = poolSize;
}
/**
* @returns {Particle}
* @private
*/
_createSourceObject () {
const particle = new CubeParticle();
particle.shadows = true;
return particle;
}
/**
* @param {Particle} sourceObject
* @private
*/
_cloneParticle (sourceObject) {
let particle = sourceObject.clone();
this._pool.push( particle );
}
/**
* @param {ParticlesBucket} particlesBucket
* @returns {number}
* @private
*/
_findBucketIndex (particlesBucket) {
return this._buckets.findIndex(bucket => {
return bucket.id === particlesBucket.id;
});
}
}