//==========================================================================//
// File:	 gosFX_DebrisCloud.hpp											//
// Project:  gosFX					                                        //
// Contents: DebrisCloud Component											//
//--------------------------------------------------------------------------//
// Date      Who    Modification                                            //
// 09/23/99  MG     Created													//
//																			//
//--------------------------------------------------------------------------//
// Copyright (C) 1997-1998, Virtual World Entertainment, Inc.               //
// All Rights reserved worldwide                                            //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL              //
//==========================================================================//
//
#pragma once

#include "gosFX.hpp"
#include "SpinningCloud.hpp"
#include <MLR\MLR.hpp>

namespace MidLevelRenderer {class MLRShape;}

namespace gosFX
{
	//############################################################################
	//#######################  DebrisCloud__Specification  #######################
	//############################################################################

	class DebrisCloud;

	class DebrisCloud__Specification:
		public Effect__Specification
	{
		friend class DebrisCloud;

	//----------------------------------------------------------------------
	// Constructors/Destructors
	//
	protected:
		DebrisCloud__Specification(
			Stuff::MemoryStream *stream,
			int gfx_version
		);

	public:
		typedef Effect__Specification BaseClass;

		DebrisCloud__Specification();
		~DebrisCloud__Specification();

		void
			Save(Stuff::MemoryStream *stream);

		static DebrisCloud__Specification*
			Make(
				Stuff::MemoryStream *stream,
				int gfx_version
			);

		void
			Copy(DebrisCloud__Specification *spec);

		void
			LoadGeometry(Stuff::MemoryStream *stream);

		void 
			BuildDefaults();
	
		virtual bool 
			IsDataValid(bool fix_data=false);

	//-------------------------------------------------------------------------
	// FCurves
	//
	public:
		ConstantCurve
			m_minimumDeviation;
		SplineCurve
			m_maximumDeviation;
		SeededCurveOf<ComplexCurve, ComplexCurve,Curve::e_ComplexComplexType>
			m_startingSpeed;
		SeededCurveOf<ComplexCurve, SplineCurve,Curve::e_ComplexSplineType>
			m_pLifeSpan;
		SeededCurveOf<ConstantCurve, LinearCurve,Curve::e_ConstantLinearType>
			m_pEtherVelocityY;
		SeededCurveOf<SplineCurve, LinearCurve,Curve::e_SplineLinearType>
			m_pAccelerationY;
		SeededCurveOf<ComplexCurve, ComplexCurve,Curve::e_ComplexComplexType>
			m_pDrag;
		SeededCurveOf<ComplexCurve, LinearCurve,Curve::e_ComplexLinearType>
			m_pAlpha;
		SeededCurveOf<ConstantCurve, LinearCurve,Curve::e_ConstantLinearType>
			m_pSpin;

	//-------------------------------------------------------------------------
	// Data
	//
		Stuff::Point3D
			m_centerOfForce;

		Stuff::DynamicArrayOf<MidLevelRenderer::MLRShape*>
			m_debrisPieces;
		Stuff::DynamicArrayOf<Stuff::Point3D>
			m_debrisPositions;
		Stuff::DynamicArrayOf<Stuff::Sphere>
			m_debrisSpheres;
		Stuff::DynamicArrayOf<Stuff::Scalar>
			m_debrisSeed;

		Stuff::MString
			m_shapeFile;
	};

	//############################################################################
	//#########################  DebrisCloud__Particle  #########################
	//############################################################################

	class DebrisCloud__Particle
	{
	public:
		Stuff::LinearMatrix4D
			m_localToParent;

		Stuff::Scalar
			m_age,
			m_ageRate,
			m_seed;

		Stuff::Vector3D
			m_angularVelocity,
			m_linearVelocity;

		Stuff::Scalar
			m_alpha;

		void
			TestInstance() const
				{}
	};

//############################################################################
//#############################  DebrisCloud  ################################
//############################################################################

	class DebrisCloud:
		public Effect
	{
	//----------------------------------------------------------------------------
	// Class Registration Support
	//
	public:
		typedef Effect BaseClass;

		static void InitializeClass();
		static void	TerminateClass();

		typedef DebrisCloud__Specification Specification;
		typedef DebrisCloud__Particle Particle;

	//----------------------------------------------------------------------------
	// Class Data Support
	//
	protected:
		DebrisCloud(
			Specification *spec,
			unsigned flags
		);

	public:
		static DebrisCloud*
			Make(
				Specification *spec,
				unsigned flags
			);

		Specification*
			GetSpecification()
				{
					Check_Object(this);
					return
						Cast_Object(Specification*, m_specification);
				}
		Particle*
			GetParticle(unsigned index)
				{
					Check_Object(this); Check_Object(GetSpecification());
					return
						Cast_Pointer(
							Particle*,
							&m_debrisPieces[index]
						);
				}


		static ClassData
			*DefaultData;

	//----------------------------------------------------------------------------
	// Testing
	//
	public:
		void
			TestInstance() const;

	//----------------------------------------------------------------------------
	// API
	//
	protected:
		bool
			Execute(ExecuteInfo *info);
		bool
			AnimateParticle(
				unsigned index,
				const Stuff::LinearMatrix4D *world_to_new_local,
				Stuff::Time till
			);
		virtual void
			DestroyParticle(unsigned index);
		void
			ComputeNewLinearVelocity(
				Particle *particle,
				Stuff::Scalar time_slice
			);

	public:
		void
			Start(ExecuteInfo *info);
		void
			Kill();
		bool
			HasFinished();
		void
			Draw(DrawInfo *info);

	protected:
		Stuff::DynamicArrayOf<DebrisCloud__Particle>
			m_debrisPieces;
	};
}
