ZoneObject.Unit.Status.cs 63 KB

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