var BaseObject = require('asteroids-object');
var different = require('asteroids-different')(0.0001);

var Velocity = module.exports = function(){
    BaseObject.call(this);
    this._speed = 0;
    this._heading = 0;
    this._omega = 0;
};
Velocity.prototype = new BaseObject();
Velocity.prototype.velocity = function(value){
    if (value) {
	var oldSpeed = this._speed;
	var oldHeading = this._heading;
	var oldOmega = this._omega;
	this._speed = value.speed || oldSpeed;
	this._heading = value.heading || oldHeading;
	this._omega = value.omega || oldHeading
	if (different(oldSpeed, this._speed) || different(oldHeading, this._heading) || different(oldOmega, this._omega)) {
	    this.notifyOf('velocity');
	}
    }
    return { 'speed': this._speed, 'heading': this._heading, 'omega': this._omega };
};
Velocity.prototype.speed = function(value){
    if (value) {
	this.velocity({ 'speed': value })
    }
    return this._speed;
};
Velocity.prototype.heading = function(value){
    if (value) {
	this.velocity({ 'heading': value });
    }
    return this._heading;
};
Velocity.prototype.omega = function(value){
    if (value) {
	this.velocity({ 'omega': value });
    }
    return this._omega;
};
Velocity.prototype.tick = function(){
    var position = this.position();
    var dx = this._speed * Math.cos(this._heading);
    var dy = this._speed * Math.sin(this._heading);
    this.position({ x: position.x + dx, y: position.y + dy });
}
Velocity.prototype.state = function(){
    var position = this.position();
    var velocity = this.velocity();
    return {
	'x': position.x,
	'y': position.y,
	'radius': this.radius(),
	'orientation': this.orientation(),
	'speed': velocity.speed,
	'heading': velocity.heading,
	'omega': velocity.omega
    };
}

@@ -338,17 +338,24 @@
 alue) %7B%0A
-%09
+        
 var oldS
@@ -374,17 +374,24 @@
 _speed;%0A
-%09
+        
 var oldH
@@ -414,17 +414,24 @@
 eading;%0A
-%09
+        
 var oldO
@@ -450,17 +450,24 @@
 _omega;%0A
-%09
+        
 this._sp
@@ -497,17 +497,24 @@
 dSpeed;%0A
-%09
+        
 this._he
@@ -550,17 +550,24 @@
 eading;%0A
-%09
+        
 this._om
@@ -598,17 +598,24 @@
 Heading%0A
-%09
+        
 if (diff
@@ -722,17 +722,24 @@
 ega)) %7B%0A
-%09
+        
     this
@@ -761,17 +761,24 @@
 city');%0A
-%09
+        
 %7D%0A    %7D%0A
@@ -918,33 +918,40 @@
    if (value) %7B%0A
-%09
+        
 this.velocity(%7B 
@@ -1056,33 +1056,40 @@
    if (value) %7B%0A
-%09
+        
 this.velocity(%7B 
@@ -1205,17 +1205,24 @@
 alue) %7B%0A
-%09
+        
 this.vel
@@ -1648,17 +1648,24 @@
 eturn %7B%0A
-%09
+        
 'x': pos
@@ -1673,17 +1673,24 @@
 tion.x,%0A
-%09
+        
 'y': pos
@@ -1698,17 +1698,24 @@
 tion.y,%0A
-%09
+        
 'radius'
@@ -1731,17 +1731,24 @@
 dius(),%0A
-%09
+        
 'orienta
@@ -1774,17 +1774,24 @@
 tion(),%0A
-%09
+        
 'speed':
@@ -1807,17 +1807,24 @@
 .speed,%0A
-%09
+        
 'heading
@@ -1848,9 +1848,16 @@
 ng,%0A
-%09
+        
 'ome

correct indent of velocity.js