BattleUnit.cs 8.2 KB

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