#include "Block.hpp"
#include "Ball.hpp"

Block::Block(Game* g, bool p_destructable):
	Entity(g),
	destructable(p_destructable),
	rotationCounter(rand()%1000/100.0f),//random start rotation
	Z(-4 + float(rand()%1500)/1000.f),
	animationCounter(0),
	v(0)
{
}


bool Block::isDestructable()
{
	return destructable;
}

void Block::onTouch(Entity* Toucher)
{
	if(dynamic_cast<Ball*>(Toucher))
	{
		onHit();
	}
}

void Block::onTick(float time)
{
	rotationCounter+=time*3.0f;

	if(Z < 0)
		v += 0.2*time;
	else if(Z > 0)
		v -= 0.5*time;
	Z += v;
	animationCounter += time;
}

void Block::Render()
{
	const float MaxRotation=4.0f;

	glPushMatrix();

	glTranslatef(position.x, position.y, Z * exp(-animationCounter*1.5));
	glRotatef(sin(rotationCounter*0.80f)*MaxRotation, 0, 1, 0);
	glRotatef(sin(rotationCounter*0.53f)*MaxRotation, 1, 0, 0);

	if(m_Model)
		m_Model->RenderModel();

	glPopMatrix();
}
