Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let noop = function() {
};
export default class ArcadeController {
static factory(moveStartCallback, moveCallback, fireCallback, cbContext) {
return new Phaser.ArcadeController(this.game, moveStartCallback, moveCallback, fireCallback, cbContext)
}
constructor(game, moveStartCallback = noop, moveCallback = noop, fireCallback = noop, callbackContext) {
game.input.holdRate = 150;
game.input.touch.consumeDocumentTouches();
game.input.onTap.add(this.onTap, this);
game.input.onHold.add(this.onHold, this);
game.input.onUp.add(this.onUp, this);
this.movePointer = null;
this.touchStartX = null;
this.lastMoveX = null;
this.moveStartCallback = moveStartCallback.bind(callbackContext);
this.moveCallback = moveCallback.bind(callbackContext);
this.fireCallback = fireCallback.bind(callbackContext);
this.game = game;
}
onTap(pointer) {
this.fireCallback(pointer.x);
}
onHold(pointer) {
if (!this.movePointer) {
this.movePointer = pointer;
this.game.input.addMoveCallback(this.onMove, this);
this.touchStartX = pointer.x;
this.moveStartCallback(pointer.x);
}
}
onUp(pointer) {
if (this.movePointer == pointer) {
this.game.input.deleteMoveCallback(this.onMove, this);
this.movePointer = null;
}
}
onMove(pointer, x, y) {
if (pointer == this.movePointer) {
let dx = x - this.touchStartX;
this.moveCallback(x, dx);
this.lastMoveX = x;
}
}
resetTouch(x = this.lastMoveX) {
this.touchStartX = x;
}
}
import monkeypatch from './PhaserMonkeyPatch';
import ArcadeControl from './ArcadeControl';
monkeypatch(ArcadeControl, ArcadeControl.factory);
var game = new Phaser.Game(...);
let controller = new game.add.arcadeController(onMoveStart, onMove, onFire);
onMoveStart(x) {
console.log('move started:', x);
}
onMove(x, dx) {
console.log('moved to:', x);
console.log('move distance:', dx);
}
onFire(x) {
console.log('fired from:', x);
}
import { camelCase } from 'lodash';
function defaultFactoryFn() {
return function() {
console.error('No factory function provided to monkey patch class', name);
}
}
export default function(klass, factoryFn = defaultFactoryFn) {
console.log('monkey patching:', klass.name);
Phaser[klass.name] = klass;
Phaser.GameObjectFactory.prototype[camelCase(klass.name)] = factoryFn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.