Skip to content

Commit 18af31f

Browse files
committed
Fixed how dead particles are managed, reduced gc churn and reset particle positions. Also removed un-needed stable sort.
1 parent fca695f commit 18af31f

1 file changed

Lines changed: 26 additions & 44 deletions

File tree

src/gameobjects/particles/ParticleEmitter.js

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,13 +2012,9 @@ var ParticleEmitter = new Class({
20122012

20132013
for (var i = 0; i < count; i++)
20142014
{
2015-
var particle;
2015+
var particle = dead.pop();
20162016

2017-
if (dead.length > 0)
2018-
{
2019-
particle = dead.pop();
2020-
}
2021-
else
2017+
if (!particle)
20222018
{
20232019
particle = new this.particleClass(this);
20242020
}
@@ -2073,47 +2069,49 @@ var ParticleEmitter = new Class({
20732069
var processors = this.manager.getProcessors();
20742070

20752071
var particles = this.alive;
2072+
var dead = this.dead;
2073+
2074+
var i = 0;
2075+
var rip = [];
20762076
var length = particles.length;
20772077

2078-
for (var index = 0; index < length; index++)
2078+
for (i = 0; i < length; i++)
20792079
{
2080-
var particle = particles[index];
2080+
var particle = particles[i];
20812081

2082-
// update returns `true` if the particle is now dead (lifeStep < 0)
2082+
// update returns `true` if the particle is now dead (lifeCurrent <= 0)
20832083
if (particle.update(delta, step, processors))
20842084
{
2085-
// Moves the dead particle to the end of the particles array (ready for splicing out later)
2086-
var last = particles[length - 1];
2087-
2088-
particles[length - 1] = particle;
2089-
particles[index] = last;
2090-
2091-
index -= 1;
2092-
length -= 1;
2085+
rip.push({ index: i, particle: particle });
20932086
}
20942087
}
20952088

20962089
// Move dead particles to the dead array
2097-
var deadLength = particles.length - length;
2090+
length = rip.length;
20982091

2099-
if (deadLength > 0)
2092+
if (length > 0)
21002093
{
2101-
var rip = particles.splice(particles.length - deadLength, deadLength);
2102-
21032094
var deathCallback = this.deathCallback;
21042095
var deathCallbackScope = this.deathCallbackScope;
21052096

2106-
if (deathCallback)
2097+
for (i = length - 1; i >= 0; i--)
21072098
{
2108-
for (var i = 0; i < rip.length; i++)
2099+
var entry = rip[i];
2100+
2101+
// Remove from particles array
2102+
particles.splice(entry.index, 1);
2103+
2104+
// Add to dead array
2105+
dead.push(entry.particle);
2106+
2107+
// Callback
2108+
if (deathCallback)
21092109
{
2110-
deathCallback.call(deathCallbackScope, rip[i]);
2110+
deathCallback.call(deathCallbackScope, entry.particle);
21112111
}
2112-
}
21132112

2114-
this.dead = this.dead.concat(rip);
2115-
2116-
StableSort.inplace(particles, this.indexSortCallback);
2113+
entry.particle.resetPosition();
2114+
}
21172115
}
21182116

21192117
if (!this.on)
@@ -2153,22 +2151,6 @@ var ParticleEmitter = new Class({
21532151
depthSortCallback: function (a, b)
21542152
{
21552153
return a.y - b.y;
2156-
},
2157-
2158-
/**
2159-
* Calculates the difference of two particles, for sorting them by index.
2160-
*
2161-
* @method Phaser.GameObjects.Particles.ParticleEmitter#indexSortCallback
2162-
* @since 3.0.0
2163-
*
2164-
* @param {object} a - The first particle.
2165-
* @param {object} b - The second particle.
2166-
*
2167-
* @return {integer} The difference of a and b's `index` properties.
2168-
*/
2169-
indexSortCallback: function (a, b)
2170-
{
2171-
return a.index - b.index;
21722154
}
21732155

21742156
});

0 commit comments

Comments
 (0)