//#include <hge.h>
#include <hgesprite.h>
#include <hgefont.h>
#include <hgeparticle.h>
#include <hgegui.h>
#include <hgeanim.h>
#include <math.h>
#include <mmsystem.h>
#include <hgeguictrls.h>
#include "Pers.h"
#include "Enemy.h"
//#include "Barrier.h"
#include "Global.h"

#define WindowWidht 800
#define WindowHeight 600


//--- PROTO ---
void CollisionMissileToEnemy();
void CollisionPersToBarriers();
void CollisionEnemyToBarriers();
void Radar();
bool FrameFunc();
bool RenderFunc();
bool Scene0();
bool Scene1();
bool S0Render();
bool S1Render();

//--- Variables ---
int Scene			= 0;
int S0				= 0;
bool Just			= false;
int LoadLineWidth	= 0;

struct TBlood
{
	float x, y, rotation, time;
	int type;
};

TBlood Blood[10000];
int CountBlood=0;
TBarrier *Barrier = 0;
HGE *hge  = 0;
float dt  = 0;
float X   = 0;
float Y   = 0;
float Rot = 0;
float MX=0, MY=0;
float PosX=0, PosY=0;

TPers			*Pers;
TEnemy			*Enemy;
//TBarrier		*Barrier;

HTEXTURE		texBody, texWeapon, texShot;
HTEXTURE		texRadar, texRadarEnemy, texRadarPers, texRadarBarrier;

HTEXTURE		texLoad, texLoadLine, texCursor, texCursor2;
HTEXTURE		texBackGround[10];

HTEXTURE		texBlood1;

hgeSprite		*sprBlood1;
hgeSprite		*sprRadar, *sprRadarEnemy, *sprRadarPers, *sprRadarBarrier;
hgeSprite		*sprLoad, *sprLoadLine, *sprCursor;
hgeSprite		*sprBackGround;
hgeGUI			*guiCursor=new hgeGUI();
hgeFont			*fnt;


//--- Code ---
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "game.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "XXX");
	hge->System_SetState(HGE_FPS, 50);
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, WindowWidht);
	hge->System_SetState(HGE_SCREENHEIGHT, WindowHeight);
	hge->System_SetState(HGE_SCREENBPP, 32);
	hge->System_SetState(HGE_SHOWSPLASH, false);


	if(hge->System_Initiate()) 
	{
		texCursor = hge->Texture_Load("Cursor2.png");
		texLoad = hge->Texture_Load("Load.jpg");
		texLoadLine = hge->Texture_Load("Line.jpg");
		texBody = texWeapon = hge->Texture_Load("Body1.png");
		texShot = hge->Texture_Load("Missiles.png");
		texRadar = hge->Texture_Load("Radar.bmp");
		texRadarEnemy = hge->Texture_Load("RadarEnemy.bmp");
		texRadarPers = hge->Texture_Load("RadarPers.bmp");
		texRadarBarrier = hge->Texture_Load("RadarBarrier.bmp");
		texBlood1 = hge->Texture_Load("Blood1.png");

		sprBlood1 = new hgeSprite(texBlood1, 0, 0, 32, 32);
		sprBlood1->SetHotSpot(16, 16);

		Pers = new TPers(texBody, texBody, texShot);
		Enemy = new TEnemy(texBody);
		Barrier = new TBarrier();
		
		sprCursor = new hgeSprite(texCursor, 0, 0, 32, 32);
		sprCursor->SetHotSpot(12, 12);
		guiCursor->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		guiCursor->SetCursor(sprCursor);

		sprRadar = new hgeSprite(texRadar, 0, 0, 223, 170);
		sprRadarEnemy = new hgeSprite(texRadarEnemy, 0, 0, 3, 3);
		sprRadarPers = new hgeSprite(texRadarPers, 0, 0, 3, 3);
		sprRadarBarrier = new hgeSprite(texRadarBarrier, 0, 0, 6, 6);

		sprLoad = new hgeSprite(texLoad, 0, 0, 800, 600);
		sprLoadLine = new hgeSprite(texLoadLine, 0, 0, 270, 26);
		sprLoadLine->SetTextureRect(0,0, LoadLineWidth,26, true);
		fnt=new hgeFont("Fonts/3.fnt");

		Just = true;
		for(int i=0; i<10000; i++)
		{
			Blood[i].type = 0;
		}

		hge->System_Start();
	}

	hge->System_Shutdown();
	hge->Release();
	return 0;
}

void CollisionMissileToEnemy()
{
	for(int Missile=0; Missile < Pers->Missiles->MaxBullets; Missile++)
	{
		for(int Any=0; Any < Enemy->MaxAlen; Any++)
		{
			if((Enemy->Alen[Any].ttx -14 < Pers->Missiles->bullets[Missile].ttx+6) && ((Enemy->Alen[Any].ttx +14) > Pers->Missiles->bullets[Missile].ttx-6))
			{
				if((Enemy->Alen[Any].tty -14 < Pers->Missiles->bullets[Missile].tty+6) && ((Enemy->Alen[Any].tty +14) > Pers->Missiles->bullets[Missile].tty-6))
				{
					if(CountBlood == 10000) CountBlood = 0;
					Blood[CountBlood].x = Enemy->Alen[Any].ttx;
					Blood[CountBlood].y = Enemy->Alen[Any].tty;
					Blood[CountBlood].rotation = hge->Random_Float(0, 6.38);
					Blood[CountBlood].type = 1;
					Blood[CountBlood].time = 60;
					CountBlood++;

					Enemy->Fre47ght5n16(Any, Pers->Missiles->bullets->type);
					Pers->Missiles->Delete(Missile);
				}
			}
		}
	}
}

void CollisionPersToBarriers()
{
	for(int i=0; i<100; i++)
	{
		if(Barrier->Bar[i].type != 0)
		{
			if((Pers->PosX+32 > Barrier->Bar[i].SensitiveX) && (Pers->PosX-32 < Barrier->Bar[i].SensitiveX + Barrier->Bar[i].SensitiveWidth))
			if((Pers->PosY+32 > Barrier->Bar[i].SensitiveY) && (Pers->PosY-32 < Barrier->Bar[i].SensitiveY + Barrier->Bar[i].SensitiveHeight))
			{
			if((Pers->BackPosX+32 > Barrier->Bar[i].SensitiveX) && (Pers->BackPosX-32 < Barrier->Bar[i].SensitiveX + Barrier->Bar[i].SensitiveWidth)) 
					Pers->Back('Y');
			if((Pers->BackPosY+32 > Barrier->Bar[i].SensitiveY) && (Pers->BackPosY-32 < Barrier->Bar[i].SensitiveY + Barrier->Bar[i].SensitiveHeight))
					Pers->Back('X');
			if(Pers->PosX != Pers->BackPosX && Pers->PosY != Pers->BackPosY) //иначезастревает, если напоролся на угол
				Pers->Back('Y');
			}
		}
	}
}

void CollisionEnemyToBarriers()
{
	for(int i=0; i<100; i++)
	{
		for(int t=0; t<Enemy->MaxAlen; t++)
		{
			if(Enemy->Alen[t].type != 0 && Barrier->Bar[i].type != 0)
			{
				if((Enemy->Alen[t].ttx+16 > Barrier->Bar[i].SensitiveX) && (Enemy->Alen[t].ttx-16 < Barrier->Bar[i].SensitiveX + Barrier->Bar[i].SensitiveWidth))
				if((Enemy->Alen[t].tty+16 > Barrier->Bar[i].SensitiveY) && (Enemy->Alen[t].tty-16 < Barrier->Bar[i].SensitiveY + Barrier->Bar[i].SensitiveHeight))
				{
					if((Enemy->Alen[t].BackPosX+16 > Barrier->Bar[i].SensitiveX) && (Enemy->Alen[t].BackPosX-16 < Barrier->Bar[i].SensitiveX + Barrier->Bar[i].SensitiveWidth)) 
					{

					}
					if((Pers->BackPosY+16 > Barrier->Bar[i].SensitiveY) && (Pers->BackPosY-16 < Barrier->Bar[i].SensitiveY + Barrier->Bar[i].SensitiveHeight))
					{

					}
				}
			}
		}
	}
}


void Radar()
{
	int x=0,y=0;
	sprRadar->Render(0, 430);
	for(int r=0; r < 100; r++)
	{
		if(Barrier->Bar[r].type != 0)
		{
			x = 200*(Barrier->Bar[r].x / 2000);
			y = 150*(Barrier->Bar[r].y / 1352);
			sprRadarBarrier->Render(x+10, y+440);
		}
	}

	for(int i=0; i<Enemy->MaxAlen; i++)
	{
		if(Enemy->Alen[i].type != 0)
		{
			x = 200*((Enemy->Alen[i].ttx-8) / 2000);
			y = 150*((Enemy->Alen[i].tty-8) / 1352);
			//if(x > -20 && y > -20 && x < 200 && y < 150) //это чтоб за пределами радара точки не отрисовывались
				sprRadarEnemy->Render(x+10, y+440);
		}
	}
	sprRadarPers->Render(200*((Pers->PosX-16) / 2000)+10, 150*((Pers->PosY-16) / 1352) + 440);
}

bool FrameFunc()
{
dt = hge->Timer_GetDelta();
hge->Input_GetMousePos(&MX, &MY);

//swith не использую, потому что с ним иногда бывают проблемы
if(Scene==0) Scene0();
else if(Scene==1) Scene1();

guiCursor->Update(dt);
return false;
}


bool RenderFunc()
{
	
	hge->Gfx_BeginScene();
	hge->Gfx_Clear(0);

	if(Scene==0) S0Render();
	else if(Scene==1) S1Render();

	Pers->GetPosition(&PosX, &PosY);

	//fnt->printf(10, 10, HGETEXT_LEFT, "FPS: %d\nMX: %d\nMY: %d\nRot: %f\nScene: %d\nX: %d\nY: %d\nPosX: %d\nPosY: %d\nMaxBullets: %d\nEnemyX: %d\nEnemyY: %d\nRotation: %f\nAngle: %f\nDelta: %f\nBackPosX: %f\nBackPosY: %f\nMaxAlen: %d",hge->Timer_GetFPS(),(int)MX, (int)MY, Rot, Scene, (int)X ,(int)Y, (int)PosX, (int)PosY, Pers->Missiles->MaxBullets, (int)Enemy->Alen->ttx, (int)Enemy->Alen->tty, Enemy->Alen[0].rotation, Enemy->Alen[0].angle, Enemy->Alen[0].delta, Pers->BackPosX, Pers->BackPosY, Enemy->MaxAlen);
	guiCursor->Render();
	hge->Gfx_EndScene();
	return false;
}

//==== SCENE_0 ====

bool Scene0()
{
	if(Just)
	{
		Pers->SetPosition(384, 284); 
		Barrier->Add(50, 50, 1);
		for(int i=0; i<100; i++)
		{
			Barrier->Add(hge->Random_Int(0,37)*50+50, hge->Random_Int(0,24)*50+50, 1);
		}
		Just = false;
	}
	if(S0==0)
	{
		texBackGround[0] = hge->Texture_Load("BackGround_0.jpg");
		sprBackGround = new hgeSprite(texBackGround[0], 0, 0, 2100, 1700);
		LoadLineWidth += 0;
		S0=1;
		//Pers->Go();
		
	}else if(S0==1){
		LoadLineWidth+=dt*500;
	}

	sprLoadLine->SetTextureRect(0,0, LoadLineWidth,26, true);
	if(LoadLineWidth >= 291) Scene=1;
return false;
}


bool S0Render()
{
	sprLoad->Render(0,0);
	sprLoadLine->Render(265, 361);

	return false;
}

//==== SCENE_1 ====

bool Scene1()
{
Pers->Update(dt);
Enemy->Update(dt);
CollisionMissileToEnemy();
CollisionPersToBarriers();
//CollisionEnemyToBarriers();


return false;
}


bool S1Render()
{
	//int i=5;

	if(hge->Input_KeyDown(HGEK_SPACE))
		for(int i=0; i<25; i++)
		{
			Enemy->Add(hge->Random_Int(0,2000), 0);
			Enemy->Add(hge->Random_Int(0,2000), 1352);
			Enemy->Add(0, hge->Random_Int(0,1352));
			Enemy->Add(2000, hge->Random_Int(0,1352));
		}

	sprBackGround->Render(-X,-Y);
	for(int i=0; i<10000; i++)
	{
		if(Blood[i].x==0 && Blood[i].type!=0)
			sprBlood1->RenderEx(Blood[i].x-X, Blood[i].y-Y, Blood[i].rotation, 1.0, 0);
	}
	Barrier->Render(X, Y);
	Enemy->Render(X, Y);
	Pers->Render(X, Y);
	Radar();

	return false;
}


