BattleUnit.cs 8.4 KB

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