Skip to content

Commit

Permalink
Fixes client up/down, left/right, front/back, clockwise/counterclockw…
Browse files Browse the repository at this point in the history
…ise functions to accept 0 speed. Adds test. Resolves felixge#106

Previous implementation would not allow one to supply 0 for speed to stop moving along that axis. The only way to stop would be to issue a client.stop(). But that would also cancel any other movement going on at the time.
For example, centering a joystick should stop pitch, but should not stop yaw being controlled by a dpad on the same controller.
  • Loading branch information
michaelshmitty committed Nov 25, 2014
1 parent 9a5d0a5 commit d57b40a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ var pcmdOptions = [

pcmdOptions.forEach(function(pair) {
Client.prototype[pair[0]] = function(speed) {
if (!speed) {
if (isNaN(speed)) {
return;
}
speed = parseFloat(speed);
Expand All @@ -281,7 +281,7 @@ pcmdOptions.forEach(function(pair) {
};

Client.prototype[pair[1]] = function(speed) {
if (!speed) {
if (isNaN(speed)) {
return;
}

Expand Down
34 changes: 34 additions & 0 deletions test/unit/test-Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,40 @@ test('Client', {
assert.strictEqual(this.client._pcmd.down, -0.5);
},

'pcmd methods accept 0 as speed': function() {
this.client.up(0);
assert.equal(this.client._pcmd.up, 0);
assert.equal(this.client._pcmd.down, undefined);

this.client.down(0);
assert.equal(this.client._pcmd.down, 0);
assert.equal(this.client._pcmd.up, undefined);

this.client.left(0);
assert.equal(this.client._pcmd.left, 0);
assert.equal(this.client._pcmd.right, undefined);

this.client.right(0);
assert.equal(this.client._pcmd.right, 0);
assert.equal(this.client._pcmd.left, undefined);

this.client.front(0);
assert.equal(this.client._pcmd.front, 0);
assert.equal(this.client._pcmd.back, undefined);

this.client.back(0);
assert.equal(this.client._pcmd.back, 0);
assert.equal(this.client._pcmd.front, undefined);

this.client.clockwise(0);
assert.equal(this.client._pcmd.clockwise, 0);
assert.equal(this.client._pcmd.counterClockwise, undefined);

this.client.counterClockwise(0);
assert.equal(this.client._pcmd.counterClockwise, 0);
assert.equal(this.client._pcmd.clockwise, undefined);
},

'pcmd checks if argument exists': function() {
// check that running without commands does not modify _pcmd state
this.client.up(0.5);
Expand Down

0 comments on commit d57b40a

Please sign in to comment.