Created
July 1, 2016 14:00
-
-
Save bartread/4b9f345f08233d7b8ec158bc59e9db73 to your computer and use it in GitHub Desktop.
Here's my original createParticle() function, showing the wasteful creation of objects every time a particle is created
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createParticle( | |
definitionName, | |
position, | |
velocity) { | |
var definition = descriptors[definitionName]; | |
if (definition === undefined) { | |
return undefined; | |
} | |
var particle = { | |
position: position, | |
previousPosition: { | |
x: position.x, | |
y: position.y | |
}, | |
velocity: velocity, | |
frameIndex: 0, | |
isWithinGameBounds: true, | |
frames: definition.frameParams, | |
isDestroyed: false, | |
isLive: isLive, | |
wrap: definition.wrap || false, | |
move: move | |
}; | |
particles.push(particle); | |
return particle; | |
function isLive() { | |
return (particle.isWithinGameBounds || particle.wrap) | |
&& !particle.isDestroyed | |
&& particle.frameIndex < particle.frames.length; | |
} | |
function move() { | |
if (!isLive()) { | |
return; | |
} | |
particle.previousPosition.x = particle.position.x; | |
particle.previousPosition.y = particle.position.y; | |
particle.position.x += particle.velocity.x; | |
particle.position.y += particle.velocity.y; | |
particle.frameIndex++; | |
if (particle.wrap) { | |
wrapper.wrap(particle); | |
} | |
else if (!gameDimensions.isWithinGameBounds(particle.position)) { | |
particle.isWithinGameBounds = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment