codesamples >> quantum movement class

Quantum: Controls class

Project:

Quantum

Engine:

XNA

Code language:

C#

Class description:

The control system works for multiple platforms, if the user wants to play Quantum on a pc with mouse and keyboard / Xbox controller / on a tablet device, the controls class adapts itself.

Controls.cs

// ...            
public void MoveForwards()
{
    if (m_Vigor.Pose == (int)Vigor.PoseState.SprintForwards) { SprintForwards(); return; }
    
    m_Camera.IsFreeCamActive = false; // Disable Free Camera Mode

    float moveSpeed;

    // adjusting speed according to gamepad if there's one
    if (m_Platform == (int)Platform.Xbox && GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Length() <= 1)
    {
        moveSpeed = m_RunningSpeed * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Length();
    }
    else
    {
        moveSpeed = m_RunningSpeed;
    }

    // Check if Vigor is Dragging
    if (m_Vigor.IsDragging == false)
    {
        Vector3 Position = m_Vigor.Position;
        Position.X += (float)Math.Sin(m_Vigor.Rotation) / (m_MaxSpeed - moveSpeed);
        Position.Z += (float)Math.Cos(m_Vigor.Rotation) / (m_MaxSpeed - moveSpeed);
        m_Vigor.FuturePosition = Position;

        // If Vigor can move, move him.
        if (m_Vigor.CanMove == true && m_Vigor.IsStrafing == false)
        {
            m_Vigor.Position = Position;
            m_Vigor.Pose = (int)Vigor.PoseState.Run;
        }
        else
        {  
            m_Vigor.Pose = (int)Vigor.PoseState.Idle;
        }
    }
    else
    {
        Vector3 Position = m_Vigor.Position;
        Position.X += (float)Math.Sin(m_Vigor.Rotation) / (m_MaxSpeed - moveSpeed);
        Position.Z += (float)Math.Cos(m_Vigor.Rotation) / (m_MaxSpeed - moveSpeed);
        m_Vigor.FuturePosition = Position;

        // If Vigor can move, move him.
        if (m_Vigor.CanMove == true && m_Vigor.IsStrafing == false && m_Vigor.IsStuck == false)
        {
            m_Vigor.Position = Position;
            m_Vigor.Pose = (int)Vigor.PoseState.Dragging;
        }
        else
        {
            m_Vigor.Pose = (int)Vigor.PoseState.Dragging;
        }
    }
}

public void SprintForwards()
{
    m_Camera.IsFreeCamActive = false; // Disable Free Camera Mode

    float moveSpeed;

    // adjusting speed according to gamepad if there's one
    if (m_Platform == (int)Platform.Xbox && GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Length() <= 1)
    {
        moveSpeed = m_RunningSpeed * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Length();
    }
    else
    {
        moveSpeed = m_RunningSpeed;
    }

    Vector3 Position = m_Vigor.Position;
    Position.X += (float)Math.Sin(m_Vigor.Rotation) / (m_MaxSpeed - moveSpeed * m_RunningSpeedEnergyBoost);
    Position.Z += (float)Math.Cos(m_Vigor.Rotation) / (m_MaxSpeed - moveSpeed * m_RunningSpeedEnergyBoost);
    m_Vigor.FuturePosition = Position;

    // If Vigor can move, move him.
    if (m_Vigor.CanMove == true && m_Vigor.IsStrafing == false)
    {
        m_Vigor.Position = Position;
        m_Vigor.Pose = (int)Vigor.PoseState.Run;
    }
    else
    {
        m_Vigor.Pose = (int)Vigor.PoseState.Idle;
    }
}
// ...