#include "ScaleUpAnimator.h"

ScaleUpAnimator::ScaleUpAnimator(float scaleSeconds, float waitSeconds)
	: m_hasFinished(false)
	, m_startTime(0)
	, m_waitTime(static_cast<u32>(waitSeconds*1000))
	, m_scaleTime(static_cast<u32>(scaleSeconds*1000))
{
}


ScaleUpAnimator::~ScaleUpAnimator()
{
}


float easeInOutQuart(float t, float b, float c, float d) {
	t /= d/2;
	if (t < 1) return c/2*t*t*t*t + b;
	t -= 2;
	return -c/2 * (t*t*t*t - 2) + b;
};

void ScaleUpAnimator::animateNode(scene::ISceneNode* node, u32 timeMs)
{
	if (node && !m_hasFinished)
	{
		u32 totalElapsed = 0;
		if (m_startTime != 0) {
			totalElapsed = timeMs - m_startTime;
			if (totalElapsed > m_scaleTime) {
				totalElapsed = m_scaleTime;
				m_hasFinished = true;
			}
		} else {
			m_startTime = timeMs;
		}

		if (m_waitTime > 0){
			if (m_waitTime > totalElapsed) {
				m_waitTime -= totalElapsed;
			} else {
				m_waitTime = 0;
			}
			m_startTime = timeMs;
			totalElapsed = 0;
		}

		const float scaleY = easeInOutQuart(static_cast<float>(totalElapsed), 0, 1, static_cast<float>(m_scaleTime));
		node->setScale(core::vector3df(1,scaleY,1));
	}
}

scene::ISceneNodeAnimator* ScaleUpAnimator::createClone(scene::ISceneNode* node, scene::ISceneManager* newManager)
{
	return 0;
}
