UnitRenderSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using CMDType = ET.EventType.PlayAnimatorEvent.AniType;
  2. using AnimationType = Mono.AnimationData.AnimationType;
  3. using UnityEngine;
  4. using System.Text.RegularExpressions;
  5. using ET.EventType;
  6. using Sirenix.Utilities;
  7. using CommonAI.Zone;
  8. using FairyGUI;
  9. namespace ET.Client
  10. {
  11. [Event]
  12. [FriendOf(typeof(ET.Client.UnitRenderComponet))]
  13. public class AnimatorEventHandler : BEvent<EventType.PlayAnimatorEvent>
  14. {
  15. protected override async ETTask OnEvent(EventType.PlayAnimatorEvent args)
  16. {
  17. await ETTask.CompletedTask;
  18. var component = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(args.UnitId);
  19. if (component == null)
  20. {
  21. Log.Debug($"Not found UnitRener of object{args.UnitId}");
  22. return;
  23. }
  24. if (component.AniData == null)
  25. {
  26. var unit = UnitMgr.Instance.GetUnit(args.UnitId);
  27. Log.Error($"UnitRender({component.GameObject.name}) not contains AnimationData component");
  28. return;
  29. }
  30. if (args.CommandType == CMDType.Skill)
  31. {
  32. var m = Regex.Match(args.SkillName, "[Ss]kill(\\d+)");
  33. if (m.Success)
  34. {
  35. int n = System.Convert.ToInt32(m.Groups[1].Value, 10);
  36. if (n >= 0 && AnimationType.SkillMax > AnimationType.Skill0 + n)
  37. {
  38. AnimatorCommand cmd = new AnimatorCommand(AnimationType.Skill0 + n)
  39. {
  40. Duration = args.Duration,
  41. Speed = args.Speed,
  42. GroupId = args.GroupId,
  43. Loop = args.Loop
  44. };
  45. component.AppendCommand(cmd);
  46. return;
  47. }
  48. }
  49. Log.Error($"Not support skill animation: {args.SkillName}");
  50. }
  51. else
  52. {
  53. AnimatorCommand cmd = args.CommandType switch
  54. {
  55. PlayAnimatorEvent.AniType.Idle => UnitRenderComponet.CMDIdle,
  56. PlayAnimatorEvent.AniType.Run => UnitRenderComponet.CMDRun,
  57. PlayAnimatorEvent.AniType.Dead => UnitRenderComponet.CMDDead,
  58. _ => UnitRenderComponet.CMDIdle
  59. };
  60. component.AppendCommand(cmd);
  61. }
  62. if(!args.Audio.IsNullOrWhitespace())
  63. {
  64. //TODO: Playaudio
  65. }
  66. }
  67. }
  68. [Event]
  69. [FriendOfAttribute(typeof(ET.Client.UnitRenderComponet))]
  70. public class ShowVipNameEventHandler : BEvent<ShowVipName>
  71. {
  72. protected override async ETTask OnEvent(ShowVipName args)
  73. {
  74. await ETTask.CompletedTask;
  75. var unitRender = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(args.ObjId);
  76. if (unitRender != null)
  77. {
  78. unitRender.NameBar.visible = true;
  79. unitRender.Vip = args.Vip;
  80. unitRender.NameBar.color = GameSetting.VipColor[args.Vip];
  81. }
  82. }
  83. }
  84. [Event]
  85. public class SyncUnitPosEventHandler : BEvent<EventType.SyncUnitPosEvent>
  86. {
  87. protected override async ETTask OnEvent(SyncUnitPosEvent args)
  88. {
  89. await ETTask.CompletedTask;
  90. var unitRender = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(args.Id);
  91. if(unitRender != null)
  92. {
  93. var transform = unitRender.GameObject.transform;
  94. transform.position = RenderUtils.UnityPosFromBattle(args.Pos);
  95. transform.rotation = RenderUtils.UnityRotationFromBattle(args.Rotation, args.RotationX);
  96. if(args.Id == UnitMgr.Instance.ActorId)
  97. {
  98. CameraMgr.FollowMe(transform.position);
  99. }
  100. unitRender.SyncHeadBarPos();
  101. }
  102. else
  103. {
  104. //Log.Debug($"Not exist unitRender: {args.Id}");
  105. }
  106. //_actor.SendUnitMove(dx, dy, true);
  107. //(_actor.ZUnit as ZoneActor).SendUnitStopMove();
  108. //StartSeek(p.X, p.Y, 0, null, false);
  109. }
  110. }
  111. [Event]
  112. public class SyncUnitHpEventHandler : BEvent<EventType.SyncUnitHpEvent>
  113. {
  114. protected override async ETTask OnEvent(SyncUnitHpEvent args)
  115. {
  116. if (GlobalViewMgr.Instance.HeadbarView.visible)
  117. {
  118. var unitRender = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(args.Id);
  119. if (unitRender != null)
  120. {
  121. unitRender.SyncHeadBarHP(args.Value).Coroutine();
  122. }
  123. }
  124. await ETTask.CompletedTask;
  125. }
  126. }
  127. [FriendOf(typeof(UnitRenderComponet))]
  128. public static class UnitRenerSystem
  129. {
  130. public class UnitRenerAwakeSystem : AwakeSystem<UnitRenderComponet, GameObject>
  131. {
  132. protected override void Awake(UnitRenderComponet self, GameObject go)
  133. {
  134. self.GameObject = go;
  135. self.AniData = self.GameObject.GetComponent<Mono.AnimationData>();
  136. self.TransHeadInfo = go.transform.Find("BindPart/headInfo");
  137. self.FrozenComponent = self.GameObject.GetComponent <Mono.IceFrozen>();
  138. }
  139. }
  140. public class UnitRenerUpdateSystem : UpdateSystem<UnitRenderComponet>
  141. {
  142. protected override void Update(UnitRenderComponet self)
  143. {
  144. if (self.AniData == null) { return; }
  145. var cmd = self.FilterCmdList();
  146. if (cmd != null)
  147. {
  148. if (self.DoingCmd?.Type != cmd.Type || cmd.IsSkillCmd())
  149. {
  150. self.ExeCommand(cmd);
  151. }
  152. }
  153. }
  154. }
  155. public class UnitRenerDestroySystem : DestroySystem<UnitRenderComponet>
  156. {
  157. protected override void Destroy(UnitRenderComponet self)
  158. {
  159. if(self.AniData!= null)
  160. {
  161. self.AniData.Animancer.Stop();
  162. }
  163. GameObjectPool.Instance?.RecycleObject(self.GameObject);
  164. if(self.HeadBar != null)
  165. {
  166. self.HeadBar.visible = false;
  167. GameObjectPool.Instance?.RecycleHeadBar(self.HeadBar);
  168. }
  169. self.Reset();
  170. }
  171. }
  172. //===UnitRener 扩展方法-------
  173. public static void AppendCommand(this UnitRenderComponet self, AnimatorCommand cmd)
  174. {
  175. self.Commands.Add(cmd);
  176. }
  177. public static void SyncHeadBarPos(this UnitRenderComponet self)
  178. {
  179. if (self.HeadBar != null)
  180. {
  181. var part = self.TransHeadInfo ?? self.GameObject.transform;
  182. var pos = Camera.main.WorldToScreenPoint(part.position);
  183. if(pos.z < 0 || pos.x < 0 || pos.x > Screen.width || pos.y < 0 || pos.y > Screen.height)
  184. {
  185. //不在视野中
  186. self.HeadBar.SetXY(999999, 999999);
  187. }
  188. else
  189. {
  190. pos.y = Screen.height - pos.y;
  191. pos = GRoot.inst.GlobalToLocal(pos);
  192. self.HeadBar.SetXY(pos.x, pos.y);
  193. }
  194. }
  195. }
  196. public static async ETTask SyncHeadBarHP(this UnitRenderComponet self, float newValue)
  197. {
  198. if (self.HPBar != null)
  199. {
  200. var old = self.HPBar.value;
  201. if ((old - newValue) > 20 || (newValue - old > 20 && old > 0))
  202. {
  203. self.HPBar.visible = true;
  204. self.HPBar.TweenValue(newValue, 0.6f);
  205. await TimerComponent.Instance.WaitAsync(700);
  206. self.HPBar.visible = false;
  207. }
  208. else
  209. {
  210. self.HPBar.value = newValue;
  211. }
  212. }
  213. }
  214. public static void PauseAnimation(this UnitRenderComponet self)
  215. {
  216. self.AniData.Pause();
  217. }
  218. public static void ResumeAnimation(this UnitRenderComponet self)
  219. {
  220. self.AniData.Resume();
  221. }
  222. public static void SetFrozen(this UnitRenderComponet self)
  223. {
  224. self.FrozenComponent?.StartFroze();
  225. }
  226. public static void ClearFrozen(this UnitRenderComponet self)
  227. {
  228. self.FrozenComponent?.ClearFroze();
  229. }
  230. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  231. //执行指令
  232. private static void ExeCommand(this UnitRenderComponet self, AnimatorCommand cmd)
  233. {
  234. self.DoingCmd = (AnimatorCommand)cmd;
  235. if(self.AniData.IsPauseStatus() && cmd.Type == AnimationType.Dead)
  236. {
  237. self.AniData.Resume();
  238. }
  239. if(!cmd.IsSkillCmd())
  240. {
  241. self.AniData.PlayAnimation(cmd.Type);
  242. }
  243. else
  244. {
  245. //Skill动作和实际技能时间不太匹配,切到idle动作
  246. self.AniData.PlayAnimation(cmd.Type, () => {
  247. if (self.DoingCmd == cmd && !self.AniData.IsPauseStatus())
  248. {
  249. self.ExeCommand(UnitRenderComponet.CMDIdle);
  250. }
  251. });
  252. }
  253. }
  254. //从指令队列中分析出当前需要执行的指令
  255. private static AnimatorCommand FilterCmdList(this UnitRenderComponet self)
  256. {
  257. var cmds = self.Commands;
  258. if (cmds.Count <= 0)
  259. {
  260. return null;
  261. }
  262. /*var cmd = cmds[0];
  263. for(var i=1; i<cmds.Count; i++)
  264. {
  265. var next = cmds[i];
  266. if(next.Type == CMDType.StopRun && cmd.Type == CMDType.Run)
  267. {
  268. cmd = UnitRener.CMDIdle;
  269. }
  270. else if(next.Type == CMDType.StopSkill && cmd.Type >= CMDType.Skill0 && cmd.Type <= CMDType.Skill3)
  271. {
  272. cmd = UnitRener.CMDIdle;
  273. }
  274. else if(next.Type > cmd.Type && cmd.Type < CMDType.Skill0)
  275. {
  276. cmd = next;
  277. }
  278. }*/
  279. var cmd = cmds[cmds.Count - 1];
  280. if(cmd.GroupId > 0)
  281. {
  282. int delIndex = -1;
  283. for(var i = cmds.Count -2; i >= 0; i--)
  284. {
  285. if (cmds[i].GroupId != cmd.GroupId)
  286. {
  287. delIndex = i;
  288. break;
  289. }
  290. }
  291. if(delIndex >= 0)
  292. {
  293. cmds.RemoveRange(0, delIndex + 1);
  294. }
  295. else
  296. {
  297. cmds.Clear();
  298. }
  299. }
  300. else
  301. {
  302. cmds.Clear();
  303. }
  304. return cmd;
  305. }
  306. }
  307. }