#include "Bullet.h"
#include "MainState.h"

GameImage Bullet::m_bullet;

Bullet::Bullet(MainState *mainState) : GameObject(mainState)
{
	m_bullet.Create(GetGameState()->GetWindow()->GetGraphics(), GetResource(RC_Bullet));
	Reset();
	radius = 7.5f;
}

Bullet::~Bullet(void)
{
	m_bullet.Destroy();
}

void Bullet::Reset()
{
	int j = rand() % 4 + 1;
	float _moveangle = 0;
	switch(j)
	{
	case UP:
		x = rand() % 400;
		y = -7.4f;
		_moveangle = (rand() % 180);
		break;
	case DOWN:
		x = rand() % 400;
		y = 407.4f;
		_moveangle = (rand() % 180) - 180;
		break;
	case LEFT:
		x = -7.4f;
		y = rand() % 400;
		_moveangle = (rand() % 180) + 90;
		break;
	case RIGHT:
		x = 407.4f;
		y = rand() % 400;
		_moveangle = (rand() % 180) + 270;
		break;
	}
	moveangle = (_moveangle / 360) * D3DX_PI;
}

void Bullet::Update(float dt)
{
	x += cosf(moveangle) * 1.175f;
	y += sinf(moveangle) * 1.175f;

	if(x < -7.5f || x > 407.5f || y < -7.5f || y > 407.5f)
		Reset();
}

void Bullet::Draw(float dt)
{
	m_bullet.Draw(x - 7.5f, y - 7.5f);
}