#include "enemy.h"
#include "graphics.h"
#include "mouse.h"
#include "player.h"
#include "stage.h"
#include "explodies.h"
#include "sound.h"
#include "startPage.h"
#include "rocket.h"

Enemy enemies[NUM_ENEMIES];

gxSprite eRed;
gxSprite eGreen;
gxSprite eBlue;
gxSprite eWhite;
gxSprite eThinGrey;
gxSprite eThinDark;
gxSprite manbearpig;

void InitEnemies ()
{
	gxLoadBmp( "eRed.tga", &eRed, 0 );  
	gxLoadBmp( "eGreen.tga", &eGreen, 0 );
	gxLoadBmp( "eBlue.tga", &eBlue, 0 );
	gxLoadBmp( "eWhite.tga", &eWhite, 0 );
	gxLoadBmp( "eThinGrey.tga", &eThinGrey, 0 );
	gxLoadBmp( "eThinDark.tga", &eThinDark, 0 );
	gxLoadBmp( "manbearpig.tga", &manbearpig, 0 );
	for (int a = 0; a < NUM_ENEMIES; a++)
	{
		enemies[a].active = false;
		enemies[a].type = rand()% 6;
		enemies[a].xDirection = rand()%20 -10;
		enemies[a].yDirection = rand()%20 -10;
		enemies[a].X  = stages[currentStage].width-64;
		enemies[a].Y  = stages[currentStage].height-64;
	}
}

void UpdateEnemies ()
{

	for (int a = 0; a < NUM_ENEMIES; a++)
	{
		if (enemies[a].active == true )
			if(enemies[a].type==0 || enemies[a].type==11)
			{
				float dx2=(float)player.X-enemies[a].X;
				float dy2=(float)player.Y-enemies[a].Y;
				float t=atan2f(dy2, dx2);
				//Move the enemy towards player.
				int xFollow=(int)(4*cosf(t));
				int yFollow=(int)(4*sinf(t));
				enemies[a].X+=xFollow*startVariables.levelNumber;
				enemies[a].Y+=yFollow*startVariables.levelNumber;
			}
			else
			{
				enemies[a].X += enemies[a].xDirection*startVariables.levelNumber;
				enemies[a].Y += enemies[a].yDirection*startVariables.levelNumber;
			}

			// If it has left the bounds of the screen, cause it to wrap to the other side.
			if ( enemies[a].X > stages[currentStage].width-64 || enemies[a].X < 0 )
				enemies[a].xDirection = -enemies[a].xDirection;
			if ( enemies[a].Y > stages[currentStage].height-64 || enemies[a].Y < 0)
				enemies[a].yDirection = -enemies[a].yDirection;
		//	if (enemies[a].Y < 0)
			//	enemies[a].Y = stages[currentStage].height-64;
	}

	// Hit By Enemy
	for (int a = 0; a < NUM_ENEMIES; a++)
	{
		if ( enemies[a].active == true )
		{
			if ( enemies[a].X <= player.X +50 && enemies[a].X >= player.X -50 && enemies[a].Y <= player.Y +50 && enemies[a].Y >= player.Y -50 && enemies[a].type >=0 && enemies[a].type <= 10)
			{
				player.health -= 5;
				if (player.health <= 0 )
					player.health = 0;
				enemies[a].active = false;
				CreateExplodies(enemies[a].X, enemies[a].Y);
			}

			if ( enemies[a].type == 0)
				gxDrawSpriteAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &eRed, 255 );
			if ( enemies[a].type == 1)
				gxDrawSpriteAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &eGreen, 255 );
			if ( enemies[a].type == 2)
				gxDrawSpriteAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &eBlue, 255 );
			if ( enemies[a].type == 3)
				gxDrawSpriteAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &eWhite, 255 );
			if ( enemies[a].type == 4)
				gxDrawSpriteAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &eThinGrey, 255 );
			if ( enemies[a].type == 5)
				gxDrawSpriteAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &eThinDark, 255 );
			if ( enemies[a].type == 11)
			{
				if (enemies[a].xDirection > 0)
				{
					gxDrawSpriteScaledAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &manbearpig, -manbearpig.width, manbearpig.height, 255 );
				}
				else
				{
					gxDrawSpriteAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &manbearpig, 255 );
				}

				//Boss
				if ( enemies[a].X <= player.X +50 && enemies[a].X >= player.X -50 && enemies[a].Y <= player.Y +64 && enemies[a].Y >= player.Y -64 && enemies[a].type >= 11)
				{
					player.health -= 10;
					if (player.health <= 0 )
					{
						player.health = 0;
					}

					CreateExplodies(enemies[a].X, enemies[a].Y);

					if (rand()%2 == 0)
					{
						enemies[a].X = player.X + 200;
					}
					else
					{
						enemies[a].X = player.X - 200;
					}
					if (rand()%2 == 0)
					{
						enemies[a].Y = player.Y + 200;
					}
					else
					{
						enemies[a].Y = player.Y - 200;
					}
				}
				if ( enemies[a].type == 11)
				{
					if (enemies[a].xDirection > 0)
					{
						gxDrawSpriteScaledAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &manbearpig, -manbearpig.width, manbearpig.height, 255 );
					}
					else
					{
						gxDrawSpriteAlpha( enemies[a].X-xAxis, enemies[a].Y-yAxis, &manbearpig, 255 );
					}
				}
			}
		}
	}

	//Reset Enemy
	for (int a = 0; a < NUM_ENEMIES; a++)
	{
		if ( enemies[a].active == false )
		{
			int randomNumber = enemies[a].X%10;
			int fade = 255 - enemies[a].timer*10 / 2;
			if(fade<0)
				fade=0;
			enemies[a].timer+=1;

			if(randomNumber == 1)//if the random number is one then print owned at the x and y of the enemy that died, etc etc.
				gxDrawText(enemies[a].X - xAxis, enemies[a].Y - yAxis, "OWNED!!", &Font, gxRGBA32(fade, fade, fade, fade), 0);

			if(randomNumber == 2)
				gxDrawText(enemies[a].X - xAxis, enemies[a].Y - yAxis, "HEADSHOT!!!!", &Font, gxRGBA32(fade, fade, fade, fade) , 0);

			if(randomNumber == 3)
				gxDrawText(enemies[a].X - xAxis, enemies[a].Y - yAxis, "PWNT!!", &Font, gxRGBA32(fade, fade, fade, fade) , 0);

			if(randomNumber == 4)
				gxDrawText(enemies[a].X - xAxis, enemies[a].Y - yAxis, "DOMINATION!", &Font, gxRGBA32(fade, fade, fade, fade) , 0);

			if(randomNumber == 5)
				gxDrawText(enemies[a].X - xAxis, enemies[a].Y - yAxis, "MONSTERMASH!", &Font, gxRGBA32(fade, fade, fade, fade) , 0);

			if(randomNumber == 6)
				gxDrawText(enemies[a].X - xAxis, enemies[a].Y - yAxis, "THWAP!!", &Font, gxRGBA32(fade, fade, fade, fade) , 0);

			if(randomNumber == 7)
				gxDrawText(enemies[a].X - xAxis, enemies[a].Y - yAxis, "BOP!!!!!", &Font, gxRGBA32(fade, fade, fade, fade) , 0);

			if(randomNumber == 8)
				gxDrawText(enemies[a].X - xAxis, enemies[a].Y - yAxis, "BAMF!!", &Font, gxRGBA32(fade, fade, fade, fade) , 0);
		}
	}
}