ZoneObject.Unit.Status.cs 63 KB

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