#include	"SpaceShooter.h"

using namespace AGK;

//void	CreateAliens()
//{
//	// create 10 aliens
//
//	// loadi mages
//	g_iShip = agk::LoadImage("space/alienshipleft.png");
//	g_iParticles = agk::LoadImage("space/shrapnel3.png");
//
//	// set initial data
//	for (int i = 0; i < 10; i++)
//	{
//		g_Aliens[i].iSprite = 0;
//		g_Aliens[i].iEngine = 0;
//
//		g_Aliens[i].fSpeed = 0.0;
//		g_Aliens[i].fValueA = 0.0;
//		g_Aliens[i].fValueB = 0.0;
//	}
//
//	// this will build them
//	ResetAliens();
//}

//void	CreateAlienExplosions()
//{
//	// create 5 explosions
//	for (int i = 0; i < 5; i++)
//	{
//		g_Explosions[i].iID = agk::CreateParticles(-1000, -1000);
//		g_Explosions[i].iActive = 0;
//	}
//}

//void	UpdateAliens()
//{
//	// update the aliens
//	for (int i = 0; i < 10; i++)
//	{
//		// get the new location
//		float fShipX = agk::GetSpriteX(g_Aliens[i].iSprite) - g_Aliens[i].fSpeed;
//		float fShipY = agk::GetSpriteY(g_Aliens[i].iSprite);
//		float fEngineX = agk::GetParticlesX(g_Aliens[i].iEngine) - g_Aliens[i].fSpeed;
//		float fEngineY = agk::GetParticlesY(g_Aliens[i].iEngine);
//
//		// apply wavy movement
//		g_Aliens[i].fValueB = g_Aliens[i].fValueB + g_Aliens[i].fValueA;
//		fShipY = fShipY + agk::Cos(g_Aliens[i].fValueB * 180.0f / 3.14f);
//		fEngineY = fEngineY + agk::Cos(g_Aliens[i].fValueB * 180.0f / 3.14f);
//
//		// reset once they have moved off the screen
//		if (agk::WorldToScreenX(fEngineX) < -200)
//		{
//			// new position off to the right
//			fShipX = 550.0f + agk::Random(20, 600);
//			fShipY = float(agk::Random(100, 300));
//
//			// update engine position
//			fEngineX = fShipX + 30;
//			fEngineY = fShipY + 10;
//
//			// convert to world position instead of screen (because of scrolling)
//			fShipX = agk::ScreenToWorldX(fShipX);
//			fShipY = agk::ScreenToWorldY(fShipY);
//			fEngineX = agk::ScreenToWorldX(fEngineX);
//			fEngineY = agk::ScreenToWorldY(fEngineY);
//
//			// generate new values for movement
//			float speed = float(agk::Random(10, 20));
//			g_Aliens[i].fSpeed = speed / 10.0f;
//			speed = float(agk::Random(1, 10));
//			g_Aliens[i].fValueA = speed / 100.0f;
//			g_Aliens[i].fValueB = 0.0f;
//
//			// clear particle colors and add new keyframes
//			agk::ClearParticlesColors(g_Aliens[i].iEngine);
//			agk::AddParticlesColorKeyFrame(g_Aliens[i].iEngine, 0.0f, agk::Random(0, 255), agk::Random(0, 255), 0, 255);
//			agk::AddParticlesColorKeyFrame(g_Aliens[i].iEngine, 0.4f, 255, 255, 0, 0);
//		}
//
//		// update position of alien and engine
//		agk::SetSpritePosition(g_Aliens[i].iSprite, fShipX, fShipY);
//		agk::SetParticlesPosition(g_Aliens[i].iEngine, fEngineX, fEngineY);
//	}
//}

//void	ResetAliens()
//{
//	// reset all aliens
//
//	// run through them
//	for (int i = 0; i < 10; i++)
//	{
//		// set position
//		int iX = 550 + agk::Random(20, 600);
//		int iY = agk::Random(100, 300);
//
//		// delete alien if it exists
//		if (agk::GetSpriteExists(g_Aliens[i].iSprite) == 1)
//		{
//			agk::DeleteSprite(g_Aliens[i].iSprite);
//		}
//
//		// delete engine if it exists
//		if (agk::GetParticlesExists(g_Aliens[i].iEngine) == 1)
//		{
//			agk::DeleteParticles(g_Aliens[i].iEngine);
//		}
//
//		// create alien and engine
//		g_Aliens[i].iSprite = agk::CreateSprite(g_iShip);
//		g_Aliens[i].iEngine = agk::CreateParticles(float(iX + 30), float(iY + 10));
//
//		// give it a box shape
//		agk::SetSpriteShape(g_Aliens[i].iSprite, 2);
//		agk::SetSpritePosition(g_Aliens[i].iSprite, float(iX), float(iY));
//
//		// set up engine
//		agk::SetParticlesImage(g_Aliens[i].iEngine, g_iParticles);
//		agk::SetParticlesStartZone(g_Aliens[i].iEngine, 0.0f, 0.0f, 0.0f, 0.0f);
//		agk::SetParticlesDirection(g_Aliens[i].iEngine, 50.0f, 0.0);
//		agk::SetParticlesLife(g_Aliens[i].iEngine, 0.4f);
//		agk::SetParticlesSize(g_Aliens[i].iEngine, 16.0f);
//		agk::SetParticlesAngle(g_Aliens[i].iEngine, 20.0f);
//		agk::SetParticlesFrequency(g_Aliens[i].iEngine, 24.0f);
//		agk::SetParticlesVelocityRange(g_Aliens[i].iEngine, 0.5f, 2.0f);
//		agk::AddParticlesColorKeyFrame(g_Aliens[i].iEngine, 0.0f, agk::Random(0, 255), agk::Random(0, 255), 0, 255);
//		agk::AddParticlesColorKeyFrame(g_Aliens[i].iEngine, 0.4f, 255, 255, 0, 0);
//
//		// add one of four forces
//		int mode = agk::Random(1, 4);
//
//		if (mode == 1)
//			agk::AddParticlesForce(g_Aliens[i].iEngine, 0.2f, 0.4f, -1500.0f, -1550.0f);
//
//		if (mode == 2)
//			agk::AddParticlesForce(g_Aliens[i].iEngine, 0.2f, 0.4f, 1500.0f, 1550.0f);
//
//		if (mode == 3)
//			agk::AddParticlesForce(g_Aliens[i].iEngine, 0.2f, 0.4f, 100.0f, 100.0f);
//
//		if (mode == 4)
//			agk::AddParticlesForce(g_Aliens[i].iEngine, 0.2f, 0.4f, -100.0f, -100.0f);
//
//		// set up for movement
//		float speed = float(agk::Random(10, 20));
//		g_Aliens[i].fSpeed = speed / 10.0f;
//		speed = float(agk::Random(1, 10));
//		g_Aliens[i].fValueA = speed / 100.0f;
//		g_Aliens[i].fValueB = 0.0f;
//	}
//}

void	ExplodeAlien(float x, float y)
{
	// destroy an alien

	// find which explosion from the 5 is available
	for (int i = 0; i < 5; i++)
	{
		if (agk::GetParticlesMaxReached(g_Explosions[i].iID) == 1)
		{
			agk::ClearParticlesColors(g_Explosions[i].iID);
			agk::ClearParticlesForces(g_Explosions[i].iID);
			g_Explosions[i].iActive = 0;
		}
	}

	int ID = -1;

	for (int i = 0; i < 5; i++)
	{
		if (g_Explosions[i].iActive == 0)
		{
			ID = i;
			break;
		}
	}

	// back out if all 5 are being used
	if (ID == -1)
	{
		return;
	}

	// store the ID
	int explode = g_Explosions[ID].iID;

	// reset
	agk::ResetParticleCount(explode);

	// set position
	agk::SetParticlesPosition(explode, x, y);

	// set properties
	agk::SetParticlesFrequency(explode, 250);
	agk::SetParticlesLife(explode, 1.0f);
	agk::SetParticlesSize(explode, 32);
	agk::SetParticlesStartZone(explode, -10, 0, 10, 0);
	agk::SetParticlesImage(explode, 4);
	agk::SetParticlesDirection(explode, 20, 20.0f);
	agk::SetParticlesAngle(explode, 360.0f);
	agk::AddParticlesColorKeyFrame(explode, 0.0f, 0, 0, 0, 0);
	agk::AddParticlesColorKeyFrame(explode, 0.2f, agk::Random(0, 100), agk::Random(100, 255), agk::Random(100, 255), 255);
	agk::AddParticlesColorKeyFrame(explode, 1.0f, 0, 100, 255, 0);
	agk::AddParticlesForce(explode, 0.2f, 0.8f, 400.0f, 0.0f);

	// set up a random force for the particles
	int a = agk::Random(200, 1000);

	if (agk::Random(1, 2) == 1)
	{
		a = -a;
	}

	int b = agk::Random(500, 5000);
	int c = 0;

	if (agk::Random(1, 2) == 1)
	{
		b = -b;
	}

	if (agk::Random(1, 3) == 1)
	{
		c = b;
	}

	// add force near the end of its life
	agk::AddParticlesForce(explode, 0.6f, 1.0f, float(c), float(a));

	// random kind of explosion
	if (agk::Random(1, 3) == 1)
	{
		agk::ClearParticlesColors(explode);
		agk::ClearParticlesForces(explode);

		int col = agk::Random(1, 3);

		if (col == 1)
		{
			agk::AddParticlesColorKeyFrame(explode, 0.0f, 255, 0, 0, 255);
			agk::AddParticlesColorKeyFrame(explode, 0.4f, 255, 100, 0, 255);
			agk::AddParticlesColorKeyFrame(explode, 0.8f, 255, 200, 0, 255);
			agk::AddParticlesColorKeyFrame(explode, 1.0f, 255, 255, 0, 0);
		}
		else if (col == 2)
		{
			agk::AddParticlesColorKeyFrame(explode, 0.0f, 0, 0, 255, 255);
			agk::AddParticlesColorKeyFrame(explode, 0.4f, 0, 100, 255, 255);
			agk::AddParticlesColorKeyFrame(explode, 0.8f, 0, 200, 255, 255);
			agk::AddParticlesColorKeyFrame(explode, 1.0f, 0, 255, 255, 0);
		}
		else
		{
			agk::AddParticlesColorKeyFrame(explode, 0.0f, 0, 0, 255, 255);
			agk::AddParticlesColorKeyFrame(explode, 0.4f, 100, 0, 255, 255);
			agk::AddParticlesColorKeyFrame(explode, 0.8f, 200, 0, 255, 255);
			agk::AddParticlesColorKeyFrame(explode, 1.0f, 255, 0, 255, 0);
		}

		agk::SetParticlesDirection(explode, 40.0f, 40.0f);

		int up = agk::Random(200, 1000);

		agk::AddParticlesForce(explode, 0.2f, 0.4f, 0.0f, float(-up));
		agk::AddParticlesForce(explode, 0.4f, 1.0f, 0.0f, float(up));
	}

	agk::SetParticlesMax(explode, 100);
}

void	CheckBulletsWithAliens()
{
	// run through all bullets and see if they collide with an alien
	for (int i = 0; i < 10; i++)
	{
		if (g_Bullets[i].iActive == 1)
		{
			for (int j = 0; j < 10; j++)
			{
				if (agk::GetSpriteColorAlpha(g_Bullets[i].iID) > 16)
				{
					if (agk::GetSpriteCollision(g_Bullets[i].iID, g_Aliens[j].iSprite))
					{
						g_iScoreTarget = g_iScoreTarget + 15;

						float x = agk::GetSpriteX(g_Aliens[j].iSprite);
						float y = agk::GetSpriteY(g_Aliens[j].iSprite);
						float x1 = agk::WorldToScreenX(x);
						float y1 = agk::WorldToScreenY(y);

						if (x1 >= 20.0f && x1 <= 500.0f)
						{
							ExplodeAlien(x, y);

							agk::SetSpritePosition(g_Aliens[j].iSprite, -300, -300);
							agk::SetParticlesPosition(g_Aliens[j].iEngine, -300, -300);

							agk::PlaySound(agk::Random(2, 4));

							g_Bullets[i].iActive = 0;
							agk::SetSpriteColorAlpha(g_Bullets[i].iID, 0);
							agk::SetSpritePosition(g_Bullets[i].iID, -100.0f, -100.0f);
							g_Bullets[i].fLife = 0.0f;
						}
					}
				}
			}
		}
	}
}
