#include <windows.h>
#include "sprite.h"

//-----------------------------------------------------------------------------
CEffectSprite::CEffectSprite()
{
	m_classType			= eSpriteClass_Effect;
	m_pRelative			= NULL;
	m_offset.m_x		= 0.0f;
	m_offset.m_y		= 0.0f;
	m_offset.m_z		= 0.0f;
	m_attachPoint		= eAttach_Null;
	m_effectMotionStep	= eEffectMotion_Step_Null;

	m_pBegin			= NULL;
	m_beginAttachPoint	= eAttach_Null;
	m_isInitialPos		= false;

	m_magicId			= 0;
	m_damageHp			= 0;
	m_damageMp			= 0;
}

//-----------------------------------------------------------------------------
CEffectSprite::~CEffectSprite()
{
	int a = 0;
}

//-----------------------------------------------------------------------------
void CEffectSprite::ReceiveDestruct( IBaseSprite* pSprite )
{
	if ( pSprite == m_pRelative )
	{
		m_pRelative = NULL;
		SetDestructTime( 0.5f );
	}
}

//-----------------------------------------------------------------------------
void CEffectSprite::Update( float elapseTime )
{
	CRuVector3 pos;

	if ( m_isInitialPos )
	{
		m_isInitialPos = false;

		if ( m_pBegin )
		{
			
			if ( !m_pBegin->GetAttachPos(m_beginAttachPoint, pos) )
				pos = m_pBegin->GetPos();

			// 初始化特效物件座標
			SetPos( pos );
		}
	}

	if ( m_pRelative )
	{
		if ( !m_pRelative->GetAttachPos(m_attachPoint, pos) )
			pos = m_pRelative->GetPos();
	}
	else
		pos = m_nowPos;

	// 設定特效物件座標
	SetEffectPos( pos + m_offset, elapseTime );
	
	switch ( m_effectMotionStep )
	{
	case eEffectMotion_Step_Appear:
		if ( IsMotionComplete() || m_effectStepTime > 5.0f )
			SetEffectMotionStep( eEffectMotion_Step_Idle );
		break;

	case eEffectMotion_Step_Idle:
		break;

	case eEffectMotion_Step_Disappear:
		if ( IsMotionComplete() || m_effectStepTime > 5.0f )
			SetEffectMotionStep( eEffectMotion_Step_Dead );
		break;

	case eEffectMotion_Step_Explode:
		if ( m_effectStepTime == 0.0f )
			MotionEvent( ruENTITYEVENT_HIT );

		if ( IsMotionComplete() || m_effectStepTime > 5.0f )
			SetEffectMotionStep( eEffectMotion_Step_Dead );
		break;

	case eEffectMotion_Step_Dead:
		break;
	}

	m_effectStepTime += elapseTime;
	IBaseSprite::Update( elapseTime );
}

//-----------------------------------------------------------------------------
void CEffectSprite::MotionEvent( int type )
{
	switch ( type )
	{
	case ruENTITYEVENT_HIT:		
		break;
	}
}

//-----------------------------------------------------------------------------
void CEffectSprite::BeginSprite( IBaseSprite* pSprite, int point )
{
	m_isInitialPos		= true;
	m_pBegin			= pSprite;
	m_beginAttachPoint	= point;
}

//-----------------------------------------------------------------------------
void CEffectSprite::SetEffectMotionStep( int step )
{	
	m_effectMotionStep = step;
	m_effectStepTime = 0.0f;

	switch ( m_effectMotionStep )
	{
	case eEffectMotion_Step_Appear:		
		if ( !PlayMotion( eMotion_Effect_Appear ) )
			SetEffectMotionStep( eEffectMotion_Step_Idle );
		break;

	case eEffectMotion_Step_Idle:
		if ( !PlayMotion( eMotion_Effect_Idle ) )
			SetEffectMotionStep( eEffectMotion_Step_Disappear );
		break;

	case eEffectMotion_Step_Disappear:
		if ( !PlayMotion( eMotion_Effect_Disappear ) )
			SetEffectMotionStep( eEffectMotion_Step_Dead );
		break;

	case eEffectMotion_Step_Explode:
		if ( !PlayMotion( eMotion_Effect_Explode ) )
			SetEffectMotionStep( eEffectMotion_Step_Dead );		
		break;

	case eEffectMotion_Step_Dead:
		SetDestructTime( 1.0f );
		break;

	default:
		return;
	}
}

//-----------------------------------------------------------------------------
void CEffectSprite::SetMagicInfo( int id, int hp, int mp )
{
	m_magicId = id;
	m_damageHp = hp;
	m_damageMp = mp;
}

//-----------------------------------------------------------------------------
CLineEffectSprite::CLineEffectSprite()
{
	m_classType			= eSpriteClass_Effect_Line;
	m_flyDelta			= 0.0f;
	m_pExplode			= NULL;
}

//-----------------------------------------------------------------------------
CLineEffectSprite::~CLineEffectSprite()
{
	ReleaseExplode();
}

//-----------------------------------------------------------------------------
void CLineEffectSprite::ReceiveDestruct( IBaseSprite* pSprite )
{
	if ( pSprite == m_pExplode )
		m_pExplode = NULL;

	CEffectSprite::ReceiveDestruct( pSprite );
}

//-----------------------------------------------------------------------------
void CLineEffectSprite::BeginSprite( IBaseSprite* pSprite, int point )
{
	m_flyDelta = 0.0f;
	CEffectSprite::BeginSprite( pSprite, point );
}

//-----------------------------------------------------------------------------
void CLineEffectSprite::SetEffectMotionStep( int step )
{
	CEffectSprite::SetEffectMotionStep( step );
	if ( m_effectMotionStep == eEffectMotion_Step_Dead && m_pExplode )
		SetDestructTime( 10.0f );
}

//-----------------------------------------------------------------------------
void CLineEffectSprite::AttachExplodeEffect( CEffectSprite* pExplode )
{
	ReleaseExplode();

	if ( pExplode )
	{
		m_pExplode = pExplode;
		m_pExplode->SetPauseTime( 10000.0f );
	}
}

//-----------------------------------------------------------------------------
void CLineEffectSprite::ReleaseExplode()
{
	if ( m_pExplode )
	{
		delete m_pExplode;
		m_pExplode = NULL;
	}
}

//-----------------------------------------------------------------------------
void CLineEffectSprite::SetEffectPos( CRuVector3& pos, float elapseTime )
{
	CRuVector3 dir = m_nowPos - pos;
	float dis = dir.Magnitude();

	if ( dis > 8.0f )
	{
		float move = (m_moveSpeed + m_flyDelta) * elapseTime;
		m_flyDelta += elapseTime * 64;
		
		SetDirection( dir );
		SetPos(m_nowPos - dir * (move / dis));
	}
	else
	{
		SetPos( pos );
		if ( m_effectMotionStep != eEffectMotion_Step_Disappear )
		{
			SetEffectMotionStep( eEffectMotion_Step_Disappear );
			if ( m_pExplode )
				m_pExplode->SetPauseTime( 0.0f );
		}
	}
}