ActorComponentSystem.cs 2.8 KB

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