Exemple #1
0
	void Alien::Update(float deltaTime)
	{
		if (!DoneMoving)
		{
			Position = Owner.Transform.Position.ToVec2();

			Steering = PathFollowing();
			Truncate(Steering, MAX_FORCE);
			Steering *= 1 / Mass;

			Velocity += Steering;
			Truncate(Velocity, MAX_VELOCITY);
			Owner.Transform.Position += vec3(Velocity.x, Velocity.y, 0);
		}
	}
/*
 * Move Entity by MoveX, MoveY pixels
 * The move is done in tiny increments determined by the speed factor
 * rather than in one large move to MoveX, MoveY
 */ 
void CEntity::Move(float MoveX, float MoveY) 
{       
    if (MoveX == 0 && MoveY == 0) return; // We've stopped
   
    CanJump = false; // We don't yet know if we can jump
    
    float speedfactor = CFramerate::FPSControl.GetSpeedFactor();
   
    MoveX *= speedfactor;
    MoveY *= speedfactor;
 
    //Store the step increment in each direction
    double NewX = 0;
    double NewY = 0; 
    
    if (MoveX != 0) NewX = (MoveX >= 0) ? speedfactor : -speedfactor; // for going left or right
    if (MoveY != 0) NewY = (MoveY >= 0) ? speedfactor : -speedfactor; // for going up or down
     
    while(true)
    {     
        if(Flags & ENTITY_FLAG_GHOST) // Can pass through everything
        {
            PositionValid((int)(X + NewX), (int)(Y + NewY)); //We don't care about collisions, but we need to send events to other entities

            X += NewX;
            Y += NewY;
        } 
        else // Check for Collisions in X & Y direction
        {
            if (OnSlope())
            {
                
                if ( PositionValid((int)(X + NewX), (int)(Y)), true )
                    X += NewX;
                else
                    SpeedX = 0;
                
                if ( PositionValid( (int)(X), (int)(Y + NewY)), true )
                    Y += NewY + Slope_Offset_Y;
                
                CanJump = true;
                SpeedY = 0;
                 
            }else
            {                
                // Can we move in X
                if ( PositionValid((int)(X + NewX), (int)(Y)) )
                {  
                    X += NewX;
                            
                    if (Carrying)
                        Carrying->X += NewX;                           
                }else
                    SpeedX = 0;
                
                // Can we move in Y                
                if ( PositionValid( (int)(X), (int)(Y + NewY)) )
                {
                    Y += NewY;
                    
                    // It's a platform (or similar) with something on it
                    if (Carrying && (MoveUp || MoveDown))
                        Carrying->Y = Y - Carrying->Height;                                      
                }
                else // We are on the floor
                {
                    CanJump = true;
                    SpeedY = 0;
                }
                 
            }
           
        } 
              
        // Check Path Following
        PathFollowing();

        // Reduce MoveX, MoveY by the step increment
        MoveX += -NewX;
        MoveY += -NewY;
     
        // Check for over-run
        if(NewX > 0 && MoveX <= 0) NewX = 0;
        if(NewX < 0 && MoveX >= 0) NewX = 0;
        
        if(NewY > 0 && MoveY <= 0) NewY = 0;
        if(NewY < 0 && MoveY >= 0) NewY = 0;
        
        if(MoveX == 0) NewX = 0;
        if(MoveY == 0) NewY = 0;
        
        // Check if we're finished
        if(MoveX == 0 && MoveY == 0) break;        
        if(NewX  == 0 && NewY == 0) break;
    }
   
}