
class Shot {
	
	private var baseClip:MovieClip;
	private var x:Number;
	private var y:Number;
	private var angle:Number;
	private var i:Number;
	private var duration:Number;
	private var shotDepth:Number

	
	function init() {
		this.i = _global.g.mainMgr.bullets.length;
		this.shotDepth = _global.g.rootClip.mainStage.getNextHighestDepth();
		this.baseClip = _global.g.rootClip.mainStage.attachMovie("shot", "shot"+i, shotDepth);
		
		//All this makes sure the shot comes out of the tip of the spaceship.  There's probably a more efficient way though.
		this.angle = 90 - _global.g.rootClip.mainMgr.spaceShip.angle;
		this.x = (_global.g.rootClip.mainMgr.spaceShip.x) + (25*Math.sin(Math.PI/180*this.angle));
		this.y = (_global.g.rootClip.mainMgr.spaceShip.y) + (-25*Math.cos(Math.PI/180*this.angle));

		this.baseClip._x = this.x;
		this.baseClip._y = this.y;
		
		this.baseClip._rotation = this.angle;
		

		this.duration = 0;
		
		//trace(i);
		
	}

	function updateXYValues() {
		if(this.duration > 40 || this.y < 0) {
			this.removeBullet();
		}else{
		//A formula I got from http://www.adobe.com/devnet/flash/articles/asteroids.html
		this.x += 15*Math.sin(Math.PI/180*this.angle);
		this.y += -15*Math.cos(Math.PI/180*this.angle);
		
		this.baseClip._x = this.x;
		this.baseClip._y = this.y;
		
		this.duration ++;
		}
	}
	
	function testCollision() {
		for (var j=(_global.g.mainMgr.removedEnemies-100); j<_global.g.mainMgr.enemies.length; j++) {
			if(this.baseClip.hitTest(_global.g.rootClip.mainStage["enemy"+j])) {
				this.removeBullet();
				_global.g.mainMgr.enemies[j].explode();
			}
		}
	}
	
	function removeBullet() {
		//I have only the vaguest idea of why this works, but it took me about 6 hours to figure out.
		removeMovieClip(_global.g.rootClip.mainStage["shot"+i]);
		delete _global.g.mainMgr.bullets[i];
		_global.g.mainMgr.removedBullets++;
	}

	
	function processTicks() {
		this.updateXYValues();
		//this.testCollision();
	}	
	
}