//===========================================================================//
// File:	Decal.cpp																			  //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 02/10/99  DPB  Created File
//---------------------------------------------------------------------------//
// Copyright (C) 1995-98, Virtual World Entertainment, Inc.                  //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "MW4Headers.hpp"

#include "Decal.hpp"

#include <Adept\Tool.hpp>

//#############################################################################
//###############################    Decal    ###############################
//#############################################################################

Decal::ClassData*
	Decal::DefaultData = NULL;

SortedChainOf<Decal*, Time>*
	Decal::decalSocket = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Decal::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			DecalClassID,
	      "MechWarrior4::Decal",
			Entity::DefaultData,
			0, NULL,
			(Entity::Factory)Make,
			(Entity::CreateMessage::Factory)CreateMessage::ConstructCreateMessage,
			ExecutionStateEngine::DefaultData,
			(Entity::GameModel::Factory)GameModel::ConstructGameModel,
			NULL,
			(Entity::GameModel::ReadAndVerifier)GameModel::ReadAndVerify,
			(Entity::GameModel::ModelWrite)GameModel::WriteToText,
			(Entity::GameModel::ModelSave)GameModel::SaveGameModel
		);
	Check_Object(DefaultData);

	decalSocket = 
			new SortedChainOf<Decal *, Time>(NULL, false);
	Check_Pointer(decalSocket);
	Check_Object(decalSocket);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Decal::TerminateClass()
{
	Check_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;

	decalSocket->DeletePlugs();
	Check_Object(decalSocket);
	delete decalSocket;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Decal*
	Decal::Make(
		CreateMessage *message,
		ReplicatorID *base_id
	)
{
	Check_Object(message);
	gos_PushCurrentHeap(Heap);
	Decal *new_entity = 
		new Decal(DefaultData, message, base_id, NULL);
	gos_PopCurrentHeap();
	Check_Object(new_entity);
	return new_entity;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Decal::Decal(
	ClassData *class_data,
	CreateMessage *message,
	ReplicatorID *base_id,
	ElementRenderer::Element *element
):
	Entity(class_data, message, base_id, element)
{
	Check_Pointer(this);
	Check_Object(message);

	SortedChainIteratorOf<Decal *, Time> iterator(decalSocket);
	if(iterator.GetSize() >= 50)
	{
		iterator.First();
		iterator.Remove();
	}
	decalSocket->AddValue(this, gos_GetElapsedTime());
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Decal::TestInstance() const
{
	Verify(IsDerivedFrom(DefaultData));
}