@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
45 lines (36 loc) • 1.28 kB
JavaScript
/**
* Decide and execute the operation based on pointer device events
*/
export class OperationRouter {
/**
*
* @param {PointerDevice} pointerDevice
* @param {function} resolveOperation
*/
constructor(pointerDevice, resolveOperation) {
this.pointer = pointerDevice;
this.resolveOperation = resolveOperation;
this.activeOperation = null;
this.pointer.on.down.add(this.start, this);
this.pointer.on.drag.add(this.run, this);
this.pointer.on.up.add(this.stop, this);
this.pointer.on.dragEnd.add(this.stop, this);
}
start(position, event) {
if (this.activeOperation) return;
const operation = this.resolveOperation(event);
if (!operation) return;
this.activeOperation = operation;
this.activeOperation.start(position, event);
}
run(position, origin, last, event) {
if (!this.activeOperation) return;
const delta = position.clone().sub(last);
this.activeOperation.drag(position, delta, event);
}
stop(position, event) {
if (!this.activeOperation) return;
this.activeOperation.end(position, event);
this.activeOperation = null;
}
}