BattleUnit.cs 8.2 KB

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