AnimatorComponentSystem.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using CMDType = ET.AnimatorEventType;
  2. using AniType = Mono.AnimationData.AnimationType;
  3. using UnityEngine;
  4. using System.Text.RegularExpressions;
  5. namespace ET.Client
  6. {
  7. [Event(SceneType.Current)]
  8. public class AnimatorEventHandler : AEvent<EventType.PlayAnimatorEvent>
  9. {
  10. protected override async ETTask Run(Scene scene, EventType.PlayAnimatorEvent args)
  11. {
  12. var component = ModelViewComponent.Instance.GetChild<AnimatorComponent>(args.UnitId);
  13. if (component == null)
  14. {
  15. Log.Debug($"Not found AnimatorComponent of object{args.UnitId}");
  16. return;
  17. }
  18. if(args.CommandType == CMDType.Skill)
  19. {
  20. var m = Regex.Match(args.SkillName, "Skill(\\d+)");
  21. if (m.Success)
  22. {
  23. int n = System.Convert.ToInt32(m.Groups[1].Value, 10);
  24. if(n >=0 && AniType.SkillMax > AniType.Skill0 + n)
  25. {
  26. AnimatorCommand cmd = new AnimatorCommand((AniType)(AniType.Skill0 + n)) {
  27. Duration = args.Duration,
  28. Speed = args.Speed,
  29. GroupId = args.GroupId,
  30. Loop = args.Loop
  31. };
  32. component.AppendCommand(cmd);
  33. return;
  34. }
  35. }
  36. Log.Error($"Not support skill animation: {args.SkillName}");
  37. }
  38. else
  39. {
  40. AnimatorCommand cmd = args.CommandType switch
  41. {
  42. AnimatorEventType.Idle => AnimatorComponent.CMDIdle,
  43. AnimatorEventType.Run => AnimatorComponent.CMDRun,
  44. AnimatorEventType.Dead => AnimatorComponent.CMDDead,
  45. _ => AnimatorComponent.CMDIdle
  46. };
  47. component.AppendCommand(cmd);
  48. }
  49. await ETTask.CompletedTask;
  50. }
  51. }
  52. [FriendOf(typeof(AnimatorComponent))]
  53. public static class AnimatorComponentSystem
  54. {
  55. [ObjectSystem]
  56. public class AnimatorComponentAwakeSystem : AwakeSystem<AnimatorComponent, GameObject>
  57. {
  58. protected override void Awake(AnimatorComponent self, GameObject go)
  59. {
  60. self.GameObject = go;
  61. self.Awake();
  62. }
  63. }
  64. [ObjectSystem]
  65. public class AnimatorComponentUpdateSystem : UpdateSystem<AnimatorComponent>
  66. {
  67. protected override void Update(AnimatorComponent self)
  68. {
  69. self.Update();
  70. }
  71. }
  72. [ObjectSystem]
  73. public class AnimatorComponentDestroySystem : DestroySystem<AnimatorComponent>
  74. {
  75. protected override void Destroy(AnimatorComponent self)
  76. {
  77. self.AniData.Animancer.Stop();
  78. UnityEngine.Object.Destroy(self.GameObject);
  79. }
  80. }
  81. //===AnimatorComponent 扩展方法-------
  82. public static void AppendCommand(this AnimatorComponent self, AnimatorCommand cmd)
  83. {
  84. self.Commands.Add(cmd);
  85. }
  86. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  87. public static void Awake(this AnimatorComponent self)
  88. {
  89. self.AniData = self.GameObject.GetComponent<Mono.AnimationData>();
  90. self.ExeCommand(AnimatorComponent.CMDIdle);
  91. }
  92. public static void Update(this AnimatorComponent self)
  93. {
  94. var cmd = self.FilterCmdList();
  95. if(cmd != null)
  96. {
  97. self.ExeCommand(cmd);
  98. }
  99. }
  100. //执行指令
  101. private static void ExeCommand(this AnimatorComponent self, AnimatorCommand cmd)
  102. {
  103. self.DoingCmd = (AnimatorCommand)cmd;
  104. self.AniData.PlayAnimation(cmd.Type);
  105. }
  106. //从指令队列中分析出当前需要执行的指令
  107. private static AnimatorCommand FilterCmdList(this AnimatorComponent self)
  108. {
  109. var cmds = self.Commands;
  110. if (cmds.Count <= 0)
  111. {
  112. return null;
  113. }
  114. /*var cmd = cmds[0];
  115. for(var i=1; i<cmds.Count; i++)
  116. {
  117. var next = cmds[i];
  118. if(next.Type == CMDType.StopRun && cmd.Type == CMDType.Run)
  119. {
  120. cmd = AnimatorComponent.CMDIdle;
  121. }
  122. else if(next.Type == CMDType.StopSkill && cmd.Type >= CMDType.Skill0 && cmd.Type <= CMDType.Skill3)
  123. {
  124. cmd = AnimatorComponent.CMDIdle;
  125. }
  126. else if(next.Type > cmd.Type && cmd.Type < CMDType.Skill0)
  127. {
  128. cmd = next;
  129. }
  130. }*/
  131. var cmd = cmds[cmds.Count - 1];
  132. if(cmd.GroupId > 0)
  133. {
  134. int delIndex = -1;
  135. for(var i = cmds.Count -2; i >= 0; i--)
  136. {
  137. if (cmds[i].GroupId != cmd.GroupId)
  138. {
  139. delIndex = i;
  140. break;
  141. }
  142. }
  143. if(delIndex >= 0)
  144. {
  145. cmds.RemoveRange(0, delIndex + 1);
  146. }
  147. }
  148. else
  149. {
  150. cmds.Clear();
  151. }
  152. return cmd;
  153. }
  154. }
  155. }