ActorComponentSystem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using ETModel;
  5. namespace ETHotfix
  6. {
  7. [ObjectSystem]
  8. public class ActorComponentAwakeSystem : AwakeSystem<ActorComponent>
  9. {
  10. public override void Awake(ActorComponent self)
  11. {
  12. self.Awake();
  13. }
  14. }
  15. [ObjectSystem]
  16. public class ActorComponentAwake1System : AwakeSystem<ActorComponent, IEntityActorHandler>
  17. {
  18. public override void Awake(ActorComponent self, IEntityActorHandler iEntityActorHandler)
  19. {
  20. self.Awake(iEntityActorHandler);
  21. }
  22. }
  23. [ObjectSystem]
  24. public class ActorComponentLoadSystem : LoadSystem<ActorComponent>
  25. {
  26. public override void Load(ActorComponent self)
  27. {
  28. self.Load();
  29. }
  30. }
  31. /// <summary>
  32. /// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server, 接收的消息将会队列处理
  33. /// </summary>
  34. public static class ActorComponentEx
  35. {
  36. public static void Awake(this ActorComponent self)
  37. {
  38. self.entityActorHandler = new CommonEntityActorHandler();
  39. self.queue = new Queue<ActorMessageInfo>();
  40. self.HandleAsync();
  41. }
  42. public static void Awake(this ActorComponent self, IEntityActorHandler iEntityActorHandler)
  43. {
  44. self.entityActorHandler = iEntityActorHandler;
  45. self.queue = new Queue<ActorMessageInfo>();
  46. self.HandleAsync();
  47. }
  48. // 热更新要重新创建接口,以便接口也能实现热更新
  49. public static void Load(this ActorComponent self)
  50. {
  51. self.entityActorHandler = (IEntityActorHandler) HotfixHelper.Create(self.entityActorHandler);
  52. }
  53. public static async Task AddLocation(this ActorComponent self)
  54. {
  55. await Game.Scene.GetComponent<LocationProxyComponent>().Add(self.Parent.InstanceId);
  56. }
  57. public static async Task RemoveLocation(this ActorComponent self)
  58. {
  59. await Game.Scene.GetComponent<LocationProxyComponent>().Remove(self.Parent.InstanceId);
  60. }
  61. public static void Add(this ActorComponent self, ActorMessageInfo info)
  62. {
  63. self.queue.Enqueue(info);
  64. if (self.tcs == null)
  65. {
  66. return;
  67. }
  68. var t = self.tcs;
  69. self.tcs = null;
  70. t.SetResult(self.queue.Dequeue());
  71. }
  72. private static Task<ActorMessageInfo> GetAsync(this ActorComponent self)
  73. {
  74. if (self.queue.Count > 0)
  75. {
  76. return Task.FromResult(self.queue.Dequeue());
  77. }
  78. self.tcs = new TaskCompletionSource<ActorMessageInfo>();
  79. return self.tcs.Task;
  80. }
  81. private static async void HandleAsync(this ActorComponent self)
  82. {
  83. while (true)
  84. {
  85. if (self.IsDisposed)
  86. {
  87. return;
  88. }
  89. try
  90. {
  91. ActorMessageInfo info = await self.GetAsync();
  92. // 返回null表示actor已经删除,协程要终止
  93. if (info.Message == null)
  94. {
  95. return;
  96. }
  97. await self.entityActorHandler.Handle(info.Session, (Entity)self.Parent, info.Message);
  98. }
  99. catch (Exception e)
  100. {
  101. Log.Error(e);
  102. }
  103. }
  104. }
  105. }
  106. }