ActorComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. namespace Model
  5. {
  6. public struct ActorMessageInfo
  7. {
  8. public Session Session;
  9. public IActorMessage Message;
  10. }
  11. [ObjectEvent]
  12. public class ActorComponentEvent : ObjectEvent<ActorComponent>, IAwake, IAwake<IEntityActorHandler>, IStart
  13. {
  14. public void Awake()
  15. {
  16. this.Get().Awake();
  17. }
  18. public void Awake(IEntityActorHandler iEntityActorHandler)
  19. {
  20. this.Get().Awake(iEntityActorHandler);
  21. }
  22. public void Start()
  23. {
  24. this.Get().Start();
  25. }
  26. }
  27. /// <summary>
  28. /// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server, 接收的消息将会队列处理
  29. /// </summary>
  30. public class ActorComponent: Component
  31. {
  32. private IEntityActorHandler entityActorHandler;
  33. private long actorId;
  34. // 队列处理消息
  35. private readonly Queue<ActorMessageInfo> queue = new Queue<ActorMessageInfo>();
  36. private TaskCompletionSource<ActorMessageInfo> tcs;
  37. public void Awake()
  38. {
  39. this.entityActorHandler = new CommonEntityActorHandler();
  40. }
  41. public void Awake(IEntityActorHandler iEntityActorHandler)
  42. {
  43. this.entityActorHandler = iEntityActorHandler;
  44. }
  45. public async void Start()
  46. {
  47. this.actorId = this.Owner.Id;
  48. Game.Scene.GetComponent<ActorManagerComponent>().Add(this.Owner);
  49. await Game.Scene.GetComponent<LocationProxyComponent>().Add(this.actorId);
  50. this.HandleAsync();
  51. }
  52. public void Add(ActorMessageInfo info)
  53. {
  54. this.queue.Enqueue(info);
  55. if (this.tcs == null)
  56. {
  57. return;
  58. }
  59. this.tcs?.SetResult(this.queue.Dequeue());
  60. this.tcs = null;
  61. }
  62. private Task<ActorMessageInfo> GetAsync()
  63. {
  64. if (this.queue.Count > 0)
  65. {
  66. return Task.FromResult(this.queue.Dequeue());
  67. }
  68. this.tcs = new TaskCompletionSource<ActorMessageInfo>();
  69. return this.tcs.Task;
  70. }
  71. private async void HandleAsync()
  72. {
  73. while (true)
  74. {
  75. try
  76. {
  77. ActorMessageInfo info = await this.GetAsync();
  78. await this.entityActorHandler.Handle(info.Session, this.Owner, info.Message);
  79. }
  80. catch (Exception e)
  81. {
  82. Log.Error(e.ToString());
  83. }
  84. }
  85. }
  86. public override async void Dispose()
  87. {
  88. try
  89. {
  90. if (this.Id == 0)
  91. {
  92. return;
  93. }
  94. base.Dispose();
  95. Game.Scene.GetComponent<ActorManagerComponent>().Remove(actorId);
  96. await Game.Scene.GetComponent<LocationProxyComponent>().Remove(this.actorId);
  97. }
  98. catch (Exception)
  99. {
  100. Log.Error($"unregister actor fail: {this.actorId}");
  101. }
  102. }
  103. }
  104. }