BattleUnit.cs 11 KB

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