diff --git a/plugins/pilot/index.js b/plugins/pilot/index.js index 956899e..deb93fe 100644 --- a/plugins/pilot/index.js +++ b/plugins/pilot/index.js @@ -14,6 +14,10 @@ function pilot(name, deps) { console.log("calibrate", cmd); return deps.client.calibrate(cmd.device_num); }); + socket.on('/pilot/animate', function (cmd) { + console.log("animate", cmd); + return deps.client.animate(cmd.action, 500); + }); }); }; diff --git a/plugins/pilot/public/js/pilot.js b/plugins/pilot/public/js/pilot.js index c6ba54e..f8f7fff 100644 --- a/plugins/pilot/public/js/pilot.js +++ b/plugins/pilot/public/js/pilot.js @@ -10,6 +10,7 @@ PILOT_ACCELERATION = 0.04; , backward = 's' , left = 'a' , right = 'd' + , flip = 'f' ; if (options && options.keyboard === 'qwerty') { } else if (options && options.keyboard === 'azerty') { @@ -52,6 +53,10 @@ PILOT_ACCELERATION = 0.04; 69 : { ev : 'drone', action : 'disableEmergency' + }, + 70 : { + ev : 'animate', + action : 'flip' } }; Keymap[keyCodeMap[forward]] = { @@ -79,6 +84,7 @@ PILOT_ACCELERATION = 0.04; console.log("Loading Pilot plugin."); this.cockpit = cockpit; this.speed = 0; + this.moving = false; this.keys = {}; // Start with magneto calibration disabled. @@ -127,19 +133,47 @@ PILOT_ACCELERATION = 0.04; */ Pilot.prototype.keyDown = function keyDown(ev) { console.log("Keydown: " + ev.keyCode); + if (ev.keyCode == 9) { + PILOT_ACCELERATION = (PILOT_ACCELERATION == 0.04) ? 0.64 : 0.04; + console.log("PILOT_ACCELERATION: " + PILOT_ACCELERATION); + ev.preventDefault(); + return; + } if (Keymap[ev.keyCode] == null) { return; } ev.preventDefault(); - + var key = ev.keyCode; var cmd = Keymap[key]; + //if flip, determine which direction to flip + var regFlip = /^flip/; + if (regFlip.test(cmd.action)) { + console.log("FLIP!"); + //check for which direction to flip + switch (this.moving) { + case 'front': + cmd.action = 'flipAhead'; + break; + case 'back': + cmd.action = 'flipBehind'; + break; + case 'right': + cmd.action = 'flipRight'; + break; + default: + cmd.action = 'flipLeft'; + break; + } + + } // If a motion command, we just update the speed if (cmd.ev == "move") { + this.moving = Keymap[ev.keyCode].action; if (typeof(this.keys[key])=='undefined' || this.keys[key]===null) { this.keys[key] = PILOT_ACCELERATION; } - } + } // Else we send the command immediately else { this.cockpit.socket.emit("/pilot/" + cmd.ev, { @@ -158,7 +192,7 @@ PILOT_ACCELERATION = 0.04; return; } ev.preventDefault(); - + // Delete the key from the tracking array var key = ev.keyCode; delete this.keys[key]; @@ -176,7 +210,7 @@ PILOT_ACCELERATION = 0.04; }); } } - + /* * Triggered by a timer, check for active keys * and send the appropriate motion commands @@ -189,7 +223,7 @@ PILOT_ACCELERATION = 0.04; action : cmd.action, speed : this.keys[k] }); - + // Update the speed this.keys[k] = this.keys[k] + PILOT_ACCELERATION / (1 - this.keys[k]); this.keys[k] = Math.min(1, this.keys[k]);