ActorComponentSystem.cs 2.7 KB

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