BattleUnit.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 System.Collections.Generic;
  9. using XmdsCommon.Plugin;
  10. public class BattleUnit : BattleObject
  11. {
  12. public BattleUnit() { }
  13. public ZoneUnit ZUnit { get { return ZoneObject as ZoneUnit; } }
  14. public bool IsDead { get { return ZUnit.CurrentState == UnitActionStatus.Dead; } }
  15. private EventDispatcher<UnitActionStatus, object> actionChangeHandler;
  16. public override void OnAwake(ZoneObject zo)
  17. {
  18. base.OnAwake(zo);
  19. actionChangeHandler = new();
  20. registerActionHandler();
  21. var zu = zo as ZoneUnit;
  22. zu.OnActionChanged += OnActionChanged;
  23. zu.OnSkillActionChanged += (ZoneUnit unit, ZoneUnit.SkillState skill, byte index) => { };
  24. zu.OnHPChanged += (ZoneUnit unit, int oldHP, int newHP)=> { OnHPChanged(); };
  25. zu.OnMPChanged += (ZoneUnit unit, int oldMaxMP, int newMaxMP) => { };
  26. zu.OnMaxHPChanged += (ZoneUnit unit, int oldHP, int newHP) => { OnHPChanged(); };
  27. zu.OnMaxMPChanged += (ZoneUnit unit, int oldMaxMP, int newMaxMP)=> { };
  28. zu.OnLaunchSkill += OnLaunchSkill;
  29. zu.OnBuffAdded += (ZoneUnit unit, ZoneUnit.BuffState buff) => {
  30. EventSystem.Instance.Publish(BuffChangeEvent.Static.Clone(Id, buff, BuffChangeEvent.BuffOP.Add));
  31. };
  32. zu.OnBuffRemoved += (ZoneUnit unit, ZoneUnit.BuffState buff) => {
  33. EventSystem.Instance.Publish(BuffChangeEvent.Static.Clone(Id, buff, BuffChangeEvent.BuffOP.Remove));
  34. };
  35. zu.OnBuffChanged += (ZoneUnit unit, ZoneUnit.BuffState buff) => {
  36. EventSystem.Instance.Publish(BuffChangeEvent.Static.Clone(Id, buff, BuffChangeEvent.BuffOP.Change));
  37. };
  38. zu.OnRemoveBuffByOverlayLevel += (ZoneUnit unit, ZoneUnit.BuffState buff) => { };
  39. zu.OnActionSubStatusChanged += OnActionSubStatusChanged;
  40. }
  41. private static uint groupId = 1;
  42. protected static uint GroupId()
  43. {
  44. return ++ groupId;
  45. }
  46. protected virtual void OnLaunchSkill(ZoneUnit _, ZoneUnit.SkillState __, UnitLaunchSkillEvent ___)
  47. {
  48. CommonAI.ZoneClient.ZoneUnit.ISkillAction action = ZUnit.CurrentSkillAction;
  49. if(action == null)
  50. {
  51. Log.Warning("object's currentSkillAction = null, cannot launch skill");
  52. return;
  53. }
  54. SkillTemplate skillTemplate = action.SkillData;
  55. var curActionIndex = action.CurrentActionIndex;
  56. if (skillTemplate.ActionQueue.Count <= curActionIndex)
  57. {
  58. Log.Error("Enter Skill state error> ({0})>CurrentActionIndex>{1}", skillTemplate.TemplateID, curActionIndex);
  59. return;
  60. }
  61. //Log.Debug($"OnLaunchSkill({Id}), skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
  62. if (skillTemplate.IsSingleAction)
  63. {
  64. UnitActionData actdata = skillTemplate.ActionQueue[curActionIndex];
  65. if (actdata.ActionName.IsNullOrWhitespace())
  66. {
  67. Log.Debug($"ActionName is null @skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
  68. return;
  69. }
  70. var duration = action.ActionTimeArray != null ? action.ActionTimeArray[0] : actdata.TotalTimeMS;
  71. EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(
  72. Id,
  73. PlayAnimatorEvent.AniType.Skill,
  74. actdata.ActionName,
  75. duration,
  76. action.ActionSpeed,
  77. 0,
  78. actdata.IsCycAction, actdata.ActionAudioName)
  79. );
  80. }
  81. else
  82. {
  83. float speed = 1.0f;
  84. bool isFrist = true;
  85. for (int i = curActionIndex; i < skillTemplate.ActionQueue.Count; i++)
  86. {
  87. UnitActionData item = skillTemplate.ActionQueue[i];
  88. if (item.ActionName.IsNullOrWhitespace())
  89. {
  90. Log.Debug($"ActionName is null @skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
  91. return;
  92. }
  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.Clone(
  102. Id,
  103. PlayAnimatorEvent.AniType.Skill,
  104. item.ActionName,
  105. action.ActionTimeArray[i],
  106. speed, GroupId(),
  107. item.IsCycAction,
  108. isFrist ? item.ActionAudioName : ""));
  109. if(!item.ActionAudioName.IsNullOrWhitespace())
  110. {
  111. if (isFrist)
  112. {
  113. isFrist = false;
  114. }
  115. else
  116. {
  117. Log.Error("not implements multiAction audio");
  118. }
  119. }
  120. }
  121. }
  122. }
  123. private void OnHPChanged()
  124. {
  125. if (ZUnit.Info.Properties is XmdsUnitProperties prop)
  126. {
  127. //Log.Debug($"hp({ZUnit.TemplateID}) change: {ZUnit.HP}");
  128. if (prop.GameStatusType == XmdsUnitProperties.StatusType.SpecialElite)
  129. {
  130. var hp = ZUnit.HP;
  131. var pg = hp * 100f / ZUnit.MaxHP;
  132. EventSystem.Instance.Publish(HPRefresh.Clone(HPRefresh.Index.Tower, pg, prop.ServerData.BaseInfo.name, hp.ToString()));
  133. return;
  134. }
  135. else if (prop.GameStatusType == XmdsUnitProperties.StatusType.SpecialBoss)
  136. {
  137. RefreshBossHPBar(prop);
  138. return;
  139. }
  140. if (prop.ShowHPBanner)
  141. {
  142. EventSystem.Instance.Publish(SyncUnitHpEvent.Clone(Id, ZUnit.HP * 100f / ZUnit.MaxHP));
  143. }
  144. }
  145. }
  146. //有3个HP血条,找合适的条条显示
  147. private static List<ZoneUnit> pgList = new();
  148. public static void Reset()
  149. {
  150. pgList.Clear();
  151. }
  152. private void RefreshBossHPBar(XmdsUnitProperties prop)
  153. {
  154. var hp = ZUnit.HP;
  155. int hide = -1;
  156. for(var i = 0; i < pgList.Count; i++)
  157. {
  158. var unit = pgList[i];
  159. if(hide >= 0)
  160. {
  161. hp = unit.HP;
  162. prop = unit.Info.Properties as XmdsUnitProperties;
  163. EventSystem.Instance.Publish(HPRefresh.Clone(HPRefresh.Index.Boss1 + i - 1, hp * 100f / unit.MaxHP, prop.ServerData.BaseInfo.name, hp.ToString()));
  164. }
  165. else if(unit.TemplateID == ZUnit.TemplateID)
  166. {
  167. if(hp <= 0)
  168. {
  169. hide = i;
  170. EventSystem.Instance.Publish(HPRefresh.Clone(HPRefresh.Index.Boss1 + pgList.Count - 1, 0, "", "", false));
  171. }
  172. else
  173. {
  174. EventSystem.Instance.Publish(HPRefresh.Clone(HPRefresh.Index.Boss1 + i, hp * 100f / ZUnit.MaxHP, prop.ServerData.BaseInfo.name, hp.ToString()));
  175. return;
  176. }
  177. }
  178. }
  179. if(hide >= 0)
  180. {
  181. pgList.RemoveAt(hide);
  182. return;
  183. }
  184. if (pgList.Count >= 4)
  185. {
  186. Log.Error("wtf, more than 4 boss.");
  187. return;
  188. }
  189. if(hp <= 0)
  190. {
  191. return;
  192. }
  193. pgList.Add(ZUnit);
  194. EventSystem.Instance.Publish(HPRefresh.Clone(HPRefresh.Index.Boss1 + pgList.Count-1, hp * 100f / ZUnit.MaxHP, prop.ServerData.BaseInfo.name, hp.ToString()));
  195. }
  196. protected virtual void OnActionChanged(ZoneUnit unit, UnitActionStatus status, object evt)
  197. {
  198. //Log.Debug($"ActionChange({unit.Info.TemplateID}): {status}<= zobject status:{ZUnit.CurrentState}");
  199. if(!actionChangeHandler.Notify(status, evt))
  200. {
  201. Log.Error($"unhandle action changed: {status}");
  202. }
  203. }
  204. protected virtual void OnActionSubStatusChanged(ZoneUnit unit, byte status, object evt)
  205. {
  206. //Log.Debug($"OnActionSubStatusChanged({unit.Info.TemplateID}): {status}<= zobject status:{ZUnit.CurrentSubState}");
  207. }
  208. private void registerActionHandler()
  209. {
  210. actionChangeHandler.AddListener(UnitActionStatus.Idle, (_) => {
  211. EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(Id, PlayAnimatorEvent.AniType.Idle));
  212. });
  213. var move = actionChangeHandler.AddListener(UnitActionStatus.Move, (o) => {
  214. EventSystem.Instance.Publish(PlayAnimatorEvent.Clone(Id, PlayAnimatorEvent.AniType.Run));
  215. });
  216. actionChangeHandler.AddListener(UnitActionStatus.Chaos, move);
  217. actionChangeHandler.AddListener(UnitActionStatus.ServerSyncMove, move);
  218. actionChangeHandler.AddListener(UnitActionStatus.Escape, move);
  219. var stun = actionChangeHandler.AddListener(UnitActionStatus.Stun, (o) => {
  220. //TODO: 判断没有FROZEN的BUFF状态,才进行stun的动作
  221. });
  222. actionChangeHandler.AddListener(UnitActionStatus.HitMove, stun);
  223. actionChangeHandler.AddListener(UnitActionStatus.Dead, (o) => {
  224. //OnHPChanged();
  225. });
  226. actionChangeHandler.AddListener(UnitActionStatus.Damage, (o) => { });
  227. actionChangeHandler.AddListener(UnitActionStatus.Pick, (o) => { });
  228. actionChangeHandler.AddListener(UnitActionStatus.Spawn, (o) => { });
  229. actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkMove, (o) => { });
  230. actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkIdle, (o) => { });
  231. actionChangeHandler.AddListener(UnitActionStatus.DaZuo, (o) => { });
  232. actionChangeHandler.AddListener(UnitActionStatus.DaZuoRecoveryAttr, (o) => { });
  233. actionChangeHandler.AddListener(UnitActionStatus.ChuanGongA, (o) => { });
  234. actionChangeHandler.AddListener(UnitActionStatus.ChuanGongB, (o) => { });
  235. actionChangeHandler.AddListener(UnitActionStatus.BuffLockStateAction, (o) => { });
  236. actionChangeHandler.AddListener(UnitActionStatus.HomeBack, (o) => { });
  237. actionChangeHandler.AddListener(UnitActionStatus.ClearYaoQi, (o) => { });
  238. actionChangeHandler.AddListener(UnitActionStatus.PetLock, (o) => { });
  239. actionChangeHandler.AddListener(UnitActionStatus.BreakShield, (o) => { });
  240. actionChangeHandler.AddListener(UnitActionStatus.Transport, (o) => { });
  241. actionChangeHandler.AddListener(UnitActionStatus.Skill, (o) => { });
  242. actionChangeHandler.AddListener(UnitActionStatus.Rebirth, (o) => { });
  243. }
  244. }