#include "asteroid.h"

#define randrange(min,max) (rand()%((max)-(min)+1)+(min))

void asteroid::init(IDirect3DDevice8** g_pD3DDevice, int r_size)
{
	float t;
	asteroid_img.Create(g_pD3DDevice,"d:\\media\\models\\asteroid.x","d:\\media\\models\\");

	this->size = r_size;

    this->visible = false;

	if(this->size == 1)
	{
		m = randrange(20,25);
		b = randrange(20,25);
		m /= 50.0f;
		b /= 50.0f;
		this->asteroid_img.MoveX(randrange(0,320)-160);
		this->asteroid_img.MoveY(randrange(0,275)-133);
	}

	if(this->size == 2)
	{
		m = randrange(10,15);
		b = randrange(10,15);
		m /= 50.0f;
		b /= 50.0f;
		this->asteroid_img.MoveX(randrange(0,247)-124);
		this->asteroid_img.MoveY(randrange(0,217)-107);

	}

	if(this->size == 3)
	{
		m = randrange(4,10);
		b = randrange(4,10);
		m /= 100.0f;
		b /= 100.0f;
		this->asteroid_img.MoveX(randrange(0,208)-104);
		this->asteroid_img.MoveY(randrange(0,175)-85);

	}

   


	
	// randomize directions;
	t = randrange(0,1);
	if(t == 1)
		this->b *= -1;

	t = randrange(0,1);
	if(t == 1)
		this->m *= -1;


}
void asteroid::reinit()
{
	float t;

	if(this->size == 1)
	{
		m = randrange(20,25);
		b = randrange(20,25);
		m /= 50.0f;
		b /= 50.0f;
	}

	if(this->size == 2)
	{
		m = randrange(10,15);
		b = randrange(10,15);
		m /= 50.0f;
		b /= 50.0f;

	}

	if(this->size == 3)
	{
		m = randrange(4,10);
		b = randrange(4,10);
		m /= 100.0f;
		b /= 100.0f;

	}

	t = randrange(0,1);
	if(t == 1)
		this->b *= -1;

	t = randrange(0,1);
	if(t == 1)
		this->m *= -1;
}
void asteroid::MoveRel(float x, float y)
{
  this->asteroid_img.MoveRel(x,y,0);
}
bool asteroid::render()
{
  if(this->visible == false)
	  return false;

	this->x = this->asteroid_img.GetXpos();
	this->y = this->asteroid_img.GetYpos();

  if(this->size == 1)
  {
	this->asteroid_img.RotateRel(.01,0,.01);

      if(this->asteroid_img.GetXpos() < -160)
			this->asteroid_img.MoveX(this->x + 320);		
	 else if (this->asteroid_img.GetXpos() > 160)
			this->asteroid_img.MoveX(this->x - 320);
		
     if(this->asteroid_img.GetYpos() < -134)
    	this->asteroid_img.MoveY(this->y + 275);
	 else if (this->asteroid_img.GetYpos() > 133)
		this->asteroid_img.MoveY(this->y - 275);

  }
  
  if(this->size == 2)
  {

	this->asteroid_img.RotateRel(0,.003,.003);

      if(this->asteroid_img.GetXpos() < -124)
			this->asteroid_img.MoveX(this->x + 247);		
	 else if (this->asteroid_img.GetXpos() > 123)
			this->asteroid_img.MoveX(this->x - 247);
		
     if(this->asteroid_img.GetYpos() < -107)
    	this->asteroid_img.MoveY(this->y + 217);
	 else if (this->asteroid_img.GetYpos() > 110)
		this->asteroid_img.MoveY(this->y - 217);

  }
  
  if(this->size == 3)
  {

	 this->asteroid_img.RotateRel(.001,.001,.001);
	
      if(this->asteroid_img.GetXpos() < -108)
			this->asteroid_img.MoveX(this->x + 208);		
	 else if (this->asteroid_img.GetXpos() > 100)
			this->asteroid_img.MoveX(this->x - 208);
		
     if(this->asteroid_img.GetYpos() < -90)
    	this->asteroid_img.MoveY(this->y + 175);
	 else if (this->asteroid_img.GetYpos() > 85)
		this->asteroid_img.MoveY(this->y - 175);
		
  }


   this->asteroid_img.MoveRel(this->m,this->b,0);
	
	this->asteroid_img.Render(this->size);
    
	return true;

}