User:MLSTRM/Modelentity with modelchoice

From Valve Developer Community
Jump to: navigation, search

NOTE:the entity is called CPropRandomMotion, change it to change the name of the entity

//Random Motion entity for VR; Origin :MLSTRM

  1. include "cbase.h"

class CPropRandomMotion : public CBaseAnimating { public: DECLARE_CLASS( CPropRandomMotion, CBaseAnimating ); DECLARE_DATADESC();

CPropRandomMotion() { m_bActive = false; }

void Spawn( void ); void Precache( void );

void MoveThink( void );

// Input function void InputToggle( inputdata_t &inputData );

private:

bool m_bActive; float m_flNextChangeTime; };

LINK_ENTITY_TO_CLASS( prop_random_motion, CPropRandomMotion );

// Start of our data description for the class BEGIN_DATADESC( CPropRandomMotion )

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


//lets you chose a model void CPropRandomMotion::Precache( void ) { if ( GetModelName() == NULL_STRING ) { Msg( "%s at (%.3f, %.3f, %.3f) has no model name!\n", GetClassname(), GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z ); SetModelName( AllocPooledString( "models/error.mdl" ) ); }

PrecacheModel( STRING( GetModelName() ) );

BaseClass::Precache(); }

//----------------------------------------------------------------------------- // Purpose: Sets up the entity's initial state //----------------------------------------------------------------------------- void CPropRandomMotion::Spawn( void ) { char *szModel = (char *)STRING( GetModelName() ); if (!szModel || !*szModel) { Warning( "prop at %.0f %.0f %0.f missing modelname\n", GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z ); UTIL_Remove( this ); return; }

PrecacheModel( szModel ); Precache(); SetModel( szModel ); } //----------------------------------------------------------------------------- // Purpose: Think function to randomly move the entity //----------------------------------------------------------------------------- void CPropRandomMotion::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 ); }

//----------------------------------------------------------------------------- // Purpose: Toggle the movement of the entity //----------------------------------------------------------------------------- void CPropRandomMotion::InputToggle( inputdata_t &inputData ) { // Toggle our active state if ( !m_bActive ) { // Start thinking SetThink( &CPropRandomMotion::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; } }