BattleUnit.cs 11 KB

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