提交 b32a9597 编写于 作者: T tanghai

EventSystem 事件方法修改,改名成publish,参数改成统一为struct,这样的好处是,不会出现订阅的事件与发布的事件参数不一致的问题

上级 d218f30e
......@@ -36,7 +36,7 @@ namespace ET
Log.Info($"server start........................ {Game.Scene.Id}");
// 先加这里,后面删掉
Game.EventSystem.Run(EventIdType.AfterScenesAdd);
Game.EventSystem.Publish(new EventType.AppStart());
while (true)
{
......
......@@ -2,10 +2,15 @@
namespace ET
{
[Event(EventIdType.AfterScenesAdd)]
public class AfterScenesAdd_CreateScene: AEvent
[Event]
public class AppStart_Init: AEvent<EventType.AppStart>
{
public override void Run()
public override void Run(EventType.AppStart args)
{
RunAsync().Coroutine();
}
public async ETVoid RunAsync()
{
Game.Scene.AddComponent<ConfigComponent>();
......@@ -30,11 +35,6 @@ namespace ET
Game.Scene.AddComponent<NetInnerComponent, string>(processConfig.InnerAddress);
RunInner().Coroutine();
}
public async ETVoid RunInner()
{
var processScenes = StartSceneConfigCategory.Instance.GetByProcess(IdGenerater.Process);
foreach (StartSceneConfig startConfig in processScenes)
{
......
namespace ET
{
public static class EventIdType
{
public const string NumbericChange = "NumbericChange";
public const string AfterScenesAdd = "AfterScenesAdd";
}
}
\ No newline at end of file
namespace ET
{
namespace EventType
{
public struct AppStart
{
}
}
}
\ No newline at end of file
......@@ -4,7 +4,7 @@ namespace ET
{
public class Options: Entity
{
[Option("process", Required = false, Default = 1)]
[Option("Process", Required = false, Default = 1)]
public int Process { get; set; }
}
}
\ No newline at end of file
......@@ -77,9 +77,6 @@
<Compile Include="..\..\Unity\Assets\Model\Base\Object\EntityFactory.cs">
<Link>Base\Object\EntityFactory.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Base\Object\EventProxy.cs">
<Link>Base\Object\EventProxy.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Base\Object\EventSystem.cs">
<Link>Base\Object\EventSystem.cs</Link>
</Compile>
......
namespace ET
{
// 分发数值监听
[Event(EventIdType.NumbericChange)]
public class NumericChangeEvent_NotifyWatcher: AEvent<long, NumericType, int>
[Event]
public class NumericChangeEvent_NotifyWatcher: AEvent<EventType.NumbericChange>
{
public override void Run(long id, NumericType numericType, int value)
public override void Run(EventType.NumbericChange args)
{
Game.Scene.GetComponent<NumericWatcherComponent>().Run(numericType, id, value);
Game.Scene.GetComponent<NumericWatcherComponent>().Run(args.NumericType, args.Parent.Id, args.New);
}
}
}
......@@ -2,12 +2,12 @@
namespace ET
{
[Event(EventIdType.LoadingBegin)]
public class LoadingBeginEvent_CreateLoadingUI : AEvent<Entity>
[Event()]
public class LoadingBeginEvent_CreateLoadingUI : AEvent<EventType.LoadingBegin>
{
public override void Run(Entity domain)
public override void Run(EventType.LoadingBegin args)
{
UI ui = UILoadingFactory.Create(domain);
UI ui = UILoadingFactory.Create(args.Scene);
Game.Scene.GetComponent<UIComponent>().Add(ui);
}
}
......
namespace ET
{
[Event(EventIdType.LoadingFinish)]
public class LoadingFinishEvent_RemoveLoadingUI : AEvent
[Event]
public class LoadingFinishEvent_RemoveLoadingUI : AEvent<EventType.LoadingFinish>
{
public override void Run()
public override void Run(EventType.LoadingFinish args)
{
Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILoading);
}
......

namespace ET
namespace ET
{
[Event(EventIdType.EnterMapFinish)]
public class EnterMapFinish_RemoveLobbyUI: AEvent
[Event]
public class EnterMapFinish_RemoveLobbyUI: AEvent<EventType.EnterMapFinish>
{
public override void Run()
public override void Run(EventType.EnterMapFinish args)
{
Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILobby);
Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle(UIType.UILobby.StringToAB());
......
......@@ -2,10 +2,10 @@
namespace ET
{
[Event(EventIdType.LoginFinish)]
public class LoginFinish_CreateLobbyUI: AEvent
[Event]
public class LoginFinish_CreateLobbyUI: AEvent<EventType.LoginFinish>
{
public override void Run()
public override void Run(EventType.LoginFinish args)
{
UI ui = UILobbyFactory.Create();
Game.Scene.GetComponent<UIComponent>().Add(ui);
......

namespace ET
{
[Event(EventIdType.InitSceneStart)]
public class InitSceneStart_CreateLoginUI: AEvent
{
public override void Run()
{
UI ui = UILoginFactory.Create();
Game.Scene.GetComponent<UIComponent>().Add(ui);
}
}
}
......@@ -2,10 +2,10 @@
namespace ET
{
[Event(EventIdType.LoginFinish)]
public class LoginFinish_RemoveLoginUI: AEvent
[Event]
public class LoginFinish_RemoveLoginUI: AEvent<EventType.LoginFinish>
{
public override void Run()
public override void Run(EventType.LoginFinish args)
{
Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILogin);
Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle(UIType.UILogin.StringToAB());
......
......@@ -2,10 +2,10 @@
namespace ET
{
[Event(EventIdType.AfterUnitCreate)]
public class AfterUnitCreate_CreateUnitView: AEvent<Unit>
[Event]
public class AfterUnitCreate_CreateUnitView: AEvent<EventType.AfterUnitCreate>
{
public override void Run(Unit unit)
public override void Run(EventType.AfterUnitCreate args)
{
// Unit View层
ResourcesComponent resourcesComponent = Game.Scene.GetComponent<ResourcesComponent>();
......@@ -14,7 +14,7 @@ namespace ET
GameObject go = UnityEngine.Object.Instantiate(prefab);
unit.AddComponent<AnimatorComponent>();
args.Unit.AddComponent<AnimatorComponent>();
}
}
}
\ No newline at end of file
......@@ -5,11 +5,5 @@ namespace ET
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class EventAttribute: BaseAttribute
{
public string Type { get; }
public EventAttribute(string type)
{
this.Type = type;
}
}
}
\ No newline at end of file
namespace ET
{
public static class EventIdType
{
public const string RecvHotfixMessage = "RecvHotfixMessage";
public const string BehaviorTreeRunTreeEvent = "BehaviorTreeRunTreeEvent";
public const string BehaviorTreeOpenEditor = "BehaviorTreeOpenEditor";
public const string BehaviorTreeClickNode = "BehaviorTreeClickNode";
public const string BehaviorTreeAfterChangeNodeType = "BehaviorTreeAfterChangeNodeType";
public const string BehaviorTreeCreateNode = "BehaviorTreeCreateNode";
public const string BehaviorTreePropertyDesignerNewCreateClick = "BehaviorTreePropertyDesignerNewCreateClick";
public const string BehaviorTreeMouseInNode = "BehaviorTreeMouseInNode";
public const string BehaviorTreeConnectState = "BehaviorTreeConnectState";
public const string BehaviorTreeReplaceClick = "BehaviorTreeReplaceClick";
public const string BehaviorTreeRightDesignerDrag = "BehaviorTreeRightDesignerDrag";
public const string SessionRecvMessage = "SessionRecvMessage";
public const string NumbericChange = "NumbericChange";
public const string MessageDeserializeFinish = "MessageDeserializeFinish";
public const string SceneChange = "SceneChange";
public const string FrameUpdate = "FrameUpdate";
public const string LoadingBegin = "LoadingBegin";
public const string LoadingFinish = "LoadingFinish";
public const string TestHotfixSubscribMonoEvent = "TestHotfixSubscribMonoEvent";
public const string MaxModelEvent = "MaxModelEvent";
public const string InitSceneStart = "InitSceneStart";
public const string LoginFinish = "LoginFinish";
public const string EnterMapFinish = "EnterMapFinish";
public const string AfterUnitCreate = "AfterUnitCreate";
}
}
\ No newline at end of file
......@@ -2,111 +2,8 @@
namespace ET
{
public interface IEvent
public abstract class AEvent<A> where A: struct
{
void Handle();
void Handle(object a);
void Handle(object a, object b);
void Handle(object a, object b, object c);
}
public abstract class AEvent : IEvent
{
public void Handle()
{
this.Run();
}
public void Handle(object a)
{
throw new NotImplementedException();
}
public void Handle(object a, object b)
{
throw new NotImplementedException();
}
public void Handle(object a, object b, object c)
{
throw new NotImplementedException();
}
public abstract void Run();
}
public abstract class AEvent<A>: IEvent
{
public void Handle()
{
throw new NotImplementedException();
}
public void Handle(object a)
{
this.Run((A)a);
}
public void Handle(object a, object b)
{
throw new NotImplementedException();
}
public void Handle(object a, object b, object c)
{
throw new NotImplementedException();
}
public abstract void Run(A a);
}
public abstract class AEvent<A, B>: IEvent
{
public void Handle()
{
throw new NotImplementedException();
}
public void Handle(object a)
{
throw new NotImplementedException();
}
public void Handle(object a, object b)
{
this.Run((A)a, (B)b);
}
public void Handle(object a, object b, object c)
{
throw new NotImplementedException();
}
public abstract void Run(A a, B b);
}
public abstract class AEvent<A, B, C>: IEvent
{
public void Handle()
{
throw new NotImplementedException();
}
public void Handle(object a)
{
throw new NotImplementedException();
}
public void Handle(object a, object b)
{
throw new NotImplementedException();
}
public void Handle(object a, object b, object c)
{
this.Run((A)a, (B)b, (C)c);
}
public abstract void Run(A a, B b, C c);
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
namespace ET
{
public class EventProxy: IEvent
{
public Action<List<object>> action;
public List<object> param = new List<object>();
public EventProxy(Action<List<object>> action)
{
this.action = action;
}
public void Handle()
{
this.param.Clear();
this.action.Invoke(this.param);
}
public void Handle(object a)
{
this.param.Clear();
this.param.Add(a);
this.action.Invoke(this.param);
}
public void Handle(object a, object b)
{
this.param.Clear();
this.param.Add(a);
this.param.Add(b);
this.action.Invoke(this.param);
}
public void Handle(object a, object b, object c)
{
this.param.Clear();
this.param.Add(a);
this.param.Add(b);
this.param.Add(c);
this.action.Invoke(this.param);
}
}
}
......@@ -36,7 +36,7 @@ namespace ET
private readonly UnOrderMultiMapSet<Type, Type> types = new UnOrderMultiMapSet<Type, Type>();
private readonly Dictionary<string, List<object>> allEvents = new Dictionary<string, List<object>>();
private readonly Dictionary<Type, List<object>> allEvents = new Dictionary<Type, List<object>>();
private readonly UnOrderMultiMap<Type, IAwakeSystem> awakeSystems = new UnOrderMultiMap<Type, IAwakeSystem>();
......@@ -140,19 +140,12 @@ namespace ET
this.allEvents.Clear();
foreach (Type type in types[typeof(EventAttribute)])
{
object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
foreach (object attr in attrs)
object obj = Activator.CreateInstance(type);
if (!this.allEvents.ContainsKey(type))
{
EventAttribute aEventAttribute = (EventAttribute)attr;
object obj = Activator.CreateInstance(type);
IEvent iEvent = obj as IEvent;
if (iEvent == null)
{
Log.Error($"{obj.GetType().Name} 没有继承IEvent");
}
this.RegisterEvent(aEventAttribute.Type, iEvent);
this.allEvents.Add(type, new List<object>());
}
this.allEvents[type].Add(obj);
}
this.Load();
......@@ -162,15 +155,6 @@ namespace ET
{
return this.assemblies[name];
}
public void RegisterEvent(string eventId, IEvent e)
{
if (!this.allEvents.ContainsKey(eventId))
{
this.allEvents.Add(eventId, new List<object>());
}
this.allEvents[eventId].Add(e);
}
public HashSet<Type> GetTypes(Type systemAttributeType)
{
......@@ -628,35 +612,10 @@ namespace ET
ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
}
public void Run(string type)
{
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
{
return;
}
foreach (object obj in iEvents)
{
try
{
if (!(obj is AEvent aEvent))
{
Log.Error($"event error: {obj.GetType().Name}");
continue;
}
aEvent.Run();
}
catch (Exception e)
{
Log.Error(e);
}
}
}
public void Run<A>(string type, A a)
public void Publish<T>(T a) where T: struct
{
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
if (!this.allEvents.TryGetValue(typeof(T), out iEvents))
{
return;
}
......@@ -664,7 +623,7 @@ namespace ET
{
try
{
if (!(obj is AEvent<A> aEvent))
if (!(obj is AEvent<T> aEvent))
{
Log.Error($"event error: {obj.GetType().Name}");
continue;
......@@ -678,55 +637,6 @@ namespace ET
}
}
public void Run<A, B>(string type, A a, B b)
{
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
{
return;
}
foreach (object obj in iEvents)
{
try
{
if (!(obj is AEvent<A, B> aEvent))
{
Log.Error($"event error: {obj.GetType().Name}");
continue;
}
aEvent.Run(a, b);
}
catch (Exception e)
{
Log.Error(e);
}
}
}
public void Run<A, B, C>(string type, A a, B b, C c)
{
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
{
return;
}
foreach (object obj in iEvents)
{
try
{
if (!(obj is AEvent<A, B, C> aEvent))
{
Log.Error($"event error: {obj.GetType().Name}");
continue;
}
aEvent.Run(a, b, c);
}
catch (Exception e)
{
Log.Error(e);
}
}
}
public override string ToString()
{
......
......@@ -34,7 +34,7 @@ namespace ET
PlayerComponent playerComponent = Game.Scene.GetComponent<PlayerComponent>();
playerComponent.MyPlayer = player;
Game.EventSystem.Run(EventIdType.LoginFinish);
Game.EventSystem.Publish(new EventType.LoginFinish());
// 测试消息有成员是class类型
G2C_PlayerInfo g2CPlayerInfo = (G2C_PlayerInfo) await SessionComponent.Instance.Session.Call(new C2G_PlayerInfo());
......
......@@ -26,7 +26,7 @@ namespace ET
Game.Scene.AddComponent<OperaComponent>();
Game.EventSystem.Run(EventIdType.EnterMapFinish);
Game.EventSystem.Publish(new EventType.EnterMapFinish());
}
catch (Exception e)
{
......
......@@ -12,7 +12,7 @@ namespace ET
unit.AddComponent<TurnComponent>();
unit.AddComponent<UnitPathComponent>();
Game.EventSystem.Run(EventIdType.AfterUnitCreate, unit);
Game.EventSystem.Publish(new EventType.AfterUnitCreate() {Unit = unit});
UnitComponent unitComponent = Game.Scene.GetComponent<UnitComponent>();
unitComponent.Add(unit);
......
namespace ET
{
namespace EventType
{
public struct LoginFinish
{}
public struct LoadingBegin
{
public Scene Scene;
}
public struct LoadingFinish
{
}
public struct EnterMapFinish
{}
public struct AfterUnitCreate
{
public Unit Unit;
}
}
}
\ No newline at end of file
......@@ -17,12 +17,12 @@ namespace ET
{
await bundleDownloaderComponent.StartAsync(url);
Game.EventSystem.Run(EventIdType.LoadingBegin);
Game.EventSystem.Publish(new EventType.LoadingBegin() {Scene = Game.Scene});
await bundleDownloaderComponent.DownloadAsync(url);
}
Game.EventSystem.Run(EventIdType.LoadingFinish);
Game.EventSystem.Publish(new EventType.LoadingFinish());
Game.Scene.GetComponent<ResourcesComponent>().LoadOneBundle("StreamingAssets");
ResourcesComponent.AssetBundleManifestObject = (AssetBundleManifest)Game.Scene.GetComponent<ResourcesComponent>().GetAsset("StreamingAssets", "AssetBundleManifest");
......
......@@ -2,6 +2,17 @@
namespace ET
{
namespace EventType
{
public struct NumbericChange
{
public Entity Parent;
public NumericType NumericType;
public int Old;
public int New;
}
}
[ObjectSystem]
public class NumericComponentAwakeSystem : AwakeSystem<NumericComponent>
{
......@@ -92,9 +103,16 @@ namespace ET
// 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100,也有些buff增加10%速度,所以一个值可以由5个值进行控制其最终结果
// final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100;
int old = this.NumericDic[final];
int result = (int)(((this.GetByKey(bas) + this.GetByKey(add)) * (100 + this.GetAsFloat(pct)) / 100f + this.GetByKey(finalAdd)) * (100 + this.GetAsFloat(finalPct)) / 100f * 10000);
this.NumericDic[final] = result;
Game.EventSystem.Run(EventIdType.NumbericChange, this.Parent.Id, (NumericType) final, result);
Game.EventSystem.Publish(new EventType.NumbericChange()
{
Parent = this.Parent,
NumericType = (NumericType) final,
Old = old,
New = result
});
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LangVersion>latest</LangVersion>
......@@ -67,7 +67,6 @@
<Compile Include="Assets\HotfixView\Demo\UI\UILobby\LoginFinish_CreateLobbyUI.cs" />
<Compile Include="Assets\HotfixView\Demo\UI\UILobby\UILobbyComponentSystem.cs" />
<Compile Include="Assets\HotfixView\Demo\UI\UILobby\UILobbyFactory.cs" />
<Compile Include="Assets\HotfixView\Demo\UI\UILogin\InitSceneStart_CreateLoginUI.cs" />
<Compile Include="Assets\HotfixView\Demo\UI\UILogin\LoginFinish_RemoveLoginUI.cs" />
<Compile Include="Assets\HotfixView\Demo\UI\UILogin\UILoginComponentSystem.cs" />
<Compile Include="Assets\HotfixView\Demo\UI\UILogin\UILoginFactory.cs" />
......
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LangVersion>latest</LangVersion>
......@@ -73,7 +73,6 @@
<Compile Include="Assets\Model\Base\Event\Env.cs" />
<Compile Include="Assets\Model\Base\Event\EnvKey.cs" />
<Compile Include="Assets\Model\Base\Event\EventAttribute.cs" />
<Compile Include="Assets\Model\Base\Event\EventIdType.cs" />
<Compile Include="Assets\Model\Base\Event\IEvent.cs" />
<Compile Include="Assets\Model\Base\Helper\ByteHelper.cs" />
<Compile Include="Assets\Model\Base\Helper\Dumper.cs" />
......@@ -101,7 +100,6 @@
<Compile Include="Assets\Model\Base\Object\EntityCreateComponet.cs" />
<Compile Include="Assets\Model\Base\Object\EntityEventAttribute.cs" />
<Compile Include="Assets\Model\Base\Object\EntityFactory.cs" />
<Compile Include="Assets\Model\Base\Object\EventProxy.cs" />
<Compile Include="Assets\Model\Base\Object\EventSystem.cs" />
<Compile Include="Assets\Model\Base\Object\HideInHierarchy.cs" />
<Compile Include="Assets\Model\Base\Object\IAwakeSystem.cs" />
......@@ -158,6 +156,7 @@
<Compile Include="Assets\Model\Entity\SceneChangeComponent.cs" />
<Compile Include="Assets\Model\Entity\SceneType.cs" />
<Compile Include="Assets\Model\Entity\TimerComponent.cs" />
<Compile Include="Assets\Model\EventType.cs" />
<Compile Include="Assets\Model\Helper\BundleHelper.cs" />
<Compile Include="Assets\Model\Helper\PathHelper.cs" />
<Compile Include="Assets\Model\Helper\PositionHelper.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册