diff --git a/src/client/engine/controls/InputHandler.js b/src/client/engine/controls/InputHandler.js index 27678d98..ff52693a 100644 --- a/src/client/engine/controls/InputHandler.js +++ b/src/client/engine/controls/InputHandler.js @@ -426,6 +426,23 @@ InputHandler.prototype = { } } }, + + _pointerMoveHandler: function(pointer) { + // Abort. We've been destroyed. + if(this.sprite === null) { return; } + + var sendEvent, data = this._pointerData[pointer.id]; + if(data.isOver || pointer.dirty) { + sendEvent = data.isOver; + + data.x = pointer.x - this.sprite.x; + data.y = pointer.y - this.sprite.y; + + if(sendEvent && this.sprite) { + this.sprite.emit('inputMove', this.sprite, pointer); + } + } + }, _pointerOutHandler: function(pointer) { // Abort. We've been destroyed. diff --git a/src/client/engine/controls/Pointer.js b/src/client/engine/controls/Pointer.js index 2331b2b0..9327b1d5 100644 --- a/src/client/engine/controls/Pointer.js +++ b/src/client/engine/controls/Pointer.js @@ -332,6 +332,8 @@ Pointer.prototype = { while(i--) { input.moveCallbacks[i].callback.call(input.moveCallbacks[i].context, this, this.x, this.y, fromClick); } + + input.interactiveItems.callAll('_pointerMoveHandler', this); // Easy out if we're dragging something and it still exists if(this.targetObject !== null && this.targetObject.isDragged === true) { diff --git a/src/client/objects/sector/InputManager.js b/src/client/objects/sector/InputManager.js index e4cb8472..f9232a90 100644 --- a/src/client/objects/sector/InputManager.js +++ b/src/client/objects/sector/InputManager.js @@ -12,32 +12,47 @@ function Selection(game) { return true; }; - this.world.on('inputUp', this._onInput, this); - this.world.on('inputDown', this._onInput, this); -}; + this.world.on('inputDown', this._onInputDown, this); + this.world.on('inputUp', this._onInputUp, this); + this.world.on('inputMove', this._onInputMove, this); +} Selection.prototype.constructor = Selection; -Selection.prototype._onInput = function(world, pointer) { +Selection.prototype._emitInputAction = function(dataType, pointer, emitPrimary, emitSecondary) { var data = { - type: pointer.isDown ? 'start' : 'stop', + type: dataType, target: { x: pointer.x, y: pointer.y } }; - if(pointer.button === engine.Mouse.LEFT_BUTTON) { + if (emitPrimary) { this.game.emit('ship/primary', data); - } else if(pointer.button === engine.Mouse.RIGHT_BUTTON) { + } + if (emitSecondary) { this.game.emit('ship/secondary', data); } }; +Selection.prototype._onInputDown = function(world, pointer) { + this._emitInputAction('start', pointer, pointer.button === engine.Mouse.LEFT_BUTTON, pointer.button === engine.Mouse.RIGHT_BUTTON); +}; + +Selection.prototype._onInputUp = function(world, pointer) { + this._emitInputAction('stop', pointer, pointer.button === engine.Mouse.LEFT_BUTTON, pointer.button === engine.Mouse.RIGHT_BUTTON); +}; + +Selection.prototype._onInputMove = function(world, pointer) { + this._emitInputAction('move', pointer, pointer.leftButton.isDown, pointer.rightButton.isDown); +}; + Selection.prototype.destroy = function() { this.input.destroy(); - this.world.removeListener('onDown', this._onInput); - this.world.removeListener('onUp', this._onInput); + this.world.removeListener('inputDown', this._onInputDown); + this.world.removeListener('inputUp', this._onInputUp); + this.world.removeListener('inputMove', this._onInputMove); this.game = this.input = undefined; diff --git a/src/client/objects/sector/ShipManager.js b/src/client/objects/sector/ShipManager.js index 64c9c465..dec6278a 100644 --- a/src/client/objects/sector/ShipManager.js +++ b/src/client/objects/sector/ShipManager.js @@ -37,6 +37,9 @@ function ShipManager(game) { this.glowEmitter = new GlowEmitter(this.game); this.shockwaveEmitter = new ShockwaveEmitter(this.game); this.fireEmitter = new FireEmitter(this.game); + + // throttled function to plot ship course + this._throttledPlot = null; this.game.particles.add(this.explosionEmitter); this.game.particles.add(this.flashEmitter); @@ -217,7 +220,7 @@ ShipManager.prototype._primary = function(data) { y: input.mousePointer.y }); }); - } else { + } else if (data.type === 'stop') { this.autofire && clock.events.remove(this.autofire); } } @@ -225,6 +228,7 @@ ShipManager.prototype._primary = function(data) { ShipManager.prototype._secondary = function(data) { var game = this.game, + clock = this.clock, ship = this.player, socket = this.socket, indicator = this.indicator, @@ -239,6 +243,14 @@ ShipManager.prototype._secondary = function(data) { uuid: ship.uuid, destination: destination }); + } else if (data.type === 'move') { + this._throttledPlot = this._throttledPlot || clock.throttle(function (data) { + socket.emit('ship/plot', data); + }, 250); // throttle threshold + this._throttledPlot({ + uuid: ship.uuid, + destination: destination + }); } } };