splat-ecs
Version:
A 2D HTML5 Canvas game engine
55 lines (49 loc) • 1.52 kB
JavaScript
var boxIntersect = require("box-intersect");
var ObjectPool = require("entity-component-system/lib/object-pool");
function boxFactory() {
return [0, 0, 0, 0];
}
var boxPool = new ObjectPool(boxFactory, 64);
function BoxPool() {
this.ids = [];
this.boxes = [];
this.length = 0;
this.handleCollisionSelf = this.handleCollisionSelf.bind(this);
this.handleCollisionOther = this.handleCollisionOther.bind(this);
}
BoxPool.prototype.add = function(id, position, size) {
this.ids.push(id);
var box = boxPool.alloc();
box[0] = position.x;
box[1] = position.y;
box[2] = position.x + size.width;
box[3] = position.y + size.height;
this.boxes.push(box);
};
BoxPool.prototype.reset = function() {
this.ids.length = 0;
for (var i = 0; i < this.boxes.length; i++) {
boxPool.free(this.boxes[i]);
}
this.boxes.length = 0;
};
BoxPool.prototype.collideOther = function(otherBoxPool, handler) {
this.otherBoxPool = otherBoxPool;
this.handler = handler;
boxIntersect(this.boxes, otherBoxPool.boxes, this.handleCollisionOther);
};
BoxPool.prototype.handleCollisionOther = function(a, b) {
var idA = this.ids[a];
var idB = this.otherBoxPool.ids[b];
return this.handler(idA, idB);
};
BoxPool.prototype.collideSelf = function(handler) {
this.handler = handler;
boxIntersect(this.boxes, this.handleCollisionSelf);
};
BoxPool.prototype.handleCollisionSelf = function(a, b) {
var idA = this.ids[a];
var idB = this.ids[b];
return this.handler(idA, idB);
};
module.exports = BoxPool;