// Blocks until the new target position is reached
void AccelStepper::runToNewPosition(long position)
{
    moveTo(position);
    runToPosition();
}
/**
 ****************************************************************************************************
	\fn			void EndUpdate( GameEngine::Entity &i_entity )
	\brief		End update the position of Enemy
	\param		i_entity entity to be updated
	\return		NONE
 ****************************************************************************************************
*/
void EnemyController::EndUpdate( GameEngine::Entity &i_entity )
{
	float frameTime_ms = Utilities::Time::GetTimeElapsedThisFrame_ms();

	FUNCTION_START;

	Utilities::Pointer::SmartPtr<GameEngine::Entity> player = g_world::Get().GetEntityByName( "Player" );
	float distanceToEnemy = (player->m_v3ProjectedPosition - i_entity.m_v3ProjectedPosition).Length();
	if( distanceToEnemy < GlobalConstant::TAG_DISTANCE )
	{
		GameEngine::Messaging::ProcessMessage( "Tag", NULL );
		return;
	}

	// If player get the flag, decide whether to tag player or continue current destination
	D3DXVECTOR3 fromPosition( player->m_v3ProjectedPosition.X(), player->m_v3ProjectedPosition.Y(), player->m_v3ProjectedPosition.Z() );
	if( g_captureTheFlag::Get().m_bPlayerHasFlag && (_enemyState != E_ENEMY_TAG_PLAYER) )
	{
		UINT32 u32NodeID;

		GameEngine::AI::FindClosestNodeIDFromPosition( fromPosition, u32NodeID );
		//_bAwareOfPlayer = true;
		float distanceToCurrentDestination = 0.0f;

		// If enemy still looking for flag, absolutely tag player
		if( _enemyState == E_ENEMY_SEARCH_FLAG )
		{
			_u32NewTargetNode = u32NodeID;
			_prevState = _enemyState;
			_enemyState = E_ENEMY_TAG_PLAYER;
			return;
		}
		else if( _enemyState == E_ENEMY_SEARCH_GOAL )
		{
			fromPosition = D3DXVECTOR3( i_entity.m_v3ProjectedPosition.X(), i_entity.m_v3ProjectedPosition.Y(), i_entity.m_v3ProjectedPosition.Z() );
			distanceToCurrentDestination = GameEngine::AI::FindDistanceToNodeID( fromPosition, GlobalConstant::GOAL_NODE_ID );
		}
		// Do nothing on other state
		else
			return;

		float totalFrameToDestination = distanceToCurrentDestination / frameTime_ms;

		float distanceToPlayer = 0.0f;
		fromPosition = D3DXVECTOR3( i_entity.m_v3ProjectedPosition.X(), i_entity.m_v3ProjectedPosition.Y(), i_entity.m_v3ProjectedPosition.Z() );
		distanceToPlayer = GameEngine::AI::FindDistanceToNodeID( fromPosition, u32NodeID );

		UINT32 totalFrameToPlayer = 0;
		UINT32 u32AvailableSprintFrame = _u32Power / GlobalConstant::SPRINT_POWER_PER_FRAME;
		float distanceTravelledPerFrameOnSprint = frameTime_ms * GlobalConstant::DEFAULT_SPRINT_SPEED;
		float totalDistanceOnSprint = u32AvailableSprintFrame * distanceTravelledPerFrameOnSprint;

		if( distanceToPlayer - totalDistanceOnSprint < 0 )
		{
			totalFrameToPlayer = static_cast<UINT32>(distanceToPlayer / distanceTravelledPerFrameOnSprint);
		}
		else
		{
			distanceToPlayer -= totalDistanceOnSprint;
			totalFrameToPlayer = u32AvailableSprintFrame;
			totalFrameToPlayer += static_cast<UINT32>(distanceToPlayer / frameTime_ms);
		}

		if( totalFrameToPlayer < totalFrameToDestination )
		{
			_prevState = _enemyState;
			_u32NewTargetNode = u32NodeID;
			_enemyState = E_ENEMY_TAG_PLAYER;
		}
	}

	// Check if player is close
	GameEngine::Math::Vector3 distanceToPlayer = player->m_v3ProjectedPosition - i_entity.m_v3ProjectedPosition;
	float lengthToPlayer = distanceToPlayer.Length();
	if( (lengthToPlayer < GlobalConstant::MINIMUM_DISTANCE_TO_PLAYER) && (_enemyState != E_ENEMY_TAG_PLAYER) && (_enemyState != E_ENEMY_ESCAPE) )
	{
		_prevState = _enemyState;

		GameEngine::Math::Vector3 runDirection = player->m_v3Velocity - i_entity.m_v3Velocity;
		GameEngine::Math::Vector3 v3RunToPosition = runDirection * GlobalConstant::DEFAULT_SPRINT_SPEED * 2 * GlobalConstant::MINIMUM_DISTANCE_TO_PLAYER;
		D3DXVECTOR3 runToPosition( v3RunToPosition.X(), v3RunToPosition.Y(), v3RunToPosition.Z() );

		GameEngine::AI::FindClosestNodeIDFromPosition( runToPosition, _u32NewTargetNode );
		g_world::Get().UpdateAIDestinationTo( i_entity.m_u8AIEntityIndex, _u32NewTargetNode );
		_enemyState = E_ENEMY_ESCAPE;
	}
	else if( _enemyState == E_ENEMY_TAG_PLAYER )
	{
		_enemyState = _prevState;
		if( _enemyState == E_ENEMY_SEARCH_FLAG )
		{
			if( g_captureTheFlag::Get().m_playerTeam == Utilities::StringHash("BlueFlag") )
				g_world::Get().UpdateAIDestinationTo( i_entity.m_u8AIEntityIndex, GlobalConstant::BLUE_FLAG_NODE_ID );
			else
				g_world::Get().UpdateAIDestinationTo( i_entity.m_u8AIEntityIndex, GlobalConstant::RED_FLAG_NODE_ID );
		}
		else if( _enemyState == E_ENEMY_SEARCH_GOAL )
		{
			g_world::Get().UpdateAIDestinationTo( i_entity.m_u8AIEntityIndex, GlobalConstant::GOAL_NODE_ID );
		}
	}

	if( _bSprint && (_u32Power > GlobalConstant::SPRINT_POWER_PER_FRAME) )
	{
		i_entity.m_v3Velocity *= GlobalConstant::DEFAULT_SPRINT_SPEED;
		_u32Power -= GlobalConstant::SPRINT_POWER_PER_FRAME;
	}

	FUNCTION_FINISH;
}
Exemple #3
0
// Blocks until the new target position is reached
void runToNewPosition(Stepper_t* motor, long position)
{
    moveTo(motor, position);
    runToPosition(motor);
}
/**
 * \par Function
 *    runToNewPosition
 * \par Description
 *    The Stepper runs to new position.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    None
 * \par Others
 *    None
 */
void MeStepperOnBoard::runToNewPosition(long position)
{
  moveTo(position,_callback, _extId);
  runToPosition();
}