Exemplo n.º 1
0
 /// <summary>
 /// 初始化任务池的新实例。
 /// </summary>
 public TaskPool()
 {
     m_FreeAgents    = new Stack <ITaskAgent <T> >();
     m_WorkingAgents = new GameFrameworkLinkedList <ITaskAgent <T> >();
     m_WaitingTasks  = new GameFrameworkLinkedList <T>();
     m_Paused        = false;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 处理事件结点。
        /// </summary>
        /// <param name="sender">事件源。</param>
        /// <param name="e">事件参数。</param>
        private void HandleEvent(object sender, T e)
        {
            int  eventId            = e.Id;
            bool noHandlerException = false;
            GameFrameworkLinkedList <EventHandler <T> > handlers = null;

            if (m_EventHandlers.TryGetValue(eventId, out handlers) && handlers.Count > 0)
            {
                LinkedListNode <EventHandler <T> > current = handlers.First;
                while (current != null)
                {
                    m_CachedNodes[e] = current.Next;
                    current.Value(sender, e);
                    current = m_CachedNodes[e];
                }

                m_CachedNodes.Remove(e);
            }
            else if (m_DefaultHandler != null)
            {
                m_DefaultHandler(sender, e);
            }
            else if ((m_EventPoolMode & EventPoolMode.AllowNoHandler) == 0)
            {
                noHandlerException = true;
            }

            ReferencePool.Release((IReference)e);

            if (noHandlerException)
            {
                throw new GameFrameworkException(Utility.Text.Format("Event '{0}' not allow no handler.", eventId.ToString()));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 订阅事件处理函数。
        /// </summary>
        /// <param name="id">事件类型编号。</param>
        /// <param name="handler">要订阅的事件处理函数。</param>
        public void Subscribe(int id, EventHandler <T> handler)
        {
            if (handler == null)
            {
                throw new GameFrameworkException("Event handler is invalid.");
            }

            GameFrameworkLinkedList <EventHandler <T> > handlers = null;

            if (!m_EventHandlers.TryGetValue(id, out handlers))
            {
                handlers = new GameFrameworkLinkedList <EventHandler <T> >();
                handlers.AddLast(handler);
                m_EventHandlers.Add(id, handlers);
            }
            else if ((m_EventPoolMode & EventPoolMode.AllowMultiHandler) == 0)
            {
                throw new GameFrameworkException(Utility.Text.Format("Event '{0}' not allow multi handler.", id.ToString()));
            }
            else if ((m_EventPoolMode & EventPoolMode.AllowDuplicateHandler) == 0 && Check(id, handler))
            {
                throw new GameFrameworkException(Utility.Text.Format("Event '{0}' not allow duplicate handler.", id.ToString()));
            }
            else
            {
                handlers.AddLast(handler);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 获取事件处理函数的数量。
        /// </summary>
        /// <param name="id">事件类型编号。</param>
        /// <returns>事件处理函数的数量。</returns>
        public int Count(int id)
        {
            GameFrameworkLinkedList <EventHandler <T> > handlers = null;

            if (m_EventHandlers.TryGetValue(id, out handlers))
            {
                return(handlers.Count);
            }

            return(0);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 检查是否存在事件处理函数。
        /// </summary>
        /// <param name="id">事件类型编号。</param>
        /// <param name="handler">要检查的事件处理函数。</param>
        /// <returns>是否存在事件处理函数。</returns>
        public bool Check(int id, EventHandler <T> handler)
        {
            if (handler == null)
            {
                throw new GameFrameworkException("Event handler is invalid.");
            }

            GameFrameworkLinkedList <EventHandler <T> > handlers = null;

            if (!m_EventHandlers.TryGetValue(id, out handlers))
            {
                return(false);
            }

            return(handlers.Contains(handler));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 取消订阅事件处理函数。
        /// </summary>
        /// <param name="id">事件类型编号。</param>
        /// <param name="handler">要取消订阅的事件处理函数。</param>
        public void Unsubscribe(int id, EventHandler <T> handler)
        {
            if (handler == null)
            {
                throw new GameFrameworkException("Event handler is invalid.");
            }

            GameFrameworkLinkedList <EventHandler <T> > handlers = null;

            if (!m_EventHandlers.TryGetValue(id, out handlers))
            {
                throw new GameFrameworkException(Utility.Text.Format("Event '{0}' not exists any handler.", id.ToString()));
            }

            if (m_CachedNodes.Count > 0)
            {
                foreach (KeyValuePair <object, LinkedListNode <EventHandler <T> > > cachedNode in m_CachedNodes)
                {
                    if (cachedNode.Value != null && cachedNode.Value.Value == handler)
                    {
                        m_TempNodes.Add(cachedNode.Key, cachedNode.Value.Next);
                    }
                }

                if (m_TempNodes.Count > 0)
                {
                    foreach (KeyValuePair <object, LinkedListNode <EventHandler <T> > > cachedNode in m_TempNodes)
                    {
                        m_CachedNodes[cachedNode.Key] = cachedNode.Value;
                    }

                    m_TempNodes.Clear();
                }
            }

            if (!handlers.Remove(handler))
            {
                throw new GameFrameworkException(Utility.Text.Format("Event '{0}' not exists specified handler.", id.ToString()));
            }
        }
 /// <summary>
 /// 初始化游戏框架多值字典类的新实例。
 /// </summary>
 public GameFrameworkMultiDictionary()
 {
     m_LinkedList = new GameFrameworkLinkedList <TValue>();
     m_Dictionary = new Dictionary <TKey, GameFrameworkLinkedListRange <TValue> >();
 }