#include <hge.h>
#include <hgesprite.h>
#include <hgeparticle.h>
#include <math.h>
#include <mmsystem.h>
#include "Pers.h" 
#include "Global.h"

TPers::TPers(HTEXTURE Body, HTEXTURE Weapon, HTEXTURE Shot)
{
	PosX   = 0;
	PosY   = 0;
	//dt   = 0;
	Shot   = false;
	Walk   = false;
	Up	   = false;
	Down   = false;
	Left   = false;
	Right  = false;

	MDownLeft		 = false;
	Pushed			 = 0;
	Rotation         = 0;
	Rotation2		 = 0;
	Angle			 = 0;
	ShotTime		 = 0;
	SpriteBodyNumber = 0;
	WalkTime		 = 0;
	BodyTime		 = 0;
	StepNumber		 = 0;
	Speed			 = 300;
	MX				 = 0;
	MY				 = 0;
	BackX			 = 0;
	BackY			 = 0;
	BackPosX		 = 0;
	BackPosY		 = 0;

	texBody = Body;
	texWeapon = Weapon;
	texShot = Shot;

	Missiles = new TMissiles(Shot);
	
	sprBody_1 = new hgeSprite(texBody, 0, 0, 64, 57);//--
	sprBody_1->SetHotSpot(32,28);					 //--
	sprBody_1->SetTextureRect(0,0,64,57,true);		 //--
	sprBody_2 = new hgeSprite(texBody, 0, 32, 32, 256);
	sprBody_2->SetHotSpot(16,16);
	sprBody_2->SetTextureRect(0,32,32,32,true);

	sprWeapon_1 = new hgeSprite(texWeapon, 0, 0, 32, 32);
	sprWeapon_2 = new hgeSprite(texWeapon, 32, 0, 32, 32);
	sprWeapon_3 = new hgeSprite(texWeapon, 64, 0, 32, 32);
	sprWeapon_4 = new hgeSprite(texWeapon, 96, 0, 32, 32);

	sprShot	= new hgeSprite(texShot, 0, 0, 32, 32);

}


TPers::Update(float DT)
{
	dt = DT;
	if(Shot)
	{
		ShotTime+=dt;
		if(ShotTime > 0.5)
		{
			Shot = false;
			ShotTime = 0;
		}
	}
	if(Walk)
	{
		WalkTime+=dt;
		if(WalkTime > 0.125)
		{
			StepNumber++;
			if(StepNumber==8) StepNumber=0;

			sprBody_2->SetTextureRect((float)StepNumber*32,32,32,32,true);
			WalkTime = 0;
		}
	}
	BodyTime += dt;
	if(BodyTime > 0.125)
	{
		SpriteBodyNumber++;
		if(SpriteBodyNumber==8) SpriteBodyNumber=0;

		//sprBody_1->SetTextureRect((float)SpriteBodyNumber*32,0,32,32,true); // !!! НУЖНО !!!
		BodyTime = 0;
	}

	Missiles->Update(dt);
	Go();
	Rotating();
	Shoting();
}

TPers::Render(float X, float Y)
{
	Missiles->Render(X, Y);
	sprBody_2->RenderEx(PosX-X, PosY-Y, Rotation, 1.0, 0);
	sprBody_1->RenderEx(PosX-X, PosY-Y, Rotation2, 1.0, 0);
	
}

TPers::SetPosition(float x, float y)
{
	PosX = x;
	PosY = y;
}

TPers::GetPosition(float* x, float* y)
{
	*x = PosX;
	*y = PosY;
}

TPers::Move(float x, float y)
{
	PosX += x;
	PosY += y;
}

TPers::Go()
{
	if(hge->Input_KeyDown(HGEK_W))
	{
		Up = true;
		Pushed++;
		Angle+=0;//для наглядности
	}
	if(hge->Input_KeyUp(HGEK_W))
	{
		Up = false;
		Pushed--;
		Angle-=0;
	}

	if(hge->Input_KeyDown(HGEK_S))
	{
		Down = true;
		Pushed++;
		Angle+=180;
	}
	if(hge->Input_KeyUp(HGEK_S))
	{
		Down = false;
		Pushed--;
		Angle-=180;
	}

	if(hge->Input_KeyDown(HGEK_A))
	{
		Left = true;
		Pushed++;
		Angle+=270;
	}
	if(hge->Input_KeyUp(HGEK_A))
	{
		Left = false;
		Pushed--;
		Angle-=270;
	}

	if(hge->Input_KeyDown(HGEK_D))
	{
		Right = true;
		Pushed++;
		Angle+=90;
	}
	if(hge->Input_KeyUp(HGEK_D))
	{
		Right = false;
		Pushed--;
		Angle-=90;
	}

	BackPosY = PosY;
	BackY = Y;
	BackPosX = PosX;
	BackX = X;


	if(Up)
	{	
		if(PosY>16)
		{
			if(PosY-dt*Speed>16)
				PosY -= dt*Speed;
			else
				PosY = 16;
		}
		if(Y>0 && PosY<1024)
		{
			if(Y-dt*Speed>=0)
				Y -= dt*Speed;
			else
				Y = 0;
		}
	}
	if(Down)
	{
		if(PosY<1320)
		{
			if(PosY+dt*Speed<1320)
				PosY+=dt*Speed;
			else
				PosY = 1320;

		}
		if(Y<736 && PosY>284)
		{
			if(Y+dt*Speed<=736)
				Y += dt*Speed;
			else
				Y = 736;
		}
	}
	if(Left)
	{
		if(PosX>16)
		{
			if(PosX-dt*Speed>16)
				PosX -= dt*Speed;
			else
				PosX = 16;
		}
		if(X>0 && PosX<1648)
		{
			if(X-dt*Speed >= 0)
				X-= dt*Speed;
			else
				X = 0;
		}
	}
	if(Right)
	{
		if(PosX<2000)
		{
			if(PosX+dt*Speed<2000)
				PosX+=dt*Speed;
			else
				PosX = 2000;
		}
		if(X<1220 && PosX>384)
		{
			if(X+dt*Speed<=1220)
				X+=dt*Speed;
			else
				X = 1220;
		}
	}


	if(Up || Down || Left || Right)
		Walk = true;
	else
		Walk = false;

	if(Pushed!=0)
	{
		if(Up && Left && Right)
			Rotation = (0)*(3.1415926535/180);
		else if(Up && Left)
			Rotation = (315)*(3.1415926535/180);
		else if(!(Left && Right && !Up && !Down))
			Rotation = (Angle/Pushed)*(3.1415926535/180);
	}

	//Rot = Angle;

	
}

TPers::Shoting()
{
	if(hge->Input_KeyDown(HGEK_LBUTTON))
	{
		MDownLeft = true;
	}
	if(hge->Input_KeyUp(HGEK_LBUTTON))
	{
		MDownLeft = false;
	}
}


TPers::Rotating()
{
	hge->Input_GetMousePos(&MX, &MY);
	Rotation2 = atan2(PosX - (MX+X), PosY - (MY+Y))*180/3.1415926535;
	if(Rotation2>0)
		Rotation2 = 360 - Rotation2;
	else
		Rotation2 *= -1;
	Rot = Rotation2;
	Rotation2 *=(3.1415926535/180);
}

TPers::Back(char Side)
{
	if(Side=='X' || Side=='x')
	{
		PosX = BackPosX;
		X = BackX;
	}
	if(Side=='Y' || Side=='y')
	{
		PosY = BackPosY;
		Y = BackY;
	}
}

