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

gxSprite bulletSprite;
gxSprite crosshairSprite;
gxSprite bigMissleSprite;
sxSound BulletSound;

Bullet bullets[NUM_BULLETS];
int bulletDelay = 0;

extern startPage startVariables;

void InitBullet ()
{
	sxLoadWav ( "bullet.wav", &BulletSound );
	gxLoadBmp( "bullet.tga", &bulletSprite, 0 );
	gxLoadBmp ( "crosshair4.tga", &crosshairSprite, 0);
	for (int i = 0; i < NUM_BULLETS; i++)
	{
		bullets[i].X = 0;
		bullets[i].Y = 0;
		bullets[i].fire = false;
	}
}


void UpdateBullet ()
{
	gxDrawSpriteAlpha( msX, msY, &crosshairSprite, 255 );

	float dx=(float)((msX+xAxis)-(player.X+32));
	float dy=(float)((msY+yAxis)-(player.Y+32));
	float length=sqrtf(dx*dx+dy*dy);
	float ndx=dx/length;
	float ndy=dy/length;
	float vx=ndx*25;
	float vy=ndy*25;

	// Shoot the bullet
	if(player.health>0)
	{
		if ( msButton1 && bulletDelay <= 0)
		{
			for (int i = 0; i < NUM_BULLETS; i++)
			{
				if(bullets[i].fire==false)
				{
					sxPlaySound ( &BulletSound );
					bulletDelay=10;

					bullets[i].fire=true;
					bullets[i].X=player.X+32;
					bullets[i].Y=player.Y+32;
					bullets[i].movementX=(int)vx;
					bullets[i].movementY=(int)vy;
					break;
				}
			}
		}
	}

	//Starts timer if a shot is fired.
	if(bulletDelay>0)
		bulletDelay--;


	//Checks all active shots
	for (int i = 0; i < NUM_BULLETS; i++)
	{
		if ( bullets[i].fire == true )
		{
			//Animates shots
			//gxDrawRect(bullets[i].X/*-xAxis*/, bullets[i].Y/*-yAxis*/, 8, 8, gxRGB32(255,0,0) );

			gxDrawSpriteAlpha( bullets[i].X-xAxis, bullets[i].Y-yAxis, &bulletSprite, 255 );

			bullets[i].X +=bullets[i].movementX;
			bullets[i].Y +=bullets[i].movementY;
			// Recycle shots
			dx=(float)((bullets[i].X)-(player.X+32));
			dy=(float)((bullets[i].Y)-(player.Y+32));
			length=sqrtf(dx*dx+dy*dy);
			if (length>450)
			//if (bullets[i].X > player.X+(400+64) || bullets[i].X < player.X-400 || bullets[i].Y > player.Y+(300+64) || bullets[i].Y < player.Y-300)
				bullets[i].fire = false;
			// Bullet Hits Enemy
			for (int a = 0; a < NUM_ENEMIES; a++)
			{
				if ( enemies[a].active == true)
				{
					if ( bullets[i].X >= enemies[a].X && bullets[i].X <= enemies[a].X +64 && bullets[i].Y >= enemies[a].Y && bullets[i].Y <= enemies[a].Y +64 )
					{
						score += startVariables.levelNumber;
						enemies[a].enemyHP -= 1;
						enemies[a].timer = 0;
						bullets[i].fire=false;
						if(enemies[a].enemyHP <=0)
						{
							enemies[a].active = false;
							CreateExplodies(enemies[a].X, enemies[a].Y);
						}
					}

					// Bullet Hits Boss
					if ( bullets[i].X >= enemies[a].X && bullets[i].X <= enemies[a].X +64 && bullets[i].Y >= enemies[a].Y && bullets[i].Y <= enemies[a].Y +128 && enemies[a].type >= 11 )
					{
						if ( enemies[a].active == true)
						{
							enemies[a].manbearpiglife -= 1;

							if ( enemies[a].manbearpiglife <= 0 )
							{
								enemies[a].active = false;
								score += 50*startVariables.levelNumber;
								bullets[i].fire = false;
								CreateExplodies(enemies[a].X, enemies[a].Y);
							}
						}
					}
				}
			}
		}
	}
}