コード例 #1
0
        /// <summary>
        /// Ticks the <see cref="TickTimer"/> at the specified game tick.
        /// </summary>
        ///
        /// <remarks>
        /// If <see cref="IsActive"/> is <c>false</c> the <see cref="Tick(int)"/> method
        /// will exit early and not do any ticking logic.
        /// </remarks>
        ///
        /// <param name="ctick">Game tick.</param>
        public void Tick(int ctick)
        {
            if (IsActive)
            {
                if (ctick < _lastTick)
                {
                    Debug.WriteLine("TickTimer went back in time!! Forgot to rest or stop?");
                }

                _lastTick = ctick;

                // Prevent some extra calculations.
                if (ctick >= _endTick)
                {
                    _duration   = 0;
                    _isActive   = false;
                    _isComplete = true;
                }
                else
                {
                    var diff        = ctick - _startTick;
                    var diffSeconds = TimeUtils.FromTick(diff);
                    var newDuration = _oriDuration - diffSeconds;

                    // Clamp value to 0.
                    if (newDuration < 0)
                    {
                        newDuration = 0;
                        _isActive   = false;
                        _isComplete = true;
                    }

                    _duration = newDuration;
                }
            }
        }