ZoneObject.Unit.Status.cs 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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. public uint ChargeTimeMS { private set; get; }
  454. internal SkillState(SkillTemplate data, ZoneUnit owner)
  455. {
  456. this.Data = data;
  457. this.Owner = owner;
  458. this.all_action_time_ms = Data.ActionQueueTimeMS;
  459. this.total_cd_time_ms = data.CoolDownMS;
  460. this.pass_time_ms = stop_time_ms = FullCDTimeMS;
  461. this.pass_time_ms = data.CoolDownMS;
  462. this.is_in_mutil_time = Data.IsSingleAction;
  463. this.use_Times = (int)data.CurUseTimes;
  464. this.is_use_tiems = this.use_Times >= 0 ? true : false;
  465. //以服务器为主,服务器那边目前没有做具体细分
  466. if (data.ActionPriority < 0)
  467. {
  468. skill_desc_type = SkillDescType.Displacement;
  469. }
  470. this.is_skill_never_launch = true;
  471. this.CurrentActionID = -1;
  472. ChargeProgress = 1f;
  473. }
  474. internal bool TryLaunch(UnitLaunchSkillAction act)
  475. {
  476. if (IsActive &&
  477. Owner.MP >= Data.CostMP &&
  478. Owner.HP >= Data.CostHP &&
  479. (!IsCD))
  480. {
  481. return true;
  482. }
  483. return false;
  484. }
  485. internal void Launch(UnitLaunchSkillEvent evt)
  486. {
  487. this.is_skill_never_launch = false;
  488. this.percent = 0;
  489. this.pass_time_ms = 0;
  490. this.is_skill_end = false;
  491. this.CurrentActionID = evt.action_index;
  492. this.action_type = Data.ActionQueue[evt.action_index].SigleActionType;
  493. this.is_Skill_Block = Data.ActionQueue[evt.action_index].IsAddSkillBtnBlock;
  494. //ClientLog.LogWarning(">launch SkillState >{0} @ {1}, isMulti:{2}", CurrentActionID, this.Data.TemplateID, this.is_in_mutil_time);
  495. if (CurrentActionID + 1 == Data.ActionQueue.Count)
  496. {
  497. this.is_period_cd_end = this.Data.IsSingleAction;
  498. }
  499. if (evt.IsActionSpeedUP)
  500. {
  501. this.action_speed = evt.action_speed;
  502. }
  503. else
  504. {
  505. this.action_speed = 1f;
  506. }
  507. if (evt.IsChangeTotalCDTime)
  508. {
  509. this.total_cd_time_ms = evt.TotalCDTimeMS;
  510. }
  511. else
  512. {
  513. this.total_cd_time_ms = ToFullTimeCD();
  514. this.is_skill_end = true;
  515. //ClientLog.LogError("is_skill_end: true");
  516. }
  517. OnCDStateChange?.Invoke();
  518. if (OnChargeStateChange != null)
  519. {
  520. if (Data.ActionQueue[CurrentActionID].ShowChargeTimeMS > 0)
  521. {
  522. ChargeProgress = 0f;
  523. ChargeTimeMS = 0;
  524. OnChargeStateChange.Invoke();
  525. }
  526. else if (ChargeProgress > 0 && ChargeProgress < 1)
  527. {
  528. ChargeProgress = 1f;
  529. OnChargeStateChange.Invoke();
  530. }
  531. }
  532. }
  533. public byte NextAction()
  534. {
  535. if (Data.IsSingleAction)
  536. {
  537. int action_step = CurrentActionID;
  538. int action_time = this.FullCDTimeMS;
  539. if (Data.IsCoolDownWithAction)
  540. {
  541. action_time = Data.ActionQueue[action_step % Data.ActionQueue.Count].TotalTimeMS;
  542. }
  543. // 是放技能时,处于多段攻击连击冷却时间范围
  544. if (pass_time_ms - stop_time_ms < Data.SingleActionCoolDownMS || pass_time_ms < (action_time + Data.SingleActionCoolDownMS))
  545. {
  546. action_step += 1;
  547. action_step = (byte)(action_step % Data.ActionQueue.Count);
  548. return (byte)action_step;
  549. }
  550. }
  551. return 0;
  552. }
  553. private int ToFullTimeCD()
  554. {
  555. if (Data.IsCoolDownWithAction)
  556. {
  557. if (Data.IsSingleAction)
  558. {
  559. return Data.ActionQueue[CurrentActionID].TotalTimeMS;
  560. }
  561. else
  562. {
  563. return all_action_time_ms;
  564. }
  565. }
  566. return Data.ToUnitSkillTotalTime(Owner.mFastCastRate);
  567. }
  568. internal void SetActive(SkillActiveState state)
  569. {
  570. this.current_state = state;
  571. }
  572. internal void ChangeAction(int step)
  573. {
  574. var aq = Data.ActionQueue[CurrentActionID];
  575. if (aq.ShowChargeTimeMS > 0 && ChargeProgress < 1)
  576. {
  577. ChargeProgress = 1f;
  578. OnChargeStateChange?.Invoke();
  579. }
  580. this.CurrentActionID = step;
  581. }
  582. internal void ClearCD()
  583. {
  584. this.pass_time_ms = FullCDTimeMS;
  585. }
  586. internal void DecreaseCD(int ms)
  587. {
  588. this.pass_time_ms += ms;
  589. }
  590. internal void DecreaseCD_Pct(float pct)
  591. {
  592. this.pass_time_ms += (int)(FullCDTimeMS * pct);
  593. }
  594. internal void PlayerStop(PlayerSkillStopEvent stop)
  595. {
  596. this.stop_time_ms = pass_time_ms;
  597. this.is_skill_end = true;
  598. this.is_Skill_Block = false;
  599. if (ChargeProgress > 0 && ChargeProgress < 1)
  600. {
  601. ChargeProgress = 1f;
  602. OnChargeStateChange?.Invoke();
  603. }
  604. }
  605. internal void Sync(ClientStruct.UnitSkillStatus syn)
  606. {
  607. this.pass_time_ms = syn.PassTime;
  608. this.use_Times = syn.useTimes;
  609. this.is_use_tiems = syn.useTimes >= 0 ? true : false;
  610. internal_update(0);
  611. }
  612. internal void TimeChange(PlayerSkillTimeChangedEvent e)
  613. {
  614. if (this.is_skill_never_launch)
  615. {
  616. //重新登录后,技能的状态会丢失,这里重新算一下
  617. if(e.SkillPassTimeMS < e.SkillTotalTimeMS)
  618. {
  619. //CD未结束的技能,action都是在最后一段(不然不会有CD)
  620. this.CurrentActionID = Data.ActionQueue.Count - 1;
  621. if (OnChargeStateChange != null)
  622. {
  623. ChargeProgress = 1f;
  624. OnChargeStateChange.Invoke();
  625. }
  626. }
  627. }
  628. this.pass_time_ms = e.SkillPassTimeMS;
  629. this.total_cd_time_ms = e.SkillTotalTimeMS;
  630. this.is_in_mutil_time = this.Data.IsSingleAction;
  631. this.is_period_cd_end = this.Data.IsSingleAction;
  632. this.is_Skill_Block = false;
  633. this.is_skill_end = true;
  634. internal_update(0);
  635. OnCDStateChange?.Invoke();
  636. if(OnChargeStateChange != null && ChargeProgress > 0 && ChargeProgress < 1)
  637. {
  638. ChargeProgress = 1f;
  639. OnChargeStateChange.Invoke();
  640. }
  641. }
  642. internal void Update(int intervalMS)
  643. {
  644. if (!IsActive && IsPauseOnDeactive) { return; }
  645. internal_update(intervalMS);
  646. if (this.isPeriodCDEnd)
  647. {
  648. SetMutilTime();
  649. }
  650. if (OnChargeStateChange != null && CurrentActionID >= 0 && CurrentActionID < Data.ActionQueue.Count)
  651. {
  652. var aq = Data.ActionQueue[CurrentActionID];
  653. if (aq.ShowChargeTimeMS > 0)
  654. {
  655. if (ChargeProgress < 1)
  656. {
  657. ChargeTimeMS += (uint)intervalMS;
  658. ChargeProgress = ChargeTimeMS / (float)aq.ShowChargeTimeMS;
  659. if (ChargeProgress > 1) ChargeProgress = 1f;
  660. OnChargeStateChange.Invoke();
  661. }
  662. }
  663. else if (ChargeProgress > 0 && ChargeProgress < 1)
  664. {
  665. ChargeProgress = 1f;
  666. OnChargeStateChange.Invoke();
  667. }
  668. }
  669. }
  670. internal void SetPassTime(int passTime)
  671. {
  672. this.pass_time_ms = passTime;
  673. internal_update(0);
  674. }
  675. void SetMutilTime()
  676. {
  677. if (CDPercent < 1)
  678. {
  679. this.is_in_mutil_time = false;
  680. }
  681. else
  682. {
  683. this.is_in_mutil_time = true;
  684. this.is_period_cd_end = false;
  685. }
  686. }
  687. private void internal_update(int intervalMS)
  688. {
  689. pass_time_ms += intervalMS;
  690. if (pass_time_ms >= FullCDTimeMS)
  691. {
  692. if (percent < 1)
  693. {
  694. percent = 1f;
  695. stop_time_ms = pass_time_ms;
  696. OnCDStateChange?.Invoke();
  697. }
  698. }
  699. else
  700. {
  701. percent = (float)pass_time_ms / FullCDTimeMS;
  702. }
  703. }
  704. //新增技能使用次数
  705. internal void SyncSkillUseTimes(int usetimes)
  706. {
  707. this.use_Times = usetimes;
  708. this.is_use_tiems = true;
  709. }
  710. public int FullCDTimeMS
  711. {
  712. get
  713. {
  714. return total_cd_time_ms;
  715. }
  716. }
  717. public bool isMutilTime
  718. {
  719. get { return this.is_in_mutil_time; }
  720. }
  721. public bool isPeriodCDEnd
  722. {
  723. get { return this.is_period_cd_end; }
  724. }
  725. public bool IsCD
  726. {
  727. get
  728. {
  729. if (Data.IsCoolDownWithAction)
  730. {
  731. return false;
  732. }
  733. return (percent < 1f);
  734. }
  735. }
  736. public float CDPercent
  737. {
  738. get
  739. {
  740. if (Data.IsCoolDownWithAction)
  741. {
  742. return 1f;
  743. }
  744. return percent;
  745. }
  746. }
  747. public float ActionSpeed { get { return action_speed; } }
  748. public int PassTimeMS { get { return pass_time_ms; } }
  749. public int StopTimeMS { get { return stop_time_ms; } }
  750. public int CurrentActionID { get; private set; }
  751. public SkillActiveState ActiveState { get { return current_state; } }
  752. public bool IsActive { get { return current_state == SkillActiveState.Active || current_state == SkillActiveState.ActiveAndHide; } }
  753. public bool IsPauseOnDeactive { get { return current_state == SkillActiveState.DeactiveAndPause; } }
  754. public ActionEnum ActionType { get { return action_type; } }
  755. public int UseTimes { get { return this.use_Times; } }
  756. public bool IsUseTimes { get { return this.is_use_tiems; } }
  757. //目前仅用于蓄力,后面如果有其它类型需要用到,注意TimeChange和StopSkill消息的顺序.......
  758. public bool IsSkillEnd {
  759. get { return this.is_skill_end; }
  760. }
  761. public bool IsSkillBlock { get { return this.is_Skill_Block; } }
  762. public SkillDescType SkillType { get { return this.skill_desc_type; } }
  763. }
  764. //[Obsolete]
  765. public List<SkillState> GetSkillStatus()
  766. {
  767. return new List<SkillState>(mSkillStatus.Skills);
  768. }
  769. public void GetSkillStatus(List<SkillState> ret)
  770. {
  771. ret.AddRange(mSkillStatus.Skills);
  772. }
  773. public SkillState GetSkillState(int templateID)
  774. {
  775. return mSkillStatus.Get(templateID);
  776. }
  777. #endregion
  778. //---------------------------------------------------------------------------
  779. // skill action
  780. //---------------------------------------------------------------------------
  781. #region SkillAction
  782. /// <summary>
  783. /// 是否正在吟唱
  784. /// </summary>
  785. public bool IsChanttingSkill
  786. {
  787. get { return mChantingSkill != null; }
  788. }
  789. public int ChantingSkillPassMS
  790. {
  791. get { return (mChantingSkill != null) ? mChantingSkill.PassTimeMS : 0; }
  792. }
  793. public int ChantingSkillTotalMS
  794. {
  795. get { return (mChantingSkill != null) ? mChantingSkill.TotalTimeMS : 0; }
  796. }
  797. public SkillTemplate ChantingSkillData
  798. {
  799. get { return (mChantingSkill != null) ? mChantingSkill.Tag : null; }
  800. }
  801. public SkillState ChantingSkill
  802. {
  803. get { if (mChantingSkill != null) { return GetSkillState(mChantingSkill.Tag.TemplateID); } return null; }
  804. }
  805. private TimeExpire<SkillTemplate> mChantingSkill;
  806. protected virtual void DoUnitChantSkillEvent(UnitChantSkillEvent e)
  807. {
  808. var temp = Templates.getSkill(e.skill_id);
  809. mChantingSkill = new TimeExpire<SkillTemplate>(temp, e.chant_ms);
  810. SkillState ss = GetSkillState(e.skill_id);
  811. if (ss != null)
  812. {
  813. if (mOnChantSkill != null)
  814. {
  815. mOnChantSkill.Invoke(this, e, mChantingSkill);
  816. }
  817. }
  818. }
  819. protected virtual void doLaunchSkillAction(UnitLaunchSkillEvent me)
  820. {
  821. clearSkillAction();
  822. var temp = Templates.getSkill(me.skill_id);
  823. if (temp != null)
  824. {
  825. mCurrentSkillAction = new UnitSkillAction(this, temp);
  826. mCurrentSkillAction.onLaunch(me);
  827. invokeSkillActionStart(mCurrentSkillAction);
  828. }
  829. }
  830. protected virtual void invokeSkillActionStart(ISkillAction act)
  831. {
  832. if (mOnSkillActionStart != null)
  833. {
  834. mOnSkillActionStart.Invoke(this, act);
  835. }
  836. }
  837. public virtual ISkillAction CurrentSkillAction
  838. {
  839. get { return mCurrentSkillAction; }
  840. }
  841. public virtual UnitActionData CurrentSkillActionData
  842. {
  843. get
  844. {
  845. if (CurrentSkillAction != null)
  846. {
  847. return CurrentSkillAction.CurrentAction;
  848. }
  849. return null;
  850. }
  851. }
  852. //-------------------------------------------------------------------------------------------
  853. // unit impl
  854. private UnitSkillAction mCurrentSkillAction;
  855. private void updateSkillAction(int intervalMS)
  856. {
  857. if (mCurrentSkillAction != null)
  858. {
  859. //UnityEngine.Debug.LogError("2 mCurrentSkillAction = " + mCurrentSkillAction.SkillData.TemplateID + " actionIndex = " + mCurrentSkillAction.CurrentActionIndex);
  860. mCurrentSkillAction.onUpdate(intervalMS);
  861. if (mCurrentSkillAction.IsDone)
  862. {
  863. clearSkillAction();
  864. }
  865. }
  866. }
  867. private void clearSkillAction()
  868. {
  869. if (mCurrentSkillAction != null)
  870. {
  871. mCurrentSkillAction.onStop();
  872. mCurrentSkillAction = null;
  873. }
  874. }
  875. public abstract class ISkillAction
  876. {
  877. private BitSet8 current_action_status = new BitSet8();
  878. public bool IsControlMoveable { get { return current_action_status.Get(0); } protected set { current_action_status.Set(0, value); } }
  879. public bool IsControlFaceable { get { return current_action_status.Get(1); } protected set { current_action_status.Set(1, value); } }
  880. public bool IsCancelableBySkill { get { return current_action_status.Get(2); } protected set { current_action_status.Set(2, value); } }
  881. public bool IsCancelableByMove { get { return current_action_status.Get(3); } protected set { current_action_status.Set(3, value); } }
  882. public bool IsNoneBlock { get { return current_action_status.Get(4); } protected set { current_action_status.Set(4, value); } }
  883. public bool IsNoneTouch { get { return current_action_status.Get(5); } protected set { current_action_status.Set(5, value); } }
  884. public bool IsFaceToTarget { get { return current_action_status.Get(6); } protected set { current_action_status.Set(6, value); } }
  885. public abstract SkillTemplate SkillData { get; }
  886. public abstract UnitLaunchSkillEvent LaunchEvent { get; }
  887. public abstract bool IsDone { get; }
  888. public abstract float ActionSpeed { get; }
  889. public abstract byte ActionStepIndex { get; }
  890. public abstract int[] ActionTimeArray { get; }
  891. public abstract int TotalTimeMS { get; }
  892. public abstract int CurrentActionIndex { get; }
  893. public abstract string CurrentActionName { get; }
  894. public abstract UnitActionData CurrentAction { get; }
  895. public abstract float ExpirePercent { get; }
  896. //把当前的执行时间抛出去给客户端用,很违和的做法,没办法数据配在扩展属性里面
  897. //CommonAI取不出来,强行操作会引发不可控的Bug
  898. public abstract int CurPassTimeMS { get; }
  899. public abstract bool IsJumpAction { get; }
  900. internal abstract void onLaunch(UnitLaunchSkillEvent e);
  901. internal abstract void onUnitSkillActionChangeEvent(UnitSkillActionChangeEvent evt);
  902. internal abstract void onUpdate(int intervalMS);
  903. internal abstract void onStop();
  904. internal abstract void RestJumpAction(bool isJumpAction);
  905. }
  906. public class UnitSkillAction : ISkillAction
  907. {
  908. readonly private ZoneUnit ownerUnit;
  909. readonly private SkillTemplate skill;
  910. private UnitLaunchSkillEvent launch_event;
  911. private int current_pass_time = 0;
  912. private int total_pass_time = 0;
  913. private int total_time_ms = 0;
  914. private bool is_done = false;
  915. private bool is_jump_action = false;
  916. private Queue<UnitActionData> action_queue;
  917. private int current_action_index = 0;
  918. private UnitActionData current_action = null;
  919. private PopupKeyFrames<UnitActionData.KeyFrame> current_frames = new PopupKeyFrames<UnitActionData.KeyFrame>();
  920. private int current_action_total_time = 0;
  921. private int[] action_time_array;
  922. public ZoneLayer Parent { get { return ownerUnit.Parent; } }
  923. public override int[] ActionTimeArray { get { return action_time_array; } }
  924. public override int TotalTimeMS { get { return total_time_ms; } }
  925. public override byte ActionStepIndex { get { return (launch_event != null) ? launch_event.action_index : (byte)0; } }
  926. public override int CurrentActionIndex { get { return current_action_index; } }
  927. public override string CurrentActionName { get { return (current_action != null) ? current_action.ActionName : null; } }
  928. public override UnitActionData CurrentAction { get { return current_action; } }
  929. public override float ExpirePercent { get { return total_pass_time / (float)TotalTimeMS; } }
  930. public override SkillTemplate SkillData { get { return skill; } }
  931. public override UnitLaunchSkillEvent LaunchEvent { get { return launch_event; } }
  932. public override bool IsDone { get { return is_done; } }
  933. public override int CurPassTimeMS { get { return current_pass_time; } }
  934. protected internal UnitSkillAction(ZoneUnit actor, SkillTemplate skill)
  935. {
  936. this.skill = skill;
  937. this.ownerUnit = actor;
  938. this.action_time_array = skill.ActionQueueTimeArray;
  939. }
  940. public override float ActionSpeed
  941. {
  942. get
  943. {
  944. if (launch_event != null)
  945. {
  946. if(launch_event.action_speed_add != null && launch_event.action_index < launch_event.action_speed_add.Length)
  947. {
  948. return launch_event.action_speed + launch_event.action_speed_add[launch_event.action_index];
  949. }
  950. return launch_event.action_speed;
  951. }
  952. return 1f;
  953. }
  954. }
  955. public override bool IsJumpAction
  956. {
  957. get
  958. {
  959. return is_jump_action;
  960. }
  961. }
  962. internal override void onLaunch(UnitLaunchSkillEvent e)
  963. {
  964. //UnityEngine.Debug.LogError("SkillID = " + e.skill_id + " ActionSpeed = " + e.action_speed + " action_index = " + e.action_index);
  965. this.launch_event = e;
  966. this.total_pass_time = 0;
  967. this.current_pass_time = 0;
  968. if (e.action_time_array != null && e.action_time_array.Length > 0)
  969. {
  970. this.total_time_ms = e.TotalActionTimeMS;
  971. this.action_time_array = e.action_time_array;
  972. }
  973. else if (skill.IsSingleAction)
  974. {
  975. this.total_time_ms = skill.ActionQueue[launch_event.action_index].TotalTimeMS;
  976. this.action_time_array = new int[] { TotalTimeMS };
  977. }
  978. else
  979. {
  980. this.total_time_ms = skill.ActionQueueTimeMS;
  981. this.action_time_array = skill.ActionQueueTimeArray;
  982. }
  983. if (skill.IsSingleAction)
  984. {
  985. var sa = skill.ActionQueue[launch_event.action_index];
  986. this.action_queue = new Queue<UnitActionData>(1);
  987. this.action_queue.Enqueue(sa);
  988. this.current_action_index = launch_event.action_index;
  989. }
  990. else
  991. {
  992. this.action_queue = new Queue<UnitActionData>(skill.ActionQueue);
  993. this.current_action_index = launch_event.action_index;
  994. }
  995. this.current_action = null;
  996. this.nextAction(this.current_action_index);
  997. }
  998. internal override void onUnitSkillActionChangeEvent(UnitSkillActionChangeEvent e)
  999. {
  1000. while (this.CurrentActionIndex < e.ActionIndex)
  1001. {
  1002. if (!nextAction(current_action_index + 1))
  1003. {
  1004. this.total_pass_time = TotalTimeMS;
  1005. this.is_done = true;
  1006. break;
  1007. }
  1008. }
  1009. }
  1010. internal override void onStop()
  1011. {
  1012. this.is_done = true;
  1013. this.total_pass_time = TotalTimeMS;
  1014. }
  1015. internal override void onUpdate(int intervalMS)
  1016. {
  1017. int time_pass = (int)(intervalMS * ActionSpeed);
  1018. this.total_pass_time += time_pass;
  1019. this.total_pass_time = Math.Min(total_pass_time, TotalTimeMS);
  1020. this.current_pass_time += time_pass;
  1021. if (current_action == null)
  1022. {
  1023. nextAction(current_action_index + 1);
  1024. }
  1025. if (current_action == null)
  1026. {
  1027. this.is_done = true;
  1028. this.total_pass_time = TotalTimeMS;
  1029. }
  1030. else
  1031. {
  1032. // 关键帧 //
  1033. using (var kfs = ListObjectPool<UnitActionData.KeyFrame>.AllocAutoRelease())
  1034. {
  1035. if (current_frames.PopKeyFrames(current_pass_time, kfs) > 0)
  1036. {
  1037. for (int i = 0; i < kfs.Count; i++)
  1038. {
  1039. doKeyFrame(kfs[i]);
  1040. }
  1041. }
  1042. }
  1043. // 下段动作 //
  1044. if ((current_action != null) && (current_pass_time >= current_action_total_time))
  1045. {
  1046. current_action = null;
  1047. if (action_queue.Count == 0)
  1048. {
  1049. this.is_done = true;
  1050. this.total_pass_time = TotalTimeMS;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. private bool nextAction(int index)
  1056. {
  1057. if (action_queue != null && action_queue.Count > 0)
  1058. {
  1059. this.current_action_index = index;
  1060. this.current_action = action_queue.Dequeue();
  1061. if (current_action != null)
  1062. {
  1063. this.current_frames.AddRange(current_action.KeyFrames);
  1064. this.current_pass_time = 0;
  1065. //NewAction//
  1066. if (launch_event.action_time_array != null)
  1067. {
  1068. if (skill.IsSingleAction)
  1069. {
  1070. this.current_action_total_time = launch_event.TotalActionTimeMS;
  1071. }
  1072. else if (current_action_index < launch_event.action_time_array.Length)
  1073. {
  1074. this.current_action_total_time = launch_event.action_time_array[current_action_index];
  1075. }
  1076. }
  1077. else
  1078. {
  1079. this.current_action_total_time = current_action.TotalTimeMS;
  1080. }
  1081. this.IsCancelableBySkill = current_action.IsCancelableBySkill;
  1082. this.IsNoneBlock = current_action.IsNoneBlock;
  1083. this.IsNoneTouch = current_action.IsNoneTouch;
  1084. this.IsFaceToTarget = current_action.IsFaceToTarget;
  1085. this.IsCancelableByMove = current_action.IsCancelable;
  1086. this.IsControlMoveable = current_action.IsControlMoveable;
  1087. this.IsControlFaceable = current_action.IsControlFaceable;
  1088. return true;
  1089. }
  1090. }
  1091. current_action = null;
  1092. return false;
  1093. }
  1094. private void doKeyFrame(UnitActionData.KeyFrame kf)
  1095. {
  1096. // 关键帧改变状态
  1097. if (kf.ChangeStatus != null)
  1098. {
  1099. this.IsNoneBlock = kf.ChangeStatus.IsNoneBlock;
  1100. this.IsNoneTouch = kf.ChangeStatus.IsNoneTouch;
  1101. this.IsFaceToTarget = kf.ChangeStatus.IsFaceToTarget;
  1102. this.IsCancelableByMove = kf.ChangeStatus.IsCancelable;
  1103. this.IsCancelableBySkill = kf.ChangeStatus.IsCancelableBySkill;
  1104. this.IsControlMoveable = kf.ChangeStatus.IsControlMoveable;
  1105. this.IsControlFaceable = kf.ChangeStatus.IsControlFaceable;
  1106. }
  1107. }
  1108. internal override void RestJumpAction(bool isJumpAction)
  1109. {
  1110. is_jump_action = isJumpAction;
  1111. }
  1112. }
  1113. //-------------------------------------------------------------------------------------------
  1114. #endregion
  1115. //--------------------------------------------------------------------------------
  1116. #region Buff
  1117. internal BuffMap mBuffStatus = new BuffMap();
  1118. public BuffMap AllBuffs { get { return mBuffStatus; } }
  1119. internal void SyncBuffStatus(ClientStruct.UnitBuffStatus[] buffs)
  1120. {
  1121. if (buffs != null)
  1122. {
  1123. for (int i = 0; i < buffs.Length; i++)
  1124. {
  1125. ClientStruct.UnitBuffStatus st = buffs[i];
  1126. TryAddBuff(st.BuffTemplateID, st.SenderID, st.IsEquip, st.TotalTime, st.OverlayLevel, st.PassTime);
  1127. }
  1128. }
  1129. }
  1130. protected void SyncBuffOverlayLevel(BuffDataNotify data)
  1131. {
  1132. BuffState bs = mBuffStatus.Get(data.buffId, data.senderId);
  1133. if (bs != null)
  1134. {
  1135. bs.SyncBuffExtData(data.overlayLevel, bs.BuffExtendData);
  1136. //YXJDebug.logError(data.buffId + " , level:" + bs.OverlayLevel + " time = " + bs.BuffTotalTime + ", per:"+bs.CDPercent);
  1137. if (mOnBuffChanged != null)
  1138. {
  1139. mOnBuffChanged.Invoke(this, bs);
  1140. }
  1141. }
  1142. }
  1143. protected virtual void DoLaunchBuff(UnitLaunchBuffEvent me)
  1144. {
  1145. //YXJDebug.logError(me.buffTemplateID + " overlayLevel = " + me.overlayLevel + " time = " + me.buffTimeMS);
  1146. TryAddBuff(me.buffTemplateID, me.senderID, me.IsEquip, me.buffTimeMS, me.overlayLevel, 0, me.buffExtData);
  1147. }
  1148. protected virtual void DoStopBuff(UnitStopBuffEvent e)
  1149. {
  1150. TryRemoveBuff(e.buffTemplateID, e.senderID);
  1151. }
  1152. //新增Buff接口
  1153. protected virtual void DoRemoveOverlayLevelBuff(SyncUnitBuffState e)
  1154. {
  1155. //UnityEngine.Debug.LogError("DoRemoveOverlayLevelBuff overlayLevel = " + e.overlayLevel + " buffExtData = " + e.buffExtData);
  1156. if (e.buffExtData > 0)
  1157. {
  1158. var bs = mBuffStatus.Get(e.buffTemplateID, e.ObjectID);
  1159. if (bs != null && mOnRemoveBuffByOverlayLeveled != null)
  1160. {
  1161. mOnRemoveBuffByOverlayLeveled(this, bs);
  1162. bs.SyncBuffExtData(e.overlayLevel, e.buffExtData);
  1163. }
  1164. }
  1165. else
  1166. {
  1167. TryRemoveBuff(e.buffTemplateID, e.ObjectID);
  1168. }
  1169. }
  1170. private void TryAddBuff(int buffTempalteID, uint senderID, bool isEquip, int totalTime, byte overlayLevel, int passTime = 0, int buffExtData = 0)
  1171. {
  1172. BuffTemplate buff = Templates.getBuff(buffTempalteID);
  1173. if (buff != null)
  1174. {
  1175. BuffState bs = mBuffStatus.Get(buffTempalteID, senderID);
  1176. if (bs != null)
  1177. {
  1178. bs.Sync(overlayLevel, totalTime, passTime, buffExtData);
  1179. if (mOnBuffChanged != null)
  1180. {
  1181. mOnBuffChanged.Invoke(this, bs);
  1182. }
  1183. }
  1184. else
  1185. {
  1186. bs = new BuffState(buff, this, senderID, isEquip);
  1187. bs.Sync(overlayLevel, totalTime, passTime, buffExtData);
  1188. mBuffStatus.Put(bs);
  1189. if (mOnBuffAdded != null)
  1190. {
  1191. mOnBuffAdded.Invoke(this, bs);
  1192. }
  1193. }
  1194. //UnityEngine.Debug.LogError("Server buffID = " + buffTempalteID + " LockStateAction = " + buff.LockStateAction);
  1195. //新增单独状态
  1196. if (!string.IsNullOrEmpty(buff.LockStateAction))
  1197. {
  1198. this.SetCurrentState(UnitActionStatus.BuffLockStateAction, CurrentSubState, buff);
  1199. }
  1200. }
  1201. }
  1202. private void TryRemoveBuff(int buffTempalteID, uint senderID)
  1203. {
  1204. BuffState bs = mBuffStatus.RemoveByKey(buffTempalteID, senderID);
  1205. if (bs != null)
  1206. {
  1207. bs.OnEnd();
  1208. if (mOnBuffRemoved != null)
  1209. {
  1210. mOnBuffRemoved.Invoke(this, bs);
  1211. }
  1212. //UnityEngine.Debug.LogError("RemoveBuff CurrentState= " + CurrentState);
  1213. //置回单独状态
  1214. if (CurrentState == UnitActionStatus.BuffLockStateAction)
  1215. {
  1216. this.SetCurrentState(UnitActionStatus.Idle);
  1217. }
  1218. }
  1219. }
  1220. private void UpdateBuffs(int intervalMS)
  1221. {
  1222. mBuffStatus.ForEachUpdate(intervalMS);
  1223. //mBuffStatus.ForEach((bs) =>
  1224. //{
  1225. // bs.OnUpdate(intervalMS);
  1226. //});
  1227. }
  1228. //返回当前buff列表中,是否含有变身buff
  1229. public bool HasMakeAvatarBuff()
  1230. {
  1231. bool has = false;
  1232. mBuffStatus.ForEach((BuffState buf) => {
  1233. if (buf.Data.MakeAvatar)
  1234. {
  1235. has = true;
  1236. }
  1237. });
  1238. return has;
  1239. }
  1240. //返回当前buff列表中,是否含有切换技能buff
  1241. public bool HasChangeSkillBuff()
  1242. {
  1243. bool has = false;
  1244. mBuffStatus.ForEach((BuffState buf) => {
  1245. if (buf.Data.UnitChangeSkills)
  1246. {
  1247. has = true;
  1248. }
  1249. });
  1250. return has;
  1251. }
  1252. public class BuffMap
  1253. {
  1254. private HashMap<int, BuffList> Map = new HashMap<int, BuffList>();
  1255. private List<BuffList> For = new List<BuffList>();
  1256. public BuffState Get(int id, uint senderID)
  1257. {
  1258. var list = Map.Get(id);
  1259. if (list != null)
  1260. {
  1261. return list.Get(senderID);
  1262. }
  1263. return null;
  1264. }
  1265. internal void Put(BuffState state)
  1266. {
  1267. var list = Map.Get(state.BuffID);
  1268. if (list == null)
  1269. {
  1270. list = new BuffList(state.Data);
  1271. Map.Add(state.BuffID, list);
  1272. For.Add(list);
  1273. }
  1274. list.Add(state);
  1275. }
  1276. internal BuffState RemoveByKey(int id, uint senderID)
  1277. {
  1278. var list = Map.Get(id);
  1279. if (list != null)
  1280. {
  1281. var st = list.Get(senderID);
  1282. if (st != null)
  1283. {
  1284. var ret = list.Remove(senderID);
  1285. if (list.Count == 0)
  1286. {
  1287. For.Remove(list);
  1288. Map.Remove(id);
  1289. }
  1290. return ret;
  1291. }
  1292. }
  1293. return null;
  1294. }
  1295. internal void Clear()
  1296. {
  1297. For.Clear();
  1298. Map.Clear();
  1299. }
  1300. public void ForEach(Action<BuffState> action)
  1301. {
  1302. for (int i = For.Count - 1; i >= 0; --i)
  1303. {
  1304. For[i].ForEach(action);
  1305. }
  1306. }
  1307. public int GetBuffCount()
  1308. {
  1309. return For.Count;
  1310. }
  1311. public void ForEachUpdate(int intervalMS)
  1312. {
  1313. for (int i = For.Count - 1; i >= 0; --i)
  1314. {
  1315. For[i].ForEachUpdate(intervalMS);
  1316. }
  1317. }
  1318. public class BuffList
  1319. {
  1320. public readonly BuffTemplate Data;
  1321. private HashMap<uint, BuffState> Map = new HashMap<uint, BuffState>();
  1322. private List<BuffState> For = new List<BuffState>();
  1323. public int Count { get { return Map.Count; } }
  1324. internal BuffList(BuffTemplate data)
  1325. {
  1326. this.Data = data;
  1327. }
  1328. internal BuffState Remove(uint senderID)
  1329. {
  1330. BuffState ret;
  1331. if (Data.IsDuplicating)
  1332. {
  1333. ret = Map.RemoveByKey(senderID);
  1334. }
  1335. else
  1336. {
  1337. ret = Map.RemoveByKey(0);
  1338. }
  1339. if (ret != null)
  1340. {
  1341. For.Remove(ret);
  1342. }
  1343. return ret;
  1344. }
  1345. internal void Add(BuffState state)
  1346. {
  1347. if (Data.IsDuplicating)
  1348. {
  1349. Map.Add(state.SenderID, state);
  1350. }
  1351. else
  1352. {
  1353. Map.Add(0, state);
  1354. }
  1355. For.Add(state);
  1356. }
  1357. internal BuffState Get(uint senderID)
  1358. {
  1359. if (Data.IsDuplicating)
  1360. {
  1361. if (senderID == 0 && Map.Count > 0)
  1362. {
  1363. return Map.First().Value;
  1364. }
  1365. else
  1366. {
  1367. return Map.Get(senderID);
  1368. }
  1369. }
  1370. else
  1371. {
  1372. return Map.Get(0);
  1373. }
  1374. }
  1375. internal void ForEach(Action<BuffState> action)
  1376. {
  1377. for (int i = For.Count - 1; i >= 0; --i)
  1378. {
  1379. action(For[i]);
  1380. }
  1381. }
  1382. public void ForEachUpdate(int intervalMS)
  1383. {
  1384. for (int i = For.Count - 1; i >= 0; --i)
  1385. {
  1386. For[i].OnUpdate(intervalMS);
  1387. }
  1388. }
  1389. }
  1390. }
  1391. public class BuffState
  1392. {
  1393. readonly public BuffTemplate Data;
  1394. readonly public bool isEquip;
  1395. readonly public ZoneUnit Owner;
  1396. readonly public uint SenderID;
  1397. private byte overlay_level;
  1398. private int total_time;
  1399. private int pass_time = 0;
  1400. private float percent = 1f;
  1401. private int buff_ext_data = 0;
  1402. private int buff_ext_data_index = -1;
  1403. private TimeInterval<BuffTemplate.KeyFrame> interval;
  1404. private PopupKeyFrames<BuffTemplate.KeyFrame> keyframes;
  1405. internal BuffState(BuffTemplate data, ZoneUnit owner, uint senderID, bool isEquip)
  1406. {
  1407. this.Data = data;
  1408. this.isEquip = isEquip;
  1409. this.Owner = owner;
  1410. this.SenderID = senderID;
  1411. }
  1412. internal void Sync(byte overlayLevel, int totalTime, int passTime, int buffExtData = 0)
  1413. {
  1414. this.overlay_level = overlayLevel;
  1415. this.total_time = totalTime;
  1416. if (passTime < this.pass_time || this.pass_time == 0)
  1417. {
  1418. this.interval = new TimeInterval<BuffTemplate.KeyFrame>(Data.HitIntervalMS);
  1419. this.interval.Tag = Data.HitKeyFrame;
  1420. this.keyframes = new PopupKeyFrames<BuffTemplate.KeyFrame>();
  1421. this.keyframes.AddRange(Data.KeyFrames);
  1422. this.pass_time = 0;
  1423. }
  1424. if (passTime > this.pass_time)
  1425. {
  1426. this.interval.SetPassTime(passTime);
  1427. this.keyframes.PopKeyFrames(passTime, null);
  1428. this.pass_time = passTime;
  1429. }
  1430. this.percent = Math.Min(1, pass_time / (float)total_time);
  1431. this.buff_ext_data = buffExtData;
  1432. SetBuffExtendDataIndex(buffExtData);
  1433. }
  1434. internal void OnEnd()
  1435. {
  1436. if (Data.EndKeyFrame != null && Data.EndKeyFrame.Effect != null)
  1437. {
  1438. Owner.Parent.PreQueueEvent(new UnitEffectEvent(Owner.ObjectID, Data.EndKeyFrame.Effect));
  1439. }
  1440. }
  1441. internal void OnUpdate(int intervalMS)
  1442. {
  1443. using (var kfs = ListObjectPool<BuffTemplate.KeyFrame>.AllocAutoRelease())
  1444. {
  1445. if (keyframes.PopKeyFrames(pass_time, kfs) > 0)
  1446. {
  1447. for (int i = 0; i < kfs.Count; i++)
  1448. {
  1449. BuffTemplate.KeyFrame kf = kfs[i];
  1450. if (kf.Effect != null)
  1451. {
  1452. Owner.Parent.PreQueueEvent(new UnitEffectEvent(Owner.ObjectID, kf.Effect));
  1453. }
  1454. }
  1455. }
  1456. }
  1457. if (interval.Update(intervalMS))
  1458. {
  1459. if (Data.HitKeyFrame != null && Data.HitKeyFrame.Effect != null)
  1460. {
  1461. Owner.Parent.PreQueueEvent(new UnitEffectEvent(Owner.ObjectID, Data.HitKeyFrame.Effect));
  1462. }
  1463. }
  1464. this.pass_time += intervalMS;
  1465. if (percent < 1f)
  1466. {
  1467. this.percent = Math.Min(1, pass_time / (float)total_time);
  1468. }
  1469. }
  1470. public void SyncBuffExtData(byte overlayLevel, int buffExtData)
  1471. {
  1472. overlay_level = overlayLevel;
  1473. buff_ext_data = buffExtData;
  1474. }
  1475. private void SetBuffExtendDataIndex(int curBuffExtData)
  1476. {
  1477. if (curBuffExtData < 1) return;
  1478. if (curBuffExtData < 10)
  1479. {
  1480. buff_ext_data_index = curBuffExtData;
  1481. }
  1482. else
  1483. {
  1484. string curString = Convert.ToString(curBuffExtData);
  1485. var str = curString.Substring(curString.Length - 1, 1);
  1486. if (!string.IsNullOrEmpty(str))
  1487. {
  1488. int.TryParse(str, out buff_ext_data_index);
  1489. }
  1490. }
  1491. }
  1492. public int BuffID { get { return Data.ID; } }
  1493. public bool IsDone { get { return (isEquip) ? false : (percent >= 1f); } }
  1494. public float CDPercent { get { return percent; } }
  1495. public byte OverlayLevel { get { return overlay_level; } }
  1496. public int BuffTotalTime { get { return total_time; } }
  1497. public int BuffLeftTime { get { return total_time - pass_time; } }
  1498. public int BuffExtendData { get { return buff_ext_data; } }
  1499. public int BuffExtendDataIndex { get { return buff_ext_data_index; } }
  1500. }
  1501. [Obsolete]
  1502. public List<BuffState> GetBuffStatus()
  1503. {
  1504. var list = new List<BuffState>();
  1505. GetBuffStatus(list);
  1506. return list;
  1507. }
  1508. public void GetBuffStatus(List<BuffState> ret)
  1509. {
  1510. mBuffStatus.ForEach((bs) => { ret.Add(bs); });
  1511. }
  1512. public int GetBuffStatusCount()
  1513. {
  1514. return mBuffStatus.GetBuffCount();
  1515. }
  1516. public BuffState GetBuff(int tempID, uint senderID)
  1517. {
  1518. return mBuffStatus.Get(tempID, senderID);
  1519. }
  1520. public BuffState GetBuff(int tempID)
  1521. {
  1522. return mBuffStatus.Get(tempID, 0);
  1523. }
  1524. #endregion
  1525. //--------------------------------------------------------------------------------
  1526. #region PickObject
  1527. private TimeExpire<UnitStartPickObjectEvent> mPickEvent;
  1528. public TimeExpire<UnitStartPickObjectEvent> PickEvent { get { return mPickEvent; } }
  1529. protected virtual void DoStartPick(UnitStartPickObjectEvent pick)
  1530. {
  1531. mPickEvent = new TimeExpire<UnitStartPickObjectEvent>(pick, pick.PickTimeMS);
  1532. SetCurrentState(UnitActionStatus.Pick, pick);
  1533. if (mOnStartPickObject != null)
  1534. {
  1535. mOnStartPickObject.Invoke(this, mPickEvent);
  1536. }
  1537. }
  1538. protected virtual void DoStopPick(UnitStopPickObjectEvent pick)
  1539. {
  1540. if (mPickEvent != null)
  1541. {
  1542. mPickEvent.End();
  1543. mPickEvent = null;
  1544. }
  1545. if (mOnStopPickObject != null)
  1546. {
  1547. mOnStopPickObject.Invoke(this, pick);
  1548. }
  1549. }
  1550. private void UpdatePickEvent(int intervalMS)
  1551. {
  1552. if (mPickEvent != null)
  1553. {
  1554. mPickEvent.Update(intervalMS);
  1555. }
  1556. }
  1557. #endregion
  1558. //--------------------------------------------------------------------------------
  1559. #region 本地技能预演
  1560. protected virtual void DoJumpToTargetEnd()
  1561. {
  1562. if (mOnClientSkillSimulation != null)
  1563. {
  1564. mOnClientSkillSimulation.Invoke(this, SimulationSkillEnum.JumpEnd);
  1565. }
  1566. }
  1567. #endregion 本地技能预演
  1568. #region MultiTimeLine
  1569. /// <summary>
  1570. /// 是否可被发现
  1571. /// </summary>
  1572. virtual public bool IsVisible { get { return !IsTimeLineEnable(2); } }
  1573. /// <summary>
  1574. /// 是否无敌
  1575. /// </summary>
  1576. virtual public bool IsInvincible { get { return IsTimeLineEnable(3); } }
  1577. /// <summary>
  1578. /// 此单位是否霸体
  1579. /// </summary>
  1580. virtual public bool IsNoneBlock { get { return IsTimeLineEnable(0); } }
  1581. /// <summary>
  1582. /// 是否为眩晕
  1583. /// </summary>
  1584. virtual public bool IsStun { get { return IsTimeLineEnable(1); } }
  1585. /// <summary>
  1586. /// 是否沉默
  1587. /// </summary>
  1588. virtual public bool IsSilent { get { return IsTimeLineEnable(4); } }
  1589. //--------------------------------------------------------------------------------
  1590. private UnitSyncMultiTimeLine mMultiTimeLine;
  1591. private void DoUnitSyncMultiTimeLine(UnitSyncMultiTimeLine e)
  1592. {
  1593. this.mMultiTimeLine = e;
  1594. }
  1595. /// <summary>
  1596. /// 指定TimeLine是否还有任务
  1597. /// </summary>
  1598. /// <param name="index"></param>
  1599. /// <returns></returns>
  1600. public bool IsTimeLineEnable(int index)
  1601. {
  1602. if (mMultiTimeLine != null && mMultiTimeLine.timelines != null && index < mMultiTimeLine.timelines.Count && index >= 0)
  1603. {
  1604. return mMultiTimeLine.timelines[index];
  1605. }
  1606. return false;
  1607. }
  1608. public void GetMultiTimeLineStatus(List<bool> list)
  1609. {
  1610. if (mMultiTimeLine != null && mMultiTimeLine.timelines != null)
  1611. {
  1612. list.AddRange(mMultiTimeLine.timelines);
  1613. }
  1614. }
  1615. #endregion
  1616. }
  1617. }