BattleUnit.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.Helper;
  3. using CommonAI.ZoneClient;
  4. using CommonLang.Geometry;
  5. using ET;
  6. using ET.EventType;
  7. using Sirenix.Utilities;
  8. using System;
  9. using System.Drawing.Drawing2D;
  10. public class BattleUnit : BattleObject
  11. {
  12. public BattleUnit() { }
  13. public ZoneUnit ZUnit { get { return ZoneObject as ZoneUnit; } }
  14. public Vector3 LastPos = Vector3.Zero;
  15. public float LastRotation = 0;
  16. private EventDispatcher<UnitActionStatus, object> actionChangeHandler;
  17. public override void OnAwake(ZoneObject zo)
  18. {
  19. base.OnAwake(zo);
  20. actionChangeHandler = new();
  21. registerActionHandler();
  22. var zu = zo as ZoneUnit;
  23. zu.OnActionChanged += OnActionChanged;
  24. zu.OnSkillActionChanged += OnSkillActionChanged;
  25. zu.OnHPChanged += OnHPChanged;
  26. zu.OnMPChanged += OnMPChanged;
  27. zu.OnMaxHPChanged += OnMaxHPChanged;
  28. zu.OnMaxMPChanged += OnMaxMPChanged;
  29. zu.OnLaunchSkill += OnLaunchSkill;
  30. zu.OnBuffAdded += OnBuffAdded;
  31. zu.OnBuffRemoved += OnBuffRemoved;
  32. zu.OnBuffChanged += OnBuffChanged;
  33. zu.OnRemoveBuffByOverlayLevel += OnRemoveBuffByOverlayLevel;
  34. zu.OnActionSubStatusChanged += OnActionSubStatusChanged;
  35. }
  36. protected virtual void OnRemoveBuffByOverlayLevel(ZoneUnit unit, ZoneUnit.BuffState buff)
  37. {
  38. throw new NotImplementedException();
  39. }
  40. protected virtual void OnBuffChanged(ZoneUnit unit, ZoneUnit.BuffState buff)
  41. {
  42. throw new NotImplementedException();
  43. }
  44. protected virtual void OnBuffRemoved(ZoneUnit unit, ZoneUnit.BuffState buff)
  45. {
  46. throw new NotImplementedException();
  47. }
  48. protected virtual void OnBuffAdded(ZoneUnit unit, ZoneUnit.BuffState buff)
  49. {
  50. throw new NotImplementedException();
  51. }
  52. private static uint groupId = 1;
  53. protected static uint GroupId()
  54. {
  55. return ++ groupId;
  56. }
  57. protected virtual void OnLaunchSkill(ZoneUnit _, ZoneUnit.SkillState __, UnitLaunchSkillEvent ___)
  58. {
  59. CommonAI.ZoneClient.ZoneUnit.ISkillAction action = ZUnit.CurrentSkillAction;
  60. SkillTemplate skillTemplate = action.SkillData;
  61. var curActionIndex = action.CurrentActionIndex;
  62. if (skillTemplate.ActionQueue.Count <= curActionIndex)
  63. {
  64. Log.Error("Enter Skill state error> ({0})>CurrentActionIndex>{1}", skillTemplate.TemplateID, curActionIndex);
  65. return;
  66. }
  67. Log.Debug($"OnLaunchSkill({Id}), skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
  68. if (skillTemplate.IsSingleAction)
  69. {
  70. UnitActionData actdata = skillTemplate.ActionQueue[curActionIndex];
  71. var duration = action.ActionTimeArray != null ? action.ActionTimeArray[0] : actdata.TotalTimeMS;
  72. EventSystem.Instance.Publish<PlayAnimatorEvent>(CurrentScene, new PlayAnimatorEvent() {
  73. UnitId = Id,
  74. CommandType = AnimatorEventType.Skill,
  75. SkillName = actdata.ActionName,
  76. Duration = duration,
  77. Speed = action.ActionSpeed,
  78. Loop = actdata.IsCycAction
  79. });
  80. if (!actdata.ActionAudioName.IsNullOrWhitespace())
  81. {
  82. SoundManager.Instance.Play3DSound(actdata.ActionAudioName, ZUnit.X, ZUnit.Y, ZUnit.Z, duration);
  83. }
  84. }
  85. else
  86. {
  87. UnitActionData item = null;
  88. float speed = 1.0f;
  89. bool isFrist = true;
  90. for (int i = curActionIndex; i < skillTemplate.ActionQueue.Count; i++)
  91. {
  92. item = skillTemplate.ActionQueue[i];
  93. if (action.LaunchEvent != null)
  94. {
  95. speed = action.LaunchEvent.action_speed;
  96. if (action.LaunchEvent.action_speed_add != null && i < action.LaunchEvent.action_speed_add.Length)
  97. {
  98. speed += action.LaunchEvent.action_speed_add[i];
  99. }
  100. }
  101. EventSystem.Instance.Publish<PlayAnimatorEvent>(CurrentScene, new PlayAnimatorEvent()
  102. {
  103. UnitId = Id,
  104. CommandType = AnimatorEventType.Skill,
  105. SkillName = item.ActionName,
  106. Duration = action.ActionTimeArray[i],
  107. Speed = speed,
  108. Loop = item.IsCycAction,
  109. GroupId = GroupId()
  110. });
  111. if(!item.ActionAudioName.IsNullOrWhitespace())
  112. {
  113. if (isFrist)
  114. {
  115. isFrist = false;
  116. SoundManager.Instance.Play3DSound(item.ActionAudioName, ZUnit.X, ZUnit.Y, ZUnit.Z, action.ActionTimeArray[i]);
  117. }
  118. else
  119. {
  120. Log.Error("not implements multiAction audio");
  121. }
  122. }
  123. }
  124. }
  125. }
  126. protected virtual void OnMaxMPChanged(ZoneUnit unit, int oldMaxMP, int newMaxMP)
  127. {
  128. throw new NotImplementedException();
  129. }
  130. protected virtual void OnMaxHPChanged(ZoneUnit unit, int oldMaxHP, int newMaxHP)
  131. {
  132. throw new NotImplementedException();
  133. }
  134. protected virtual void OnMPChanged(ZoneUnit unit, int oldMP, int newMP)
  135. {
  136. throw new NotImplementedException();
  137. }
  138. protected virtual void OnHPChanged(ZoneUnit unit, int oldHP, int newHP)
  139. {
  140. throw new NotImplementedException();
  141. }
  142. protected virtual void OnSkillActionChanged(ZoneUnit unit, ZoneUnit.SkillState skill, byte index)
  143. {
  144. //技能状态
  145. }
  146. protected virtual void OnActionChanged(ZoneUnit unit, UnitActionStatus status, object evt)
  147. {
  148. Log.Debug($"ActionChange: {status}, zobject status:{ZUnit.CurrentState}");
  149. if(!actionChangeHandler.Notify(status, evt))
  150. {
  151. Log.Error($"unhandle action changed: {status}");
  152. }
  153. }
  154. protected virtual void OnActionSubStatusChanged(ZoneUnit unit, byte status, object evt)
  155. {
  156. }
  157. private void registerActionHandler()
  158. {
  159. actionChangeHandler.AddListener(UnitActionStatus.Idle, (_) => {
  160. EventSystem.Instance.Publish<PlayAnimatorEvent>(CurrentScene, new PlayAnimatorEvent() { UnitId = Id, CommandType = AnimatorEventType.Idle });
  161. });
  162. var move = actionChangeHandler.AddListener(UnitActionStatus.Move, (o) => {
  163. EventSystem.Instance.Publish<PlayAnimatorEvent>(CurrentScene, new PlayAnimatorEvent() { UnitId = Id, CommandType = AnimatorEventType.Run });
  164. });
  165. actionChangeHandler.AddListener(UnitActionStatus.Chaos, move);
  166. actionChangeHandler.AddListener(UnitActionStatus.ServerSyncMove, move);
  167. actionChangeHandler.AddListener(UnitActionStatus.Escape, move);
  168. var stun = actionChangeHandler.AddListener(UnitActionStatus.Stun, (o) => { });
  169. actionChangeHandler.AddListener(UnitActionStatus.HitMove, stun);
  170. actionChangeHandler.AddListener(UnitActionStatus.Dead, (o) => { });
  171. actionChangeHandler.AddListener(UnitActionStatus.Damage, (o) => { });
  172. actionChangeHandler.AddListener(UnitActionStatus.Pick, (o) => { });
  173. actionChangeHandler.AddListener(UnitActionStatus.Spawn, (o) => { });
  174. actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkMove, (o) => { });
  175. actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkIdle, (o) => { });
  176. actionChangeHandler.AddListener(UnitActionStatus.DaZuo, (o) => { });
  177. actionChangeHandler.AddListener(UnitActionStatus.DaZuoRecoveryAttr, (o) => { });
  178. actionChangeHandler.AddListener(UnitActionStatus.ChuanGongA, (o) => { });
  179. actionChangeHandler.AddListener(UnitActionStatus.ChuanGongB, (o) => { });
  180. actionChangeHandler.AddListener(UnitActionStatus.BuffLockStateAction, (o) => { });
  181. actionChangeHandler.AddListener(UnitActionStatus.HomeBack, (o) => { });
  182. actionChangeHandler.AddListener(UnitActionStatus.ClearYaoQi, (o) => { });
  183. actionChangeHandler.AddListener(UnitActionStatus.PetLock, (o) => { });
  184. actionChangeHandler.AddListener(UnitActionStatus.BreakShield, (o) => { });
  185. actionChangeHandler.AddListener(UnitActionStatus.Transport, (o) => { });
  186. actionChangeHandler.AddListener(UnitActionStatus.Skill, (o) => { });
  187. }
  188. }