Main
using System;

namespace Libraries
{
    // Purpose:
    // Object used to produce a bobbing position change.
    // Given a range, over time it will determine a new position within the bob range.
    public class BobAction
    {
        // Purpose:
        // Default constructor.
        public BobAction ()
        {
           
        }

        // Purpose:
        // Constructor.
        // rangeStart, rangeEnd - Start and end points of the range.
        // time                 - Current bob time.  Position is based off this time.
        // fullBobTime          - How long a full bob takes.
        // repeat               - True if the bobbing should repeat when the full bob has finished, false if not.
        public BobAction(float rangeStart, float rangeEnd,
                         int time,
                         int fullBobTime,
                         bool repeat)
        {
            m_rangeStart = rangeStart;
            m_rangeEnd = rangeEnd;
            m_time = time;
            m_fullBobTime = fullBobTime;
            m_repeat = repeat;
        }

        // Purpose:
        // Retrive the current position based off the time.
        public float GetCurrValue()
        {
            float halfTime = m_fullBobTime/2.0f;
            float currTime = (float)m_time;

            if(currTime <= halfTime)
                return m_rangeStart + ((m_rangeEnd - m_rangeStart)*(currTime/halfTime));
            else
                return m_rangeStart + ((m_rangeEnd - m_rangeStart)*(1-((currTime-halfTime)/halfTime)));    
        }

        // Purpose:
        // Update the time passed.  Adds the given time to the current time and handles overflows/repeats.
        public void Update(int timePassed)
        {
            m_time += timePassed;
            if(m_time > m_fullBobTime)
            {
                if(m_repeat)
                {
                    while(m_time > m_fullBobTime)
                        m_time -= m_fullBobTime;
                }
                else
                {
                    m_time = m_fullBobTime;
                }
            }
        }

        // Purpose:
        // Reset the time passed to be 0.
        public void ResetTime()
        {
            m_time = 0;
        }

        // Start and end points of the range.
        private float m_rangeStart = 0;
        private float m_rangeEnd = 0;
   
        // Current bob time.  Position is based off this time.
        private int m_time = 0;

        // How long a full bob takes.
        private int m_fullBobTime = 0;

        // True if the bobbing should repeat when the full bob has finished, false if not.
        private bool m_repeat = false;
    }
}

 
Website copyright (c) 2012 Benjamin Millek, all rights reserved.