Skip to content

Instantly share code, notes, and snippets.

@jotson
Last active December 15, 2015 01:49
Show Gist options
  • Save jotson/5183151 to your computer and use it in GitHub Desktop.
Save jotson/5183151 to your computer and use it in GitHub Desktop.
Crafty ObjectPool
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