Authoring a Brush Entity/Code: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:
   
   
  <span style="color:blue;">#include</span> <span style="color:brown;">"cbase.h"</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>
   
   
  <span style="color:blue;">class</span> CMyBrushEntity : <span style="color:blue;">public</span> CBaseToggle
  <span style="color:blue;">class</span> CMyBrushEntity : <span style="color:blue;">public</span> CBaseTrigger
  {
  {
  <span style="color:blue;">public</span>:
  <span style="color:blue;">public</span>:
  DECLARE_CLASS( CMyBrushEntity, CBaseToggle );
  DECLARE_CLASS( CMyBrushEntity, CBaseTrigger );
  DECLARE_DATADESC();
  DECLARE_DATADESC();
   
   
  <span style="color:blue;">void</span> Spawn();
  <span style="color:blue;">void</span> Spawn();
<span style="color:blue;">bool</span> CreateVPhysics();
   
   
  <span style="color:blue;">void</span> BrushTouch( CBaseEntity *pOther );
  <span style="color:blue;">void</span> BrushTouch( CBaseEntity *pOther );
Line 23: Line 23:
  BEGIN_DATADESC( CMyBrushEntity )
  BEGIN_DATADESC( CMyBrushEntity )
 
 
  <span style="color:green;">// Declare this function as being a touch function</span>
  <span style="color:green;">// Declare this function as being the touch function</span>
  DEFINE_ENTITYFUNC( BrushTouch ),
  DEFINE_ENTITYFUNC( BrushTouch ),
   
   
Line 38: Line 38:
  <span style="color:green;">// We should collide with physics</span>
  <span style="color:green;">// We should collide with physics</span>
  SetSolid( SOLID_VPHYSICS );
  SetSolid( SOLID_VPHYSICS );
<span style="color:green;">// Use our brushmodel</span>
SetModel( STRING( GetModelName() ) );
 
 
  <span style="color:green;">// We push things out of our way</span>
  <span style="color:green;">// We push things out of our way</span>
  SetMoveType( MOVETYPE_PUSH );
  SetMoveType( MOVETYPE_PUSH );
<span style="color:green;">// Use our brushmodel</span>
SetModel( STRING( GetModelName() ) );
   
   
  <span style="color:green;">// Create our physics hull information</span>
  <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> );
  VPhysicsInitShadow( <span style="color:blue;">false</span>, <span style="color:blue;">false</span> );
<span style="color:blue;">return true</span>;
  }
  }
   
   
Line 73: Line 62:
  vecPushDir.Negate();
  vecPushDir.Negate();
  vecPushDir.z = 0.0f;
  vecPushDir.z = 0.0f;
<span style="color:green;">// 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 );</span>
   
   
  <span style="color:green;">// Move slowly in that direction</span>
  <span style="color:green;">// Move slowly in that direction</span>

Revision as of 14:18, 3 March 2008

//===== 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()
{
	// 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