Skip to content

Instantly share code, notes, and snippets.

@SomeKittens
Created September 7, 2014 01:08
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 SomeKittens/5234e9fb88da4a18663d to your computer and use it in GitHub Desktop.
Save SomeKittens/5234e9fb88da4a18663d to your computer and use it in GitHub Desktop.
Collection
'use strict';
function inherits (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
}
function CanvasItem () {
this.x = 0;
this.y = 0;
this.img = null;
}
CanvasItem.prototype = {
constructor: CanvasItem,
update: function () {},
destroyed: function () {},
render: function () {
context.drawImage(this.img, this.x, this.y);
}
};
function CanvasCollection (item) {
if (!(item.prototype instanceof CanvasItem)) {
throw new Error('BAD USER');
}
this.item = item;
this.items = [];
}
CanvasCollection.prototype = {
constructor: CanvasCollection,
create: function (params) {
var newItem = new this.item(params);
this.items.push(newItem);
return newItem;
},
update: function () {
for (var i = 0; i < this.items.length; i++) {
this.items[i].update();
}
},
gc: function () {
this.items = this.items.filter(function (item) {
return !item.destroyed();
});
},
render: function () {
for (var i = 0; i < this.items.length; i++) {
this.items[i].render();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment