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