#include "StdAfx.h"
#include "BALL.h"


BALL::BALL(IMAGE*	Res,IMAGE	*Dead)
{
	this->Res		= Res;
	this->DeadRes	= Dead;

//	ENEMY::ENEMY();
	this->Live = true;
	this->Rot = 2;
	this->Pic = Res[0];

	MoveSize = 0;
	Direction = MUP;
	Speed = 0.8f;
}
void	BALL::Flip(void)
{
	bool Triger = 0;
	int goo =33;
	if(MoveSize < 0 )
	{
		MoveSize = (rand()%4)*64;
		Direction = rand()%4;

	}

	switch(Direction)
	{
	case MLEFT:
		this->SetPosition(this->Position.x-Speed,Position.y);
		this->Rot = MLEFT;
		MoveSize-=Speed;
		for(int i = 0;i < (int)(*Map).size();i++)
		{
			if(Collid.Collide((*Map)[i]))
			{
				Triger = 1;
				if((*Map)[i].Position.y > Position.y+goo)
					SetPosition(Position.x,(*Map)[i].Position.y-64);
				else if((*Map)[i].Position.y+goo  < Position.y)
					SetPosition(Position.x,(*Map)[i].Position.y+64);
				else
				{
					this->SetPosition((*Map)[i].Position.x+64,Position.y);
					MoveSize = 0;
				}
			}
		}
		for(int i = 0;i < (int)(*Bombs).size();i++)
		{
			if(Collid.Collide((*Bombs)[i].DrawImage))
			{
				Triger = 1;
				if((*Bombs)[i].Position.y > Position.y+goo)
					SetPosition(Position.x,(*Bombs)[i].Position.y-64);
				else if((*Bombs)[i].Position.y+goo  < Position.y)
					SetPosition(Position.x,(*Bombs)[i].Position.y+64);
				else
				{
					this->SetPosition((*Bombs)[i].Position.x+64,Position.y);
					MoveSize = 0;
				}
			}
		}
		if(Triger)
		{
			SetPosition(this->Position.x+Speed,Position.y);
		}
		break;
	case MUP:
		this->SetPosition(Position.x,this->Position.y-Speed);
		this->Rot = MUP;
		MoveSize-=Speed;
		for(int i = 0;i < (int)(*Map).size();i++)
		{
			if(Collid.Collide((*Map)[i]))
			{
				Triger = 1;
				if((*Map)[i].Position.x > Position.x+goo)
					SetPosition((*Map)[i].Position.x-64,Position.y);
				else if((*Map)[i].Position.x+goo  < Position.x)
					SetPosition((*Map)[i].Position.x+64,Position.y);
				else
				{
					this->SetPosition(Position.x,(*Map)[i].Position.y+64);
					MoveSize = 0;
				}
			}
		}
		for(int i = 0;i < (int)(*Bombs).size();i++)
		{
			if(Collid.Collide((*Bombs)[i].DrawImage))
			{
				Triger = 1;
				if((*Bombs)[i].Position.x > Position.x+goo)
					SetPosition((*Bombs)[i].Position.x-64,Position.y);
				else if((*Bombs)[i].Position.x+goo  < Position.x)
					SetPosition((*Bombs)[i].Position.x+64,Position.y);
				else
				{
					this->SetPosition(Position.x,(*Bombs)[i].Position.y+64);
					MoveSize = 0;
				}
			}
		}

		if(Triger)
		{
			SetPosition(Position.x,this->Position.y+Speed);
		}
		break;
	case MRIGHT:
		this->SetPosition(this->Position.x+Speed,Position.y);
		this->Rot = MRIGHT;
		MoveSize-=Speed;
		for(int i = 0;i < (int)(*Map).size();i++)
		{
			if(Collid.Collide((*Map)[i]))
			{
				Triger = 1;
				if((*Map)[i].Position.y > Position.y+goo)
					SetPosition(Position.x,(*Map)[i].Position.y-64);
				else if((*Map)[i].Position.y+goo  < Position.y)
					SetPosition(Position.x,(*Map)[i].Position.y+64);
				else
				{
					this->SetPosition((*Map)[i].Position.x-64,Position.y);
					MoveSize = 0;

				}
			}
		}
		for(int i = 0;i < (int)(*Bombs).size();i++)
		{
			if(Collid.Collide((*Bombs)[i].DrawImage))
			{
				Triger = 1;
				if((*Bombs)[i].Position.y > Position.y+goo)
					SetPosition(Position.x,(*Bombs)[i].Position.y-64);
				else if((*Bombs)[i].Position.y+goo  < Position.y)
					SetPosition(Position.x,(*Bombs)[i].Position.y+64);
				else
				{
					this->SetPosition((*Bombs)[i].Position.x-64,Position.y);
					MoveSize = 0;

				}
			}
		}
		if(Triger)
		{
			SetPosition(this->Position.x-Speed,Position.y);

		}
		break;
	case MDOWN:
		this->SetPosition(Position.x,this->Position.y+Speed);
		this->Rot = MDOWN;
		MoveSize-=Speed;
		for(int i = 0;i < (int)(*Map).size();i++)
		{
			if(Collid.Collide((*Map)[i]))
			{
				Triger = 1;
				if((*Map)[i].Position.x > Position.x+goo)
					SetPosition((*Map)[i].Position.x-64,Position.y);
				else if((*Map)[i].Position.x+goo < Position.x)
					SetPosition((*Map)[i].Position.x+64,Position.y);
				else
				{
					this->SetPosition(Position.x,(*Map)[i].Position.y-64);
					MoveSize = 0;

				}
			}
		}
		for(int i = 0;i < (int)(*Bombs).size();i++)
		{
			if(Collid.Collide((*Bombs)[i].DrawImage))
			{
				Triger = 1;
				if((*Bombs)[i].Position.x > Position.x+goo)
					SetPosition((*Bombs)[i].Position.x-64,Position.y);
				else if((*Bombs)[i].Position.x+goo < Position.x)
					SetPosition((*Bombs)[i].Position.x+64,Position.y);
				else
				{
					this->SetPosition(Position.x,(*Bombs)[i].Position.y-64);
					MoveSize = 0;

				}
			}
		}
		if(Triger)
		{
			SetPosition(Position.x,this->Position.y-Speed);

		}
		break;

	}
}

BALL::~BALL(void)
{
	//ENEMY::~ENEMY();
}
