BattleUnit.cs 8.8 KB

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