Exemplo n.º 1
0
        /// <summary>
        /// 分发事件
        /// </summary>
        public void DispatchEvent(int evtType, Param param = null)
        {
            if (HotfixMgr.IsFromHotfix(param))
            {
                LOGGER.Fatal($"不能添加hotfix工程的类型作为参数 DispatchEvent {param.GetType()}");
                return;
            }

            Event evt = new Event();

            evt.EventId = evtType;
            evt.Data    = param;

            if (eventHandlers.ContainsKey(evtType))
            {
                var list = eventHandlers[evtType];
                foreach (var handlerType in list)
                {
                    try
                    {
                        var handler   = HotfixMgr.GetInstance <IEventListener>(handlerType);
                        var agentType = handler.GetType().BaseType.GenericTypeArguments[1];
                        if (agentType.GetInterface(typeof(IComponentActorAgent).FullName) != null)
                        {
                            //actor
                            ownerActor.SendAsync(() => handler.InternalHandleEvent(ownerActor.GetAgent(agentType), evt));
                        }
                        else if (agentType.GetInterface(typeof(IComponentAgent).FullName) != null)
                        {
                            //component
                            var compType = agentType.BaseType.GenericTypeArguments[0];
                            ownerActor.SendAsync(async() => {
                                var comp = await ownerActor.GetComponent(compType);
                                await handler.InternalHandleEvent(comp.GetAgent(agentType), evt);
                            }, false);
                        }
                    }
                    catch (Exception e)
                    {
                        LOGGER.Error(e.ToString());
                    }
                }
            }
        }