示例#1
0
/*
=============
LinearMove

calculate pev->velocity and pev->nextthink to reach vecDest from
pev->origin traveling at flSpeed
===============
*/
void CBaseToggle ::  LinearMove( Vector	vecDest, float flSpeed )
{
	ASSERTSZ(flSpeed != 0, "LinearMove:  no speed is defined!");
//	ASSERTSZ(m_pfnCallWhenMoveDone != NULL, "LinearMove: no post-move function defined");
	
	m_vecFinalDest = vecDest;

	// Already there?
	if (vecDest == pev->origin)
	{
		LinearMoveDone();
		return;
	}

	SetThink( &CBaseToggle::LinearMoveDone );
		
	// set destdelta to the vector needed to move
	Vector vecDestDelta = vecDest - pev->origin;
	
	// divide vector length by speed to get time to reach dest
	float flTravelTime = vecDestDelta.Length() / flSpeed;

	if( flTravelTime < 0.1f )
	{
		pev->velocity = g_vecZero;
		pev->nextthink = pev->ltime + 0.1f;
		return;
	}

	// set nextthink to trigger a call to LinearMoveDone when dest is reached
	pev->nextthink = pev->ltime + flTravelTime;

	// scale the destdelta vector by the time spent traveling to get velocity
	pev->velocity = vecDestDelta / flTravelTime;
}
示例#2
0
void CBaseToggle::MoveDone( void )
{
	switch ( m_movementType )
	{
	case MOVE_TOGGLE_LINEAR:
		LinearMoveDone();
		break;
	case MOVE_TOGGLE_ANGULAR:
		AngularMoveDone();
		break;
	}
	m_movementType = MOVE_TOGGLE_NONE;
	BaseClass::MoveDone();
}
示例#3
0
void CBaseToggle::LinearMove(Vector vecDest, float flSpeed)
{
	m_vecFinalDest = vecDest;

	if (vecDest == pev->origin)
	{
		LinearMoveDone();
		return;
	}

	Vector vecDestDelta = vecDest - pev->origin;
	float flTravelTime = vecDestDelta.Length() / flSpeed;
	pev->nextthink = pev->ltime + flTravelTime;
	SetThink(&CBaseToggle::LinearMoveDone);
	pev->velocity = vecDestDelta / flTravelTime;
}
void CBaseToggle :: LinearMoveNow( void )   // AJH Now supports acceleration
{
//	ALERT(at_console, "LMNow %s\n", STRING(pev->targetname));

	Vector vecDest;
//	if (m_pMoveWith || m_pChildMoveWith )
//		ALERT(at_console,"THINK: LinearMoveNow happens at %f, speed %f\n",gpGlobals->time, m_flLinearMoveSpeed);

	if (m_pMoveWith)
	{
	    vecDest = m_vecFinalDest + m_pMoveWith->pev->origin;
	}
	else
	    vecDest = m_vecFinalDest;

	// Already there?
	if (vecDest == pev->origin)
	{
		ALERT(at_console, "%s Already There!!\n", STRING(pev->targetname));
		LinearMoveDone();
		return;
	}
		
	// set destdelta to the vector needed to move
	Vector vecDestDelta = vecDest - pev->origin;

	// divide vector length by speed to get time to reach dest
	float flTravelTime = vecDestDelta.Length() / m_flLinearMoveSpeed;
	if (m_flLinearAccel >0 || m_flLinearDecel >0){   //AJH Are we using acceleration/deceleration?
		
		if(m_bDecelerate==true){	// Are we slowing down?
			m_flCurrentTime-=ACCELTIMEINCREMENT;
			if (m_flCurrentTime<=0){  
			//	ALERT(at_debug, "%s has finished moving\n", STRING(pev->targetname));
				LinearMoveDone();	//Finished slowing.

				m_flCurrentTime = 0;	// 
				m_bDecelerate = false;	// reset 

			}else{

				
			UTIL_SetVelocity( this, vecDestDelta.Normalize()*(m_flLinearDecel*m_flCurrentTime));  //Slow down
			//	ALERT(at_debug, "%s is decelerating, time: %f speed: %f vector: %f %f %f\n", STRING(pev->targetname),m_flCurrentTime,(m_flLinearDecel*m_flCurrentTime),vecDestDelta.Normalize().x,vecDestDelta.Normalize().y,vecDestDelta.Normalize().z);
			
			// Continually calls LinearMoveNow every ACCELTIMEINCREMENT (seconds?) till stopped
			if(m_flCurrentTime<ACCELTIMEINCREMENT){
				SetNextThink( m_flCurrentTime, TRUE );
				m_flCurrentTime=0;
			}else{
				SetNextThink( ACCELTIMEINCREMENT, TRUE );
			}
			SetThink(&CBaseToggle :: LinearMoveNow );
			}

		}else{

			if(m_flCurrentTime < m_flAccelTime){	// We are Accelerating.

			//	ALERT(at_console, "%s is accelerating\n", STRING(pev->targetname));
				UTIL_SetVelocity( this, vecDestDelta.Normalize()*(m_flLinearAccel*m_flCurrentTime));
				
				// Continually calls LinearMoveNow every 0.1 (seconds?) till up to speed
				SetNextThink(ACCELTIMEINCREMENT, TRUE );
				SetThink(&CBaseToggle :: LinearMoveNow );
				m_flCurrentTime+=ACCELTIMEINCREMENT;	
				
				//BUGBUG this will mean that we will be going faster than maxspeed on the last call to 'accelerate'

			}else {			// We are now at full speed.

			//	m_flAccelTime = m_flCurrentTime;	
			//	ALERT(at_console, "%s is traveling at constant speed\n", STRING(pev->targetname));
				
				UTIL_SetVelocity( this, vecDestDelta.Normalize()*(m_flLinearMoveSpeed)); //we are probably going slightly faster.
				
				// set nextthink to trigger a recall to LinearMoveNow when we need to slow down.
				SetNextThink( (vecDestDelta.Length()-(m_flLinearMoveSpeed*m_flDecelTime/2))/(m_flLinearMoveSpeed), TRUE );
				SetThink(&CBaseToggle :: LinearMoveNow );
				//	m_flCurrentTime = (flTravelTime);
				m_bDecelerate=true;  //Set boolean so next call we know we are decelerating.
				m_flDecelTime+=(m_flCurrentTime-m_flAccelTime); //Hack to fix time increment bug
				m_flCurrentTime=m_flDecelTime;
			}
		}

	}else{  // We are not using acceleration.
	
		// set nextthink to trigger a call to LinearMoveDone when dest is reached
		SetNextThink( flTravelTime, TRUE );
		SetThink(&CBaseToggle :: LinearMoveDone );

		// scale the destdelta vector by the time spent traveling to get velocity
		//	pev->velocity = vecDestDelta / flTravelTime;
		UTIL_SetVelocity( this, vecDestDelta / flTravelTime );

	//		ALERT(at_console, "LMNow \"%s\": Vel %f %f %f, think %f\n", STRING(pev->targetname), pev->velocity.x, pev->velocity.y, pev->velocity.z, pev->nextthink);
	}
}