
class Ship {
	
	private var baseClip:MovieClip;
	private var x:Number;
	private var y:Number;
	private var cursX:Number;
	private var cursY:Number;
	private var gap:Number;
	
	private var adjside:Number;
	private var oppside:Number;
	private var angle:Number;
	
	public var bullet:Shot;

	
	function init() {
		var shipDepth:Number = 10000;
		this.baseClip = _global.g.rootClip.mainStage.attachMovie("ship", "ship", shipDepth);

		
		this.x = _global.g.mainMgr.width/2;
		this.y = _global.g.mainMgr.height-50;  //Ship's distance from the bottom of the window
		
	}

	function updateXYValues() {
		this.cursX = _global.g.mainMgr.cursor.x;
		
		//Finds distance between cursor and ship and closes the gap.  The farther away, the faster it moves.
		if(this.x < this.cursX){						
			this.gap = this.cursX - this.x;
			this.x = this.x + (this.gap)*0.12;
		}else{
		if(this.x > this.cursX){
			this.gap = this.x - this.cursX;
			this.x = this.x - (this.gap)*0.12;
		
		}else{
			this.x = this.cursX;
		}
		}
		
		this.baseClip._x = this.x;
		this.baseClip._y = this.y;
	}
	
	function shipRotate() {
		//A formula I got from the actionscript.org forums to convert coordinates to degrees
		this.adjside = _global.g.mainMgr.cursor.x-this.x;
		this.oppside = -1*(_global.g.mainMgr.cursor.y-this.y);
		this.angle = Math.atan2(this.oppside, this.adjside);
		this.angle = Math.round(this.angle/Math.PI*180);
		
		
		//if(this.angle < -90){
			//this.baseClip._rotation = -90;
		//}else{
		//if(this.angle < 0){
			//this.baseClip._rotation = 90;
		//}else{
			this.baseClip._rotation = 90-angle;
		//}
		//}
	}
	
	function fireShot() {
		var newBullet = new Shot();
		newBullet.init()
		_global.g.mainMgr.bullets.push(newBullet);
	}

	function explode() {
		delete _global.g.mainMgr.spaceShip;
		removeMovieClip(_global.g.rootClip.mainStage.ship)
		this.baseClip = _global.g.rootClip.mainStage.attachMovie("shipExplosion", "shipExplosion", 10000);
		this.baseClip._x = this.x;
		this.baseClip._y = this.y;
		_global.g.rootClip.mainStage.attachMovie("gameOver", "gameOver", 30000);
		_global.g.rootClip.mainStage.gameOver._x = _global.g.mainMgr.width/2;
		_global.g.rootClip.mainStage.gameOver._y = _global.g.mainMgr.height/2;
		_global.g.rootClip.mainStage.gameOver.accuracy.text = "accuracy:  " +(int(((_global.g.mainMgr.hit/_global.g.mainMgr.removedBullets)*100)*10)/10) +"%";
	}
	
	function processTicks() {
		this.updateXYValues();
		this.shipRotate();
	}
	
}