//
// Asura_Entity_AnimatedFragment.cpp
//
// Created by Richard Rice - 02/03/04
// 
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Entity/Ephemera/Asura_Entity_AnimatedFragment.h"

#include "Animation/Asura_Animation.h"
#include "Core/Asura_Timers.h"

Asura_Entity_AnimatedFragment::Asura_Entity_AnimatedFragment(Asura_Hash_ID uSkinHash,Asura_Hash_ID uAnimHash,const Asura_Vector_3& xPos,const Asura_Matrix_3x3& xOrient)
{
	m_fFadeTimer = 1.0f;

	//setup the shape instance
	m_xShapeInstance.SetSkinAndAnimID(uSkinHash,uAnimHash,0);
	m_xShapeInstance.SetNewPosAndOri(xPos,xOrient);

	// add to the correct lists
	AddToUpdateableClientList(this);	
}

Asura_Entity_AnimatedFragment::~Asura_Entity_AnimatedFragment()
{
	RemoveFromUpdateableClientList(this);
}

void Asura_Entity_AnimatedFragment::Update()
{
	//see if the animation has finished
	const Asura_Animation* pxAnim = m_xShapeInstance.GetAnimation();
	if(pxAnim && pxAnim->m_xPrimaryWrapper.GetPrimaryAnimHashID())
	{
		if(pxAnim->m_xPrimaryWrapper.GetPrimaryTimer() == 1.0f)
		{
			//anim has finished , do fade out
			m_fFadeTimer -= Asura_Timers::GetGameTimeSlice();
			if(m_fFadeTimer<0) m_fFadeTimer = 0;
		}
	}
	else
	{
		//no anim , so just kill this now
		m_fFadeTimer = 0;
	}

	if(m_fFadeTimer > 0)
	{
		m_xShapeInstance.SetAlpha(Asura_Maths::Min(1.0f, m_fFadeTimer));
	}
	else
	{
		SetAsDestroyed();
	}
}

void Asura_Entity_AnimatedFragment::Render()
{
	m_xShapeInstance.Render();
}
