AnimatorComponentSystem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using CMDType = ET.Client.AnimatorComponent.CommandType;
  2. using AniType = Mono.AnimationData.AnimationType;
  3. namespace ET.Client
  4. {
  5. [FriendOfAttribute(typeof(GameObjectComponent))]
  6. [FriendOf(typeof(AnimatorComponent))]
  7. public static class AnimatorComponentSystem
  8. {
  9. [ObjectSystem]
  10. public class AnimatorComponentAwakeSystem : AwakeSystem<AnimatorComponent>
  11. {
  12. protected override void Awake(AnimatorComponent self)
  13. {
  14. self.Awake();
  15. }
  16. }
  17. [ObjectSystem]
  18. public class AnimatorComponentUpdateSystem : UpdateSystem<AnimatorComponent>
  19. {
  20. protected override void Update(AnimatorComponent self)
  21. {
  22. self.Update();
  23. }
  24. }
  25. [ObjectSystem]
  26. public class AnimatorComponentDestroySystem : DestroySystem<AnimatorComponent>
  27. {
  28. protected override void Destroy(AnimatorComponent self)
  29. {
  30. self.AniData.Animancer.Stop();
  31. }
  32. }
  33. //===AnimatorComponent 扩展方法-------
  34. public static void AppendCommand(this AnimatorComponent self, CMDType type)
  35. {
  36. self.Commands.Add(new AnimatorComponent.Command(type));
  37. }
  38. public static void AppendCommand(this AnimatorComponent self, AnimatorComponent.Command cmd)
  39. {
  40. self.Commands.Add(cmd);
  41. }
  42. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  43. public static void Awake(this AnimatorComponent self)
  44. {
  45. GameObjectComponent gameObjectComponent = self.GetParent<Unit>().GetComponent<GameObjectComponent>();
  46. self.AniData = gameObjectComponent.GameObject.GetComponent<Mono.AnimationData>();
  47. self.DoingType = AniType.Dead;
  48. self.ExeCommand(AnimatorComponent.CMDIdle);
  49. }
  50. public static void Update(this AnimatorComponent self)
  51. {
  52. self.ExeCommand(self.FilterCmdList());
  53. }
  54. //执行指令
  55. private static void ExeCommand(this AnimatorComponent self, AnimatorComponent.Command? cmd)
  56. {
  57. if (cmd == null) return;
  58. var ani = cmd?.Type switch
  59. {
  60. CMDType.Idle => AniType.Idle,
  61. CMDType.Run => AniType.Run,
  62. CMDType.StopRun => AniType.Idle,
  63. CMDType.StopSkill => AniType.Idle,
  64. CMDType.Skill0 => AniType.Skill0,
  65. CMDType.Skill1 => AniType.Skill1,
  66. CMDType.Skill2 => AniType.Skill2,
  67. CMDType.Skill3 => AniType.Skill3,
  68. CMDType.Dead => AniType.Dead,
  69. _ => AniType.Idle
  70. };
  71. if (self.DoingType == ani) return;
  72. self.DoingType = ani;
  73. self.AniData.PlayAnimation(ani, () => {
  74. self.DoingType = AniType.Idle;
  75. self.AniData.PlayAnimation(AniType.Idle);
  76. });
  77. }
  78. //从指令队列中分析出当前需要执行的指令
  79. private static AnimatorComponent.Command? FilterCmdList(this AnimatorComponent self)
  80. {
  81. var cmds = self.Commands;
  82. if (cmds.Count <= 0)
  83. {
  84. return null;
  85. }
  86. var cmd = cmds[0];
  87. for(var i=1; i<cmds.Count; i++)
  88. {
  89. var next = cmds[i];
  90. if(next.Type == CMDType.StopRun && cmd.Type == CMDType.Run)
  91. {
  92. cmd = AnimatorComponent.CMDIdle;
  93. }
  94. else if(next.Type == CMDType.StopSkill && cmd.Type >= CMDType.Skill0 && cmd.Type <= CMDType.Skill3)
  95. {
  96. cmd = AnimatorComponent.CMDIdle;
  97. }
  98. else if(next.Type > cmd.Type && cmd.Type < CMDType.Skill0)
  99. {
  100. cmd = next;
  101. }
  102. }
  103. cmds.Clear();
  104. return cmd;
  105. }
  106. }
  107. }