Zh/Authoring a Model Entity/Code

Authoring a Model Entity/Code
//=====版权所有©1996-2005，Valve Corporation，保留所有权利========
//
[[Authoring a Model Entity/Code]]
//目的：随机移动和改变方向的简单模型实体
//当激活时。
//=============================================================================
#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 );
	// 输入函数
	void InputToggle( inputdata_t &inputData );
private:
	bool	m_bActive;
	float	m_flNextChangeTime;
};
LINK_ENTITY_TO_CLASS( my_model_entity, CMyModelEntity );
// 开始我们对类的数据描述
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()
// 我们实体模型的名称
#define	ENTITY_MODEL	"models/gibs/airboat_broken_engine.mdl"
//-----------------------------------------------------------------------------
// 用途：实体使用的预制资产
//-----------------------------------------------------------------------------
void CMyModelEntity::Precache( void )
{
	PrecacheModel( ENTITY_MODEL );
	BaseClass::Precache();
}
//-----------------------------------------------------------------------------
// 目的：设置实体的初始状态
//-----------------------------------------------------------------------------
void CMyModelEntity::Spawn( void )
{
	Precache();
	SetModel( ENTITY_MODEL );
	SetSolid( SOLID_BBOX );
	UTIL_SetSize( this, -Vector(20,20,20), Vector(20,20,20) );
}
//-----------------------------------------------------------------------------
// 目的：使用Think函数随机移动实体
//-----------------------------------------------------------------------------
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 );
}
//-----------------------------------------------------------------------------
// 目的：切换实体的移动
//-----------------------------------------------------------------------------
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 ==
* Authoring a Model Entity
Category:Programming