Beispiel #1
0
        // actions

        /// <summary>
        /// Adds an action with a target.
        ///  If the target is already present, then the action will be added to the existing target.
        ///  If the target is not present, a new instance of this target will be created either paused or not, and the action will be added to the newly created target.
        ///  When the target is paused, the queued actions won't be 'ticked'.
        /// </summary>
        public void addAction(CCAction action, CCNode target, bool paused)
        {
            Debug.Assert(action != null);
            Debug.Assert(target != null);

            tHashElement element = null;

            if (!m_pTargets.ContainsKey(target))
            {
                element        = new tHashElement();
                element.paused = paused;
                element.target = target;
                m_pTargets.Add(target, element);
            }
            else
            {
                element = m_pTargets[target];
            }

            actionAllocWithHashElement(element);

            Debug.Assert(!element.actions.Contains(action));
            element.actions.Add(action);

            action.startWithTarget(target);
        }
Beispiel #2
0
 protected void actionAllocWithHashElement(tHashElement element)
 {
     if (element.actions == null)
     {
         element.actions = new List <CCAction>();
     }
 }
Beispiel #3
0
        protected void removeActionAtIndex(int index, tHashElement element)
        {
            CCAction action = element.actions[index];

            if (action == element.currentAction && (!element.currentActionSalvaged))
            {
                element.currentActionSalvaged = true;
            }

            element.actions.RemoveAt(index);

            // update actionIndex in case we are in tick. looping over the actions
            if (element.actionIndex >= index)
            {
                element.actionIndex--;
            }

            if (element.actions.Count == 0)
            {
                if (m_pCurrentTarget == element)
                {
                    m_bCurrentTargetSalvaged = true;
                }
                else
                {
                    deleteHashElement(element);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Gets an action given its tag an a target
        /// </summary>
        /// <returns>
        /// @return the Action the with the given tag
        /// </returns>
        public CCAction getActionByTag(uint tag, CCObject target)
        {
            Debug.Assert((int)tag != (int)ActionTag.kCCActionTagInvalid);

            if (m_pTargets.ContainsKey(target))
            {
                tHashElement element = m_pTargets[target];

                if (element.actions != null)
                {
                    int limit = element.actions.Count;
                    for (int i = 0; i < limit; i++)
                    {
                        CCAction action = element.actions[i];

                        if (action.tag == (int)tag)
                        {
                            return(action);
                        }
                    }
                }
                else
                {
                    CCLog.Log("cocos2d : getActionByTag: Target not found");
                }
            }
            else
            {
                CCLog.Log("cocos2d : getActionByTag: Target not found");
            }

            return(null);
        }
Beispiel #5
0
        /// <summary>
        /// Removes all actions from a certain target.
        /// All the actions that belongs to the target will be removed.
        /// </summary>
        public void removeAllActionsFromTarget(CCObject target)
        {
            if (target == null)
            {
                return;
            }

            if (m_pTargets.ContainsKey(target))
            {
                tHashElement element = m_pTargets[target];

                if (element.actions.Contains(element.currentAction) && (!element.currentActionSalvaged))
                {
                    element.currentActionSalvaged = true;
                }

                element.actions.Clear();
                if (m_pCurrentTarget == element)
                {
                    m_bCurrentTargetSalvaged = true;
                }
                else
                {
                    deleteHashElement(element);
                }
            }
            else
            {
                CCLog.Log("cocos2d: removeAllActionsFromTarget: Target not found");
            }
        }
Beispiel #6
0
        /// <summary>
        /// Returns the numbers of actions that are running in a certain target.
        /// Composable actions are counted as 1 action. Example:
        /// - If you are running 1 Sequence of 7 actions, it will return 1.
        /// - If you are running 7 Sequences of 2 actions, it will return 7.
        /// </summary>
        public uint numberOfRunningActionsInTarget(CCObject target)
        {
            if (m_pTargets.ContainsKey(target))
            {
                tHashElement element = m_pTargets[target];
                return((element.actions != null) ? (uint)element.actions.Count : 0);
            }

            return(0);
        }
Beispiel #7
0
        public void update(float dt)
        {
            CCObject[] keys = new CCObject[m_pTargets.Keys.Count];
            m_pTargets.Keys.CopyTo(keys, 0);

            for (int i = 0; i < keys.Length; i++)
            {
                if (!m_pTargets.ContainsKey(keys[i]))
                {
                    // http://www.cocos2d-x.org/boards/17/topics/11072
                    continue;
                }
                tHashElement elt = m_pTargets[keys[i]];
                m_pCurrentTarget         = elt;
                m_bCurrentTargetSalvaged = false;

                if (!m_pCurrentTarget.paused)
                {
                    // The 'actions' may change while inside this loop.
                    for (m_pCurrentTarget.actionIndex = 0; m_pCurrentTarget.actionIndex < m_pCurrentTarget.actions.Count;
                         m_pCurrentTarget.actionIndex++)
                    {
                        m_pCurrentTarget.currentAction = m_pCurrentTarget.actions[m_pCurrentTarget.actionIndex];
                        if (m_pCurrentTarget.currentAction == null)
                        {
                            continue;
                        }

                        m_pCurrentTarget.currentActionSalvaged = false;

                        m_pCurrentTarget.currentAction.step(dt);

                        if (m_pCurrentTarget.currentAction.isDone())
                        {
                            m_pCurrentTarget.currentAction.stop();

                            CCAction action = m_pCurrentTarget.currentAction;
                            // Make currentAction nil to prevent removeAction from salvaging it.
                            m_pCurrentTarget.currentAction = null;
                            removeAction(action);
                        }

                        m_pCurrentTarget.currentAction = null;
                    }
                }

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

            m_pCurrentTarget = null;
        }
Beispiel #8
0
        protected void deleteHashElement(tHashElement element)
        {
            element.actions.Clear();
            element.target = null;
            CCObject keyToDelete = null;

            foreach (KeyValuePair <CCObject, tHashElement> kvp in m_pTargets)
            {
                if (element == kvp.Value)
                {
                    keyToDelete = kvp.Key;
                    break;
                }
            }

            if (keyToDelete != null)
            {
                m_pTargets.Remove(keyToDelete);
            }
        }
Beispiel #9
0
        /// <summary>
        /// Removes an action given its tag and the target
        /// </summary>
        public void removeActionByTag(int tag, CCObject target)
        {
            Debug.Assert((tag != (int)ActionTag.kCCActionTagInvalid));
            Debug.Assert(target != null);

            if (m_pTargets.ContainsKey(target))
            {
                tHashElement element = m_pTargets[target];

                int limit = element.actions.Count;
                for (int i = 0; i < limit; i++)
                {
                    CCAction action = element.actions[i];

                    if (action.tag == (int)tag && action.originalTarget == target)
                    {
                        removeActionAtIndex(i, element);
                        break;
                    }
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// Removes an action given an action reference.
        /// </summary>
        public void removeAction(CCAction action)
        {
            if (action == null)
            {
                return;
            }

            CCObject target = action.originalTarget;

            if (m_pTargets.ContainsKey(target))
            {
                tHashElement element = m_pTargets[target];
                int          i       = element.actions.IndexOf(action);

                if (i != -1)
                {
                    removeActionAtIndex(i, element);
                }
            }
            else
            {
                CCLog.Log("cocos2d: removeAction: Target not found");
            }
        }