OperaComponentSystem.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using UnityEngine;
  2. namespace ET.Client
  3. {
  4. [EntitySystemOf(typeof(OperaComponent))]
  5. public static partial class OperaComponentSystem
  6. {
  7. [EntitySystem]
  8. private static void Awake(this OperaComponent self)
  9. {
  10. self.mapMask = LayerMask.GetMask("Map");
  11. }
  12. [EntitySystem]
  13. private static void Update(this OperaComponent self)
  14. {
  15. if (Input.GetMouseButtonDown(1))
  16. {
  17. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  18. RaycastHit hit;
  19. if (Physics.Raycast(ray, out hit, 1000, self.mapMask))
  20. {
  21. C2M_PathfindingResult c2MPathfindingResult = C2M_PathfindingResult.Create();
  22. c2MPathfindingResult.Position = hit.point;
  23. self.Root().GetComponent<ClientSenderComponent>().Send(c2MPathfindingResult);
  24. }
  25. }
  26. if (Input.GetKeyDown(KeyCode.R))
  27. {
  28. CodeLoader.Instance.Reload();
  29. }
  30. if (Input.GetKeyDown(KeyCode.Q))
  31. {
  32. self.Test1().NoContext();
  33. }
  34. if (Input.GetKeyDown(KeyCode.W))
  35. {
  36. self.Test2().NoContext();
  37. }
  38. if (Input.GetKeyDown(KeyCode.A))
  39. {
  40. self.TestCancelAfter().WithContext(new ETCancellationToken());
  41. }
  42. if (Input.GetKeyDown(KeyCode.T))
  43. {
  44. C2M_TransferMap c2MTransferMap = C2M_TransferMap.Create();
  45. self.Root().GetComponent<ClientSenderComponent>().Call(c2MTransferMap).NoContext();
  46. }
  47. }
  48. private static async ETTask Test1(this OperaComponent self)
  49. {
  50. Log.Debug($"Croutine 1 start1 ");
  51. using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(1, 20000, 3000))
  52. {
  53. await self.Root().GetComponent<TimerComponent>().WaitAsync(6000);
  54. }
  55. Log.Debug($"Croutine 1 end1");
  56. }
  57. private static async ETTask Test2(this OperaComponent self)
  58. {
  59. ETCancellationToken oldCancellationToken = await ETTaskHelper.GetContextAsync<ETCancellationToken>();
  60. Log.Debug($"Croutine 2 start2");
  61. using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(1, 20000, 3000))
  62. {
  63. await self.Root().GetComponent<TimerComponent>().WaitAsync(1000);
  64. }
  65. Log.Debug($"Croutine 2 end2");
  66. }
  67. private static async ETTask TestCancelAfter(this OperaComponent self)
  68. {
  69. ETCancellationToken oldCancellationToken = await ETTaskHelper.GetContextAsync<ETCancellationToken>();
  70. Log.Debug($"TestCancelAfter start");
  71. ETCancellationToken newCancellationToken = new();
  72. await self.Root().GetComponent<TimerComponent>().WaitAsync(3000).TimeoutAsync(newCancellationToken, 1000);
  73. if (newCancellationToken.IsCancel())
  74. {
  75. Log.Debug($"TestCancelAfter newCancellationToken is cancel!");
  76. }
  77. if (oldCancellationToken != null && !oldCancellationToken.IsCancel())
  78. {
  79. Log.Debug($"TestCancelAfter oldCancellationToken is not cancel!");
  80. }
  81. Log.Debug($"TestCancelAfter end");
  82. }
  83. }
  84. }