Last active
December 15, 2015 01:49
-
-
Save jotson/5183151 to your computer and use it in GitHub Desktop.
Crafty ObjectPool
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
var ObjectPool = { | |
/** | |
* Get a cached entity or create a new one | |
* @param string component Component string (e.g. "player") | |
* @return entity | |
*/ | |
get: function(component) { | |
var objects = Crafty(component); | |
for(var i = 0; i < objects.length; i++) { | |
var e = Crafty(objects[i]); | |
if (!e.visible) { | |
e.visible = true; | |
e.active = true; | |
if (typeof(e.revive) == 'function') { | |
e.revive(); | |
} | |
return e; | |
} | |
} | |
if (DEBUG) console.log("Creating new " + component); | |
var e = Crafty.e(component); | |
e.setName(component); | |
e.visible = true; | |
e.active = true; | |
return e; | |
}, | |
/** | |
* Recycle | |
* @param entity o Entity | |
*/ | |
recycle: function(e) { | |
e.visible = false; | |
e.active = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment