BattleUnit.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. private EventDispatcher<UnitActionStatus, object> actionChangeHandler;
  14. public override void OnAwake(ZoneObject zo)
  15. {
  16. base.OnAwake(zo);
  17. actionChangeHandler = new();
  18. registerActionHandler();
  19. var zu = zo as ZoneUnit;
  20. zu.OnActionChanged += OnActionChanged;
  21. zu.OnSkillActionChanged += OnSkillActionChanged;
  22. zu.OnHPChanged += OnHPChanged;
  23. zu.OnMPChanged += OnMPChanged;
  24. zu.OnMaxHPChanged += OnMaxHPChanged;
  25. zu.OnMaxMPChanged += OnMaxMPChanged;
  26. zu.OnLaunchSkill += OnLaunchSkill;
  27. zu.OnBuffAdded += OnBuffAdded;
  28. zu.OnBuffRemoved += OnBuffRemoved;
  29. zu.OnBuffChanged += OnBuffChanged;
  30. zu.OnRemoveBuffByOverlayLevel += OnRemoveBuffByOverlayLevel;
  31. zu.OnActionSubStatusChanged += OnActionSubStatusChanged;
  32. }
  33. protected virtual void OnRemoveBuffByOverlayLevel(ZoneUnit unit, ZoneUnit.BuffState buff)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. protected virtual void OnBuffChanged(ZoneUnit unit, ZoneUnit.BuffState buff)
  38. {
  39. throw new NotImplementedException();
  40. }
  41. protected virtual void OnBuffRemoved(ZoneUnit unit, ZoneUnit.BuffState buff)
  42. {
  43. throw new NotImplementedException();
  44. }
  45. protected virtual void OnBuffAdded(ZoneUnit unit, ZoneUnit.BuffState buff)
  46. {
  47. throw new NotImplementedException();
  48. }
  49. private static uint groupId = 1;
  50. protected static uint GroupId()
  51. {
  52. return ++ groupId;
  53. }
  54. protected virtual void OnLaunchSkill(ZoneUnit _, ZoneUnit.SkillState __, UnitLaunchSkillEvent ___)
  55. {
  56. CommonAI.ZoneClient.ZoneUnit.ISkillAction action = ZUnit.CurrentSkillAction;
  57. if(action == null)
  58. {
  59. Log.Warning("object's currentSkillAction = null, cannot launch skill");
  60. return;
  61. }
  62. SkillTemplate skillTemplate = action.SkillData;
  63. var curActionIndex = action.CurrentActionIndex;
  64. if (skillTemplate.ActionQueue.Count <= curActionIndex)
  65. {
  66. Log.Error("Enter Skill state error> ({0})>CurrentActionIndex>{1}", skillTemplate.TemplateID, curActionIndex);
  67. return;
  68. }
  69. //Log.Debug($"OnLaunchSkill({Id}), skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
  70. if (skillTemplate.IsSingleAction)
  71. {
  72. UnitActionData actdata = skillTemplate.ActionQueue[curActionIndex];
  73. if (actdata.ActionName.IsNullOrWhitespace())
  74. {
  75. Log.Debug($"ActionName is null @skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
  76. return;
  77. }
  78. var duration = action.ActionTimeArray != null ? action.ActionTimeArray[0] : actdata.TotalTimeMS;
  79. EventSystem.Instance.Publish<PlayAnimatorEvent>(PlayAnimatorEvent.Static.Clone(
  80. Id,
  81. AnimatorEventType.Skill,
  82. actdata.ActionName,
  83. duration,
  84. action.ActionSpeed,
  85. 0,
  86. actdata.IsCycAction, actdata.ActionAudioName)
  87. );
  88. }
  89. else
  90. {
  91. float speed = 1.0f;
  92. bool isFrist = true;
  93. for (int i = curActionIndex; i < skillTemplate.ActionQueue.Count; i++)
  94. {
  95. UnitActionData item = skillTemplate.ActionQueue[i];
  96. if (item.ActionName.IsNullOrWhitespace())
  97. {
  98. Log.Debug($"ActionName is null @skill({skillTemplate.TemplateID}), actionIndex:{curActionIndex}");
  99. return;
  100. }
  101. if (action.LaunchEvent != null)
  102. {
  103. speed = action.LaunchEvent.action_speed;
  104. if (action.LaunchEvent.action_speed_add != null && i < action.LaunchEvent.action_speed_add.Length)
  105. {
  106. speed += action.LaunchEvent.action_speed_add[i];
  107. }
  108. }
  109. EventSystem.Instance.Publish<PlayAnimatorEvent>(PlayAnimatorEvent.Static.Clone(
  110. Id,
  111. AnimatorEventType.Skill,
  112. item.ActionName,
  113. action.ActionTimeArray[i],
  114. speed, GroupId(),
  115. item.IsCycAction,
  116. isFrist ? item.ActionAudioName : ""));
  117. if(!item.ActionAudioName.IsNullOrWhitespace())
  118. {
  119. if (isFrist)
  120. {
  121. isFrist = false;
  122. }
  123. else
  124. {
  125. Log.Error("not implements multiAction audio");
  126. }
  127. }
  128. }
  129. }
  130. }
  131. protected virtual void OnMaxMPChanged(ZoneUnit unit, int oldMaxMP, int newMaxMP)
  132. {
  133. throw new NotImplementedException();
  134. }
  135. protected virtual void OnMPChanged(ZoneUnit unit, int oldMP, int newMP)
  136. {
  137. throw new NotImplementedException();
  138. }
  139. protected virtual void OnMaxHPChanged(ZoneUnit unit, int oldMaxHP, int newMaxHP)
  140. {
  141. OnHPChanged();
  142. }
  143. protected virtual void OnHPChanged(ZoneUnit unit, int oldHP, int newHP)
  144. {
  145. OnHPChanged();
  146. }
  147. private void OnHPChanged()
  148. {
  149. var prop = ZUnit.Info.Properties as XmdsUnitProperties;
  150. if (prop != null)
  151. {
  152. if(prop.GameStatusType == XmdsUnitProperties.StatusType.SpecialElite)
  153. {
  154. var hp = ZUnit.HP;
  155. var pg = (float)ZUnit.HP * 100 / ZUnit.MaxHP;
  156. EventSystem.Instance.Publish<HPRefresh>(HPRefresh.Static.Clone(HPRefresh.Index.Tower, pg, hp));
  157. //Log.Debug($"tower({ZUnit.ObjectID}@{ZUnit.Info.Name}) hp: {ZUnit.HP}/{ZUnit.MaxHP}");
  158. }
  159. else if(prop.GameStatusType == XmdsUnitProperties.StatusType.SpecialBoss)
  160. {
  161. var hp = ZUnit.HP;
  162. var pg = (float)ZUnit.HP * 100 / ZUnit.MaxHP;
  163. EventSystem.Instance.Publish<HPRefresh>(HPRefresh.Static.Clone(HPRefresh.Index.Boss, pg, hp));
  164. //Log.Debug($"Boss({ZUnit.ObjectID}) hp: {ZUnit.HP}/{ZUnit.MaxHP}");
  165. }
  166. }
  167. //Log.Debug($"hp({ZUnit.ObjectID}) change: {ZUnit.HP}");
  168. }
  169. protected virtual void OnSkillActionChanged(ZoneUnit unit, ZoneUnit.SkillState skill, byte index)
  170. {
  171. //技能状态
  172. }
  173. protected virtual void OnActionChanged(ZoneUnit unit, UnitActionStatus status, object evt)
  174. {
  175. //Log.Debug($"ActionChange({Id}): {status}, zobject status:{ZUnit.CurrentState}");
  176. if(!actionChangeHandler.Notify(status, evt))
  177. {
  178. Log.Error($"unhandle action changed: {status}");
  179. }
  180. }
  181. protected virtual void OnActionSubStatusChanged(ZoneUnit unit, byte status, object evt)
  182. {
  183. }
  184. private void registerActionHandler()
  185. {
  186. actionChangeHandler.AddListener(UnitActionStatus.Idle, (_) => {
  187. EventSystem.Instance.Publish<PlayAnimatorEvent>(PlayAnimatorEvent.Static.Clone(Id, AnimatorEventType.Idle));
  188. });
  189. var move = actionChangeHandler.AddListener(UnitActionStatus.Move, (o) => {
  190. EventSystem.Instance.Publish<PlayAnimatorEvent>(PlayAnimatorEvent.Static.Clone(Id, AnimatorEventType.Run));
  191. });
  192. actionChangeHandler.AddListener(UnitActionStatus.Chaos, move);
  193. actionChangeHandler.AddListener(UnitActionStatus.ServerSyncMove, move);
  194. actionChangeHandler.AddListener(UnitActionStatus.Escape, move);
  195. var stun = actionChangeHandler.AddListener(UnitActionStatus.Stun, (o) => { });
  196. actionChangeHandler.AddListener(UnitActionStatus.HitMove, stun);
  197. actionChangeHandler.AddListener(UnitActionStatus.Dead, (o) => {
  198. Log.Debug($"Unit({Id}) dead");
  199. EventSystem.Instance.Publish<PlayAnimatorEvent>(PlayAnimatorEvent.Static.Clone(
  200. Id, AnimatorEventType.Dead ));
  201. var launcheffect = ZUnit.Info.DeadActionEffect;
  202. if (launcheffect != null)
  203. {
  204. EventSystem.Instance.Publish<PlayEffectEvent>(PlayEffectEvent.Static.Clone(launcheffect, Id, CommonLang.Geometry.Vector3.Zero));
  205. }
  206. //TODO: 过几秒后,自行清理尸体
  207. });
  208. actionChangeHandler.AddListener(UnitActionStatus.Damage, (o) => { });
  209. actionChangeHandler.AddListener(UnitActionStatus.Pick, (o) => { });
  210. actionChangeHandler.AddListener(UnitActionStatus.Spawn, (o) => { });
  211. actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkMove, (o) => { });
  212. actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkIdle, (o) => { });
  213. actionChangeHandler.AddListener(UnitActionStatus.DaZuo, (o) => { });
  214. actionChangeHandler.AddListener(UnitActionStatus.DaZuoRecoveryAttr, (o) => { });
  215. actionChangeHandler.AddListener(UnitActionStatus.ChuanGongA, (o) => { });
  216. actionChangeHandler.AddListener(UnitActionStatus.ChuanGongB, (o) => { });
  217. actionChangeHandler.AddListener(UnitActionStatus.BuffLockStateAction, (o) => { });
  218. actionChangeHandler.AddListener(UnitActionStatus.HomeBack, (o) => { });
  219. actionChangeHandler.AddListener(UnitActionStatus.ClearYaoQi, (o) => { });
  220. actionChangeHandler.AddListener(UnitActionStatus.PetLock, (o) => { });
  221. actionChangeHandler.AddListener(UnitActionStatus.BreakShield, (o) => { });
  222. actionChangeHandler.AddListener(UnitActionStatus.Transport, (o) => { });
  223. actionChangeHandler.AddListener(UnitActionStatus.Skill, (o) => { });
  224. }
  225. }