ActorComponentSystem.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.ActorType = ActorType.Common;
  13. self.Queue.Clear();
  14. }
  15. }
  16. [ObjectSystem]
  17. public class ActorComponentAwake1System : AwakeSystem<ActorComponent, string>
  18. {
  19. public override void Awake(ActorComponent self, string actorType)
  20. {
  21. self.ActorType = actorType;
  22. self.Queue.Clear();
  23. }
  24. }
  25. [ObjectSystem]
  26. public class ActorComponentStartSystem : StartSystem<ActorComponent>
  27. {
  28. public override void Start(ActorComponent self)
  29. {
  30. self.HandleAsync();
  31. }
  32. }
  33. [ObjectSystem]
  34. public class ActorComponentDestroySystem : DestroySystem<ActorComponent>
  35. {
  36. public override void Destroy(ActorComponent self)
  37. {
  38. }
  39. }
  40. /// <summary>
  41. /// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server, 接收的消息将会队列处理
  42. /// </summary>
  43. public static class ActorComponentEx
  44. {
  45. public static async Task AddLocation(this ActorComponent self)
  46. {
  47. await Game.Scene.GetComponent<LocationProxyComponent>().Add(self.Entity.Id, self.Entity.InstanceId);
  48. }
  49. public static async Task RemoveLocation(this ActorComponent self)
  50. {
  51. await Game.Scene.GetComponent<LocationProxyComponent>().Remove(self.Entity.Id);
  52. }
  53. public static void Add(this ActorComponent self, ActorMessageInfo info)
  54. {
  55. self.Queue.Enqueue(info);
  56. if (self.Tcs == null)
  57. {
  58. return;
  59. }
  60. var t = self.Tcs;
  61. self.Tcs = null;
  62. t.SetResult(self.Queue.Dequeue());
  63. }
  64. private static Task<ActorMessageInfo> GetAsync(this ActorComponent self)
  65. {
  66. if (self.Queue.Count > 0)
  67. {
  68. return Task.FromResult(self.Queue.Dequeue());
  69. }
  70. self.Tcs = new TaskCompletionSource<ActorMessageInfo>();
  71. return self.Tcs.Task;
  72. }
  73. public static async void HandleAsync(this ActorComponent self)
  74. {
  75. ActorMessageDispatherComponent actorMessageDispatherComponent = Game.Scene.GetComponent<ActorMessageDispatherComponent>();
  76. while (true)
  77. {
  78. if (self.IsDisposed)
  79. {
  80. return;
  81. }
  82. try
  83. {
  84. ActorMessageInfo info = await self.GetAsync();
  85. // 返回null表示actor已经删除,协程要终止
  86. if (info.Message == null)
  87. {
  88. return;
  89. }
  90. // 根据这个actor的类型分发给相应的ActorHandler处理
  91. await actorMessageDispatherComponent.ActorTypeHandle(self.ActorType, (Entity)self.Parent, info);
  92. }
  93. catch (Exception e)
  94. {
  95. Log.Error(e);
  96. }
  97. }
  98. }
  99. }
  100. }