ZoneObject.Unit.Status.cs 63 KB

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