-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSFXPool.js
136 lines (107 loc) · 3.44 KB
/
SFXPool.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
var SFXPool = (function () {
function allocate (pool, amount) {
if(!pool) { return; }
if(isNaN(amount) || amount <= 0) {
amount = 1;
}
if(amount > pool.length){
amount = pool.length;
}
var initialAmount = amount;
var returned = [];
//try to find free
for(var i = pool.length - 1; i >= 0; i--) {
if(!pool[i].inUse){
returned.push(pool[i]);
amount--;
if(amount == 0) { break; }
}
}
//if not enough allocate those in use
if(returned.length < initialAmount) {
for(var i = pool.length - 1; i >= 0; i--){
if(pool[i].inUse){
returned.push(pool[i]);
amount--;
if(amount == 0) { break; }
}
}
}
return returned;
};
function getSpawned (pool) {
if(!pool) { return; }
var inUse = [];
for(var i = pool.length - 1; i >= 0; i--){
if(pool[i].inUse){
inUse.push(pool[i]);
}
}
return inUse;
};
function SFXPool () {
this._scoreSFXPool = [];
this._gorePool = [];
this._knivesPool = [];
this._sparkSFXPool = [];
this._smokeSFXPool = [];
};
SFXPool.prototype.init = function () {
this._scoreSFXPool = [];
this._gorePool = [];
this._knivesPool = [];
this._sparkSFXPool = [];
this._smokeSFXPool = [];
var scoreSFXAmount = 4;
var goreAmount = 20;
var knivesAmount = 10;
var sparksAmount = 4;
var smokeAmount = 2;
for(var i = scoreSFXAmount - 1; i >= 0; i--) {
this._scoreSFXPool.push(new ScoreSFX());
}
for(var i = goreAmount - 1; i >= 0; i--) {
this._gorePool.push(new Gore());
}
for(var i = knivesAmount - 1; i >= 0; i--) {
this._knivesPool.push(new Knife());
}
for(var i = sparksAmount - 1; i >= 0; i--) {
this._sparkSFXPool.push(new SparkSFX());
}
for(var i = smokeAmount - 1; i >= 0; i--) {
this._smokeSFXPool.push(new SmokeSFX());
}
};
SFXPool.prototype.allocateSmokeSFX = function (amount) {
return allocate(this._smokeSFXPool, amount);
};
SFXPool.prototype.allocateScoreSFX = function (amount) {
return allocate(this._scoreSFXPool, amount);
};
SFXPool.prototype.getSpawnedScoreSFX = function () {
return getSpawned(this._scoreSFXPool);
};
SFXPool.prototype.allocateGore = function (amount) {
return allocate(this._gorePool, amount);
};
SFXPool.prototype.getSpawnedGore = function () {
return getSpawned(this._gorePool);
};
SFXPool.prototype.allocateKnives = function (amount) {
return allocate(this._knivesPool, amount);
};
SFXPool.prototype.getSpawnedKnives = function () {
return getSpawned(this._knivesPool);
};
SFXPool.prototype.allocateSparkSFX = function (amount) {
return allocate(this._sparkSFXPool, amount);
};
SFXPool.prototype.getSpawnedSparkSFX = function () {
return getSpawned(this._sparkSFXPool);
};
SFXPool.prototype.getSpawnedSmokeSFX = function () {
return getSpawned(this._smokeSFXPool);
};
return SFXPool;
})();