ActorComponentSystem.cs 2.6 KB

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