Beispiel #1
0
        void RemoveHashElement(HashTimeEntry element)
        {
            hashForTimers.Remove(element.Target);

            element.Timers.Clear();
            element.Target = null;
        }
Beispiel #2
0
        /** The scheduled method will be called every 'interval' seconds.
         * If paused is YES, then it won't be called until it is resumed.
         * If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdateForTarget:' instead.
         * If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
         * repeat let the action be repeated repeat + 1 times, use RepeatForever to let the action run continuously
         * delay is the amount of time the action will wait before it'll start
         *
         * @since v0.99.3, repeat and delay added in v1.1
         */

        public void Schedule(Action <float> selector, ICCUpdatable target, float interval, uint repeat,
                             float delay, bool paused)
        {
            Debug.Assert(selector != null);
            Debug.Assert(target != null);

            HashTimeEntry element;

            lock (hashForTimers)
            {
                if (!hashForTimers.TryGetValue(target, out element))
                {
                    element = new HashTimeEntry {
                        Target = target
                    };
                    hashForTimers[target] = element;

                    // Is this the 1st element ? Then set the pause level to all the selectors of this target
                    element.Paused = paused;
                }
                else
                {
                    if (element != null)
                    {
                        Debug.Assert(element.Paused == paused, "CCScheduler.Schedule: All are paused");
                    }
                }
                if (element != null)
                {
                    if (element.Timers == null)
                    {
                        element.Timers = new List <CCTimer>();
                    }
                    else
                    {
                        CCTimer[] timers = element.Timers.ToArray();
                        foreach (var timer in timers)
                        {
                            if (timer == null)
                            {
                                continue;
                            }
                            if (selector == timer.Selector)
                            {
                                CCLog.Log(
                                    "CCSheduler#scheduleSelector. Selector already scheduled. Updating interval from: {0} to {1}",
                                    timer.Interval, interval);
                                timer.Interval = interval;
                                return;
                            }
                        }
                    }

                    element.Timers.Add(new CCTimer(this, target, selector, interval, repeat, delay));
                }
            }
        }
Beispiel #3
0
        /** The scheduled method will be called every 'interval' seconds.
         * If paused is YES, then it won't be called until it is resumed.
         * If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdateForTarget:' instead.
         * If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
         * repeat let the action be repeated repeat + 1 times, use kCCRepeatForever to let the action run continuously
         * delay is the amount of time the action will wait before it'll start
         *
         * @since v0.99.3, repeat and delay added in v1.1
         */

        public void ScheduleSelector(Action <float> selector, ICCSelectorProtocol target, float interval, uint repeat,
                                     float delay, bool paused)
        {
            Debug.Assert(selector != null);
            Debug.Assert(target != null);

            HashTimeEntry element;

            if (!m_pHashForTimers.TryGetValue(target, out element))
            {
                element = new HashTimeEntry {
                    Target = target
                };
                m_pHashForTimers[target] = element;

                // Is this the 1st element ? Then set the pause level to all the selectors of this target
                element.Paused = paused;
            }
            else
            {
                Debug.Assert(element.Paused == paused);
            }

            if (element.Timers == null)
            {
                element.Timers = new List <CCTimer>();
            }
            else
            {
                foreach (var timer in element.Timers)
                {
                    if (selector == timer.Selector)
                    {
                        Debug.WriteLine(
                            "CCSheduler#scheduleSelector. Selector already scheduled. Updating interval from: {0} to {1}",
                            timer.Interval, interval);
                        timer.Interval = interval;
                        return;
                    }
                }
            }

            element.Timers.Add(new CCTimer(this, target, selector, interval, repeat, delay));
        }
Beispiel #4
0
        internal void Update(float dt)
        {
            isUpdateHashLocked = true;

            try
            {
                if (TimeScale != 1.0f)
                {
                    dt *= TimeScale;
                }

                LinkedListNode <ListEntry> next;

                // updates with priority < 0
                //foreach (ListEntry entry in _updatesNegList)
                for (LinkedListNode <ListEntry> node = updatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // updates with priority == 0
                //foreach (ListEntry entry in _updates0List)
                for (LinkedListNode <ListEntry> node = updates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode <ListEntry> node = updatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // Iterate over all the custom selectors
                var count = hashForTimers.Count;
                if (count > 0)
                {
                    if (tmpSelectorArray.Length < count)
                    {
                        tmpSelectorArray = new ICCUpdatable[tmpSelectorArray.Length * 2];
                    }
                    hashForTimers.Keys.CopyTo(tmpSelectorArray, 0);

                    for (int i = 0; i < count; i++)
                    {
                        ICCUpdatable key = tmpSelectorArray[i];
                        if (!hashForTimers.ContainsKey(key))
                        {
                            continue;
                        }
                        HashTimeEntry elt = hashForTimers[key];

                        currentTarget           = elt;
                        isCurrentTargetSalvaged = false;

                        if (!currentTarget.Paused)
                        {
                            // The 'timers' array may change while inside this loop
                            for (elt.TimerIndex = 0; elt.TimerIndex < elt.Timers.Count; ++elt.TimerIndex)
                            {
                                elt.CurrentTimer = elt.Timers[elt.TimerIndex];
                                if (elt.CurrentTimer != null)
                                {
                                    elt.CurrentTimerSalvaged = false;

                                    elt.CurrentTimer.Update(dt);

                                    elt.CurrentTimer = null;
                                }
                            }
                        }

                        // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
                        if (isCurrentTargetSalvaged && currentTarget.Timers.Count == 0)
                        {
                            RemoveHashElement(currentTarget);
                        }
                    }
                }

                // delete all updates that are marked for deletion
                // updates with priority < 0
                for (LinkedListNode <ListEntry> node = updatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updatesNegList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority == 0
                for (LinkedListNode <ListEntry> node = updates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updates0List.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode <ListEntry> node = updatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updatesPosList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }
            }
            finally
            {
                // Always do this just in case there is a problem

                isUpdateHashLocked = false;
                currentTarget      = null;
            }
        }
Beispiel #5
0
        private void RemoveHashElement(HashTimeEntry element)
        {
            m_pHashForTimers.Remove(element.Target);

            element.Timers.Clear();
            element.Target = null;
        }
Beispiel #6
0
        /** The scheduled method will be called every 'interval' seconds.
         If paused is YES, then it won't be called until it is resumed.
         If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdateForTarget:' instead.
         If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
         repeat let the action be repeated repeat + 1 times, use kCCRepeatForever to let the action run continuously
         delay is the amount of time the action will wait before it'll start

         @since v0.99.3, repeat and delay added in v1.1
         */

        public void ScheduleSelector(Action<float> selector, ICCSelectorProtocol target, float interval, uint repeat,
                                     float delay, bool paused)
        {
            Debug.Assert(selector != null);
            Debug.Assert(target != null);

            HashTimeEntry element;

            if (!m_pHashForTimers.TryGetValue(target, out element))
            {
                element = new HashTimeEntry {Target = target};
                m_pHashForTimers[target] = element;

                // Is this the 1st element ? Then set the pause level to all the selectors of this target
                element.Paused = paused;
            }
            else
            {
                Debug.Assert(element.Paused == paused);
            }

            if (element.Timers == null)
            {
                element.Timers = new List<CCTimer>();
            }
            else
            {
                foreach (var timer in element.Timers)
                {
                    if (selector == timer.Selector)
                    {
                        Debug.WriteLine(
                            "CCSheduler#scheduleSelector. Selector already scheduled. Updating interval from: {0} to {1}",
                            timer.Interval, interval);
                        timer.Interval = interval;
                        return;
                    }
                }
            }

            element.Timers.Add(new CCTimer(this, target, selector, interval, repeat, delay));
        }
Beispiel #7
0
        internal void update(float dt)
        {
            m_bUpdateHashLocked = true;

            try
            {
                if (TimeScale != 1.0f)
                {
                    dt *= TimeScale;
                }

                LinkedListNode<ListEntry> next;

                // updates with priority < 0
                //foreach (ListEntry entry in _updatesNegList)
                for (LinkedListNode<ListEntry> node = m_pUpdatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // updates with priority == 0
                //foreach (ListEntry entry in _updates0List)
                for (LinkedListNode<ListEntry> node = m_pUpdates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode<ListEntry> node = m_pUpdatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // Iterate over all the custom selectors
                var count = m_pHashForTimers.Keys.Count;
                if (s_pTmpSelectorArray.Length < count)
                {
                    s_pTmpSelectorArray = new ICCSelectorProtocol[s_pTmpSelectorArray.Length * 2];
                }
                m_pHashForTimers.Keys.CopyTo(s_pTmpSelectorArray, 0);

                for (int i = 0; i < count; i++)
                {
                    ICCSelectorProtocol key = s_pTmpSelectorArray[i];
                    if (!m_pHashForTimers.ContainsKey(key))
                    {
                        continue;
                    }
                    HashTimeEntry elt = m_pHashForTimers[key];

                    m_pCurrentTarget = elt;
                    m_bCurrentTargetSalvaged = false;

                    if (!m_pCurrentTarget.Paused)
                    {
                        // The 'timers' array may change while inside this loop
                        for (elt.TimerIndex = 0; elt.TimerIndex < elt.Timers.Count; ++elt.TimerIndex)
                        {
                            elt.CurrentTimer = elt.Timers[elt.TimerIndex];
                            elt.CurrentTimerSalvaged = false;

                            elt.CurrentTimer.Update(dt);

                            elt.CurrentTimer = null;
                        }
                    }

                    // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
                    if (m_bCurrentTargetSalvaged && m_pCurrentTarget.Timers.Count == 0)
                    {
                        RemoveHashElement(m_pCurrentTarget);
                    }
                }
                /*
                // Iterate over all the script callbacks
                if (m_pScriptHandlerEntries)
                {
                    for (int i = m_pScriptHandlerEntries->count() - 1; i >= 0; i--)
                    {
                        CCSchedulerScriptHandlerEntry* pEntry = static_cast<CCSchedulerScriptHandlerEntry*>(m_pScriptHandlerEntries->objectAtIndex(i));
                        if (pEntry->isMarkedForDeletion())
                        {
                            m_pScriptHandlerEntries->removeObjectAtIndex(i);
                        }
                        else if (!pEntry->isPaused())
                        {
                            pEntry->getTimer()->update(dt);
                        }
                    }
                }             
                */

                // delete all updates that are marked for deletion
                // updates with priority < 0
                for (LinkedListNode<ListEntry> node = m_pUpdatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        m_pUpdatesNegList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority == 0
                for (LinkedListNode<ListEntry> node = m_pUpdates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        m_pUpdates0List.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode<ListEntry> node = m_pUpdatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        m_pUpdatesPosList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }
            }
            finally
            {
                // Always do this just in case there is a problem

                m_bUpdateHashLocked = false;
                m_pCurrentTarget = null;
            }
        }
Beispiel #8
0
        /** The scheduled method will be called every 'interval' seconds.
         If paused is YES, then it won't be called until it is resumed.
         If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdateForTarget:' instead.
         If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
         repeat let the action be repeated repeat + 1 times, use RepeatForever to let the action run continuously
         delay is the amount of time the action will wait before it'll start

         @since v0.99.3, repeat and delay added in v1.1
         */

        public void Schedule (Action<float> selector, ICCUpdatable target, float interval, uint repeat,
                                     float delay, bool paused)
        {
            Debug.Assert(selector != null);
            Debug.Assert(target != null);

            HashTimeEntry element;

            lock (hashForTimers)
            {
                if (!hashForTimers.TryGetValue(target, out element))
                {
                    element = new HashTimeEntry { Target = target };
                    hashForTimers[target] = element;

                    // Is this the 1st element ? Then set the pause level to all the selectors of this target
                    element.Paused = paused;
                }
                else
                {
                    if (element != null)
                    {
						Debug.Assert(element.Paused == paused, "CCScheduler.Schedule: All are paused");
                    }
                }
                if (element != null)
                {
                    if (element.Timers == null)
                    {
                        element.Timers = new List<CCTimer>();
                    }
                    else
                    {
                        CCTimer[] timers = element.Timers.ToArray();
                        foreach (var timer in timers)
                        {
                            if (timer == null)
                            {
                                continue;
                            }
                            if (selector == timer.Selector)
                            {
                                CCLog.Log(
                                    "CCSheduler#scheduleSelector. Selector already scheduled. Updating interval from: {0} to {1}",
                                    timer.Interval, interval);
                                timer.Interval = interval;
                                return;
                            }
                        }
                    }

                    element.Timers.Add(new CCTimer(this, target, selector, interval, repeat, delay));
                }
            }
        }
Beispiel #9
0
		internal void Update (float dt)
        {
            isUpdateHashLocked = true;

            try
            {
                if (TimeScale != 1.0f)
                {
                    dt *= TimeScale;
                }

                LinkedListNode<ListEntry> next;

                // updates with priority < 0
                //foreach (ListEntry entry in _updatesNegList)
                for (LinkedListNode<ListEntry> node = updatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // updates with priority == 0
                //foreach (ListEntry entry in _updates0List)
                for (LinkedListNode<ListEntry> node = updates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode<ListEntry> node = updatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // Iterate over all the custom selectors
                var count = hashForTimers.Keys.Count;
                if (tmpSelectorArray.Length < count)
                {
                    tmpSelectorArray = new ICCUpdatable[tmpSelectorArray.Length * 2];
                }
                hashForTimers.Keys.CopyTo(tmpSelectorArray, 0);

                for (int i = 0; i < count; i++)
                {
                    ICCUpdatable key = tmpSelectorArray[i];
                    if (!hashForTimers.ContainsKey(key))
                    {
                        continue;
                    }
                    HashTimeEntry elt = hashForTimers[key];

                    currentTarget = elt;
                    isCurrentTargetSalvaged = false;

                    if (!currentTarget.Paused)
                    {
                        // The 'timers' array may change while inside this loop
                        for (elt.TimerIndex = 0; elt.TimerIndex < elt.Timers.Count; ++elt.TimerIndex)
                        {
                            elt.CurrentTimer = elt.Timers[elt.TimerIndex];
							if(elt.CurrentTimer != null) {
	                            elt.CurrentTimerSalvaged = false;

	                            elt.CurrentTimer.Update(dt);

	                            elt.CurrentTimer = null;
							}
                        }
                    }

                    // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
                    if (isCurrentTargetSalvaged && currentTarget.Timers.Count == 0)
                    {
                        RemoveHashElement(currentTarget);
                    }
                }

                // delete all updates that are marked for deletion
                // updates with priority < 0
                for (LinkedListNode<ListEntry> node = updatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updatesNegList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority == 0
                for (LinkedListNode<ListEntry> node = updates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updates0List.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode<ListEntry> node = updatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updatesPosList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }
            }
            finally
            {
                // Always do this just in case there is a problem

                isUpdateHashLocked = false;
                currentTarget = null;
            }
        }
Beispiel #10
0
        internal void update(float dt)
        {
            m_bUpdateHashLocked = true;

            try
            {
                if (TimeScale != 1.0f)
                {
                    dt *= TimeScale;
                }

                LinkedListNode <ListEntry> next;

                // updates with priority < 0
                //foreach (ListEntry entry in _updatesNegList)
                for (LinkedListNode <ListEntry> node = m_pUpdatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        UpdateTarget(node.Value.Target, dt);
                    }
                }

                // updates with priority == 0
                //foreach (ListEntry entry in _updates0List)
                for (LinkedListNode <ListEntry> node = m_pUpdates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        UpdateTarget(node.Value.Target, dt);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode <ListEntry> node = m_pUpdatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        UpdateTarget(node.Value.Target, dt);
                    }
                }

                // Iterate over all the custom selectors
                var count = m_pHashForTimers.Keys.Count;
                if (s_pTmpSelectorArray.Length < count)
                {
                    s_pTmpSelectorArray = new ICCSelectorProtocol[s_pTmpSelectorArray.Length * 2];
                }
                m_pHashForTimers.Keys.CopyTo(s_pTmpSelectorArray, 0);

                for (int i = 0; i < count; i++)
                {
                    ICCSelectorProtocol key = s_pTmpSelectorArray[i];
                    if (key == null || !m_pHashForTimers.ContainsKey(key))
                    {
                        continue;
                    }
                    HashTimeEntry elt = m_pHashForTimers[key];

                    m_pCurrentTarget         = elt;
                    m_bCurrentTargetSalvaged = false;

                    if (elt != null && !m_pCurrentTarget.Paused)
                    {
                        // The 'timers' array may change while inside this loop
                        for (elt.TimerIndex = 0; elt.TimerIndex < elt.Timers.Count; ++elt.TimerIndex)
                        {
                            elt.CurrentTimer = elt.Timers[elt.TimerIndex];
                            if (elt.CurrentTimer != null)
                            {
                                elt.CurrentTimerSalvaged = false;
                                UpdateTarget(elt.CurrentTimer, dt);
                                elt.CurrentTimer = null;
                            }
                        }
                    }

                    // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
                    if (m_bCurrentTargetSalvaged && m_pCurrentTarget.Timers.Count == 0)
                    {
                        RemoveHashElement(m_pCurrentTarget);
                    }
                }

                /*
                 * // Iterate over all the script callbacks
                 * if (m_pScriptHandlerEntries)
                 * {
                 *  for (int i = m_pScriptHandlerEntries->count() - 1; i >= 0; i--)
                 *  {
                 *      CCSchedulerScriptHandlerEntry* pEntry = static_cast<CCSchedulerScriptHandlerEntry*>(m_pScriptHandlerEntries->objectAtIndex(i));
                 *      if (pEntry->isMarkedForDeletion())
                 *      {
                 *          m_pScriptHandlerEntries->removeObjectAtIndex(i);
                 *      }
                 *      else if (!pEntry->isPaused())
                 *      {
                 *          pEntry->getTimer()->update(dt);
                 *      }
                 *  }
                 * }
                 */

                // delete all updates that are marked for deletion
                // updates with priority < 0
                for (LinkedListNode <ListEntry> node = m_pUpdatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        m_pUpdatesNegList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority == 0
                for (LinkedListNode <ListEntry> node = m_pUpdates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        m_pUpdates0List.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode <ListEntry> node = m_pUpdatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        m_pUpdatesPosList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }
            }
            finally
            {
                // Always do this just in case there is a problem

                m_bUpdateHashLocked = false;
                m_pCurrentTarget    = null;
            }
        }