#include "ientity.h"
#include "world.h"

#include <glm/gtx/quaternion.hpp>

static int s_entityId = 0;

IEntityBase::IEntityBase() :
	m_worldTM(1.0f),
	m_position(0.0f),
	m_rotation(0.0f),
	m_scale(1.0f),
	m_classname(nullptr),
	m_id(-1),
	m_dirtyTransform(true)
{
	m_id = s_entityId++;
}

IEntityBase::~IEntityBase()
{
}

void IEntityBase::Update(float dt)
{
}

void IEntityBase::Render()
{
}

void IEntityBase::OnCollide(IEntityBase* other)
{
}

void IEntityBase::SetPosition(const glm::vec3& pos)
{
	m_position = pos;
}

const glm::mat4& IEntityBase::GetWorldTransform()
{
	return m_worldTM;
}

void IEntityBase::UpdateTransform()
{
	if (!m_dirtyTransform)
		return;

	glm::vec3 radiansRotation = glm::vec3(glm::radians(m_rotation.x), glm::radians(m_rotation.y), glm::radians(m_rotation.z));
	glm::mat4 rotation = glm::toMat4(glm::quat(radiansRotation));

	m_worldTM = glm::mat4(1.0f);
	m_worldTM = glm::translate(m_worldTM, m_position) * rotation * glm::scale(m_worldTM, m_scale);

	m_dirtyTransform = false;
}

uint32_t IEntityBase::GetID()
{
	return m_id;
}

void IEntityBase::ResetEntityID()
{
	s_entityId = 0;
}

void IEntityBase::MarkForDelete()
{
	if (g_world)
		g_world->MarkEntityToDelete(this);
}

void IEntityBase::SetDirty()
{
	m_dirtyTransform = true;
}
