Day 3 - Full code

Heres the full code from day 3.

_root.attachMovie ("gunbarrell","gunbarrell",_root.getNextHighestDepth())
gunbarrell._x = 275;
gunbarrell._y = 780;

enemy_xspeeds = new Array();
enemy_yspeeds = new Array();
enemy_rspeeds = new Array();
enemy_maxspeeds = new Array();
enemy_hps = new Array();      //current health
enemy_starthps = new Array(); //start health. both needed to get progressbar length.
enemy_IDs  = new Array();
bullet_xspeeds = new Array(); 
bullet_yspeeds = new Array();
bullet_rspeeds = new Array(); //rotation speed
bullet_powers = new Array();  //amount the bullet repels enemies
bullet_hps = new Array();  //amount of damage done by the bullet
bullet_IDs = new Array();
var statuschanged:Boolean; 
var gameover:Boolean;

enemycount = 0;
nextenemyID = 0;
bulletcount = 0;
nextbulletID = 0;

shottimer = 0;  //controls shooting speed
leveltimer = 0; //controls timing of enemies
stagexmin = 0;  //stage boundaries
stagexmax = 550;
stageymin = 40;
stageymax = 800;

curID = 0;
reloadspeed = 200;  //lower number = faster reload speed
money = 0;
lives = 3;
score = 0;
statuschanged = true;
gameover = false;
gravity = 0.15;
xfriction = 1; //changed to 1 so that x friction doesn't apply.  I left the variable here in case I want to turn it back on.
bl = gunbarrell._height;

makeenemy(1);
gunbarrell.onEnterFrame = function(){
	if (bulletcount > 0){bulletstep();}
	if (enemycount > 0){enemystep();}
	if (statuschanged == true){
		txtMoney.text = money;
		txtScore.text = score;
		txtLives.text = lives;
	}
	if (((getTimer()-leveltimer)>=5000)&&(gameover==false)){
		makeenemy(1);
		leveltimer = getTimer();
	}
}

onMouseDown = function () {  //when the mouse is clicked
	timesinceshot = getTimer() - shottimer;
	if (timesinceshot >= reloadspeed){
		shottimer = getTimer();
		makebullet();
	}
}
onMouseMove = function () {  //when mouse is moved
    mouse_xdist = _root._xmouse-gunbarrell._x;
    mouse_ydist = _root._ymouse-gunbarrell._y;
    // calculate the angle
    barrell_rad = Math.atan2(mouse_ydist, mouse_xdist);
    // convert to degrees and set rotation
    barrell_angle = toDeg(barrell_rad) + 90;
    gunbarrell._rotation = barrell_angle;
}
function toDeg(Rad:Number){return (Rad * 180 / Math.PI);}

function makebullet(){
    //bullet appears at end point of barrell
    //bl = barrell length, barrell_rad = barrell angle in radians
    bxl = Math.cos(barrell_rad) * bl;
    byl = Math.sin(barrell_rad) * bl;
    bulletspeed = 10;
     
    //split bulletspeed into an x and y value
    ratio = bulletspeed / bl;
    bullet_xspeeds[bulletcount] = bxl * ratio;
    bullet_yspeeds[bulletcount] = byl * ratio;
    bullet_rspeeds[bulletcount] = 10; //rotation speed
    bullet_hps[bulletcount] = 5;     //damage
    bullet_powers[bulletcount] = 4;   //repelling power
    bullet_IDs[bulletcount] = nextbulletID;
     
    //attach a new instance of bullet from the library with name bullet_1, bullet_2 etc.
    bul = _root.attachMovie("bullet","bullet_"+nextbulletID,_root.getNextHighestDepth());
    bul._x = gunbarrell._x + bxl;  //position the movie clip
    bul._y = gunbarrell._y + byl;
    nextbulletID++;   //increment IDs
    bulletcount++;    //increment bullet count
}

function bulletstep(){
    for (a=0;a<bulletcount;a++){  //step through the array from 0
        curID = bullet_IDs[a] 
        _root['bullet_'+curID]._x += bullet_xspeeds[a];  //apply new positions to
        _root['bullet_'+curID]._y += bullet_yspeeds[a];  //relevant bullet on screen
        _root['bullet_'+curID]._rotation += bullet_rspeeds[a]; //rotate bullet
        bulletcheck(a);  //check screen boundaries etc.
    }
}

function bulletcheck(arraypos:Number){
    cx = _root['bullet_'+bullet_IDs[arraypos]]._x;  //get x and y position
    cy = _root['bullet_'+bullet_IDs[arraypos]]._y;
    //check stage boundaries
    if (cx < stagexmin || cx > stagexmax || cy < stageymin || cy > stageymax){killbullet(arraypos);}
	//check if it hits an enemy
	for (b=0;b<enemycount;b++){
		if (_root['bullet_'+bullet_IDs[arraypos]].hitTest(_root['enemy_'+enemy_IDs[b]])){
			hitenemy(b,arraypos);
		}
	}
}

function killbullet(arraypos:Number){
    removeMovieClip(_root['bullet_'+bullet_IDs[arraypos]]); //remove the bullet
    if (!(arraypos == (bulletcount - 1))){        //if its not the end of the array
        for (i = arraypos+1;i<bulletcount;i++){ //then move everything in the array   
            bullet_xspeeds[i-1] = bullet_xspeeds[i];
            bullet_yspeeds[i-1] = bullet_yspeeds[i];
            bullet_rspeeds[i-1] = bullet_rspeeds[i];
            bullet_powers[i-1] = bullet_powers[i];
            bullet_hps[i-1] = bullet_hps[i];
            bullet_IDs[i-1] = bullet_IDs[i];
        }
    }
    bulletcount--;
}

function makeenemy(type:Number){
	switch(type){
		case 1:
			enemy_maxspeeds[enemycount] = 4;
			enemy_starthps[enemycount] = 20;
			size = 40;
			break;
	}
	
	enemy_xspeeds[enemycount] = 0;
	enemy_yspeeds[enemycount] = enemy_maxspeeds[enemycount];
	enemy_hps[enemycount] = enemy_starthps[enemycount];	
	enemy_IDs[enemycount] = nextenemyID;
	enemy_rspeeds[enemycount] = 0;

	en = _root.attachMovie("enemy_"+type,"enemy_"+nextenemyID,_root.getNextHighestDepth());
	en._x = Math.round(Math.random()*(stagexmax-stagexmin-10))+5;
	en._y = stageymin + (size/2);
	en._width = size
	en._height = size
	progressbar = en.attachMovie("progressbar","progressbar",_root.getNextHighestDepth());
	progressbar._width = 40;
	progressbar._height = 6;
	nextenemyID++;
	enemycount++;
}

function enemystep(){
	for (a=0;a<enemycount;a++){
		if (enemy_yspeeds[a] < enemy_maxspeeds[a]){enemy_yspeeds[a] += gravity;}
		if (enemy_yspeeds[a] > enemy_maxspeeds[a]){enemy_yspeeds[a] = enemy_maxspeeds[a];}
		if (enemy_xspeeds[a] != 0){enemy_xspeeds[a] *= xfriction;}
		_root['enemy_'+enemy_IDs[a]]._x += enemy_xspeeds[a];
		_root['enemy_'+enemy_IDs[a]]._y += enemy_yspeeds[a];
		_root['enemy_'+enemy_IDs[a]]._rotation += enemy_rspeeds[a];
		enemycheck(a);
	}
}	

function enemycheck(arraypos:Number){
	//make enemy bounce off wales and also detect when enemy goes off the bottom of the screen
	ex = _root['enemy_'+enemy_IDs[arraypos]]._x;
	ey = _root['enemy_'+enemy_IDs[arraypos]]._y;
	er = _root['enemy_'+enemy_IDs[arraypos]]._width / 2;
	if ((ex < stagexmin+er) || (ex > stagexmax-er)){ //if hit wall 
		enemy_xspeeds[arraypos]*=-1; //bounce
	} 
	if (ey > stageymax - er){ //if off bottom of screen
		killenemy(arraypos);
		lives--;
		if (lives == 0){
			gameover = true;
			for (c=0;c<enemycount;c++){killenemy(c);}
		}
	}
}

function hitenemy(enemyarraypos:Number, bulletarraypos:Number){
	enemy_hps[enemyarraypos]-=bullet_hps[bulletarraypos];
	if (enemy_hps[enemyarraypos]<=0){
		money += enemy_starthps[enemyarraypos]/4;
		score += enemy_starthps[enemyarraypos];
		statuschanged = true;
		killenemy(enemyarraypos);
	} else {
		statuschanged = true;
		score += bullet_hps[bulletarraypos];
		//split the bullets power into x and y vals based on the angle of the bullet to the enemy.
		c = Math.sqrt(Math.pow(bullet_xspeeds[bulletarraypos],2) + Math.pow(bullet_yspeeds[bulletarraypos],2));
		xforce = bullet_powers[bulletarraypos] * bullet_xspeeds[bulletarraypos] / c;
		yforce = bullet_powers[bulletarraypos] * bullet_yspeeds[bulletarraypos] / c;
		enemy_xspeeds[enemyarraypos]+=xforce;
		enemy_yspeeds[enemyarraypos]+=yforce;
		//apply a rotation based on which side of the enemy was struck
		if (_root['bullet_'+bullet_IDs[bulletarraypos]]._x < _root['enemy_'+enemy_IDs[enemyarraypos]]._x){
			enemy_rspeeds[enemyarraypos] += Math.round(Math.random()*4)+1;
		} else {
			enemy_rspeeds[enemyarraypos] -= Math.round(Math.random()*4)+1;
		}
		_root['enemy_'+enemy_IDs[enemyarraypos]].progressbar._width = (enemy_hps[enemyarraypos] / enemy_starthps[enemyarraypos]) * 40;
	}
	killbullet(bulletarraypos);
}

function killenemy(arraypos:Number){
	removeMovieClip(_root['enemy_'+enemy_IDs[arraypos]]);
	if (!(arraypos == (enemycount - 1))){
		for (i = arraypos + 1;i<enemycount;i++){
			enemy_xspeeds[i-1] = enemy_xspeeds[i];
			enemy_yspeeds[i-1] = enemy_yspeeds[i];
			enemy_rspeeds[i-1] = enemy_rspeeds[i];
			enemy_maxspeeds[i-1] = enemy_maxspeeds[i];
			enemy_hps[i-1] = enemy_hps[i];
			enemy_starthps[i-1] = enemy_starthps[i];
			enemy_IDs[i-1] = enemy_IDs[i];
		}
	}
	enemycount--;
}