gibbon.js
Version:
Actor/Component system for use with pixi.js.
105 lines (104 loc) • 3.27 kB
JavaScript
import { Component } from '../core/component';
import { Grid } from './grid';
import { Collider2d } from '../components/collider2d';
import { EngineEvent } from '../events/engine-events';
/**
* Wraps quadtree to give access to geometric testing.
*/
export class HitDetection extends Component {
/**
* underlying quadtree that provides collision tests.
*/
//private tree: Quadtree<Indexable>;
/**
* Properties used for creating quadtree.
*/
//private treeProps: QuadtreeProps;
grid;
_transform;
/**
*
* @param props
* @param props.parent - parent whose children with collider2ds are detected for collisions.
* @param props.width
* @param props.height
*/
constructor(props) {
super();
this.grid = new Grid({ width: props.width, height: props.height });
/*if (!tree) {
this.tree = tree ?? new Quadtree({
maxLevels: 5,
maxObjects: 10,
width: 100,
height: 100
});
} else if (tree instanceof Quadtree) {
this.tree = tree;
} else {
this.tree = new Quadtree(tree);
}*/
}
init() {
// Find initial colliders.
this._transform = this.actor.transform;
const colliders = this._transform.findInChildren(Collider2d);
for (let i = colliders.length - 1; i >= 0; i--) {
this.grid.addItem(colliders[i]);
}
this.actor.on(EngineEvent.ChildAdded, this.childAdded, this);
this.actor.off(EngineEvent.ChildRemoved, this.childRemoved, this);
this.actor.on(EngineEvent.ActorDestroyed, this.childRemoved, this);
}
childAdded(t) {
const collider = t.get(Collider2d);
if (collider) {
this.grid.addItem(collider);
}
}
/**
* Update collider's position in grid.
* @param collider
*/
updateItem(collider) {
this.grid.removeItem(collider);
this.grid.addItem(collider);
}
childRemoved(t) {
const collider = t.get(Collider2d);
if (collider) {
this.grid.removeItem(collider);
}
}
addCollider(collider) {
//this.tree.insert(obj);
this.grid.addItem(collider);
}
removeCollider(collider) {
this.grid.removeItem(collider);
}
update(delta) {
/**
* Updating Positions: In future might actually detect changes in colliders themselves?
*/
for (const child of this._transform) {
const collider = child.get(Collider2d);
if (collider && !collider.isStatic) {
this.grid.removeItem(collider);
this.grid.addItem(collider);
}
}
const hitResults = [];
for (const child of this._transform) {
const collider = child.get(Collider2d);
if (collider) {
hitResults.length = 0;
this.grid.getHits(collider, hitResults);
if (hitResults.length > 0) {
collider.actor.emit(EngineEvent.Collision, collider, hitResults);
}
}
}
}
}
//# sourceMappingURL=hit-detection.js.map