Authoring a Brush Entity/Code: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Reverted edit of AcelcOalca, changed back to last version by Remmiz)
No edit summary
Line 1: Line 1:
[[Category:Programming]]
<span style="color:green;">//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
<pre>
//
//===== 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;">class</span> CMyBrushEntity : <span style="color:blue;">public</span> CBaseToggle
{
<span style="color:blue;">public</span>:
DECLARE_CLASS( CMyBrushEntity, CBaseToggle );
DECLARE_DATADESC();
<span style="color:blue;">void</span> Spawn();
<span style="color:blue;">bool</span> CreateVPhysics();
<span style="color:blue;">void</span> BrushTouch( CBaseEntity *pOther );
};
LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity );
BEGIN_DATADESC( CMyBrushEntity )
<span style="color:green;">// Declare this function as being a touch function</span>
DEFINE_ENTITYFUNC( BrushTouch ),
END_DATADESC()
<span style="color:green;">//-----------------------------------------------------------------------------
// Purpose: Sets up the entity's initial state
//-----------------------------------------------------------------------------</span>
<span style="color:blue;">void</span> CMyBrushEntity::Spawn()
{
<span style="color:green;">// We want to capture touches from other entities</span>
SetTouch( &CMyBrushEntity::BrushTouch );
<span style="color:green;">// We should collide with physics</span>
SetSolid( SOLID_VPHYSICS );
<span style="color:green;">// We push things out of our way</span>
SetMoveType( MOVETYPE_PUSH );
<span style="color:green;">// Use our brushmodel</span>
SetModel( STRING( GetModelName() ) );
<span style="color:green;">// Create our physics hull information</span>
CreateVPhysics();
}
<span style="color:green;">//-----------------------------------------------------------------------------
// Purpose: Setup our physics information so we collide properly
//-----------------------------------------------------------------------------</span>
<span style="color:blue;">bool</span> CMyBrushEntity::CreateVPhysics()
{
<span style="color:green;">// For collisions with physics objects</span>
VPhysicsInitShadow( <span style="color:blue;">false</span>, <span style="color:blue;">false</span> );
<span style="color:blue;">return true</span>;
}
<span style="color:green;">//-----------------------------------------------------------------------------
// 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 )
{
<span style="color:green;">// Get the collision information</span>
<span style="color:blue;">const</span> trace_t &tr = GetTouchTrace();
<span style="color:green;">// We want to move away from the impact point along our surface</span>
Vector vecPushDir = tr.plane.normal;
vecPushDir.Negate();
vecPushDir.z = 0.0f;
<span style="color:green;">// Move slowly in that direction</span>
LinearMove( GetAbsOrigin() + ( vecPushDir * 64.0f ), 32.0f );
}


#include "cbase.h"
== See also ==
*[[Authoring a Brush Entity]]


class CMyBrushEntity : public CBaseToggle
[[Category:Programming]]
{
public:
DECLARE_CLASS( CMyBrushEntity, CBaseToggle );
DECLARE_DATADESC();
 
void Spawn( void );
bool CreateVPhysics( void );
 
void BrushTouch( CBaseEntity *pOther );
};
 
LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity );
 
// Start of our data description for the class
BEGIN_DATADESC( CMyBrushEntity )
// Declare this function as being a touch function
DEFINE_ENTITYFUNC( BrushTouch ),
 
END_DATADESC()
 
//-----------------------------------------------------------------------------
// Purpose: Sets up the entity's initial state
//-----------------------------------------------------------------------------
void CMyBrushEntity::Spawn( void )
{
// We want to capture touches from other entities
SetTouch( &CMyBrushEntity::BrushTouch );
 
// We should collide with physics
SetSolid( SOLID_VPHYSICS );
// We push things out of our way
SetMoveType( MOVETYPE_PUSH );
// Use our brushmodel
SetModel( STRING( GetModelName() ) );
 
// Create our physics hull information
CreateVPhysics();
}
 
//-----------------------------------------------------------------------------
// Purpose: Setup our physics information so we collide properly
//-----------------------------------------------------------------------------
bool CMyBrushEntity::CreateVPhysics( void )
{
// For collisions with physics objects
VPhysicsInitShadow( false, false );
 
return true;
}
 
//-----------------------------------------------------------------------------
// 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;
 
// Move slowly in that direction
LinearMove( GetAbsOrigin() + ( vecPushDir * 64.0f ), 32.0f );
}
</pre>

Revision as of 14:01, 2 March 2008

//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ========
//
// Purpose: Simple brush entity that moves when touched
//
//=============================================================================

#include "cbase.h"

class CMyBrushEntity : public CBaseToggle
{
public:
	DECLARE_CLASS( CMyBrushEntity, CBaseToggle );
	DECLARE_DATADESC();

	void Spawn();
	bool CreateVPhysics();

	void BrushTouch( CBaseEntity *pOther );
};

LINK_ENTITY_TO_CLASS( my_brush_entity, CMyBrushEntity );

BEGIN_DATADESC( CMyBrushEntity )
	
	// Declare this function as being a touch function
	DEFINE_ENTITYFUNC( BrushTouch ),

END_DATADESC()

//-----------------------------------------------------------------------------
// Purpose: Sets up the entity's initial state
//-----------------------------------------------------------------------------
void CMyBrushEntity::Spawn()
{
	// We want to capture touches from other entities
	SetTouch( &CMyBrushEntity::BrushTouch );

	// We should collide with physics
	SetSolid( SOLID_VPHYSICS );
	
	// We push things out of our way
	SetMoveType( MOVETYPE_PUSH );
	
	// Use our brushmodel
	SetModel( STRING( GetModelName() ) );

	// Create our physics hull information
	CreateVPhysics();
}

//-----------------------------------------------------------------------------
// Purpose: Setup our physics information so we collide properly
//-----------------------------------------------------------------------------
bool CMyBrushEntity::CreateVPhysics()
{
	// For collisions with physics objects
	VPhysicsInitShadow( false, false );

	return true;
}

//-----------------------------------------------------------------------------
// 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;

	// Move slowly in that direction
	LinearMove( GetAbsOrigin() + ( vecPushDir * 64.0f ), 32.0f );
}

See also