Created
May 19, 2019 21:58
-
-
Save KinoAR/a3fa44282a67219dc9c9944ac474988a to your computer and use it in GitHub Desktop.
Example object pooling code written for the Script-8
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 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