diff --git a/src/client/objects/sector/ArrowKeysManager.js b/src/client/objects/sector/ArrowKeysManager.js new file mode 100644 index 00000000..46b9cf43 --- /dev/null +++ b/src/client/objects/sector/ArrowKeysManager.js @@ -0,0 +1,60 @@ + +var engine = require('engine'); + +function KeySelection(game) { + this.game = game; + this.world = game.world; + this.settings = { + stepOffset: 100, + stepRotation: 1, + speed: 1.5 + }; + + this.game.input.on('keydown', this._onDown, this); + this.game.input.on('keyup', this._onUp, this); + +}; + +KeySelection.prototype.constructor = KeySelection; + +KeySelection.prototype._onDown = function(event, key) { + var keyboard = this.game.input.keyboard, + move = false, + data = { + offset: 0, + rotation: 0 + }; + + if(keyboard.isDown(engine.Keyboard.UP)) { + data.offset = -this.settings.stepOffset * this.settings.speed; + move = true; + } + if(keyboard.isDown(engine.Keyboard.LEFT)) { + data.offset = -this.settings.stepOffset * this.settings.speed; + data.rotation = -this.settings.stepRotation; + move = true; + } + if(keyboard.isDown(engine.Keyboard.RIGHT)) { + data.offset = -this.settings.stepOffset * this.settings.speed; + data.rotation = this.settings.stepRotation; + move = true; + } + + move && this.game.emit('ship/moveTo', data); +}; + +KeySelection.prototype._onUp = function(event, key) { + var keyboard = this.game.input.keyboard; + var data = { + offset: 1, + rotation: 0 + }; + if(keyboard.isDown(engine.Keyboard.LEFT) || keyboard.isDown(engine.Keyboard.UP) || keyboard.isDown(engine.Keyboard.RIGHT)){ + this._onDown(); + } else{ + this.game.emit('ship/moveTo', data); + } + +}; + +module.exports = KeySelection; \ No newline at end of file diff --git a/src/client/objects/sector/ShipManager.js b/src/client/objects/sector/ShipManager.js index 64c9c465..d70773d8 100644 --- a/src/client/objects/sector/ShipManager.js +++ b/src/client/objects/sector/ShipManager.js @@ -78,6 +78,8 @@ function ShipManager(game) { this.game.on('ship/removed', this._removed, this); this.game.on('ship/disabled', this._disabled, this); this.game.on('ship/enabled', this._enabled, this); + + this.game.on('ship/moveTo', this._moveTo, this); }; ShipManager.prototype.constructor = ShipManager; @@ -243,6 +245,18 @@ ShipManager.prototype._secondary = function(data) { } }; +ShipManager.prototype._moveTo = function(data) { + var ship = this.player, + socket = this.socket; + if(ship) { + var destination = new engine.Point(data.offset, 0).rotate(0, 0, ship.rotation + data.rotation ); + socket.emit('ship/plot', { + uuid: ship.uuid, + destination: destination + }); + } +}; + ShipManager.prototype._follow = function(ship) { if(ship.position) { this.game.camera.follow(ship); diff --git a/src/client/states/SectorState.js b/src/client/states/SectorState.js index c9a9a43f..84ef5529 100644 --- a/src/client/states/SectorState.js +++ b/src/client/states/SectorState.js @@ -5,6 +5,7 @@ var engine = require('engine'), // ShockwaveManager = require('../fx/ShockwaveManager'), Snow = require('../fx/Snow'), InputManager = require('../objects/sector/InputManager'), + ArrowsKeysManager = require('../objects/sector/ArrowKeysManager'), ShipManager = require('../objects/sector/ShipManager'), StationManager = require('../objects/sector/StationManager'), Asteroid = require('../objects/sector/misc/Asteroid'); @@ -128,6 +129,7 @@ SectorState.prototype.createManagers = function() { this.shipManager = new ShipManager(this.game); this.inputManager = new InputManager(this.game); + this.arrowKeysManager = new ArrowsKeysManager(this.game); }; SectorState.prototype.createSnow = function() { @@ -163,7 +165,7 @@ SectorState.prototype.update = function() { x = 0, y = 0, amount = 512; - if(!this.scrollLock) { + if(!this.scrollLock && !this.arrowKeysManager) { if(keyboard.isDown(engine.Keyboard.A) || keyboard.isDown(engine.Keyboard.LEFT)) { x = -amount; }