Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Created May 19, 2019 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KinoAR/a3fa44282a67219dc9c9944ac474988a to your computer and use it in GitHub Desktop.
Save KinoAR/a3fa44282a67219dc9c9944ac474988a to your computer and use it in GitHub Desktop.
Example object pooling code written for the Script-8
function fireProjectiles(input,player, projectiles) {
if(input.startPressed) {
const availableProjectiles = projectiles.filter( projectile => projectile.visible === false);
const nextProjectile = (availableProjectiles !== null && availableProjectiles.length > 0) ? availableProjectiles[0] : null
if(nextProjectile !== null) {
nextProjectile.visible = true
nextProjectile.x = player.x + 3
nextProjectile.y = player.y
nextProjectile.dx = PROJECTILESPEED
}
}
}
function updateProjectiles(projectiles) {
projectiles.forEach(projectile => {
projectile.x += projectile.dx
})
}
function poolPlayerProjectiles(projectiles) {
projectiles.forEach(projectile => {
if(projectile.x > 128) {
projectile.visible = false
projectile.dx = 0
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment