BattleUnit.cs 9.0 KB

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