/** 
 * \file MagicFlower.cpp
 * \brief Handles magic flower loading and rendering.
 * 
 * \author Petar Bajic, MPE (C) All Rights Reserved, Homepage: www.mystic-peanut.com
 * \date January, 16 2007.
 */

#include "MagicFlower.h"

/**
 * \brief Standard constructor.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
CMagicFlower::CMagicFlower()
{
	m_Model = NULL;
	m_ParticleFlame = NULL;
	m_Alive = false;
	m_Position = D3DXVECTOR3(0,0,0);
	m_Rotation = 0.0f;
	m_Color = 0xFFFFFFFF;
}

/**
 * \brief Standard destructor.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
CMagicFlower::~CMagicFlower()
{
	SAFE_DELETE(m_ParticleFlame);
	SAFE_DELETE(m_Model);
}

/**
 * \brief Loads static flower model and particle flame.
 * \param color - color of magic flame above flower
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
bool CMagicFlower::Load(HWND hWnd, LPDIRECT3DDEVICE9 pd3dDevice, WCHAR* strFilePath, WCHAR* strFileName, D3DCOLOR color)
{
	//Init Model
	m_Model = new CModelStatic();
	if(!m_Model->Load(pd3dDevice,strFilePath,strFileName))
	{
		MessageBox(hWnd,L"Could not load Media/Models/mfdouble.x file",L"Error",MB_OK);
		return false;
	}
	m_Color = color;
	//Init Flame
	m_ParticleFlame = new CFlame(30, 2.0f,m_Color);
	if(!m_ParticleFlame->init(pd3dDevice,L"Media/Pics/bullet.png"))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/bullet.png file",L"Error",MB_OK);
		return false;
	}

	return true;
}

/**
 * \brief Its important to call this function BEFORE device is reset, like when switching to/from fullscreen mode.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMagicFlower::OnLostDevice()
{
	SAFE_DELETE(m_ParticleFlame);
}

/**
 * \brief Its important to call this function AFTER device is reset, like when switching to/from fullscreen mode.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMagicFlower::OnResetDevice(LPDIRECT3DDEVICE9 pd3dDevice)
{
	m_ParticleFlame = new CFlame(30, 2.0f, m_Color);
	m_ParticleFlame->init(pd3dDevice,L"Media/Pics/bullet.png");
}

/**
 * \brief Render function renders static flower model.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMagicFlower::OnRender(LPDIRECT3DDEVICE9 pd3dDevice)
{
	if(m_Alive)
	{
		m_Model->OnRender(pd3dDevice);
		m_ParticleFlame->Render();
	}
}

/**
 * \brief Move function rotates flower model to the left and right. It also animates particle flame.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CMagicFlower::OnFrameMove(LPDIRECT3DDEVICE9 pd3dDevice, float elapsed)
{
	static bool rising = true;
	if(m_Alive)
	{
		if (rising)
		{
			m_Rotation +=3*elapsed;
			if (m_Rotation > D3DX_PI)
				rising = false;
		}
		else
		{
			m_Rotation -= 3*elapsed;
			if (m_Rotation < 0.0f)
				rising = true;
		}
		m_Model->OnFrameMove(pd3dDevice,m_Rotation,m_Position);
		m_ParticleFlame->Update(elapsed, false, false);
	}

}
