BattleUnit.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.Drawing.Drawing2D;
  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 unit, ZoneUnit.SkillState skill, UnitLaunchSkillEvent ev)
  55. {
  56. CommonAI.ZoneClient.ZoneUnit.ISkillAction action = ZUnit.CurrentSkillAction;
  57. SkillTemplate skillTemplate = action.SkillData;
  58. var curActionIndex = action.CurrentActionIndex;
  59. if (skillTemplate.ActionQueue.Count <= curActionIndex)
  60. {
  61. Log.Error("Enter Skill state error> ({0})>CurrentActionIndex>{1}", skillTemplate.TemplateID, curActionIndex);
  62. return;
  63. }
  64. if (skillTemplate.IsSingleAction)
  65. {
  66. UnitActionData actdata = skillTemplate.ActionQueue[curActionIndex];
  67. var duration = action.ActionTimeArray != null ? action.ActionTimeArray[0] : actdata.TotalTimeMS;
  68. EventSystem.Instance.Publish<PlayAnimatorEvent>(CurrentScene, new PlayAnimatorEvent() {
  69. UnitId = Id,
  70. CommandType = AnimatorEventType.Skill,
  71. SkillName = actdata.ActionName,
  72. Duration = duration,
  73. Speed = action.ActionSpeed,
  74. Loop = actdata.IsCycAction
  75. });
  76. if (!actdata.ActionAudioName.IsNullOrWhitespace())
  77. {
  78. SoundManager.Instance.Play3DSound(actdata.ActionAudioName, ZUnit.X, ZUnit.Y, ZUnit.Z, duration);
  79. }
  80. }
  81. else
  82. {
  83. UnitActionData item = null;
  84. float speed = 1.0f;
  85. bool isFrist = true;
  86. for (int i = curActionIndex; i < skillTemplate.ActionQueue.Count; i++)
  87. {
  88. item = skillTemplate.ActionQueue[i];
  89. if (action.LaunchEvent != null)
  90. {
  91. speed = action.LaunchEvent.action_speed;
  92. if (action.LaunchEvent.action_speed_add != null && i < action.LaunchEvent.action_speed_add.Length)
  93. {
  94. speed += action.LaunchEvent.action_speed_add[i];
  95. }
  96. }
  97. EventSystem.Instance.Publish<PlayAnimatorEvent>(CurrentScene, new PlayAnimatorEvent()
  98. {
  99. UnitId = Id,
  100. CommandType = AnimatorEventType.Skill,
  101. SkillName = item.ActionName,
  102. Duration = action.ActionTimeArray[i],
  103. Speed = speed,
  104. Loop = item.IsCycAction,
  105. GroupId = GroupId()
  106. });
  107. if(!item.ActionAudioName.IsNullOrWhitespace())
  108. {
  109. if (isFrist)
  110. {
  111. isFrist = false;
  112. SoundManager.Instance.Play3DSound(item.ActionAudioName, ZUnit.X, ZUnit.Y, ZUnit.Z, action.ActionTimeArray[i]);
  113. }
  114. else
  115. {
  116. Log.Error("not implements multiAction audio");
  117. }
  118. }
  119. }
  120. }
  121. }
  122. protected virtual void OnMaxMPChanged(ZoneUnit unit, int oldMaxMP, int newMaxMP)
  123. {
  124. throw new NotImplementedException();
  125. }
  126. protected virtual void OnMaxHPChanged(ZoneUnit unit, int oldMaxHP, int newMaxHP)
  127. {
  128. throw new NotImplementedException();
  129. }
  130. protected virtual void OnMPChanged(ZoneUnit unit, int oldMP, int newMP)
  131. {
  132. throw new NotImplementedException();
  133. }
  134. protected virtual void OnHPChanged(ZoneUnit unit, int oldHP, int newHP)
  135. {
  136. throw new NotImplementedException();
  137. }
  138. protected virtual void OnSkillActionChanged(ZoneUnit unit, ZoneUnit.SkillState skill, byte index)
  139. {
  140. //技能状态
  141. }
  142. protected virtual void OnActionChanged(ZoneUnit unit, UnitActionStatus status, object evt)
  143. {
  144. Log.Debug($"ActionChange2: {status}, zobject status:{ZUnit.CurrentState}");
  145. if(!actionChangeHandler.Notify(status, evt))
  146. {
  147. Log.Error($"unhandle action changed: {status}");
  148. }
  149. }
  150. protected virtual void OnActionSubStatusChanged(ZoneUnit unit, byte status, object evt)
  151. {
  152. }
  153. private void registerActionHandler()
  154. {
  155. actionChangeHandler.AddListener(UnitActionStatus.Idle, (_) => {
  156. EventSystem.Instance.Publish<PlayAnimatorEvent>(CurrentScene, new PlayAnimatorEvent() { UnitId = Id, CommandType = AnimatorEventType.Idle });
  157. });
  158. var move = actionChangeHandler.AddListener(UnitActionStatus.Move, (o) => {
  159. EventSystem.Instance.Publish<PlayAnimatorEvent>(CurrentScene, new PlayAnimatorEvent() { UnitId = Id, CommandType = AnimatorEventType.Run });
  160. });
  161. actionChangeHandler.AddListener(UnitActionStatus.Chaos, move);
  162. actionChangeHandler.AddListener(UnitActionStatus.ServerSyncMove, move);
  163. actionChangeHandler.AddListener(UnitActionStatus.Escape, move);
  164. var stun = actionChangeHandler.AddListener(UnitActionStatus.Stun, (o) => { });
  165. actionChangeHandler.AddListener(UnitActionStatus.HitMove, stun);
  166. actionChangeHandler.AddListener(UnitActionStatus.Dead, (o) => { });
  167. actionChangeHandler.AddListener(UnitActionStatus.Damage, (o) => { });
  168. actionChangeHandler.AddListener(UnitActionStatus.Pick, (o) => { });
  169. actionChangeHandler.AddListener(UnitActionStatus.Spawn, (o) => { });
  170. actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkMove, (o) => { });
  171. actionChangeHandler.AddListener(UnitActionStatus.ChargeAtkIdle, (o) => { });
  172. actionChangeHandler.AddListener(UnitActionStatus.DaZuo, (o) => { });
  173. actionChangeHandler.AddListener(UnitActionStatus.DaZuoRecoveryAttr, (o) => { });
  174. actionChangeHandler.AddListener(UnitActionStatus.ChuanGongA, (o) => { });
  175. actionChangeHandler.AddListener(UnitActionStatus.ChuanGongB, (o) => { });
  176. actionChangeHandler.AddListener(UnitActionStatus.BuffLockStateAction, (o) => { });
  177. actionChangeHandler.AddListener(UnitActionStatus.HomeBack, (o) => { });
  178. actionChangeHandler.AddListener(UnitActionStatus.ClearYaoQi, (o) => { });
  179. actionChangeHandler.AddListener(UnitActionStatus.PetLock, (o) => { });
  180. actionChangeHandler.AddListener(UnitActionStatus.BreakShield, (o) => { });
  181. actionChangeHandler.AddListener(UnitActionStatus.Transport, (o) => { });
  182. actionChangeHandler.AddListener(UnitActionStatus.Skill, (o) => { });
  183. }
  184. }