//====== Copyright © 1996-2003, Valve Corporation, All rights reserved. =======
//
// Purpose: Entity to project an unlit texture on the ground
//
//=============================================================================

#include "cbase.h"
#include "shareddefs.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

#define ENV_PROJECTEDTEXTURE_STARTON			(1<<0)

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
class CEnvProjectedTextureUnlit : public CBaseEntity
{
	DECLARE_CLASS( CEnvProjectedTextureUnlit, CBaseEntity );
public:
	DECLARE_DATADESC();
	DECLARE_SERVERCLASS();

	CEnvProjectedTextureUnlit();

	// Always transmit to clients
	virtual int UpdateTransmitState();
	virtual void Activate( void );

	void InputTurnOn( inputdata_t &inputdata );
	void InputTurnOff( inputdata_t &inputdata );
	void InputSetProjectedTexture( inputdata_t &inputdata );


private:
	CNetworkVar( bool, m_bState );
	CNetworkString( m_ProjectedTextureName, MAX_PATH );
	CNetworkVar( int, m_nProjectedTextureFrame );
	string_t m_ProjectedTextureNameTemp;
};

LINK_ENTITY_TO_CLASS( env_projectedtextureunlit, CEnvProjectedTextureUnlit );

BEGIN_DATADESC( CEnvProjectedTextureUnlit )
	DEFINE_FIELD( m_bState, FIELD_BOOLEAN ),
	DEFINE_AUTO_ARRAY(m_ProjectedTextureName, FIELD_CHARACTER),
	//DEFINE_AUTO_ARRAY_KEYFIELD( m_ProjectedTextureName, FIELD_CHARACTER, "texturename" ),
	//DEFINE_KEYFIELD( m_ProjectedTextureName, FIELD_STRING, "texturename"),
	DEFINE_KEYFIELD( m_nProjectedTextureFrame, FIELD_INTEGER, "textureframe" ),
	DEFINE_KEYFIELD( m_ProjectedTextureNameTemp,	  FIELD_STRING,  "texturename" ),

	DEFINE_INPUTFUNC( FIELD_VOID, "TurnOn", InputTurnOn ),
	DEFINE_INPUTFUNC( FIELD_VOID, "TurnOff", InputTurnOff ),
	DEFINE_INPUTFUNC( FIELD_STRING, "ProjectedTexture", InputSetProjectedTexture ),
END_DATADESC()

IMPLEMENT_SERVERCLASS_ST( CEnvProjectedTextureUnlit, DT_EnvProjectedTextureUnlit )
	SendPropBool( SENDINFO( m_bState ) ),
	SendPropString( SENDINFO( m_ProjectedTextureName ) ),
	SendPropInt( SENDINFO( m_nProjectedTextureFrame ) ),
END_SEND_TABLE()

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CEnvProjectedTextureUnlit::CEnvProjectedTextureUnlit( void )
{
	m_bState = false;

	m_ProjectedTextureNameTemp = NULL_STRING;
	m_ProjectedTextureName.GetForModify()[0] = 0;

	m_nProjectedTextureFrame = 0;

}

void CEnvProjectedTextureUnlit::InputTurnOn( inputdata_t &inputdata )
{
	m_bState = true;
}

void CEnvProjectedTextureUnlit::InputTurnOff( inputdata_t &inputdata )
{
	m_bState = false;
}

void CEnvProjectedTextureUnlit::InputSetProjectedTexture( inputdata_t &inputdata )
{
	Q_strcpy( m_ProjectedTextureName.GetForModify(), inputdata.value.String() );
}

void CEnvProjectedTextureUnlit::Activate( void )
{
	if ( GetSpawnFlags() & ENV_PROJECTEDTEXTURE_STARTON )
	{
		m_bState = true;
	}

	Q_strncpy( m_ProjectedTextureName.GetForModify(), STRING( m_ProjectedTextureNameTemp ), MAX_PATH );
	//m_ProjectedTextureName.GetForModify();

	//SetThink( &CEnvProjectedTextureUnlit::InitialThink );
	//SetNextThink( gpGlobals->curtime + 0.1f );

	BaseClass::Activate();
}

int CEnvProjectedTextureUnlit::UpdateTransmitState()
{
	return SetTransmitState( FL_EDICT_ALWAYS );
}

