//===== Copyright © 1996-2005, Sander Corporation, All rights reserved. ========
//
// Purpose: 
//
//=============================================================================

#include "cbase.h"
#include "Sprite.h"
#include "hl2wars/rts_shareddefs.h"

class CEnvRTSObjective : public CPointEntity, public IGameEventListener2 
{
public:
	DECLARE_CLASS( CEnvRTSObjective , CPointEntity );
	DECLARE_DATADESC();
	DECLARE_SERVERCLASS();  // make this entity networkable

	// Constructor
	CEnvRTSObjective ( void );
	~CEnvRTSObjective ( void );
	virtual void Spawn();
	virtual void FireGameEvent( IGameEvent *event);

	int UpdateTransmitState()	// set transmit filter to transmit always
	{
		return SetTransmitState( FL_EDICT_PVSCHECK );
	}

private:
	CNetworkVar( int, m_iObjective );
	EHANDLE m_hSprite;
};

LINK_ENTITY_TO_CLASS( env_rts_objective, CEnvRTSObjective  );

BEGIN_DATADESC( CEnvRTSObjective  )
	DEFINE_KEYFIELD( m_iObjective, FIELD_INTEGER, "Objective" ),
	DEFINE_FIELD( m_hSprite, FIELD_EHANDLE ),
END_DATADESC()

IMPLEMENT_SERVERCLASS_ST( CEnvRTSObjective, DT_EnvRTSObjective )
	SendPropInt		(SENDINFO(m_iObjective), 4, SPROP_UNSIGNED ),
END_SEND_TABLE()

CEnvRTSObjective::CEnvRTSObjective ( void )
{
	gameeventmanager->AddListener( this, "objective_updated", false );
}

CEnvRTSObjective::~CEnvRTSObjective ( void )
{
	gameeventmanager->RemoveListener(this);
}

void CEnvRTSObjective::Spawn()
{
	BaseClass::Spawn();

	// create a sprite
	m_hSprite = CSprite::SpriteCreate( "VGUI/icons/objectivebig_icon.vmt", GetAbsOrigin(), false );
	CSprite *pSprite = (CSprite *)m_hSprite.Get();
	if ( pSprite )
	{
		pSprite->SetTransparency( kRenderTransAlpha, 255, 255, 255, 255, kRenderFxNoDissipation );
		pSprite->SetBrightness( 255, 0.5f );
		pSprite->SetScale( 0.75f, 0.0f );
		//pSprite->TurnOn();
		pSprite->TurnOff();
	}
}

//-----------------------------------------------------------------------------
// Purpose: Game event
//-----------------------------------------------------------------------------
void CEnvRTSObjective::FireGameEvent( IGameEvent *event )
{
	const char * type = event->GetName();

	if( Q_strcmp(type, "objective_updated") == 0 )
	{
		int objective = event->GetInt("objectiveID");
		if( objective != m_iObjective )
			return;

		CSprite *pSprite = (CSprite *)m_hSprite.Get();
		if ( !pSprite )
			return;

		switch( event->GetInt("type")  )
		{
		case OBJECTIVE_COMPLETED:
		case OBJECTIVE_FAILED:
		case OBJECTIVE_HIDE:
			pSprite->TurnOff();
			break;
		case OBJECTIVE_SETVISIBLE:
			pSprite->TurnOn();
			break;
		default:
			break;
		}
	}
}