Difference between revisions of "Authoring a Model Entity/Code"

From Valve Developer Community
Jump to: navigation, search
m
(syntax highlighting)
 
Line 1: Line 1:
<span style="color:green;">//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
+
<syntaxhighlight lang="cpp">
//
+
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
// Purpose: Simple model entity that randomly moves and changes direction
+
//
// when activated.
+
// Purpose: Simple model entity that randomly moves and changes direction
//
+
// when activated.
//=============================================================================</span>
+
//
+
//=============================================================================
<span style="color:blue;">#include</span> <span style="color:brown;">"cbase.h"</span>
+
 
+
#include "cbase.h"
<span style="color:blue;">class</span> CMyModelEntity : <span style="color:blue;">public</span> CBaseAnimating
+
 
{
+
class CMyModelEntity : public CBaseAnimating
<span style="color:blue;">public</span>:
+
{
DECLARE_CLASS( CMyModelEntity, CBaseAnimating );
+
public:
DECLARE_DATADESC();
+
DECLARE_CLASS( CMyModelEntity, CBaseAnimating );
+
DECLARE_DATADESC();
CMyModelEntity()
+
 
{
+
CMyModelEntity()
m_bActive = <span style="color:blue;">false</span>;
+
{
}
+
m_bActive = false;
+
}
<span style="color:blue;">void</span> Spawn( <span style="color:blue;">void</span> );
+
 
<span style="color:blue;">void</span> Precache( <span style="color:blue;">void</span> );
+
void Spawn( void );
+
void Precache( void );
<span style="color:blue;">void</span> MoveThink( <span style="color:blue;">void</span> );
+
 
+
void MoveThink( void );
<span style="color:green;">// Input function</span>
+
 
<span style="color:blue;">void</span> InputToggle( inputdata_t &inputData );
+
// Input function
+
void InputToggle( inputdata_t &inputData );
<span style="color:blue;">private</span>:
+
 
+
private:
<span style="color:blue;">bool</span> m_bActive;
+
 
<span style="color:blue;">float</span> m_flNextChangeTime;
+
bool m_bActive;
};
+
float m_flNextChangeTime;
+
};
LINK_ENTITY_TO_CLASS( my_model_entity, CMyModelEntity );
+
 
+
LINK_ENTITY_TO_CLASS( my_model_entity, CMyModelEntity );
<span style="color:green;">// Start of our data description for the class</span>
+
 
BEGIN_DATADESC( CMyModelEntity )
+
// Start of our data description for the class
+
BEGIN_DATADESC( CMyModelEntity )
<span style="color:green;">// Save/restore our active state</span>
+
DEFINE_FIELD( m_bActive, FIELD_BOOLEAN ),
+
// Save/restore our active state
DEFINE_FIELD( m_flNextChangeTime, FIELD_TIME ),
+
DEFINE_FIELD( m_bActive, FIELD_BOOLEAN ),
+
DEFINE_FIELD( m_flNextChangeTime, FIELD_TIME ),
<span style="color:green;">// Links our input name from Hammer to our input member function</span>
+
 
DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),
+
// Links our input name from Hammer to our input member function
+
DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),
<span style="color:green;">// Declare our think function</span>
+
 
DEFINE_THINKFUNC( MoveThink ),
+
// Declare our think function
+
DEFINE_THINKFUNC( MoveThink ),
END_DATADESC()
+
 
+
END_DATADESC()
<span style="color:green;">// Name of our entity's model</span>
+
 
<span style="color:blue;">#define</span> ENTITY_MODEL <span style="color:brown;">"models/gibs/airboat_broken_engine.mdl"</span>
+
// Name of our entity's model
+
#define ENTITY_MODEL "models/gibs/airboat_broken_engine.mdl"
<span style="color:green;">//-----------------------------------------------------------------------------
+
 
// Purpose: Precache assets used by the entity
+
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------</span>
+
// Purpose: Precache assets used by the entity
<span style="color:blue;">void</span> CMyModelEntity::Precache( <span style="color:blue;">void</span> )
+
//-----------------------------------------------------------------------------
{
+
void CMyModelEntity::Precache( void )
PrecacheModel( ENTITY_MODEL );
+
{
+
PrecacheModel( ENTITY_MODEL );
BaseClass::Precache();
+
 
}
+
BaseClass::Precache();
+
}
<span style="color:green;">//-----------------------------------------------------------------------------
+
 
// Purpose: Sets up the entity's initial state
+
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------</span>
+
// Purpose: Sets up the entity's initial state
<span style="color:blue;">void</span> CMyModelEntity::Spawn( <span style="color:blue;">void</span> )
+
//-----------------------------------------------------------------------------
{
+
void CMyModelEntity::Spawn( void )
Precache();
+
{
+
Precache();
SetModel( ENTITY_MODEL );
+
 
SetSolid( SOLID_BBOX );
+
SetModel( ENTITY_MODEL );
UTIL_SetSize( <span style="color:blue;">this</span>, -Vector(20,20,20), Vector(20,20,20) );
+
SetSolid( SOLID_BBOX );
}
+
UTIL_SetSize( this, -Vector(20,20,20), Vector(20,20,20) );
+
}
<span style="color:green;">//-----------------------------------------------------------------------------
+
 
// Purpose: Think function to randomly move the entity
+
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------</span>
+
// Purpose: Think function to randomly move the entity
<span style="color:blue;">void</span> CMyModelEntity::MoveThink( <span style="color:blue;">void</span> )
+
//-----------------------------------------------------------------------------
{
+
void CMyModelEntity::MoveThink( void )
<span style="color:green;">// See if we should change direction again</span>
+
{
<span style="color:blue;">if</span> ( m_flNextChangeTime < gpGlobals->curtime )
+
// See if we should change direction again
{
+
if ( m_flNextChangeTime < gpGlobals->curtime )
<span style="color:green;">// Randomly take a new direction and speed</span>
+
{
Vector vecNewVelocity = RandomVector( -64.0f, 64.0f );
+
// Randomly take a new direction and speed
SetAbsVelocity( vecNewVelocity );
+
Vector vecNewVelocity = RandomVector( -64.0f, 64.0f );
+
SetAbsVelocity( vecNewVelocity );
<span style="color:green;">// Randomly change it again within one to three seconds</span>
+
 
m_flNextChangeTime = gpGlobals->curtime + random->RandomFloat( 1.0f, 3.0f );
+
// Randomly change it again within one to three seconds
}
+
m_flNextChangeTime = gpGlobals->curtime + random->RandomFloat( 1.0f, 3.0f );
+
}
<span style="color:green;">// Snap our facing to where we're heading</span>
+
 
Vector velFacing = GetAbsVelocity();
+
// Snap our facing to where we're heading
QAngle angFacing;
+
Vector velFacing = GetAbsVelocity();
VectorAngles( velFacing, angFacing );
+
QAngle angFacing;
  SetAbsAngles( angFacing );
+
VectorAngles( velFacing, angFacing );
+
SetAbsAngles( angFacing );
<span style="color:green;">// Think every 20Hz</span>
+
 
SetNextThink( gpGlobals->curtime + 0.05f );
+
// Think every 20Hz
}
+
SetNextThink( gpGlobals->curtime + 0.05f );
+
}
<span style="color:green;">//-----------------------------------------------------------------------------
+
 
// Purpose: Toggle the movement of the entity
+
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------</span>
+
// Purpose: Toggle the movement of the entity
<span style="color:blue;">void</span> CMyModelEntity::InputToggle( inputdata_t &inputData )
+
//-----------------------------------------------------------------------------
{
+
void CMyModelEntity::InputToggle( inputdata_t &inputData )
<span style="color:green;">// Toggle our active state</span>
+
{
<span style="color:blue;">if</span> ( !m_bActive )
+
// Toggle our active state
{
+
if ( !m_bActive )
<span style="color:green;">// Start thinking</span>
+
{
SetThink( &CMyModelEntity::MoveThink );
+
// Start thinking
+
SetThink( &CMyModelEntity::MoveThink );
SetNextThink( gpGlobals->curtime + 0.05f );
+
 
+
SetNextThink( gpGlobals->curtime + 0.05f );
<span style="color:green;">// Start moving</span>
+
SetMoveType( MOVETYPE_FLY );
+
// Start moving
+
SetMoveType( MOVETYPE_FLY );
<span style="color:green;">// Force MoveThink() to choose a new speed and direction immediately</span>
+
 
m_flNextChangeTime = gpGlobals->curtime;
+
// Force MoveThink() to choose a new speed and direction immediately
+
m_flNextChangeTime = gpGlobals->curtime;
<span style="color:green;">// Update m_bActive to reflect our new state</span>
+
 
  m_bActive = <span style="color:blue;">true</span>;
+
// Update m_bActive to reflect our new state
}
+
m_bActive = true;
<span style="color:blue;">else</span>
+
}
{
+
else
<span style="color:green;">// Stop thinking</span>
+
{
SetThink( NULL );
+
// Stop thinking
+
SetThink( NULL );
<span style="color:green;">// Stop moving</span>
+
SetAbsVelocity( vec3_origin );
+
// Stop moving
  SetMoveType( MOVETYPE_NONE );
+
SetAbsVelocity( vec3_origin );
+
  SetMoveType( MOVETYPE_NONE );
m_bActive = <span style="color:blue;">false</span>;
+
}
+
m_bActive = false;
}
+
}
 +
}
 +
</syntaxhighlight>
  
 
== See also ==
 
== See also ==

Latest revision as of 23:36, 28 June 2011

//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
//
// Purpose: Simple model entity that randomly moves and changes direction
//			when activated.
//
//=============================================================================

#include "cbase.h"

class CMyModelEntity : public CBaseAnimating
{
public:
	DECLARE_CLASS( CMyModelEntity, CBaseAnimating );
	DECLARE_DATADESC();

	CMyModelEntity()
	{
		m_bActive = false;
	}

	void Spawn( void );
	void Precache( void );

	void MoveThink( void );

	// Input function
	void InputToggle( inputdata_t &inputData );

private:

	bool	m_bActive;
	float	m_flNextChangeTime;
};

LINK_ENTITY_TO_CLASS( my_model_entity, CMyModelEntity );

// Start of our data description for the class
BEGIN_DATADESC( CMyModelEntity )
	
	// Save/restore our active state
	DEFINE_FIELD( m_bActive, FIELD_BOOLEAN ),
	DEFINE_FIELD( m_flNextChangeTime, FIELD_TIME ),

	// Links our input name from Hammer to our input member function
	DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),

	// Declare our think function
	DEFINE_THINKFUNC( MoveThink ),

END_DATADESC()

// Name of our entity's model
#define	ENTITY_MODEL	"models/gibs/airboat_broken_engine.mdl"

//-----------------------------------------------------------------------------
// Purpose: Precache assets used by the entity
//-----------------------------------------------------------------------------
void CMyModelEntity::Precache( void )
{
	PrecacheModel( ENTITY_MODEL );

	BaseClass::Precache();
}

//-----------------------------------------------------------------------------
// Purpose: Sets up the entity's initial state
//-----------------------------------------------------------------------------
void CMyModelEntity::Spawn( void )
{
	Precache();

	SetModel( ENTITY_MODEL );
	SetSolid( SOLID_BBOX );
	UTIL_SetSize( this, -Vector(20,20,20), Vector(20,20,20) );
}

//-----------------------------------------------------------------------------
// Purpose: Think function to randomly move the entity
//-----------------------------------------------------------------------------
void CMyModelEntity::MoveThink( void )
{
	// See if we should change direction again
	if ( m_flNextChangeTime < gpGlobals->curtime )
	{
		// Randomly take a new direction and speed
		Vector vecNewVelocity = RandomVector( -64.0f, 64.0f );
		SetAbsVelocity( vecNewVelocity );

		// Randomly change it again within one to three seconds
		m_flNextChangeTime = gpGlobals->curtime + random->RandomFloat( 1.0f, 3.0f );
	}

	// Snap our facing to where we're heading
	Vector velFacing = GetAbsVelocity();
	QAngle angFacing;
	VectorAngles( velFacing, angFacing );
 	SetAbsAngles( angFacing );

	// Think every 20Hz
	SetNextThink( gpGlobals->curtime + 0.05f );
}

//-----------------------------------------------------------------------------
// Purpose: Toggle the movement of the entity
//-----------------------------------------------------------------------------
void CMyModelEntity::InputToggle( inputdata_t &inputData )
{
	// Toggle our active state
	if ( !m_bActive )
	{
		// Start thinking
		SetThink( &CMyModelEntity::MoveThink );

		SetNextThink( gpGlobals->curtime + 0.05f );
		
		// Start moving
		SetMoveType( MOVETYPE_FLY );

		// Force MoveThink() to choose a new speed and direction immediately
		m_flNextChangeTime = gpGlobals->curtime;

		// Update m_bActive to reflect our new state
		m_bActive = true;
	}
	else
	{
		// Stop thinking
		SetThink( NULL );
		
		// Stop moving
		SetAbsVelocity( vec3_origin );
 		SetMoveType( MOVETYPE_NONE );
		
		m_bActive = false;
	}
}

See also