示例#1
0
        /// <summary>
        /// Runs multiple actions at a uniform delay time
        /// </summary>
        /// <param name="uniformDelay">Time to wait between each action</param>
        /// <param name="actions">Actions to run</param>
        /// <returns>An instance of the timer</returns>
        public TimeInfoIndexed StartTimerSequence(float uniformDelay, params System.Action[] actions)
        {
            TimeNode[] times = new TimeNode[actions.Length];
            for (int i = 0; i < times.Length; i++)
            {
                times[i] = new TimeNode()
                {
                    time     = uniformDelay,
                    callback = actions[i]
                };
            }
            TimeInfoIndexed instance = new TimeInfoIndexed()
            {
                currentIndex = 0,
                info         = new TimeInfo()
                {
                    nodes = times
                }
            };

            added.Enqueue(instance);

            return(instance);
        }
示例#2
0
        private void Update()
        {
            while (added.Count > 0)
            {
                timers.Add(added.Dequeue());
            }

            while (removed.Count > 0)
            {
                timers.Remove(removed.Dequeue());
            }

            for (int i = timers.Count - 1; i >= 0; i--)
            {
                TimeInfoIndexed currentTimer = timers[i];
                if (currentTimer.useSequence == false)
                {
                    TimeNode currentNode = currentTimer.info.nodes[currentTimer.currentIndex];
                    //Whatever the current index is, reduce its time by deltaTime
                    currentNode.time -= Time.deltaTime;

                    //If the timer has run out
                    if (currentNode.time <= 0)
                    {
                        //Run the callback
                        currentNode.callback();

                        //Increase the index, if it's over the amount of nodes we have, delete it.
                        if (currentTimer.repeat)
                        {
                            currentNode.time = currentTimer.delaySavedTime;
                        }
                        currentTimer.currentIndex++;
                        if (currentTimer.currentIndex >= currentTimer.info.nodes.Length)
                        {
                            if (currentTimer.repeat)
                            {
                                currentTimer.currentIndex = 0;
                            }
                            else
                            {
                                timers.RemoveAt(i);
                            }
                        }
                    }
                }
                else
                {
                    //Decrease the time
                    currentTimer.optionalSequenceTime -= Time.deltaTime;

                    if (currentTimer.optionalSequenceTime <= 0)
                    {
                        //If its time to move to the next step, do so and set the new time
                        if (currentTimer.optionalSequence.MoveNext())
                        {
                            currentTimer.optionalSequenceTime = currentTimer.optionalSequence.Current;
                        }
                        else
                        {
                            timers.RemoveAt(i);
                        }
                    }
                }
            }
        }