Authoring a Brush Entity/Code: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
(syntax highlighting)
 
(2 intermediate revisions by 2 users not shown)
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 brush entity that moves when touched
//
//
// Purpose: Simple brush entity that moves when touched
//=============================================================================</span>
//
//=============================================================================
<span style="color:blue;">#include</span> <span style="color:brown;">"cbase.h"</span>
 
<span style="color:blue;">#include</span> <span style="color:brown;">"triggers.h"</span>
#include "cbase.h"
#include "triggers.h"
<span style="color:blue;">class</span> CMyBrushEntity : <span style="color:blue;">public</span> CBaseTrigger
 
{
class CMyBrushEntity : public CBaseTrigger
<span style="color:blue;">public</span>:
{
DECLARE_CLASS( CMyBrushEntity, CBaseTrigger );
public:
DECLARE_DATADESC();
DECLARE_CLASS( CMyBrushEntity, CBaseTrigger );
DECLARE_DATADESC();
<span style="color:blue;">void</span> Spawn();
 
void Spawn();
<span style="color:blue;">void</span> BrushTouch( CBaseEntity *pOther );
 
};
void BrushTouch( CBaseEntity *pOther );
};
LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity );
 
LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity );
BEGIN_DATADESC( CMyBrushEntity )
 
BEGIN_DATADESC( CMyBrushEntity )
<span style="color:green;">// Declare this function as being the touch function</span>
DEFINE_ENTITYFUNC( BrushTouch ),
// Declare this function as being the touch function
DEFINE_ENTITYFUNC( BrushTouch ),
END_DATADESC()
 
END_DATADESC()
<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> CMyBrushEntity::Spawn()
//-----------------------------------------------------------------------------
{
void CMyBrushEntity::Spawn()
<span style="color:green;">// We want to capture touches from other entities</span>
{
SetTouch( &CMyBrushEntity::BrushTouch );
BaseClass::Spawn();
 
<span style="color:green;">// We should collide with physics</span>
// We want to capture touches from other entities
SetSolid( SOLID_VPHYSICS );
SetTouch( &CMyBrushEntity::BrushTouch );
 
<span style="color:green;">// Use our brushmodel</span>
// We should collide with physics
SetModel( STRING( GetModelName() ) );
SetSolid( SOLID_VPHYSICS );
<span style="color:green;">// We push things out of our way</span>
// Use our brushmodel
SetMoveType( MOVETYPE_PUSH );
SetModel( STRING( GetModelName() ) );
<span style="color:green;">// Create our physics hull information</span>
// We push things out of our way
VPhysicsInitShadow( <span style="color:blue;">false</span>, <span style="color:blue;">false</span> );
SetMoveType( MOVETYPE_PUSH );
}
 
// Create our physics hull information
<span style="color:green;">//-----------------------------------------------------------------------------
VPhysicsInitShadow( false, false );
// Purpose: Move away from an entity that touched us
}
// Input  : *pOther - the entity we touched
 
//-----------------------------------------------------------------------------</span>
//-----------------------------------------------------------------------------
<span style="color:blue;">void</span> CMyBrushEntity::BrushTouch( CBaseEntity *pOther )
// Purpose: Move away from an entity that touched us
{
// Input  : *pOther - the entity we touched
<span style="color:green;">// Get the collision information</span>
//-----------------------------------------------------------------------------
<span style="color:blue;">const</span> trace_t &tr = GetTouchTrace();
void CMyBrushEntity::BrushTouch( CBaseEntity *pOther )
{
<span style="color:green;">// We want to move away from the impact point along our surface</span>
// Get the collision information
Vector vecPushDir = tr.plane.normal;
const trace_t &tr = GetTouchTrace();
vecPushDir.Negate();
 
vecPushDir.z = 0.0f;
// We want to move away from the impact point along our surface
Vector vecPushDir = tr.plane.normal;
<span style="color:green;">// Uncomment this line to print plane information to the console in developer mode
vecPushDir.Negate();
//DevMsg ( "%s (%s) touch plane's normal: [%f %f]\n", GetClassname(), GetDebugName(),tr.plane.normal.x, tr.plane.normal.y );</span>
vecPushDir.z = 0.0f;
 
<span style="color:green;">// Move slowly in that direction</span>
// Uncomment this line to print plane information to the console in developer mode
LinearMove( GetAbsOrigin() + ( vecPushDir * 64.0f ), 32.0f );
//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 );
}
</syntaxhighlight>


== See also ==
== See also ==
*[[Authoring a Brush Entity]]
*[[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 );
}

See also