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

sxSound RocketSound;
gxSprite rocketSprite;
gxSprite crosshairSprite2;

Rocket rockets[NUM_ROCKETS];
int rocketDelay = 0;
int rocketsLeft = 3*startVariables.levelNumber;;

void InitRocket ()
{
	gxLoadBmp( "rocket1.tga", &rocketSprite, 0);
	gxLoadBmp( "crosshair4.tga", &crosshairSprite2, 0);
	sxLoadWav ( "rocket.wav", &RocketSound );

	for (int i = 0; i < NUM_ROCKETS; i++)
	{
		rockets[i].X = 0;
		rockets[i].Y = 0;
		rockets[i].fire = false;
	}

	rocketsLeft = 3*startVariables.levelNumber;
}


void UpdateRocket ()
{
	gxDrawSpriteAlpha( msX, msY, &crosshairSprite2, 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 rocket
	if(player.health>0)
	{
		if ( msButton2 && rocketDelay <= 0 && rocketsLeft > 0)
		{
			for (int i = 0; i < NUM_ROCKETS; i++)
			{
				if(rockets[i].fire==false)
				{
					sxPlaySound ( &RocketSound );
					rocketDelay=20;
					rocketsLeft -= 1;
					rockets[i].fire=true;
					rockets[i].X=player.X+32;
					rockets[i].Y=player.Y+32;
					rockets[i].movementX=(int)vx;
					rockets[i].movementY=(int)vy;
					break;
				}
			}
		}
	}

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


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

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

			rockets[i].X +=rockets[i].movementX;
			rockets[i].Y +=rockets[i].movementY;

			// Recycle shots
			if (rockets[i].X > stages[currentStage].width || rockets[i].X < 0 || rockets[i].Y > stages[currentStage].height || rockets[i].Y < 0)
				rockets[i].fire = false;


			// Rocket Hits Enemy
			for (int a = 0; a < NUM_ENEMIES; a++)
			{
				if ( enemies[a].active == true)
				{
					if ( rockets[i].X >= enemies[a].X && rockets[i].X <= enemies[a].X +100 && rockets[i].Y >= enemies[a].Y && rockets[i].Y <= enemies[a].Y +100 )
					{
						enemies[a].enemyHP -= 5;
						if(enemies[a].enemyHP <= 0)
						{
							enemies[a].active = false;
							score += 1;
							CreateExplodies(enemies[a].X, enemies[a].Y);
						}
					}

					// Rocket Hits Boss
					if ( rockets[i].X >= enemies[a].X && rockets[i].X <= enemies[a].X +64 && rockets[i].Y >= enemies[a].Y && rockets[i].Y <= enemies[a].Y +128 && enemies[a].type >= 11 )
					{
						if ( enemies[a].active == true)
						{
							enemies[a].manbearpiglife -= 50;
							if ( enemies[a].manbearpiglife <= 0 )
							{
								enemies[a].active = false;
								score += 50;
								CreateExplodies(enemies[a].X, enemies[a].Y);
							}
						}
					}
				}
			}
		}
	}
}