Created
September 7, 2014 01:08
-
-
Save SomeKittens/5234e9fb88da4a18663d to your computer and use it in GitHub Desktop.
Collection
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
'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