#include	"ppLevel.h"

CLevel::CLevel()
{

	m_iBackImgID = agk::LoadImage("space/space.jpg");
	m_iObstacleImgID = agk::LoadImage("space/MetalBlock1.png");

	// create background
	m_iBackSprID = agk::CreateSprite(m_iBackImgID);
	agk::FixSpriteToScreen(m_iBackSprID, 1);

	// create starfield
	m_iStarfieldPfxID = agk::CreateParticles(550.0, 0.0);

	agk::SetParticlesImage(m_iStarfieldPfxID, m_iStarfieldImgID);
	agk::SetParticlesStartZone(m_iStarfieldPfxID, 0, 10, 0, 400);
	agk::SetParticlesDirection(m_iStarfieldPfxID, -50.0, 0.0);
	agk::SetParticlesLife(m_iStarfieldPfxID, 15);
	agk::SetParticlesSize(m_iStarfieldPfxID, 4);
	agk::SetParticlesAngle(m_iStarfieldPfxID, 0);
	agk::SetParticlesFrequency(m_iStarfieldPfxID, 60);
	agk::SetParticlesVelocityRange(m_iStarfieldPfxID, 0.5, 4.0);
	agk::FixParticlesToScreen(m_iStarfieldPfxID, 1);

	// create level
	m_iObstacleImgID = agk::LoadImage("space/MetalBlock1.png");

	// top random
	CreateStrip(m_iObstacleImgID, 32.0f, 0.0f, 0);
	// bottom random
	CreateStrip(m_iObstacleImgID, 25.0f, 0.0f, 1);
	// top and bottom straight
	CreateStrip(m_iObstacleImgID, 0.0f, 0.0f, 2);
}

CLevel::~CLevel()
{
}

int CLevel::CheckCollisionWithLevel(int iSprite)
{
	// counting through all obstacles
	for (int i = 0; i < m_iSceneryCount; i++)
	{
		// if obstacle exist ..
		if (agk::GetSpriteExists(m_iScenery[i]) == 1)
			// ... then check sprite collision with obstacle
			if (agk::GetSpriteCollision(iSprite, m_iScenery[i]) == 1)
				return 1;
	}

	return 0;
}

// create strips for level build
void CLevel::CreateStrip(int iImage, float fX, float fY, int iMode)
{
	for (int i = 0; i <= 250; i++)
	{
		int built = 0;
		int	iSprite = -1;
		int	iSpriteB = -1;

		if (iMode == 0)
		{
			if (agk::Random(1, 2) == 1)
			{
				iSprite = agk::CreateSprite(iImage);
				agk::SetSpritePosition(iSprite, fX, float(368 - agk::Random(64, 64 + 32)));
				built = 1;
			}

			if (fX > 300 && agk::Random(1, 5) == 3)
			{
				iSpriteB = agk::CreateSprite(iImage);
				agk::SetSpritePosition(iSpriteB, fX, float(agk::Random(120, 200)));
				agk::SetSpriteColor(iSpriteB, 255, 255, 255, 200);
				agk::SetSpriteAngle(iSpriteB, float(agk::Random(0, 360)));
				built = 1;
			}
		}

		if (iMode == 1)
		{
			if (agk::Random(1, 3) == 1)
			{
				iSprite = agk::CreateSprite(iImage);
				agk::SetSpritePosition(iSprite, fX, float(agk::Random(32, 64)));
				built = 1;
			}
		}

		if (iMode == 2)
		{
			iSprite = agk::CreateSprite(iImage);
			agk::SetSpritePosition(iSprite, fX, 0.0);

			iSpriteB = agk::CreateSprite(iImage);
			agk::SetSpritePosition(iSpriteB, fX, 400 - 64);

			built = 1;
		}

		if (built == 1)
		{

			if (iSprite != -1)
			{
				m_iScenery[m_iSceneryCount] = iSprite;
				m_iSceneryCount++;
			}

			if (iSpriteB != -1)
			{
				m_iScenery[m_iSceneryCount] = iSpriteB;
				m_iSceneryCount++;
			}
		}

		fX = fX + 64;
	}
}
