// TurretController.cpp
#include "TurretController.h"
#include "PropertySystem/Properties/Sprite.h"

void TurretController::OnInit(void)
{
	// get the distance between the point we're rotating around and the turret gun's rotation point
	m_baseCenter = m_hBaseEnt->GetCenterPos();
	m_rotationPoint = m_baseCenter;
	//m_rotationPoint.x -= 5.0f;
	m_rotationPoint.y -= 20.0f;
}

void TurretController::OnUpdate(const float deltaMs)
{
	Process::OnUpdate(deltaMs);
	if (IsTimeToUpdate(deltaMs))
	{
		//Entity* pBaseEnt = *m_hBaseEnt;
		//Sprite* pBaseSprite = (Sprite*)pBaseEnt->GetProperty("Sprite");
		Entity* pTurretEnt = *m_hTurretEnt;
		Sprite* pTurretSprite = (Sprite*)pTurretEnt->GetProperty("Sprite");
		
		// get the distance between the point we're rotating around and the turret gun's rotation point
		Vec2 distVector = m_rotationPoint - m_baseCenter;
		
		// perform rotation
		float rot = pTurretSprite->GetRotationRadians();
		rot += 0.05f;
		if (rot >= _2PI)
			rot = 0;
		pTurretSprite->SetRotationRadians(rot);
		
		float tempRot = /*_2PI -*/ rot;
		
		// rotate the vector
		distVector.x = (cos(tempRot) * distVector.x) - (sin(tempRot) * distVector.y);
		distVector.y = (sin(tempRot) * distVector.x) + (cos(tempRot) * distVector.y);

		// set the new position
		Vec2 offsetVec(pTurretSprite->GetWidth()*0.5f,pTurretSprite->GetHeight()*0.5f);
		Vec2 pos = m_baseCenter + distVector - offsetVec;
		pTurretEnt->SetPos(pos);
	}
}



