#include "stdafx.h"
#include "refCMAct揰僼儔僢僔儏丒夞揮塇.h"
#include "..\嫟捠\refUtils.h"
#include "Direct3D9\valVector2.h"
#include "Direct3D9\valVector3.h"
#include "Direct3D9\CDirect3DNative.h"

namespace FDK
{
// public:

	void CMAct揰僼儔僢僔儏丒夞揮塇::t2D敪壩( Point pt拞墰px )
	{
		D3DVIEWPORT9 Viewport = CDirect3DNative::GetViewPort();

		this->t3D敪壩( 
			(float) ( pt拞墰px.X - Viewport.Width / 2.0f ),
 			(float) -( pt拞墰px.Y - Viewport.Height / 2.0f),
			0.0f );
	}
	void CMAct揰僼儔僢僔儏丒夞揮塇::t2D敪壩( Vector2 v2拞墰px )
	{
		D3DVIEWPORT9 Viewport = CDirect3DNative::GetViewPort();

		this->t3D敪壩( 
			(float) ( v2拞墰px.X - Viewport.Width / 2.0f ),
 			(float) -( v2拞墰px.Y - Viewport.Height / 2.0f),
			0.0f );
	}
	void CMAct揰僼儔僢僔儏丒夞揮塇::t2D敪壩( int n拞墰Xpx, int n拞墰Ypx )
	{
		D3DVIEWPORT9 Viewport = CDirect3DNative::GetViewPort();

		this->t3D敪壩( 
			(float) ( n拞墰Xpx - Viewport.Width / 2.0f ),
 			(float) -( n拞墰Ypx - Viewport.Height / 2.0f),
			0.0f );
	}
	void CMAct揰僼儔僢僔儏丒夞揮塇::t3D敪壩( float f拞墰X, float f拞墰Y, float f拞墰Z )
	{
		this->t3D敪壩( Vector3( f拞墰X, f拞墰Y, f拞墰Z ) );
	}
	void CMAct揰僼儔僢僔儏丒夞揮塇::t3D敪壩( Vector3 v3拞墰 )
	{
		float n妏搙 = ( float ) this->Random->Next( 360 );

		for( int n塇斣崋 = 0; n塇斣崋 < 8; n塇斣崋++ )	// 0乣3:戝塇丄4乣7:彫塇
		{
			for( int i = 0; i < this->n塇偺嵟戝枃悢; i++ )
			{
				if( !this->st塇[ i ].b巊梡拞 )
				{
					this->st塇[ i ].b巊梡拞 = true;
					this->st塇[ i ].ct恑峴 = gcnew CCounter( 0, 70, 3 );
					this->st塇[ i ].f夞揮扨埵 = Utils::DegreeToRadian( (float) ( n妏搙 + ( n塇斣崋 * 90.0f ) ) );
					this->st塇[ i ].f夞揮曽岦 = ( n塇斣崋 < 4 ) ? 1.0f : -2.0f;
					this->st塇[ i ].f僒僀僘 = ( n塇斣崋 < 4 ) ? 1.0f : 0.5f;
					this->st塇[ i ].v3拞墰 = v3拞墰;
					break;
				}
			}
		}
	}

	CMAct揰僼儔僢僔儏丒夞揮塇::CMAct揰僼儔僢僔儏丒夞揮塇( int n僼儔僢僔儏偺嵟戝摨帪敪壩悢 )
	{
		this->Random = gcnew System::Random( DateTime::Now.Millisecond );
		this->n塇偺嵟戝枃悢 = n僼儔僢僔儏偺嵟戝摨帪敪壩悢 * 8;		// 1敪壩偵偮偒8枃塇
		this->st塇 = gcnew array<ST塇>( this->n塇偺嵟戝枃悢 );
	}

	void CMAct揰僼儔僢僔儏丒夞揮塇::On妶惈壔()
	{
		// st塇[] 偺撪梕傪儕僙僢僩丅

		for( int i = 0; i < this->n塇偺嵟戝枃悢; i++ )
		{
			this->st塇[ i ].b巊梡拞 = false;
			this->st塇[ i ].ct恑峴 = gcnew CCounter();
		}
	}
	void CMAct揰僼儔僢僔儏丒夞揮塇::On旕妶惈壔()
	{
		for( int i = 0; i < this->n塇偺嵟戝枃悢; i++ )
			SAFE_REF_DELETE( this->st塇[ i ].ct恑峴 );
	}
	int CMAct揰僼儔僢僔儏丒夞揮塇::On恑峴()
	{
		for( int i = 0; i < this->n塇偺嵟戝枃悢; i++ )
		{
			if( this->st塇[ i ].b巊梡拞 )
			{
				if( this->st塇[ i ].ct恑峴->t恑峴()->b廔椆抣偵払偟偨 )
				{
					this->st塇[ i ].ct恑峴->t掆巭();
					this->st塇[ i ].b巊梡拞 = false;
				}
			}
		}

		return 0;
	}
	void CMAct揰僼儔僢僔儏丒夞揮塇::On昤夋( IntPtr hDevice, CTexture^ tx塇 )
	{
		if( nullptr == tx塇 )
			return;

		for( int i = 0; i < this->n塇偺嵟戝枃悢; i++ )
		{
			if( !this->st塇[ i ].b巊梡拞 )
				continue;

			float f恑峴妱崌0to1 = ( (float) this->st塇[ i ].ct恑峴->n尰嵼偺抣 ) / 70.0f;
			float fZ幉夞揮棪 = this->st塇[ i ].f夞揮扨埵 + ( this->st塇[ i ].f夞揮曽岦 * Utils::DegreeToRadian( (float) ( 60.0f * f恑峴妱崌0to1 ) ) );
			float fX曽岦奼戝棪 = ( (float) ( 0.2 + ( 0.8 * Math::Cos( ( ( (double) this->st塇[ i ].ct恑峴->n尰嵼偺抣 ) / 50.0 ) * ( Math::PI / 2.0 ) ) ) ) ) * this->st塇[ i ].f僒僀僘;
			float f堏摦検 = ( (float) ( 0.8 * Math::Sin( f恑峴妱崌0to1 * ( Math::PI / 2.0 ) ) ) ) * this->st塇[ i ].f僒僀僘;

			Matrix mat = Matrix::Identity;

			mat *= Matrix::Scaling( 0.2f + fX曽岦奼戝棪, 0.2f + this->st塇[ i ].f僒僀僘, 1.0f );
			mat *= Matrix::RotationZ( fZ幉夞揮棪 + (float) ( Math::PI / 2.0 ) );
			mat *= Matrix::Translation(
				this->st塇[ i ].v3拞墰.X + (float) ( Math::Cos( (double) fZ幉夞揮棪 ) * f堏摦検 ),
				this->st塇[ i ].v3拞墰.Y + (float) ( Math::Sin( (double) fZ幉夞揮棪 ) * f堏摦検 ),
				this->st塇[ i ].v3拞墰.Z );

			if( 0 == tx塇->list昤夋斖埻->Count )
				tx塇->t3D昤夋( hDevice, mat );
			else
				tx塇->t3D昤夋( hDevice, mat, tx塇->list昤夋斖埻[ 0 ] );
		}
	}
}