asteroids-fighter
Advanced tools
+47
-3
| var Velocity = require('asteroids-asteroid'); | ||
| var defaultOptions = { | ||
| 'speedIncrement': 1, | ||
| 'rotation': Math.PI/36 | ||
| }; | ||
| var doNothing = function() { /* NOTHING! */ }; | ||
| var Fighter = module.exports = function (initializer) { | ||
| var Fighter = module.exports = function (initializer, options) { | ||
| Velocity.call(this, initializer); | ||
| this.options = options || {}; | ||
| for (var key in defaultOptions) { | ||
| if (!this.options[key]) { | ||
| this.options[key] = defaultOptions[key]; | ||
| } | ||
| } | ||
| } | ||
| Fighter.prototype = new Velocity(); | ||
@@ -13,1 +21,37 @@ Fighter.prototype.fire = function() { | ||
| } | ||
| Fighter.prototype.rotateLeft = function(locked){ | ||
| var oldOrientation = this.orientation(); | ||
| this.orientation(oldOrientation + this.options.rotation); | ||
| if (locked) { | ||
| this.turnLeft(); | ||
| } | ||
| }; | ||
| Fighter.prototype.rotateRight = function(locked){ | ||
| var oldOrientation = this.orientation(); | ||
| this.orientation(oldOrientation - this.options.rotation); | ||
| if (locked) { | ||
| this.turnRight(); | ||
| } | ||
| }; | ||
| Fighter.prototype.turnLeft = function(locked){ | ||
| var oldHeading = this.heading(); | ||
| this.heading(oldHeading + this.options.rotation); | ||
| if (locked) { | ||
| this.rotateLeft(); | ||
| } | ||
| }; | ||
| Fighter.prototype.turnRight = function(locked){ | ||
| var oldHeading = this.heading(); | ||
| this.heading(oldHeading - this.options.rotation); | ||
| if (locked) { | ||
| this.rotateRight(); | ||
| } | ||
| }; | ||
| Fighter.prototype.speedUp = function(){ | ||
| var oldSpeed = this.speed(); | ||
| this.speed(oldSpeed + this.options['speedIncrement']); | ||
| } | ||
| Fighter.prototype.slowDown = function(){ | ||
| var oldSpeed = this.speed(); | ||
| this.speed(Math.max(oldSpeed - this.options['speedIncrement'], 0.1)); | ||
| } |
+1
-1
| { | ||
| "name": "asteroids-fighter", | ||
| "version": "0.5.0", | ||
| "version": "0.6.0", | ||
| "description": "Ze destroyer of androids!", | ||
@@ -5,0 +5,0 @@ "main": "lib/fighter.js", |
+129
-3
@@ -21,5 +21,2 @@ var expect = require('chai').expect; | ||
| }); | ||
| }); | ||
@@ -71,1 +68,130 @@ | ||
| }); | ||
| describe('movement', function(){ | ||
| var options = { | ||
| 'speedIncrement': 2, | ||
| 'rotation': Math.PI/36 | ||
| }; | ||
| var fighter; | ||
| beforeEach(function(){ | ||
| fighter = new Fighter(function(){}, options); | ||
| }); | ||
| ['rotateLeft', 'rotateRight', 'turnLeft', 'turnRight', 'speedUp', 'slowDown'].forEach(function(method){ | ||
| it('should respond to ' + method, function(){ | ||
| expect(fighter).to.respondTo(method); | ||
| }); | ||
| }); | ||
| describe('speedUp', function(){ | ||
| it('should increase the speed', function(){ | ||
| var start = 1; | ||
| fighter.speed(start) | ||
| fighter.speedUp(); | ||
| expect(fighter.speed()).to.equal(start + options.speedIncrement); | ||
| }); | ||
| }); | ||
| describe('slowUp', function(){ | ||
| it('should decrease the speed', function(){ | ||
| var start = 3; | ||
| fighter.speed(start) | ||
| fighter.slowDown(); | ||
| expect(fighter.speed()).to.equal(start - options.speedIncrement); | ||
| }); | ||
| it('should not decrease the speed below zero', function(){ | ||
| var start = 1; | ||
| fighter.speed(start) | ||
| fighter.slowDown(); | ||
| expect(fighter.speed()).to.equal(0.1); // TODO fix asteroids-velocity to accept 0 | ||
| }); | ||
| }); | ||
| describe('rotateLeft', function(){ | ||
| it('should change orientation', function(){ | ||
| fighter.orientation(0); | ||
| fighter.rotateLeft(); | ||
| expect(fighter.orientation()).to.equal(options.rotation); | ||
| }); | ||
| it('should change heading when locked', function(){ | ||
| fighter.orientation(0); | ||
| fighter.heading(0); | ||
| fighter.rotateLeft(true); | ||
| expect(fighter.orientation()).to.equal(options.rotation); | ||
| expect(fighter.heading()).to.equal(options.rotation); | ||
| }); | ||
| }); | ||
| describe('rotateRight', function(){ | ||
| it('should change orientation', function(){ | ||
| fighter.orientation(0); | ||
| fighter.rotateRight(); | ||
| expect(fighter.orientation()).to.equal(-options.rotation); | ||
| }) | ||
| it('should change heading when locked', function(){ | ||
| fighter.orientation(0); | ||
| fighter.heading(0); | ||
| fighter.rotateRight(true); | ||
| expect(fighter.orientation()).to.equal(-options.rotation); | ||
| expect(fighter.heading()).to.equal(-options.rotation); | ||
| }); | ||
| }); | ||
| describe('turnLeft', function(){ | ||
| it('should change heading', function(){ | ||
| fighter.heading(0); | ||
| fighter.turnLeft(); | ||
| expect(fighter.heading()).to.equal(options.rotation); | ||
| }); | ||
| it('should change orientation when locked', function(){ | ||
| fighter.orientation(0); | ||
| fighter.heading(0); | ||
| fighter.turnLeft(true); | ||
| expect(fighter.orientation()).to.equal(options.rotation); | ||
| expect(fighter.heading()).to.equal(options.rotation); | ||
| }); | ||
| }); | ||
| describe('turnRight', function(){ | ||
| it('should change heading', function(){ | ||
| fighter.heading(0); | ||
| fighter.turnRight(); | ||
| expect(fighter.heading()).to.equal(-options.rotation); | ||
| }) | ||
| it('should change orientation when locked', function(){ | ||
| fighter.orientation(0); | ||
| fighter.heading(0); | ||
| fighter.turnRight(true); | ||
| expect(fighter.orientation()).to.equal(-options.rotation); | ||
| expect(fighter.heading()).to.equal(-options.rotation); | ||
| }); | ||
| }); | ||
| }) |
8236
99.76%205
205.97%