123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- using CMDType = ET.AnimatorEventType;
- using AniType = Mono.AnimationData.AnimationType;
- using UnityEngine;
- using System.Text.RegularExpressions;
- using ET.EventType;
- namespace ET.Client
- {
- [Event(SceneType.Current)]
- public class AnimatorEventHandler : AEvent<EventType.PlayAnimatorEvent>
- {
- protected override async ETTask Run(Scene scene, EventType.PlayAnimatorEvent args)
- {
- var component = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(args.UnitId);
- if (component == null)
- {
- Log.Debug($"Not found UnitRener of object{args.UnitId}");
- return;
- }
- if(args.CommandType == CMDType.Skill)
- {
- var m = Regex.Match(args.SkillName, "[Ss]kill(\\d+)");
- if (m.Success)
- {
- int n = System.Convert.ToInt32(m.Groups[1].Value, 10);
- if(n >=0 && AniType.SkillMax > AniType.Skill0 + n)
- {
- AnimatorCommand cmd = new AnimatorCommand((AniType)(AniType.Skill0 + n)) {
- Duration = args.Duration,
- Speed = args.Speed,
- GroupId = args.GroupId,
- Loop = args.Loop
- };
- component.AppendCommand(cmd);
- return;
- }
- }
- Log.Error($"Not support skill animation: {args.SkillName}");
- }
- else
- {
- AnimatorCommand cmd = args.CommandType switch
- {
- AnimatorEventType.Idle => UnitRenderComponet.CMDIdle,
- AnimatorEventType.Run => UnitRenderComponet.CMDRun,
- AnimatorEventType.Dead => UnitRenderComponet.CMDDead,
- _ => UnitRenderComponet.CMDIdle
- };
- component.AppendCommand(cmd);
- }
-
- await ETTask.CompletedTask;
- }
- }
- [Event(SceneType.None)]
- public class SyncUnitPosEventHandler : BEvent<EventType.SyncUnitPosEvent>
- {
- public override void OnEvent(SyncUnitPosEvent args)
- {
- var unitRender = ModelViewComponent.Instance.GetChild<UnitRenderComponet>(args.Id);
- if(unitRender != null)
- {
- var transform = unitRender.GameObject.transform;
- transform.position = RenderUtils.UnityPosFromBattle(args.Pos);
- transform.rotation = RenderUtils.UnityRotationFromBattle(args.Rotation);
- if(args.Id == UnitMgr.Instance.ActorId)
- {
- CameraMgr.FollowMe(transform.position);
- }
- }
- else
- {
- Log.Debug($"Not exist unitRender: {args.Id}");
- }
-
-
-
- }
- }
- [FriendOf(typeof(UnitRenderComponet))]
- public static class UnitRenerSystem
- {
- [ObjectSystem]
- public class UnitRenerAwakeSystem : AwakeSystem<UnitRenderComponet, GameObject>
- {
- protected override void Awake(UnitRenderComponet self, GameObject go)
- {
- self.GameObject = go;
- self.Awake();
- }
- }
- [ObjectSystem]
- public class UnitRenerUpdateSystem : UpdateSystem<UnitRenderComponet>
- {
- protected override void Update(UnitRenderComponet self)
- {
- self.Update();
- }
- }
- [ObjectSystem]
- public class UnitRenerDestroySystem : DestroySystem<UnitRenderComponet>
- {
- protected override void Destroy(UnitRenderComponet self)
- {
- self.AniData.Animancer.Stop();
- UnityEngine.Object.Destroy(self.GameObject);
- }
- }
-
- public static void AppendCommand(this UnitRenderComponet self, AnimatorCommand cmd)
- {
- self.Commands.Add(cmd);
- }
-
- public static void Awake(this UnitRenderComponet self)
- {
- self.AniData = self.GameObject.GetComponent<Mono.AnimationData>();
- self.ExeCommand(UnitRenderComponet.CMDIdle);
- }
- public static void Update(this UnitRenderComponet self)
- {
- var cmd = self.FilterCmdList();
- if(cmd != null)
- {
- self.ExeCommand(cmd);
- }
- }
-
- private static void ExeCommand(this UnitRenderComponet self, AnimatorCommand cmd)
- {
- self.DoingCmd = (AnimatorCommand)cmd;
- self.AniData.PlayAnimation(cmd.Type);
- }
-
- private static AnimatorCommand FilterCmdList(this UnitRenderComponet self)
- {
- var cmds = self.Commands;
- if (cmds.Count <= 0)
- {
- return null;
- }
-
- var cmd = cmds[cmds.Count - 1];
- if(cmd.GroupId > 0)
- {
- int delIndex = -1;
- for(var i = cmds.Count -2; i >= 0; i--)
- {
- if (cmds[i].GroupId != cmd.GroupId)
- {
- delIndex = i;
- break;
- }
- }
- if(delIndex >= 0)
- {
- cmds.RemoveRange(0, delIndex + 1);
- }
- }
- else
- {
- cmds.Clear();
- }
- return cmd;
- }
- }
- }
|