
class Enemy {
	
	private var baseClip:MovieClip;
	private var x:Number;
	private var y:Number;
	private var i:Number;
	private var enemyDepth:Number;
	private var screenWidth:Number; 
	private var screenHeight:Number;
	private var xSpeed:Number;
	private var ySpeed:Number;
	private var size:Number;
	private var xDirection:Number;
	private var yDirection:Number;
	
	private var rockCollision = 1; //0 = Off   1 = On

	
	function init() {
		this.i = _global.g.mainMgr.enemies.length;
		this.enemyDepth = _global.g.rootClip.mainStage.getNextHighestDepth();
		this.baseClip = _global.g.rootClip.mainStage.attachMovie("enemy", "enemy"+i, i);
		
		this.screenWidth = _global.g.mainMgr.width;
		this.screenHeight = _global.g.mainMgr.height;
		
		this.size = (Math.floor(Math.random() * (100 - 25 + 1)) + 25) * 0.02;
		
		this.baseClip._width = this.baseClip._width*this.size;
		this.baseClip._height = this.baseClip._height*this.size;		
		
		this.x = (Math.floor(Math.random() * ((this.screenWidth - this.baseClip._width) - this.baseClip._width + 1)) + this.baseClip._width);
		this.y = -30;
		
		this.xSpeed = (Math.floor(Math.random() * (2 - 1 + 1)) + 1) * _global.g.mainMgr.difficulty;
		//this.xSpeed = 2 * _global.g.mainMgr.difficulty;
		this.ySpeed = (Math.floor(Math.random() * (2 - 1 + 1)) + 1) * _global.g.mainMgr.difficulty;
		
		//Right = 1  Left = -1
		this.xDirection = random(2);
		if(this.xDirection == 0) {this.xDirection = -1;}
		
		this.yDirection = 1;
		
		this.baseClip._x = this.x;
		this.baseClip._y = this.y;
		
	}

	function updateXYValues() {
		if(this.y > this.screenHeight+20) {
			this.removeEnemy();
		}else{
			//this.y += this.speed*this.yDirection;
			this.y += this.ySpeed;
		

			this.x += this.xSpeed*this.xDirection;
	
			this.baseClip._x = this.x;
			this.baseClip._y = this.y;
			
			this.baseClip._rotation += this.xSpeed*this.xDirection;
		}
	}
	
	function testCollision() {
		//Tests collision with ship
		if(this.y > (this.screenHeight-100)) {
			for (var p=1; p<7; p++) {
				for (var j=1; j<4; j++) {
					if(this.baseClip["enemyPerim"+p].hitTest(_global.g.rootClip.mainStage.ship["perimeter"+j])) {
						//Game Over
						_global.g.mainMgr.spaceShip.explode();
					}
				}	
			}
		}	
		//Point to shape hitTest for bullets
		for (var j=(_global.g.mainMgr.removedBullets-20); j<_global.g.mainMgr.bullets.length; j++) {
			if(this.baseClip.hitTest(_global.g.rootClip.mainStage["shot"+j]._x, _global.g.rootClip.mainStage["shot"+j]._y, true)) {
				this.explode();
				_global.g.mainMgr.bullets[j].removeBullet();
				_global.g.mainMgr.hit++;
			}
		}
		
		//Circle to circle test for asteroid to asteroid collision
		if(this.rockCollision ==1) {
			for (var j=(_global.g.mainMgr.removedEnemies-10); j<this.i; j++) {
				var hittingDist = (this.baseClip._width/2.4) + (_global.g.rootClip.mainStage["enemy"+j]._width/2.4);
				if(distance( this.baseClip._x, this.baseClip._y, _global.g.rootClip.mainStage["enemy"+j]._x, _global.g.rootClip.mainStage["enemy"+j]._y) < (hittingDist*hittingDist) ) {
					if((this.baseClip._x < _global.g.rootClip.mainStage["enemy"+j]._x && this.xDirection == 1) || (this.baseClip._x > _global.g.rootClip.mainStage["enemy"+j]._x && this.xDirection == -1)){
					this.xDirection = this.xDirection*(-1);
					//this.yDirection = this.yDirection*(-1);
					}
				}
			}
			
			for (var j=(this.i+1); j<_global.g.mainMgr.enemies.length; j++) {
				var hittingDist = (this.baseClip._width/2.4) + (_global.g.rootClip.mainStage["enemy"+j]._width/2.4);
				if(distance( this.baseClip._x, this.baseClip._y, _global.g.rootClip.mainStage["enemy"+j]._x, _global.g.rootClip.mainStage["enemy"+j]._y) < (hittingDist*hittingDist) ) {
					if((this.baseClip._x < _global.g.rootClip.mainStage["enemy"+j]._x && this.xDirection == 1) || (this.baseClip._x > _global.g.rootClip.mainStage["enemy"+j]._x && this.xDirection == -1)){
					this.xDirection = this.xDirection*(-1);
					//this.yDirection = this.yDirection*(-1);
					}
				}
			}
		}
		
		//Tests collision with walls
		if((this.x + this.baseClip._width/2) > this.screenWidth) {this.xDirection = -1;}
		if((this.x - this.baseClip._width/2) < 0) {this.xDirection = 1;}

	}
	
	function distance( x1, y1, x2, y2 ) {
		var dx = x2 - x1;
		var dy = y2 - y1;

		return (dx*dx) + (dy*dy);
	}
	
	function explode() {
		this.removeEnemy();
		_global.g.mainMgr.updateScore(this.size/0.02);
		this.baseClip = _global.g.rootClip.mainStage.attachMovie("explosion", "explosion"+i, i+1000);
		this.baseClip.score.text = (this.size/0.02);
		this.baseClip._width = this.baseClip._width*this.size;
		this.baseClip._height = this.baseClip._height*this.size;
		this.baseClip._x = this.x;
		this.baseClip._y = this.y;
	}
	
	function removeEnemy() {
		//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["enemy"+i]); //Does this mean mainStage is actually an array?
		delete _global.g.mainMgr.enemies[i];
		
		_global.g.mainMgr.removedEnemies++;
		//trace(_global.g.mainMgr.removedEnemies);
	}

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