ZoneObject.Unit.Status.cs 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. using CommonAI.RTS;
  2. using CommonLang.Vector;
  3. using CommonAI.Zone;
  4. using CommonAI.Zone.Attributes;
  5. using CommonAI.Zone.Helper;
  6. using CommonLang;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using CommonAI.Zone.Formula;
  12. using System.Collections;
  13. using CommonLang.Log;
  14. namespace CommonAI.ZoneClient
  15. {
  16. public partial class ZoneUnit
  17. { //--------------------------------------------------------------------------------
  18. #region DamageDeadFly
  19. private TimeExpire<UnitDamageEvent> mDamageTime;
  20. private TimeExpire<UnitDeadEvent> mDeadTime;
  21. private FallingDown mHitFlyState;
  22. public FallingDown StartFly(float z_speed, float gravity, float z_limit)
  23. {
  24. if (!Parent.IsSyncZ && z_speed != 0)
  25. {
  26. mHitFlyState = new FallingDown(this, z_speed, gravity, z_limit);
  27. return mHitFlyState;
  28. }
  29. return null;
  30. }
  31. protected virtual void DoDamage(UnitDamageEvent e)
  32. {
  33. mDamageTime = new TimeExpire<UnitDamageEvent>(e, e.DamageTimeMS);
  34. if (e.HasFly)
  35. {
  36. StartFly(e.ZSpeedSEC, e.ZGravity, e.ZLimit);
  37. }
  38. if (Info.DamageEffect != null)
  39. {
  40. Parent.PreQueueEvent(new UnitEffectEvent(this.ObjectID, Info.DamageEffect));
  41. }
  42. SetCurrentState(UnitActionStatus.Damage, e);
  43. }
  44. protected virtual void DoJump(UnitJumpEvent e)
  45. {
  46. StartFly(e.ZSpeed, e.ZGravity, e.ZLimit);
  47. }
  48. protected virtual void DoDead(UnitDeadEvent me)
  49. {
  50. if (me.RebirthTimeMS > 0)
  51. {
  52. mDeadTime = new TimeExpire<UnitDeadEvent>(me, me.RebirthTimeMS);
  53. }
  54. //所有的怪都清了一遍,都是先下发UnitDeadEvent,然后在下发死亡状态,为了防止切换状态的时候清掉特效,逻辑层的死亡特效屏蔽掉,放在表现层下
  55. //if (Info.DeadEffect != null)
  56. //{
  57. // Parent.PreQueueEvent(new UnitEffectEvent(this.ObjectID, Info.DeadEffect));
  58. //}
  59. if (me.Crushed)
  60. {
  61. if (Info.CrushEffect != null)
  62. {
  63. Parent.PreQueueEvent(new UnitEffectEvent(this.ObjectID, Info.CrushEffect));
  64. }
  65. }
  66. }
  67. private void UpdateDamage(int intervalMS)
  68. {
  69. if (mDeadTime != null && mDeadTime.Update(intervalMS))
  70. {
  71. mDeadTime = null;
  72. }
  73. if (mDamageTime != null && mDamageTime.Update(intervalMS))
  74. {
  75. mDamageTime = null;
  76. }
  77. }
  78. /// <summary>
  79. /// 落体运动
  80. /// </summary>
  81. public class FallingDown
  82. {
  83. private readonly ZoneUnit unit;
  84. private readonly float gravity;
  85. private readonly float z_limit;
  86. private float z_speed;
  87. public FallingDown(ZoneUnit unit, float zspeed, float zgravity, float zlimit)
  88. {
  89. this.unit = unit;
  90. this.z_limit = zlimit;
  91. this.z_speed = zspeed;
  92. this.gravity = zgravity == 0 ? unit.Templates.CFG.GLOBAL_GRAVITY : zgravity;
  93. this.ExpectTimeMS = MoveHelper.CalculateFlyTimeMS(unit.Z, zspeed, zlimit, gravity, 10);
  94. this.IsEnd = false;
  95. }
  96. public bool Update(int intervalMS)
  97. {
  98. //向上受到ZLimit限制//
  99. unit.Z += MoveHelper.GetDistance(intervalMS, z_speed);
  100. unit.Z = Math.Max(0, unit.Z);
  101. this.IsEnd = unit.Z <= 0;
  102. //骤停//
  103. if (z_limit != 0 && z_speed > 0 && unit.Z > z_limit)
  104. {
  105. z_speed = 0;
  106. }
  107. this.z_speed -= MoveHelper.GetDistance(intervalMS, gravity);
  108. return IsEnd;
  109. }
  110. public bool IsEnd { get; private set; }
  111. public int ExpectTimeMS { get; private set; }
  112. public float ZSpeedSEC { get { return z_speed; } }
  113. public float ZLimit { get { return z_limit; } }
  114. }
  115. #endregion
  116. //--------------------------------------------------------------------------------
  117. #region Skill
  118. public float GetSkillAttackRange(SkillTemplate skill)
  119. {
  120. return IsAttackRangeIncludeBodySize ? BodySize + skill.AttackRange : skill.AttackRange;
  121. }
  122. internal void SyncSkillStatus(ClientStruct.UnitSkillStatus[] skills)
  123. {
  124. if (skills != null)
  125. {
  126. foreach (ClientStruct.UnitSkillStatus st in skills)
  127. {
  128. SkillState status = this.GetSkillState(st.SkillTemplateID);
  129. if (status != null)
  130. {
  131. status.Sync(st);
  132. }
  133. }
  134. }
  135. }
  136. public virtual void InitSkills()
  137. {
  138. mSkillStatus.Clear();
  139. if (Info.BaseSkillID != null)
  140. {
  141. BaseSkillID = Info.BaseSkillID.SkillID;
  142. if (BaseSkillID != 0)
  143. {
  144. SkillTemplate skt = Templates.getSkill(BaseSkillID);
  145. if(skt == null)
  146. {
  147. ClientLog.LogError("Skill template({0}@{1}) Not Found.", BaseSkillID, Info.Name);
  148. return;
  149. }
  150. mSkillStatus.Put(new SkillState(skt, this));
  151. }
  152. }
  153. else
  154. {
  155. BaseSkillID = 0;
  156. }
  157. foreach (LaunchSkill skid in Info.Skills)
  158. {
  159. if (skid.SkillID != 0)
  160. {
  161. SkillTemplate skt = Templates.getSkill(skid.SkillID);
  162. if (skt == null)
  163. {
  164. ClientLog.LogError("Skill template({0}@{1}) Not Found.", skid.SkillID, Info.Name);
  165. return;
  166. }
  167. mSkillStatus.Put(new SkillState(skt, this));
  168. }
  169. }
  170. if (mOnSkillChanged != null)
  171. {
  172. mOnSkillChanged.Invoke(SkillOption.Init, this, BaseSkillID, mSkillStatus.Keys.ToArray());
  173. }
  174. }
  175. protected virtual void DoSkillChanged(PlayerSkillChangedEvent e)
  176. {
  177. this.mFastCastRate = e.unitFastCastRate;
  178. //一项一项排查,是不是有不同
  179. bool dif = false;
  180. if(e.baseSkill != null && e.baseSkill.TemplateID == BaseSkillID)
  181. {
  182. if(e.skills.Count == mSkillStatus.Count - 1)
  183. {
  184. foreach(var skt in e.skills)
  185. {
  186. if(!mSkillStatus.ContainsKey(skt.TemplateID))
  187. {
  188. dif = true;
  189. break;
  190. }
  191. }
  192. }
  193. else
  194. {
  195. dif = true;
  196. }
  197. }
  198. else
  199. {
  200. dif = true;
  201. }
  202. if (!dif)
  203. {
  204. //YXJDebug.logDebug(">skills are exactly the same, why you post >PlayerSkillChangedEvent");
  205. return;
  206. }
  207. mSkillStatus.Clear();
  208. if (e.baseSkill != null)
  209. {
  210. BaseSkillID = e.baseSkill.TemplateID;
  211. mSkillStatus.Put(new SkillState(e.baseSkill, this));
  212. }
  213. else
  214. {
  215. BaseSkillID = 0;
  216. }
  217. foreach (var skt in e.skills)
  218. {
  219. mSkillStatus.Put(new SkillState(skt, this));
  220. }
  221. if (mOnSkillChanged != null)
  222. {
  223. mOnSkillChanged.Invoke(SkillOption.Reset, this, BaseSkillID, mSkillStatus.Keys.ToArray());
  224. }
  225. }
  226. protected virtual void DoPlayerSkillActiveChangedEvent(PlayerSkillActiveChangedEvent e)
  227. {
  228. for (int i = e.Skills.Count - 1; i >= 0; --i)
  229. {
  230. PlayerSkillActiveChangedEvent.State sat = e.Skills[i];
  231. SkillState ss = GetSkillState(sat.SkillTemplateID);
  232. if (ss != null)
  233. {
  234. ss.SetActive(sat.ST);
  235. }
  236. }
  237. if (mOnSkillChanged != null)
  238. {
  239. mOnSkillChanged.Invoke(SkillOption.ActiveChange, this, 0, null);
  240. }
  241. }
  242. protected void AddSkill(SkillTemplate skill, bool isDefault)
  243. {
  244. mSkillStatus.Put(new SkillState(skill, this));
  245. if (isDefault)
  246. {
  247. BaseSkillID = skill.ID;
  248. }
  249. if (mOnSkillChanged != null)
  250. {
  251. mOnSkillChanged.Invoke(SkillOption.Add, this, skill.ID, mSkillStatus.Keys.ToArray());
  252. }
  253. }
  254. protected void RemoveSkill(int skillTemplateID)
  255. {
  256. if (mSkillStatus.RemoveByKey(skillTemplateID) != null)
  257. {
  258. if (BaseSkillID == skillTemplateID)
  259. {
  260. BaseSkillID = 0;
  261. }
  262. if (mOnSkillChanged != null)
  263. {
  264. mOnSkillChanged.Invoke(SkillOption.Remove, this, skillTemplateID, mSkillStatus.Keys.ToArray());
  265. }
  266. }
  267. }
  268. private void UpdateSkills(int intervalMS)
  269. {
  270. for (int i = 0; i < mSkillStatus.Count; i++)
  271. {
  272. SkillState ss = mSkillStatus.GetAt(i);
  273. ss.Update(intervalMS);
  274. }
  275. if (mChantingSkill != null && mChantingSkill.Update(intervalMS))
  276. {
  277. mChantingSkill = null;
  278. }
  279. }
  280. //新增技能使用次数
  281. protected virtual void DoPlayerSkillUseTimeChangedEvent(PlayerSkillUseTimeChangedEvent evt)
  282. {
  283. SkillState ss = mSkillStatus.Get(evt.SkillTemplateID);
  284. if (ss != null)
  285. {
  286. //UnityEngine.Debug.LogError("PlayerSkillUseTimeChangedEvent usetimes = " + evt.useTimes);
  287. ss.SyncSkillUseTimes(evt.useTimes);
  288. }
  289. }
  290. protected virtual void DoSkillAdded(PlayerSkillAddedEvent e)
  291. {
  292. this.AddSkill(e.Skill, e.IsDefault);
  293. }
  294. protected virtual void DoSkillRemoved(PlayerSkillRemovedEvent e)
  295. {
  296. this.RemoveSkill(e.SkillID);
  297. }
  298. protected virtual void DoLaunchSkill(UnitLaunchSkillEvent me)
  299. {
  300. //ClientLog.LogWarning(">doLaunchSkillAction>{0} @ {1}", me.action_index, me.skill_id);
  301. SkillState ss = mSkillStatus.Get(me.skill_id);
  302. if (ss != null)
  303. {
  304. this.mLastLaunchSkill = ss;
  305. ss.Launch(me);
  306. }
  307. doLaunchSkillAction(me);
  308. if (mOnLaunchSkill != null)
  309. {
  310. mOnLaunchSkill.Invoke(this, ss, me);
  311. }
  312. }
  313. protected virtual void DoSkillCDChanged(PlayerCDEvent evt)
  314. {
  315. if (evt.is_all)
  316. {
  317. for (int i = 0; i < mSkillStatus.Count; i++)
  318. {
  319. SkillState ss = mSkillStatus.GetAt(i);
  320. if (evt.is_clear)
  321. ss.ClearCD();
  322. else if (evt.is_decrease_time)
  323. ss.DecreaseCD(evt.decrease_timeMS);
  324. else if (evt.is_decrease_pct)
  325. ss.DecreaseCD_Pct(evt.decrease_pct);
  326. }
  327. }
  328. else
  329. {
  330. SkillState ss = mSkillStatus.Get(evt.skill_template_id);
  331. if (ss != null)
  332. {
  333. if (evt.is_clear)
  334. ss.ClearCD();
  335. else if (evt.is_decrease_time)
  336. ss.DecreaseCD(evt.decrease_timeMS);
  337. else if (evt.is_decrease_pct)
  338. ss.DecreaseCD_Pct(evt.decrease_pct);
  339. }
  340. }
  341. }
  342. protected virtual void DoChangeAction(UnitSkillActionChangeEvent e)
  343. {
  344. if (mLastLaunchSkill != null)
  345. {
  346. mLastLaunchSkill.ChangeAction(e.ActionIndex);
  347. }
  348. if (mCurrentSkillAction != null)
  349. {
  350. mCurrentSkillAction.onUnitSkillActionChangeEvent(e);
  351. }
  352. if (mLastLaunchSkill != null && mOnSkillActionChanged != null)
  353. {
  354. mOnSkillActionChanged.Invoke(this, mLastLaunchSkill, e.ActionIndex);
  355. }
  356. }
  357. protected virtual void DoPlayerSkillStopEvent(PlayerSkillStopEvent e)
  358. {
  359. SkillState ss = mSkillStatus.Get(e.SkillID);
  360. if (ss != null)
  361. {
  362. ss.PlayerStop(e);
  363. }
  364. mChantingSkill = null;
  365. clearSkillAction();
  366. }
  367. protected virtual void DoPlayerSkillTimeChangedEvent(PlayerSkillTimeChangedEvent e)
  368. {
  369. SkillState ss = mSkillStatus.Get(e.SkillTemplateID);
  370. if (ss != null)
  371. {
  372. ss.TimeChange(e);
  373. }
  374. }
  375. public int BaseSkillID { get; private set; }
  376. internal SkillMap mSkillStatus = new SkillMap();
  377. private SkillState mLastLaunchSkill;
  378. public class SkillMap
  379. {
  380. private HashMap<int, SkillState> Map = new HashMap<int, SkillState>();
  381. private List<SkillState> For = new List<SkillState>();
  382. public int Count { get { return For.Count; } }
  383. public ICollection<int> Keys { get { return Map.Keys; } }
  384. public IEnumerable<SkillState> Skills { get { return For; } }
  385. public SkillState[] SkillsArray { get { return For.ToArray(); } }
  386. public SkillState GetAt(int i)
  387. {
  388. return For[i];
  389. }
  390. public SkillState Get(int id)
  391. {
  392. return Map.Get(id);
  393. }
  394. public void Put(SkillState state)
  395. {
  396. SkillState old = Map.Get(state.Data.ID);
  397. if (old != null)
  398. {
  399. For.Remove(old);
  400. }
  401. Map.Put(state.Data.ID, state);
  402. For.Add(state);
  403. }
  404. public SkillState RemoveByKey(int id)
  405. {
  406. SkillState ret = Map.RemoveByKey(id);
  407. if (ret != null)
  408. {
  409. For.Remove(ret);
  410. }
  411. return ret;
  412. }
  413. public bool ContainsKey(int id)
  414. {
  415. return Map.ContainsKey(id);
  416. }
  417. public void Clear()
  418. {
  419. Map.Clear();
  420. For.Clear();
  421. }
  422. }
  423. public class SkillState
  424. {
  425. readonly public SkillTemplate Data;
  426. readonly public ZoneUnit Owner;
  427. private SkillActiveState current_state;
  428. private int all_action_time_ms;
  429. private int pass_time_ms;
  430. private int stop_time_ms;
  431. private float percent = 1f;
  432. private float action_speed = 1f;
  433. private int total_cd_time_ms;
  434. private bool is_in_mutil_time = false;
  435. private bool is_period_cd_end = false;
  436. //新增蓄力
  437. private ActionEnum action_type;
  438. //新增使用次数
  439. private int use_Times;
  440. private bool is_use_tiems = false;
  441. private bool is_skill_end = false;
  442. private bool is_Skill_Block = false;
  443. //新增技能描述类型
  444. private SkillDescType skill_desc_type = SkillDescType.None;
  445. //当前技能是否还从未触发过
  446. private bool is_skill_never_launch;
  447. //CD状态变化事件
  448. public System.Action OnCDStateChange;
  449. //蓄力状态发生了变化
  450. public System.Action OnChargeStateChange;
  451. //蓄力条进度
  452. public float ChargeProgress { private set; get; }
  453. internal SkillState(SkillTemplate data, ZoneUnit owner)
  454. {
  455. this.Data = data;
  456. this.Owner = owner;
  457. this.all_action_time_ms = Data.ActionQueueTimeMS;
  458. this.total_cd_time_ms = data.CoolDownMS;
  459. this.pass_time_ms = stop_time_ms = FullCDTimeMS;
  460. this.pass_time_ms = data.CoolDownMS;
  461. this.is_in_mutil_time = Data.IsSingleAction;
  462. this.use_Times = (int)data.CurUseTimes;
  463. this.is_use_tiems = this.use_Times >= 0 ? true : false;
  464. //以服务器为主,服务器那边目前没有做具体细分
  465. if (data.ActionPriority < 0)
  466. {
  467. skill_desc_type = SkillDescType.Displacement;
  468. }
  469. this.is_skill_never_launch = true;
  470. }
  471. internal bool TryLaunch(UnitLaunchSkillAction act)
  472. {
  473. if (IsActive &&
  474. Owner.MP >= Data.CostMP &&
  475. Owner.HP >= Data.CostHP &&
  476. (!IsCD))
  477. {
  478. return true;
  479. }
  480. return false;
  481. }
  482. internal void Launch(UnitLaunchSkillEvent evt)
  483. {
  484. this.is_skill_never_launch = false;
  485. this.percent = 0;
  486. this.pass_time_ms = 0;
  487. this.is_skill_end = false;
  488. this.CurrentActionID = evt.action_index;
  489. this.action_type = Data.ActionQueue[evt.action_index].SigleActionType;
  490. this.is_Skill_Block = Data.ActionQueue[evt.action_index].IsAddSkillBtnBlock;
  491. //ClientLog.LogWarning(">launch SkillState >{0} @ {1}, isMulti:{2}", CurrentActionID, this.Data.TemplateID, this.is_in_mutil_time);
  492. if (CurrentActionID + 1 == Data.ActionQueue.Count)
  493. {
  494. this.is_period_cd_end = this.Data.IsSingleAction;
  495. }
  496. if (evt.IsActionSpeedUP)
  497. {
  498. this.action_speed = evt.action_speed;
  499. }
  500. else
  501. {
  502. this.action_speed = 1f;
  503. }
  504. if (evt.IsChangeTotalCDTime)
  505. {
  506. this.total_cd_time_ms = evt.TotalCDTimeMS;
  507. }
  508. else
  509. {
  510. this.total_cd_time_ms = ToFullTimeCD();
  511. this.is_skill_end = true;
  512. //ClientLog.LogError("is_skill_end: true");
  513. }
  514. OnCDStateChange?.Invoke();
  515. if (Data.ActionQueue[CurrentActionID].ShowChargeTimeMS > 0)
  516. {
  517. ChargeProgress = 0f;
  518. OnChargeStateChange?.Invoke();
  519. }
  520. else if (ChargeProgress > 0 && ChargeProgress < 1)
  521. {
  522. ChargeProgress = 1f;
  523. OnChargeStateChange?.Invoke();
  524. }
  525. }
  526. public byte NextAction()
  527. {
  528. if (Data.IsSingleAction)
  529. {
  530. int action_step = CurrentActionID;
  531. int action_time = this.FullCDTimeMS;
  532. if (Data.IsCoolDownWithAction)
  533. {
  534. action_time = Data.ActionQueue[action_step % Data.ActionQueue.Count].TotalTimeMS;
  535. }
  536. // 是放技能时,处于多段攻击连击冷却时间范围
  537. if (pass_time_ms - stop_time_ms < Data.SingleActionCoolDownMS || pass_time_ms < (action_time + Data.SingleActionCoolDownMS))
  538. {
  539. action_step += 1;
  540. action_step = (byte)(action_step % Data.ActionQueue.Count);
  541. return (byte)action_step;
  542. }
  543. }
  544. return 0;
  545. }
  546. private int ToFullTimeCD()
  547. {
  548. if (Data.IsCoolDownWithAction)
  549. {
  550. if (Data.IsSingleAction)
  551. {
  552. return Data.ActionQueue[CurrentActionID].TotalTimeMS;
  553. }
  554. else
  555. {
  556. return all_action_time_ms;
  557. }
  558. }
  559. return Data.ToUnitSkillTotalTime(Owner.mFastCastRate);
  560. }
  561. internal void SetActive(SkillActiveState state)
  562. {
  563. this.current_state = state;
  564. }
  565. internal void ChangeAction(int step)
  566. {
  567. this.CurrentActionID = step;
  568. }
  569. internal void ClearCD()
  570. {
  571. this.pass_time_ms = FullCDTimeMS;
  572. }
  573. internal void DecreaseCD(int ms)
  574. {
  575. this.pass_time_ms += ms;
  576. }
  577. internal void DecreaseCD_Pct(float pct)
  578. {
  579. this.pass_time_ms += (int)(FullCDTimeMS * pct);
  580. }
  581. internal void PlayerStop(PlayerSkillStopEvent stop)
  582. {
  583. this.stop_time_ms = pass_time_ms;
  584. this.is_skill_end = true;
  585. this.is_Skill_Block = false;
  586. if (ChargeProgress > 0 && ChargeProgress < 1)
  587. {
  588. ChargeProgress = 1f;
  589. OnChargeStateChange?.Invoke();
  590. }
  591. }
  592. internal void Sync(ClientStruct.UnitSkillStatus syn)
  593. {
  594. this.pass_time_ms = syn.PassTime;
  595. this.use_Times = syn.useTimes;
  596. this.is_use_tiems = syn.useTimes >= 0 ? true : false;
  597. internal_update();
  598. }
  599. internal void TimeChange(PlayerSkillTimeChangedEvent e)
  600. {
  601. if (this.is_skill_never_launch)
  602. {
  603. //重新登录后,技能的状态会丢失,这里重新算一下
  604. if(e.SkillPassTimeMS < e.SkillTotalTimeMS)
  605. {
  606. //CD未结束的技能,action都是在最后一段(不然不会有CD)
  607. this.CurrentActionID = Data.ActionQueue.Count - 1;
  608. }
  609. }
  610. this.pass_time_ms = e.SkillPassTimeMS;
  611. this.total_cd_time_ms = e.SkillTotalTimeMS;
  612. this.is_in_mutil_time = this.Data.IsSingleAction;
  613. this.is_period_cd_end = this.Data.IsSingleAction;
  614. this.is_Skill_Block = false;
  615. this.is_skill_end = true;
  616. internal_update();
  617. OnCDStateChange?.Invoke();
  618. if(ChargeProgress > 0 && ChargeProgress < 1)
  619. {
  620. ChargeProgress = 1f;
  621. OnChargeStateChange?.Invoke();
  622. }
  623. }
  624. internal void Update(int intervalMS)
  625. {
  626. if (!IsActive && IsPauseOnDeactive) { return; }
  627. this.pass_time_ms += intervalMS;
  628. internal_update();
  629. if (this.isPeriodCDEnd)
  630. {
  631. SetMutilTime();
  632. }
  633. var aq = Data.ActionQueue[CurrentActionID];
  634. if (aq.ShowChargeTimeMS > 0)
  635. {
  636. if (ChargeProgress < 1)
  637. {
  638. ChargeProgress = pass_time_ms / aq.ShowChargeTimeMS;
  639. if (ChargeProgress > 1) ChargeProgress = 1f;
  640. OnChargeStateChange?.Invoke();
  641. }
  642. }
  643. else if (ChargeProgress > 0 && ChargeProgress < 1)
  644. {
  645. ChargeProgress = 1f;
  646. OnChargeStateChange?.Invoke();
  647. }
  648. }
  649. internal void SetPassTime(int passTime)
  650. {
  651. this.pass_time_ms = passTime;
  652. internal_update();
  653. }
  654. void SetMutilTime()
  655. {
  656. if (CDPercent < 1)
  657. {
  658. this.is_in_mutil_time = false;
  659. }
  660. else
  661. {
  662. this.is_in_mutil_time = true;
  663. this.is_period_cd_end = false;
  664. }
  665. }
  666. private void internal_update()
  667. {
  668. if (pass_time_ms >= FullCDTimeMS)
  669. {
  670. if (percent < 1)
  671. {
  672. percent = 1f;
  673. stop_time_ms = pass_time_ms;
  674. OnCDStateChange?.Invoke();
  675. }
  676. }
  677. else
  678. {
  679. percent = (float)pass_time_ms / FullCDTimeMS;
  680. }
  681. }
  682. //新增技能使用次数
  683. internal void SyncSkillUseTimes(int usetimes)
  684. {
  685. this.use_Times = usetimes;
  686. this.is_use_tiems = true;
  687. }
  688. public int FullCDTimeMS
  689. {
  690. get
  691. {
  692. return total_cd_time_ms;
  693. }
  694. }
  695. public bool isMutilTime
  696. {
  697. get { return this.is_in_mutil_time; }
  698. }
  699. public bool isPeriodCDEnd
  700. {
  701. get { return this.is_period_cd_end; }
  702. }
  703. public bool IsCD
  704. {
  705. get
  706. {
  707. if (Data.IsCoolDownWithAction)
  708. {
  709. return false;
  710. }
  711. return (percent < 1f);
  712. }
  713. }
  714. public float CDPercent
  715. {
  716. get
  717. {
  718. if (Data.IsCoolDownWithAction)
  719. {
  720. return 1f;
  721. }
  722. return percent;
  723. }
  724. }
  725. public float ActionSpeed { get { return action_speed; } }
  726. public int PassTimeMS { get { return pass_time_ms; } }
  727. public int StopTimeMS { get { return stop_time_ms; } }
  728. public int CurrentActionID { get; private set; }
  729. public SkillActiveState ActiveState { get { return current_state; } }
  730. public bool IsActive { get { return current_state == SkillActiveState.Active || current_state == SkillActiveState.ActiveAndHide; } }
  731. public bool IsPauseOnDeactive { get { return current_state == SkillActiveState.DeactiveAndPause; } }
  732. public ActionEnum ActionType { get { return action_type; } }
  733. public int UseTimes { get { return this.use_Times; } }
  734. public bool IsUseTimes { get { return this.is_use_tiems; } }
  735. //目前仅用于蓄力,后面如果有其它类型需要用到,注意TimeChange和StopSkill消息的顺序.......
  736. public bool IsSkillEnd {
  737. get { return this.is_skill_end; }
  738. }
  739. public bool IsSkillBlock { get { return this.is_Skill_Block; } }
  740. public SkillDescType SkillType { get { return this.skill_desc_type; } }
  741. }
  742. [Obsolete]
  743. public List<SkillState> GetSkillStatus()
  744. {
  745. return new List<SkillState>(mSkillStatus.Skills);
  746. }
  747. public void GetSkillStatus(List<SkillState> ret)
  748. {
  749. ret.AddRange(mSkillStatus.Skills);
  750. }
  751. public SkillState GetSkillState(int templateID)
  752. {
  753. return mSkillStatus.Get(templateID);
  754. }
  755. #endregion
  756. //---------------------------------------------------------------------------
  757. // skill action
  758. //---------------------------------------------------------------------------
  759. #region SkillAction
  760. /// <summary>
  761. /// 是否正在吟唱
  762. /// </summary>
  763. public bool IsChanttingSkill
  764. {
  765. get { return mChantingSkill != null; }
  766. }
  767. public int ChantingSkillPassMS
  768. {
  769. get { return (mChantingSkill != null) ? mChantingSkill.PassTimeMS : 0; }
  770. }
  771. public int ChantingSkillTotalMS
  772. {
  773. get { return (mChantingSkill != null) ? mChantingSkill.TotalTimeMS : 0; }
  774. }
  775. public SkillTemplate ChantingSkillData
  776. {
  777. get { return (mChantingSkill != null) ? mChantingSkill.Tag : null; }
  778. }
  779. public SkillState ChantingSkill
  780. {
  781. get { if (mChantingSkill != null) { return GetSkillState(mChantingSkill.Tag.TemplateID); } return null; }
  782. }
  783. private TimeExpire<SkillTemplate> mChantingSkill;
  784. protected virtual void DoUnitChantSkillEvent(UnitChantSkillEvent e)
  785. {
  786. var temp = Templates.getSkill(e.skill_id);
  787. mChantingSkill = new TimeExpire<SkillTemplate>(temp, e.chant_ms);
  788. SkillState ss = GetSkillState(e.skill_id);
  789. if (ss != null)
  790. {
  791. if (mOnChantSkill != null)
  792. {
  793. mOnChantSkill.Invoke(this, e, mChantingSkill);
  794. }
  795. }
  796. }
  797. protected virtual void doLaunchSkillAction(UnitLaunchSkillEvent me)
  798. {
  799. clearSkillAction();
  800. var temp = Templates.getSkill(me.skill_id);
  801. if (temp != null)
  802. {
  803. mCurrentSkillAction = new UnitSkillAction(this, temp);
  804. mCurrentSkillAction.onLaunch(me);
  805. invokeSkillActionStart(mCurrentSkillAction);
  806. }
  807. }
  808. protected virtual void invokeSkillActionStart(ISkillAction act)
  809. {
  810. if (mOnSkillActionStart != null)
  811. {
  812. mOnSkillActionStart.Invoke(this, act);
  813. }
  814. }
  815. public virtual ISkillAction CurrentSkillAction
  816. {
  817. get { return mCurrentSkillAction; }
  818. }
  819. public virtual UnitActionData CurrentSkillActionData
  820. {
  821. get
  822. {
  823. if (CurrentSkillAction != null)
  824. {
  825. return CurrentSkillAction.CurrentAction;
  826. }
  827. return null;
  828. }
  829. }
  830. //-------------------------------------------------------------------------------------------
  831. // unit impl
  832. private UnitSkillAction mCurrentSkillAction;
  833. private void updateSkillAction(int intervalMS)
  834. {
  835. if (mCurrentSkillAction != null)
  836. {
  837. //UnityEngine.Debug.LogError("2 mCurrentSkillAction = " + mCurrentSkillAction.SkillData.TemplateID + " actionIndex = " + mCurrentSkillAction.CurrentActionIndex);
  838. mCurrentSkillAction.onUpdate(intervalMS);
  839. if (mCurrentSkillAction.IsDone)
  840. {
  841. clearSkillAction();
  842. }
  843. }
  844. }
  845. private void clearSkillAction()
  846. {
  847. if (mCurrentSkillAction != null)
  848. {
  849. mCurrentSkillAction.onStop();
  850. mCurrentSkillAction = null;
  851. }
  852. }
  853. public abstract class ISkillAction
  854. {
  855. private BitSet8 current_action_status = new BitSet8();
  856. public bool IsControlMoveable { get { return current_action_status.Get(0); } protected set { current_action_status.Set(0, value); } }
  857. public bool IsControlFaceable { get { return current_action_status.Get(1); } protected set { current_action_status.Set(1, value); } }
  858. public bool IsCancelableBySkill { get { return current_action_status.Get(2); } protected set { current_action_status.Set(2, value); } }
  859. public bool IsCancelableByMove { get { return current_action_status.Get(3); } protected set { current_action_status.Set(3, value); } }
  860. public bool IsNoneBlock { get { return current_action_status.Get(4); } protected set { current_action_status.Set(4, value); } }
  861. public bool IsNoneTouch { get { return current_action_status.Get(5); } protected set { current_action_status.Set(5, value); } }
  862. public bool IsFaceToTarget { get { return current_action_status.Get(6); } protected set { current_action_status.Set(6, value); } }
  863. public abstract SkillTemplate SkillData { get; }
  864. public abstract UnitLaunchSkillEvent LaunchEvent { get; }
  865. public abstract bool IsDone { get; }
  866. public abstract float ActionSpeed { get; }
  867. public abstract byte ActionStepIndex { get; }
  868. public abstract int[] ActionTimeArray { get; }
  869. public abstract int TotalTimeMS { get; }
  870. public abstract int CurrentActionIndex { get; }
  871. public abstract string CurrentActionName { get; }
  872. public abstract UnitActionData CurrentAction { get; }
  873. public abstract float ExpirePercent { get; }
  874. //把当前的执行时间抛出去给客户端用,很违和的做法,没办法数据配在扩展属性里面
  875. //CommonAI取不出来,强行操作会引发不可控的Bug
  876. public abstract int CurPassTimeMS { get; }
  877. public abstract bool IsJumpAction { get; }
  878. internal abstract void onLaunch(UnitLaunchSkillEvent e);
  879. internal abstract void onUnitSkillActionChangeEvent(UnitSkillActionChangeEvent evt);
  880. internal abstract void onUpdate(int intervalMS);
  881. internal abstract void onStop();
  882. internal abstract void RestJumpAction(bool isJumpAction);
  883. }
  884. public class UnitSkillAction : ISkillAction
  885. {
  886. readonly private ZoneUnit ownerUnit;
  887. readonly private SkillTemplate skill;
  888. private UnitLaunchSkillEvent launch_event;
  889. private int current_pass_time = 0;
  890. private int total_pass_time = 0;
  891. private int total_time_ms = 0;
  892. private bool is_done = false;
  893. private bool is_jump_action = false;
  894. private Queue<UnitActionData> action_queue;
  895. private int current_action_index = 0;
  896. private UnitActionData current_action = null;
  897. private PopupKeyFrames<UnitActionData.KeyFrame> current_frames = new PopupKeyFrames<UnitActionData.KeyFrame>();
  898. private int current_action_total_time = 0;
  899. private int[] action_time_array;
  900. public ZoneLayer Parent { get { return ownerUnit.Parent; } }
  901. public override int[] ActionTimeArray { get { return action_time_array; } }
  902. public override int TotalTimeMS { get { return total_time_ms; } }
  903. public override byte ActionStepIndex { get { return (launch_event != null) ? launch_event.action_index : (byte)0; } }
  904. public override int CurrentActionIndex { get { return current_action_index; } }
  905. public override string CurrentActionName { get { return (current_action != null) ? current_action.ActionName : null; } }
  906. public override UnitActionData CurrentAction { get { return current_action; } }
  907. public override float ExpirePercent { get { return total_pass_time / (float)TotalTimeMS; } }
  908. public override SkillTemplate SkillData { get { return skill; } }
  909. public override UnitLaunchSkillEvent LaunchEvent { get { return launch_event; } }
  910. public override bool IsDone { get { return is_done; } }
  911. public override int CurPassTimeMS { get { return current_pass_time; } }
  912. protected internal UnitSkillAction(ZoneUnit actor, SkillTemplate skill)
  913. {
  914. this.skill = skill;
  915. this.ownerUnit = actor;
  916. this.action_time_array = skill.ActionQueueTimeArray;
  917. }
  918. public override float ActionSpeed
  919. {
  920. get
  921. {
  922. if (launch_event != null)
  923. {
  924. if(launch_event.action_speed_add != null && launch_event.action_index < launch_event.action_speed_add.Length)
  925. {
  926. return launch_event.action_speed + launch_event.action_speed_add[launch_event.action_index];
  927. }
  928. return launch_event.action_speed;
  929. }
  930. return 1f;
  931. }
  932. }
  933. public override bool IsJumpAction
  934. {
  935. get
  936. {
  937. return is_jump_action;
  938. }
  939. }
  940. internal override void onLaunch(UnitLaunchSkillEvent e)
  941. {
  942. //UnityEngine.Debug.LogError("SkillID = " + e.skill_id + " ActionSpeed = " + e.action_speed + " action_index = " + e.action_index);
  943. this.launch_event = e;
  944. this.total_pass_time = 0;
  945. this.current_pass_time = 0;
  946. if (e.action_time_array != null && e.action_time_array.Length > 0)
  947. {
  948. this.total_time_ms = e.TotalActionTimeMS;
  949. this.action_time_array = e.action_time_array;
  950. }
  951. else if (skill.IsSingleAction)
  952. {
  953. this.total_time_ms = skill.ActionQueue[launch_event.action_index].TotalTimeMS;
  954. this.action_time_array = new int[] { TotalTimeMS };
  955. }
  956. else
  957. {
  958. this.total_time_ms = skill.ActionQueueTimeMS;
  959. this.action_time_array = skill.ActionQueueTimeArray;
  960. }
  961. if (skill.IsSingleAction)
  962. {
  963. var sa = skill.ActionQueue[launch_event.action_index];
  964. this.action_queue = new Queue<UnitActionData>(1);
  965. this.action_queue.Enqueue(sa);
  966. this.current_action_index = launch_event.action_index;
  967. }
  968. else
  969. {
  970. this.action_queue = new Queue<UnitActionData>(skill.ActionQueue);
  971. this.current_action_index = launch_event.action_index;
  972. }
  973. this.current_action = null;
  974. this.nextAction(this.current_action_index);
  975. }
  976. internal override void onUnitSkillActionChangeEvent(UnitSkillActionChangeEvent e)
  977. {
  978. while (this.CurrentActionIndex < e.ActionIndex)
  979. {
  980. if (!nextAction(current_action_index + 1))
  981. {
  982. this.total_pass_time = TotalTimeMS;
  983. this.is_done = true;
  984. break;
  985. }
  986. }
  987. }
  988. internal override void onStop()
  989. {
  990. this.is_done = true;
  991. this.total_pass_time = TotalTimeMS;
  992. }
  993. internal override void onUpdate(int intervalMS)
  994. {
  995. int time_pass = (int)(intervalMS * ActionSpeed);
  996. this.total_pass_time += time_pass;
  997. this.total_pass_time = Math.Min(total_pass_time, TotalTimeMS);
  998. this.current_pass_time += time_pass;
  999. if (current_action == null)
  1000. {
  1001. nextAction(current_action_index + 1);
  1002. }
  1003. if (current_action == null)
  1004. {
  1005. this.is_done = true;
  1006. this.total_pass_time = TotalTimeMS;
  1007. }
  1008. else
  1009. {
  1010. // 关键帧 //
  1011. using (var kfs = ListObjectPool<UnitActionData.KeyFrame>.AllocAutoRelease())
  1012. {
  1013. if (current_frames.PopKeyFrames(current_pass_time, kfs) > 0)
  1014. {
  1015. for (int i = 0; i < kfs.Count; i++)
  1016. {
  1017. doKeyFrame(kfs[i]);
  1018. }
  1019. }
  1020. }
  1021. // 下段动作 //
  1022. if ((current_action != null) && (current_pass_time >= current_action_total_time))
  1023. {
  1024. current_action = null;
  1025. if (action_queue.Count == 0)
  1026. {
  1027. this.is_done = true;
  1028. this.total_pass_time = TotalTimeMS;
  1029. }
  1030. }
  1031. }
  1032. }
  1033. private bool nextAction(int index)
  1034. {
  1035. if (action_queue != null && action_queue.Count > 0)
  1036. {
  1037. this.current_action_index = index;
  1038. this.current_action = action_queue.Dequeue();
  1039. if (current_action != null)
  1040. {
  1041. this.current_frames.AddRange(current_action.KeyFrames);
  1042. this.current_pass_time = 0;
  1043. //NewAction//
  1044. if (launch_event.action_time_array != null)
  1045. {
  1046. if (skill.IsSingleAction)
  1047. {
  1048. this.current_action_total_time = launch_event.TotalActionTimeMS;
  1049. }
  1050. else if (current_action_index < launch_event.action_time_array.Length)
  1051. {
  1052. this.current_action_total_time = launch_event.action_time_array[current_action_index];
  1053. }
  1054. }
  1055. else
  1056. {
  1057. this.current_action_total_time = current_action.TotalTimeMS;
  1058. }
  1059. this.IsCancelableBySkill = current_action.IsCancelableBySkill;
  1060. this.IsNoneBlock = current_action.IsNoneBlock;
  1061. this.IsNoneTouch = current_action.IsNoneTouch;
  1062. this.IsFaceToTarget = current_action.IsFaceToTarget;
  1063. this.IsCancelableByMove = current_action.IsCancelable;
  1064. this.IsControlMoveable = current_action.IsControlMoveable;
  1065. this.IsControlFaceable = current_action.IsControlFaceable;
  1066. return true;
  1067. }
  1068. }
  1069. current_action = null;
  1070. return false;
  1071. }
  1072. private void doKeyFrame(UnitActionData.KeyFrame kf)
  1073. {
  1074. // 关键帧改变状态
  1075. if (kf.ChangeStatus != null)
  1076. {
  1077. this.IsNoneBlock = kf.ChangeStatus.IsNoneBlock;
  1078. this.IsNoneTouch = kf.ChangeStatus.IsNoneTouch;
  1079. this.IsFaceToTarget = kf.ChangeStatus.IsFaceToTarget;
  1080. this.IsCancelableByMove = kf.ChangeStatus.IsCancelable;
  1081. this.IsCancelableBySkill = kf.ChangeStatus.IsCancelableBySkill;
  1082. this.IsControlMoveable = kf.ChangeStatus.IsControlMoveable;
  1083. this.IsControlFaceable = kf.ChangeStatus.IsControlFaceable;
  1084. }
  1085. }
  1086. internal override void RestJumpAction(bool isJumpAction)
  1087. {
  1088. is_jump_action = isJumpAction;
  1089. }
  1090. }
  1091. //-------------------------------------------------------------------------------------------
  1092. #endregion
  1093. //--------------------------------------------------------------------------------
  1094. #region Buff
  1095. internal BuffMap mBuffStatus = new BuffMap();
  1096. internal void SyncBuffStatus(ClientStruct.UnitBuffStatus[] buffs)
  1097. {
  1098. if (buffs != null)
  1099. {
  1100. for (int i = 0; i < buffs.Length; i++)
  1101. {
  1102. ClientStruct.UnitBuffStatus st = buffs[i];
  1103. TryAddBuff(st.BuffTemplateID, st.SenderID, st.IsEquip, st.TotalTime, st.OverlayLevel, st.PassTime);
  1104. }
  1105. }
  1106. }
  1107. protected void SyncBuffOverlayLevel(BuffDataNotify data)
  1108. {
  1109. BuffState bs = mBuffStatus.Get(data.buffId, data.senderId);
  1110. if (bs != null)
  1111. {
  1112. bs.SyncBuffExtData(data.overlayLevel, bs.BuffExtendData);
  1113. //YXJDebug.logError(data.buffId + " , level:" + bs.OverlayLevel + " time = " + bs.BuffTotalTime + ", per:"+bs.CDPercent);
  1114. if (mOnBuffChanged != null)
  1115. {
  1116. mOnBuffChanged.Invoke(this, bs);
  1117. }
  1118. }
  1119. }
  1120. protected virtual void DoLaunchBuff(UnitLaunchBuffEvent me)
  1121. {
  1122. //YXJDebug.logError(me.buffTemplateID + " overlayLevel = " + me.overlayLevel + " time = " + me.buffTimeMS);
  1123. TryAddBuff(me.buffTemplateID, me.senderID, me.IsEquip, me.buffTimeMS, me.overlayLevel, 0, me.buffExtData);
  1124. }
  1125. protected virtual void DoStopBuff(UnitStopBuffEvent e)
  1126. {
  1127. TryRemoveBuff(e.buffTemplateID, e.senderID);
  1128. }
  1129. //新增Buff接口
  1130. protected virtual void DoRemoveOverlayLevelBuff(SyncUnitBuffState e)
  1131. {
  1132. //UnityEngine.Debug.LogError("DoRemoveOverlayLevelBuff overlayLevel = " + e.overlayLevel + " buffExtData = " + e.buffExtData);
  1133. if (e.buffExtData > 0)
  1134. {
  1135. var bs = mBuffStatus.Get(e.buffTemplateID, e.ObjectID);
  1136. if (bs != null && mOnRemoveBuffByOverlayLeveled != null)
  1137. {
  1138. mOnRemoveBuffByOverlayLeveled(this, bs);
  1139. bs.SyncBuffExtData(e.overlayLevel, e.buffExtData);
  1140. }
  1141. }
  1142. else
  1143. {
  1144. TryRemoveBuff(e.buffTemplateID, e.ObjectID);
  1145. }
  1146. }
  1147. private void TryAddBuff(int buffTempalteID, uint senderID, bool isEquip, int totalTime, byte overlayLevel, int passTime = 0, int buffExtData = 0)
  1148. {
  1149. BuffTemplate buff = Templates.getBuff(buffTempalteID);
  1150. if (buff != null)
  1151. {
  1152. BuffState bs = mBuffStatus.Get(buffTempalteID, senderID);
  1153. if (bs != null)
  1154. {
  1155. bs.Sync(overlayLevel, totalTime, passTime, buffExtData);
  1156. if (mOnBuffChanged != null)
  1157. {
  1158. mOnBuffChanged.Invoke(this, bs);
  1159. }
  1160. }
  1161. else
  1162. {
  1163. bs = new BuffState(buff, this, senderID, isEquip);
  1164. bs.Sync(overlayLevel, totalTime, passTime, buffExtData);
  1165. mBuffStatus.Put(bs);
  1166. if (mOnBuffAdded != null)
  1167. {
  1168. mOnBuffAdded.Invoke(this, bs);
  1169. }
  1170. }
  1171. //UnityEngine.Debug.LogError("Server buffID = " + buffTempalteID + " LockStateAction = " + buff.LockStateAction);
  1172. //新增单独状态
  1173. if (!string.IsNullOrEmpty(buff.LockStateAction))
  1174. {
  1175. this.SetCurrentState(UnitActionStatus.BuffLockStateAction, CurrentSubState, buff);
  1176. }
  1177. }
  1178. }
  1179. private void TryRemoveBuff(int buffTempalteID, uint senderID)
  1180. {
  1181. BuffState bs = mBuffStatus.RemoveByKey(buffTempalteID, senderID);
  1182. if (bs != null)
  1183. {
  1184. bs.OnEnd();
  1185. if (mOnBuffRemoved != null)
  1186. {
  1187. mOnBuffRemoved.Invoke(this, bs);
  1188. }
  1189. //UnityEngine.Debug.LogError("RemoveBuff CurrentState= " + CurrentState);
  1190. //置回单独状态
  1191. if (CurrentState == UnitActionStatus.BuffLockStateAction)
  1192. {
  1193. this.SetCurrentState(UnitActionStatus.Idle);
  1194. }
  1195. }
  1196. }
  1197. private void UpdateBuffs(int intervalMS)
  1198. {
  1199. mBuffStatus.ForEachUpdate(intervalMS);
  1200. //mBuffStatus.ForEach((bs) =>
  1201. //{
  1202. // bs.OnUpdate(intervalMS);
  1203. //});
  1204. }
  1205. //返回当前buff列表中,是否含有变身buff
  1206. public bool HasMakeAvatarBuff()
  1207. {
  1208. bool has = false;
  1209. mBuffStatus.ForEach((BuffState buf) => {
  1210. if (buf.Data.MakeAvatar)
  1211. {
  1212. has = true;
  1213. }
  1214. });
  1215. return has;
  1216. }
  1217. //返回当前buff列表中,是否含有切换技能buff
  1218. public bool HasChangeSkillBuff()
  1219. {
  1220. bool has = false;
  1221. mBuffStatus.ForEach((BuffState buf) => {
  1222. if (buf.Data.UnitChangeSkills)
  1223. {
  1224. has = true;
  1225. }
  1226. });
  1227. return has;
  1228. }
  1229. public class BuffMap
  1230. {
  1231. private HashMap<int, BuffList> Map = new HashMap<int, BuffList>();
  1232. private List<BuffList> For = new List<BuffList>();
  1233. public BuffState Get(int id, uint senderID)
  1234. {
  1235. var list = Map.Get(id);
  1236. if (list != null)
  1237. {
  1238. return list.Get(senderID);
  1239. }
  1240. return null;
  1241. }
  1242. internal void Put(BuffState state)
  1243. {
  1244. var list = Map.Get(state.BuffID);
  1245. if (list == null)
  1246. {
  1247. list = new BuffList(state.Data);
  1248. Map.Add(state.BuffID, list);
  1249. For.Add(list);
  1250. }
  1251. list.Add(state);
  1252. }
  1253. internal BuffState RemoveByKey(int id, uint senderID)
  1254. {
  1255. var list = Map.Get(id);
  1256. if (list != null)
  1257. {
  1258. var st = list.Get(senderID);
  1259. if (st != null)
  1260. {
  1261. var ret = list.Remove(senderID);
  1262. if (list.Count == 0)
  1263. {
  1264. For.Remove(list);
  1265. Map.Remove(id);
  1266. }
  1267. return ret;
  1268. }
  1269. }
  1270. return null;
  1271. }
  1272. internal void Clear()
  1273. {
  1274. For.Clear();
  1275. Map.Clear();
  1276. }
  1277. public void ForEach(Action<BuffState> action)
  1278. {
  1279. for (int i = For.Count - 1; i >= 0; --i)
  1280. {
  1281. For[i].ForEach(action);
  1282. }
  1283. }
  1284. public int GetBuffCount()
  1285. {
  1286. return For.Count;
  1287. }
  1288. public void ForEachUpdate(int intervalMS)
  1289. {
  1290. for (int i = For.Count - 1; i >= 0; --i)
  1291. {
  1292. For[i].ForEachUpdate(intervalMS);
  1293. }
  1294. }
  1295. public class BuffList
  1296. {
  1297. public readonly BuffTemplate Data;
  1298. private HashMap<uint, BuffState> Map = new HashMap<uint, BuffState>();
  1299. private List<BuffState> For = new List<BuffState>();
  1300. public int Count { get { return Map.Count; } }
  1301. internal BuffList(BuffTemplate data)
  1302. {
  1303. this.Data = data;
  1304. }
  1305. internal BuffState Remove(uint senderID)
  1306. {
  1307. BuffState ret;
  1308. if (Data.IsDuplicating)
  1309. {
  1310. ret = Map.RemoveByKey(senderID);
  1311. }
  1312. else
  1313. {
  1314. ret = Map.RemoveByKey(0);
  1315. }
  1316. if (ret != null)
  1317. {
  1318. For.Remove(ret);
  1319. }
  1320. return ret;
  1321. }
  1322. internal void Add(BuffState state)
  1323. {
  1324. if (Data.IsDuplicating)
  1325. {
  1326. Map.Add(state.SenderID, state);
  1327. }
  1328. else
  1329. {
  1330. Map.Add(0, state);
  1331. }
  1332. For.Add(state);
  1333. }
  1334. internal BuffState Get(uint senderID)
  1335. {
  1336. if (Data.IsDuplicating)
  1337. {
  1338. if (senderID == 0 && Map.Count > 0)
  1339. {
  1340. return Map.First().Value;
  1341. }
  1342. else
  1343. {
  1344. return Map.Get(senderID);
  1345. }
  1346. }
  1347. else
  1348. {
  1349. return Map.Get(0);
  1350. }
  1351. }
  1352. internal void ForEach(Action<BuffState> action)
  1353. {
  1354. for (int i = For.Count - 1; i >= 0; --i)
  1355. {
  1356. action(For[i]);
  1357. }
  1358. }
  1359. public void ForEachUpdate(int intervalMS)
  1360. {
  1361. for (int i = For.Count - 1; i >= 0; --i)
  1362. {
  1363. For[i].OnUpdate(intervalMS);
  1364. }
  1365. }
  1366. }
  1367. }
  1368. public class BuffState
  1369. {
  1370. readonly public BuffTemplate Data;
  1371. readonly public bool isEquip;
  1372. readonly public ZoneUnit Owner;
  1373. readonly public uint SenderID;
  1374. private byte overlay_level;
  1375. private int total_time;
  1376. private int pass_time = 0;
  1377. private float percent = 1f;
  1378. private int buff_ext_data = 0;
  1379. private int buff_ext_data_index = -1;
  1380. private TimeInterval<BuffTemplate.KeyFrame> interval;
  1381. private PopupKeyFrames<BuffTemplate.KeyFrame> keyframes;
  1382. internal BuffState(BuffTemplate data, ZoneUnit owner, uint senderID, bool isEquip)
  1383. {
  1384. this.Data = data;
  1385. this.isEquip = isEquip;
  1386. this.Owner = owner;
  1387. this.SenderID = senderID;
  1388. }
  1389. internal void Sync(byte overlayLevel, int totalTime, int passTime, int buffExtData = 0)
  1390. {
  1391. this.overlay_level = overlayLevel;
  1392. this.total_time = totalTime;
  1393. if (passTime < this.pass_time || this.pass_time == 0)
  1394. {
  1395. this.interval = new TimeInterval<BuffTemplate.KeyFrame>(Data.HitIntervalMS);
  1396. this.interval.Tag = Data.HitKeyFrame;
  1397. this.keyframes = new PopupKeyFrames<BuffTemplate.KeyFrame>();
  1398. this.keyframes.AddRange(Data.KeyFrames);
  1399. this.pass_time = 0;
  1400. }
  1401. if (passTime > this.pass_time)
  1402. {
  1403. this.interval.SetPassTime(passTime);
  1404. this.keyframes.PopKeyFrames(passTime, null);
  1405. this.pass_time = passTime;
  1406. }
  1407. this.percent = Math.Min(1, pass_time / (float)total_time);
  1408. this.buff_ext_data = buffExtData;
  1409. SetBuffExtendDataIndex(buffExtData);
  1410. }
  1411. internal void OnEnd()
  1412. {
  1413. if (Data.EndKeyFrame != null && Data.EndKeyFrame.Effect != null)
  1414. {
  1415. Owner.Parent.PreQueueEvent(new UnitEffectEvent(Owner.ObjectID, Data.EndKeyFrame.Effect));
  1416. }
  1417. }
  1418. internal void OnUpdate(int intervalMS)
  1419. {
  1420. using (var kfs = ListObjectPool<BuffTemplate.KeyFrame>.AllocAutoRelease())
  1421. {
  1422. if (keyframes.PopKeyFrames(pass_time, kfs) > 0)
  1423. {
  1424. for (int i = 0; i < kfs.Count; i++)
  1425. {
  1426. BuffTemplate.KeyFrame kf = kfs[i];
  1427. if (kf.Effect != null)
  1428. {
  1429. Owner.Parent.PreQueueEvent(new UnitEffectEvent(Owner.ObjectID, kf.Effect));
  1430. }
  1431. }
  1432. }
  1433. }
  1434. if (interval.Update(intervalMS))
  1435. {
  1436. if (Data.HitKeyFrame != null && Data.HitKeyFrame.Effect != null)
  1437. {
  1438. Owner.Parent.PreQueueEvent(new UnitEffectEvent(Owner.ObjectID, Data.HitKeyFrame.Effect));
  1439. }
  1440. }
  1441. this.pass_time += intervalMS;
  1442. if (percent < 1f)
  1443. {
  1444. this.percent = Math.Min(1, pass_time / (float)total_time);
  1445. }
  1446. }
  1447. public void SyncBuffExtData(byte overlayLevel, int buffExtData)
  1448. {
  1449. overlay_level = overlayLevel;
  1450. buff_ext_data = buffExtData;
  1451. }
  1452. private void SetBuffExtendDataIndex(int curBuffExtData)
  1453. {
  1454. if (curBuffExtData < 1) return;
  1455. if (curBuffExtData < 10)
  1456. {
  1457. buff_ext_data_index = curBuffExtData;
  1458. }
  1459. else
  1460. {
  1461. string curString = Convert.ToString(curBuffExtData);
  1462. var str = curString.Substring(curString.Length - 1, 1);
  1463. if (!string.IsNullOrEmpty(str))
  1464. {
  1465. int.TryParse(str, out buff_ext_data_index);
  1466. }
  1467. }
  1468. }
  1469. public int BuffID { get { return Data.ID; } }
  1470. public bool IsDone { get { return (isEquip) ? false : (percent >= 1f); } }
  1471. public float CDPercent { get { return percent; } }
  1472. public byte OverlayLevel { get { return overlay_level; } }
  1473. public int BuffTotalTime { get { return total_time; } }
  1474. public int BuffLeftTime { get { return total_time - pass_time; } }
  1475. public int BuffExtendData { get { return buff_ext_data; } }
  1476. public int BuffExtendDataIndex { get { return buff_ext_data_index; } }
  1477. }
  1478. [Obsolete]
  1479. public List<BuffState> GetBuffStatus()
  1480. {
  1481. var list = new List<BuffState>();
  1482. GetBuffStatus(list);
  1483. return list;
  1484. }
  1485. public void GetBuffStatus(List<BuffState> ret)
  1486. {
  1487. mBuffStatus.ForEach((bs) => { ret.Add(bs); });
  1488. }
  1489. public int GetBuffStatusCount()
  1490. {
  1491. return mBuffStatus.GetBuffCount();
  1492. }
  1493. public BuffState GetBuff(int tempID, uint senderID)
  1494. {
  1495. return mBuffStatus.Get(tempID, senderID);
  1496. }
  1497. public BuffState GetBuff(int tempID)
  1498. {
  1499. return mBuffStatus.Get(tempID, 0);
  1500. }
  1501. #endregion
  1502. //--------------------------------------------------------------------------------
  1503. #region PickObject
  1504. private TimeExpire<UnitStartPickObjectEvent> mPickEvent;
  1505. public TimeExpire<UnitStartPickObjectEvent> PickEvent { get { return mPickEvent; } }
  1506. protected virtual void DoStartPick(UnitStartPickObjectEvent pick)
  1507. {
  1508. mPickEvent = new TimeExpire<UnitStartPickObjectEvent>(pick, pick.PickTimeMS);
  1509. SetCurrentState(UnitActionStatus.Pick, pick);
  1510. if (mOnStartPickObject != null)
  1511. {
  1512. mOnStartPickObject.Invoke(this, mPickEvent);
  1513. }
  1514. }
  1515. protected virtual void DoStopPick(UnitStopPickObjectEvent pick)
  1516. {
  1517. if (mPickEvent != null)
  1518. {
  1519. mPickEvent.End();
  1520. mPickEvent = null;
  1521. }
  1522. if (mOnStopPickObject != null)
  1523. {
  1524. mOnStopPickObject.Invoke(this, pick);
  1525. }
  1526. }
  1527. private void UpdatePickEvent(int intervalMS)
  1528. {
  1529. if (mPickEvent != null)
  1530. {
  1531. mPickEvent.Update(intervalMS);
  1532. }
  1533. }
  1534. #endregion
  1535. //--------------------------------------------------------------------------------
  1536. #region 本地技能预演
  1537. protected virtual void DoJumpToTargetEnd()
  1538. {
  1539. if (mOnClientSkillSimulation != null)
  1540. {
  1541. mOnClientSkillSimulation.Invoke(this, SimulationSkillEnum.JumpEnd);
  1542. }
  1543. }
  1544. #endregion 本地技能预演
  1545. #region MultiTimeLine
  1546. /// <summary>
  1547. /// 是否可被发现
  1548. /// </summary>
  1549. virtual public bool IsVisible { get { return !IsTimeLineEnable(2); } }
  1550. /// <summary>
  1551. /// 是否无敌
  1552. /// </summary>
  1553. virtual public bool IsInvincible { get { return IsTimeLineEnable(3); } }
  1554. /// <summary>
  1555. /// 此单位是否霸体
  1556. /// </summary>
  1557. virtual public bool IsNoneBlock { get { return IsTimeLineEnable(0); } }
  1558. /// <summary>
  1559. /// 是否为眩晕
  1560. /// </summary>
  1561. virtual public bool IsStun { get { return IsTimeLineEnable(1); } }
  1562. /// <summary>
  1563. /// 是否沉默
  1564. /// </summary>
  1565. virtual public bool IsSilent { get { return IsTimeLineEnable(4); } }
  1566. //--------------------------------------------------------------------------------
  1567. private UnitSyncMultiTimeLine mMultiTimeLine;
  1568. private void DoUnitSyncMultiTimeLine(UnitSyncMultiTimeLine e)
  1569. {
  1570. this.mMultiTimeLine = e;
  1571. }
  1572. /// <summary>
  1573. /// 指定TimeLine是否还有任务
  1574. /// </summary>
  1575. /// <param name="index"></param>
  1576. /// <returns></returns>
  1577. public bool IsTimeLineEnable(int index)
  1578. {
  1579. if (mMultiTimeLine != null && mMultiTimeLine.timelines != null && index < mMultiTimeLine.timelines.Count && index >= 0)
  1580. {
  1581. return mMultiTimeLine.timelines[index];
  1582. }
  1583. return false;
  1584. }
  1585. public void GetMultiTimeLineStatus(List<bool> list)
  1586. {
  1587. if (mMultiTimeLine != null && mMultiTimeLine.timelines != null)
  1588. {
  1589. list.AddRange(mMultiTimeLine.timelines);
  1590. }
  1591. }
  1592. #endregion
  1593. }
  1594. }