Authoring a Brush Entity/Code: Difference between revisions
		
		
		
		
		
		Jump to navigation
		Jump to search
		
				
		
		
	
| AcelcOalca (talk | contribs) mNo edit summary |  (syntax highlighting) | ||
| (19 intermediate revisions by 9 users not shown) | |||
| Line 1: | Line 1: | ||
| <syntaxhighlight lang="cpp"> | |||
| //===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======== | |||
| < | |||
| //===== Copyright  | |||
| // | // | ||
| // Purpose: Simple brush entity that moves when touched | // Purpose: Simple brush entity that moves when touched | ||
| Line 9: | Line 7: | ||
| #include "cbase.h" | #include "cbase.h" | ||
| #include "triggers.h" | |||
| class CMyBrushEntity : public  | class CMyBrushEntity : public CBaseTrigger | ||
| { | { | ||
| public: | public: | ||
| 	DECLARE_CLASS( CMyBrushEntity,  | 	DECLARE_CLASS( CMyBrushEntity, CBaseTrigger ); | ||
| 	DECLARE_DATADESC(); | 	DECLARE_DATADESC(); | ||
| 	void Spawn(  | 	void Spawn(); | ||
| 	void BrushTouch( CBaseEntity *pOther ); | 	void BrushTouch( CBaseEntity *pOther ); | ||
| Line 24: | Line 22: | ||
| LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity ); | LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity ); | ||
| BEGIN_DATADESC( CMyBrushEntity ) | BEGIN_DATADESC( CMyBrushEntity ) | ||
| 	// Declare this function as being  | 	// Declare this function as being the touch function | ||
| 	DEFINE_ENTITYFUNC( BrushTouch ), | 	DEFINE_ENTITYFUNC( BrushTouch ), | ||
| Line 35: | Line 32: | ||
| // Purpose: Sets up the entity's initial state | // Purpose: Sets up the entity's initial state | ||
| //----------------------------------------------------------------------------- | //----------------------------------------------------------------------------- | ||
| void CMyBrushEntity::Spawn(  | void CMyBrushEntity::Spawn() | ||
| { | { | ||
| 	BaseClass::Spawn(); | |||
| 	// We want to capture touches from other entities | 	// We want to capture touches from other entities | ||
| 	SetTouch( &CMyBrushEntity::BrushTouch ); | 	SetTouch( &CMyBrushEntity::BrushTouch ); | ||
| Line 42: | Line 41: | ||
| 	// We should collide with physics | 	// We should collide with physics | ||
| 	SetSolid( SOLID_VPHYSICS ); | 	SetSolid( SOLID_VPHYSICS ); | ||
| 	// Use our brushmodel | |||
| 	SetModel( STRING( GetModelName() ) ); | |||
| 	// We push things out of our way | 	// We push things out of our way | ||
| 	SetMoveType( MOVETYPE_PUSH ); | 	SetMoveType( MOVETYPE_PUSH ); | ||
| 	// Create our physics hull information | 	// Create our physics hull information | ||
| 	VPhysicsInitShadow( false, false ); | 	VPhysicsInitShadow( false, false ); | ||
| } | } | ||
| Line 77: | Line 65: | ||
| 	vecPushDir.Negate(); | 	vecPushDir.Negate(); | ||
| 	vecPushDir.z = 0.0f; | 	vecPushDir.z = 0.0f; | ||
| 	// Uncomment this line to print plane information to the console in developer mode | |||
| 	//DevMsg ( "%s (%s) touch plane's normal: [%f %f]\n", GetClassname(), GetDebugName(),tr.plane.normal.x, tr.plane.normal.y ); | |||
| 	// Move slowly in that direction | 	// Move slowly in that direction | ||
| 	LinearMove( GetAbsOrigin() + ( vecPushDir * 64.0f ), 32.0f ); | 	LinearMove( GetAbsOrigin() + ( vecPushDir * 64.0f ), 32.0f ); | ||
| } | } | ||
| </ | </syntaxhighlight> | ||
| == See also == | |||
| *[[Authoring a Brush Entity]] | |||
| [[Category:Programming]] | |||
| [[Category:Tutorials]] | |||
Latest revision as of 16:40, 28 June 2011
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
//
// Purpose: Simple brush entity that moves when touched
//
//=============================================================================
#include "cbase.h"
#include "triggers.h"
class CMyBrushEntity : public CBaseTrigger
{
public:
	DECLARE_CLASS( CMyBrushEntity, CBaseTrigger );
	DECLARE_DATADESC();
	void Spawn();
	void BrushTouch( CBaseEntity *pOther );
};
LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity );
BEGIN_DATADESC( CMyBrushEntity )
	
	// Declare this function as being the touch function
	DEFINE_ENTITYFUNC( BrushTouch ),
END_DATADESC()
//-----------------------------------------------------------------------------
// Purpose: Sets up the entity's initial state
//-----------------------------------------------------------------------------
void CMyBrushEntity::Spawn()
{
	BaseClass::Spawn();
	// We want to capture touches from other entities
	SetTouch( &CMyBrushEntity::BrushTouch );
	// We should collide with physics
	SetSolid( SOLID_VPHYSICS );
	
	// Use our brushmodel
	SetModel( STRING( GetModelName() ) );
	
	// We push things out of our way
	SetMoveType( MOVETYPE_PUSH );
	// Create our physics hull information
	VPhysicsInitShadow( false, false );
}
//-----------------------------------------------------------------------------
// Purpose: Move away from an entity that touched us
// Input  : *pOther - the entity we touched
//-----------------------------------------------------------------------------
void CMyBrushEntity::BrushTouch( CBaseEntity *pOther )
{
	// Get the collision information
	const trace_t &tr = GetTouchTrace();
	// We want to move away from the impact point along our surface
	Vector	vecPushDir = tr.plane.normal;
	vecPushDir.Negate();
	vecPushDir.z = 0.0f;
	// Uncomment this line to print plane information to the console in developer mode
	//DevMsg ( "%s (%s) touch plane's normal: [%f %f]\n", GetClassname(), GetDebugName(),tr.plane.normal.x, tr.plane.normal.y );
	// Move slowly in that direction
	LinearMove( GetAbsOrigin() + ( vecPushDir * 64.0f ), 32.0f );
}