InstanceUnit.Status.cs 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonLang;
  5. using CommonAI.Zone.Helper;
  6. using CommonAI.Zone.Formula;
  7. using CommonAI.Zone.UnitTriggers;
  8. using CommonAI.Zone.Attributes;
  9. using CommonLang.Property;
  10. using CommonAI.RTS;
  11. using CommonLang.Vector;
  12. using CommonAI.ZoneClient;
  13. using CommonAI.data;
  14. using static CommonAI.Zone.LaunchSpell;
  15. using CommonAI.Data;
  16. namespace CommonAI.Zone.Instance
  17. {
  18. public delegate int ReadBuffDelegate(InstanceUnit.BuffState buffState);
  19. /// <summary>
  20. /// 所有常态状态(Buff,技能,被动系)
  21. /// </summary>
  22. partial class InstanceUnit
  23. {
  24. //--------------------------------------------------------------------------------------------------------------
  25. #region _时效性效果_
  26. //--------------------------------------------------------------------------------------------------------------
  27. /// <summary>
  28. /// 是否可被发现
  29. /// </summary>
  30. virtual public bool IsVisible { get { return !mInvisibleTimeMS.Enable;/* && !DeadHide; */} }
  31. /// <summary>
  32. /// 是否无敌
  33. /// </summary>
  34. virtual public bool IsInvincible { get { return mInvincibleTimeMS.Enable; } }
  35. /// <summary>
  36. /// 此单位是否霸体
  37. /// </summary>
  38. virtual public bool IsNoneBlock { get { return mNoneBlockTimeMS.Enable; } }
  39. /// <summary>
  40. /// 是否为眩晕
  41. /// </summary>
  42. virtual public bool IsStun { get { return mStunTimeMS.Enable; } }
  43. /// <summary>
  44. /// 是否沉默
  45. /// </summary>
  46. virtual public bool IsSilent { get { return mSilentTimeMS.Enable; } }
  47. /// <summary>
  48. /// 是否免控
  49. /// </summary>
  50. virtual public bool IsIgnoreControl { get { return mIgnoreControl.Enable; } }
  51. /** 是否不允许移动 */
  52. public bool MarkCannotMove = false;
  53. virtual public bool IsCannotMove { get { return mCannotMoveTimeMS.Enable; } }
  54. //死亡隐藏
  55. private bool mDeadHide = false;
  56. public bool DeadHide{ get { return mDeadHide; } set { mDeadHide = value; } }
  57. public bool IsInPVP() { return this.Virtual.IsInPVP(); }
  58. public bool IsInPVE() { return this.Virtual.IsInPVE(); }
  59. virtual public string PlayerUUID
  60. {
  61. get { return ""; }
  62. }
  63. //--------------------------------------------------------------------------------------------------------------
  64. private readonly List<MultiTimeLine> mMultiTimeLineGroup = new List<MultiTimeLine>();
  65. private UnitSyncMultiTimeLine mMultiTimeLineSync;
  66. /// <summary>
  67. /// 注册自定义MultiTimeLine
  68. /// </summary>
  69. /// <param name="index"></param>
  70. /// <returns></returns>
  71. public MultiTimeLine RegistMultiTimeLine(out int index)
  72. {
  73. index = mMultiTimeLineGroup.Count;
  74. var timeline = new MultiTimeLine(MultiTimeLineRefresh);
  75. mMultiTimeLineGroup.Add(timeline);
  76. return timeline;
  77. }
  78. /// <summary>
  79. /// 获取指定ID TimeLine
  80. /// </summary>
  81. /// <param name="index"></param>
  82. /// <returns></returns>
  83. public MultiTimeLine GetTimeLine(int index)
  84. {
  85. return mMultiTimeLineGroup[index];
  86. }
  87. public bool IsMoveAble()
  88. {
  89. return this.Info.IsMoveable && this.Info.MoveSpeedSEC > 0;
  90. }
  91. /// <summary>
  92. /// 指定TimeLine是否还有任务
  93. /// </summary>
  94. /// <param name="index"></param>
  95. /// <returns></returns>
  96. public bool IsTimeLineEnable(int index)
  97. {
  98. return mMultiTimeLineGroup[index].Enable;
  99. }
  100. /// <summary>
  101. /// 获取指定 TimeLine 的 ID
  102. /// </summary>
  103. /// <param name="timeline"></param>
  104. /// <returns></returns>
  105. public int GetTimeLineIndex(MultiTimeLine timeline)
  106. {
  107. return mMultiTimeLineGroup.IndexOf(timeline);
  108. }
  109. //是否跟随队长
  110. public virtual bool IsFollowMaster() { return false; }
  111. /// <summary>
  112. /// 添加TimeLine任务
  113. /// </summary>
  114. /// <param name="index"></param>
  115. /// <param name="timeMS"></param>
  116. /// <returns></returns>
  117. public TimeExpire<int> AddTimeLineTask(int index, int timeMS)
  118. {
  119. return mMultiTimeLineGroup[index].Add(timeMS);
  120. }
  121. /// <summary>
  122. /// 强制移除TimeLine任务
  123. /// </summary>
  124. /// <param name="index"></param>
  125. /// <param name="task"></param>
  126. /// <returns></returns>
  127. public bool RemoveTimeLineTask(int index, TimeExpire<int> task)
  128. {
  129. return mMultiTimeLineGroup[index].Remove(task);
  130. }
  131. //--------------------------------------------------------------------------------------------------------------
  132. /// <summary>
  133. /// 被击中后不会被中断(霸体),是一个叠加值
  134. /// </summary>
  135. private MultiTimeLine mNoneBlockTimeMS;
  136. /// <summary>
  137. /// 眩晕,移动限制状态,是一个叠加值
  138. /// </summary>
  139. private MultiTimeLine mStunTimeMS;
  140. /// <summary>
  141. /// 隐形,是一个叠加值
  142. /// </summary>
  143. private MultiTimeLine mInvisibleTimeMS;
  144. /// <summary>
  145. /// 无敌时间,是个叠加值
  146. /// </summary>
  147. private MultiTimeLine mInvincibleTimeMS;
  148. /// <summary>
  149. /// 沉默时间,是个叠加值
  150. /// </summary>
  151. private MultiTimeLine mSilentTimeMS;
  152. /// <summary>
  153. /// 沉默时间,是个叠加值
  154. /// </summary>
  155. private MultiTimeLine mCannotMoveTimeMS;
  156. /// <summary>
  157. /// 免控时间
  158. /// </summary>
  159. private MultiTimeLine mIgnoreControl;
  160. //是否刷新标记
  161. private bool mNeedUpdate = false;
  162. public bool IsCanLaunchSkill(int skillID)
  163. {
  164. if (this.IsStun || (this.IsSilent == true && skillID != this.Virtual.GetBaseSkillID()))
  165. {
  166. return false;
  167. }
  168. return true;
  169. }
  170. private void InitTimeLines()
  171. {
  172. int index;
  173. mNoneBlockTimeMS = RegistMultiTimeLine(out index);
  174. mStunTimeMS = RegistMultiTimeLine(out index);
  175. mInvisibleTimeMS = RegistMultiTimeLine(out index);
  176. mInvincibleTimeMS = RegistMultiTimeLine(out index);
  177. mSilentTimeMS = RegistMultiTimeLine(out index);
  178. mCannotMoveTimeMS = RegistMultiTimeLine(out index);
  179. mIgnoreControl = RegistMultiTimeLine(out index);
  180. mMultiTimeLineSync = new UnitSyncMultiTimeLine(this.ID);
  181. }
  182. private void UpdateTimeLines(int intervalMS)
  183. {
  184. if(!mNeedUpdate)
  185. {
  186. return;
  187. }
  188. bool hasUpdate = false;
  189. for (int i = mMultiTimeLineGroup.Count - 1; i >= 0; --i)
  190. {
  191. if (mMultiTimeLineGroup[i].Update(intervalMS))
  192. {
  193. hasUpdate = true;
  194. }
  195. }
  196. // 全部timer都没有,不刷新
  197. if (!hasUpdate)
  198. {
  199. mNeedUpdate = false;
  200. }
  201. //不能移动单独刷新,此状态需要通知客户端
  202. if(this.MarkCannotMove && !this.IsCannotMove)
  203. {
  204. this.MarkCannotMove = false;
  205. this.RemoveActionSubState(UnitActionSubStatus.CanNotMove);
  206. }
  207. if (mMultiTimeLineSync.Update(mMultiTimeLineGroup))
  208. {
  209. queueEvent(mMultiTimeLineSync);
  210. }
  211. }
  212. public void MultiTimeLineRefresh(bool isAdd)
  213. {
  214. this.mNeedUpdate = true;
  215. }
  216. /// <summary>
  217. /// 设置霸体时间,如果当前已霸体,则取最大值
  218. /// </summary>
  219. /// <param name="timeMS"></param>
  220. public TimeExpire<int> SetNoneBlockTimeMS(int timeMS)
  221. {
  222. return mNoneBlockTimeMS.Add(timeMS);
  223. }
  224. /// <summary>
  225. /// 设置眩晕时间,如果当前已眩晕,则取最大值
  226. /// </summary>
  227. /// <param name="timeMS"></param>
  228. public TimeExpire<int> SetStunTimeMS(int timeMS)
  229. {
  230. TimeExpire<int> ret = mStunTimeMS.Add(timeMS);
  231. if (!IsStateDead)
  232. {
  233. changeState(new StateStun(this));
  234. }
  235. return ret;
  236. }
  237. /// <summary>
  238. /// 设置隐身时间,如果当前已隐身,则取最大值
  239. /// </summary>
  240. /// <param name="timeMS"></param>
  241. public TimeExpire<int> SetInvisibleTimeMS(int timeMS)
  242. {
  243. return mInvisibleTimeMS.Add(timeMS);
  244. }
  245. /// <summary>
  246. /// 设置无敌时间,如果当前已无敌,则取最大值
  247. /// </summary>
  248. /// <param name="timeMS"></param>
  249. public TimeExpire<int> SetInvincibleTimeMS(int timeMS)
  250. {
  251. return mInvincibleTimeMS.Add(timeMS);
  252. }
  253. /// <summary>
  254. /// 设置沉默时间
  255. /// </summary>
  256. /// <param name="timeMS"></param>
  257. public TimeExpire<int> SetSilentTimeMS(int timeMS)
  258. {
  259. return mSilentTimeMS.Add(timeMS);
  260. }
  261. /** 设置无法移动时间 */
  262. public TimeExpire<int> SetCannotMoveTimeMS(int timeMS)
  263. {
  264. this.MarkCannotMove = true;
  265. this.AddActionSubState(UnitActionSubStatus.CanNotMove);
  266. return mCannotMoveTimeMS.Add(timeMS);
  267. }
  268. /// <summary>
  269. /// 设置免控时间
  270. /// </summary>
  271. /// <param name="timeMS"></param>
  272. public TimeExpire<int> SetIgnoreControlTimeMS(int timeMS)
  273. {
  274. return mIgnoreControl.Add(timeMS);
  275. }
  276. public bool RemoveNoneBlock(TimeExpire<int> task)
  277. {
  278. return mNoneBlockTimeMS.Remove(task);
  279. }
  280. public bool RemoveStun(TimeExpire<int> task)
  281. {
  282. return mStunTimeMS.Remove(task);
  283. }
  284. public bool RemoveInvisible(TimeExpire<int> task)
  285. {
  286. return mInvisibleTimeMS.Remove(task);
  287. }
  288. public bool RemoveInvincible(TimeExpire<int> task)
  289. {
  290. return mInvincibleTimeMS.Remove(task);
  291. }
  292. public bool RemoveSilent(TimeExpire<int> task)
  293. {
  294. return mSilentTimeMS.Remove(task);
  295. }
  296. public bool RemoveCannotMove(TimeExpire<int> task)
  297. {
  298. return mCannotMoveTimeMS.Remove(task);
  299. }
  300. public bool RemoveIgnoreControl(TimeExpire<int> task)
  301. {
  302. return mIgnoreControl.Remove(task);
  303. }
  304. public void ClearNoneBlock()
  305. {
  306. mNoneBlockTimeMS.Clear();
  307. }
  308. public void ClearStun()
  309. {
  310. mStunTimeMS.Clear();
  311. }
  312. public void ClearInvisible()
  313. {
  314. mInvisibleTimeMS.Clear();
  315. }
  316. public void ClearInvincible()
  317. {
  318. mInvincibleTimeMS.Clear();
  319. }
  320. public void ClearSilent()
  321. {
  322. mSilentTimeMS.Clear();
  323. }
  324. public void ClearCannotMove()
  325. {
  326. mCannotMoveTimeMS.Clear();
  327. }
  328. public void ClearIgnoreControl()
  329. {
  330. mIgnoreControl.Clear();
  331. }
  332. #endregion
  333. //-----------------------------------------------------------------------------------------------------//
  334. //-----------------------------------------------------------------------------------------------
  335. #region _技能状态类_
  336. public PlayerSkillChangedEvent GetSkillEvent()
  337. {
  338. PlayerSkillChangedEvent evt = new PlayerSkillChangedEvent(ID);
  339. evt.unitFastCastRate = this.mFastCastRate;
  340. if (mDefaultSkill != null)
  341. {
  342. evt.baseSkill = mDefaultSkill.Data;
  343. }
  344. foreach (SkillState sk in mSkillStatus.Skills)
  345. {
  346. if (sk.GetSkillType() != XmdsSkillType.cardSkill && (evt.baseSkill == null || sk.Data.TemplateID != evt.baseSkill.ID))
  347. {
  348. sk.Data.CurUseTimes = (short)sk.OveryLayer;
  349. evt.skills.Add(sk.Data);
  350. }
  351. }
  352. return evt;
  353. }
  354. public ClientStruct.UnitSkillStatus[] GetCurrentSkillStatus()
  355. {
  356. ClientStruct.UnitSkillStatus[] ret = new ClientStruct.UnitSkillStatus[mSkillStatus.Count];
  357. int i = 0;
  358. foreach (SkillState sk in mSkillStatus.Skills)
  359. {
  360. ret[i].SkillTemplateID = (sk.Data.TemplateID);
  361. ret[i].PassTime = (sk.PassTime);
  362. ret[i].useTimes = sk.OveryLayer;
  363. i++;
  364. }
  365. return ret;
  366. }
  367. /// <summary>
  368. /// 初始化技能可用性
  369. /// </summary>
  370. public PlayerSkillActiveChangedEvent GetSyncSkillActives()
  371. {
  372. PlayerSkillActiveChangedEvent mSyncSkillActives = new PlayerSkillActiveChangedEvent(this.ID, mSkillStatus.Count);
  373. foreach (SkillState st in mSkillStatus.Skills)
  374. {
  375. PlayerSkillActiveChangedEvent.State sat = new PlayerSkillActiveChangedEvent.State();
  376. sat.SkillTemplateID = st.ID;
  377. sat.ST = st.ActiveState;
  378. mSyncSkillActives.Skills.Add(sat);
  379. }
  380. return mSyncSkillActives;
  381. }
  382. private SkillMap mSkillStatus = new SkillMap();
  383. private SkillState mDefaultSkill;
  384. private List<SkillTemplate> mAllSkills = new List<SkillTemplate>();
  385. private bool mSyncSkillActivesChanged = false;
  386. private void updateSyncSkillActives()
  387. {
  388. if (mSyncSkillActivesChanged)
  389. {
  390. mSyncSkillActivesChanged = false;
  391. queueEvent(GetSyncSkillActives());
  392. }
  393. }
  394. protected bool DoTryAddSkill(ref SkillTemplate sk)
  395. {
  396. bool ret = true;
  397. if (mOnTryAddSkill != null)
  398. {
  399. foreach (TryAddSkill tryadd in mOnTryAddSkill.GetInvocationList())
  400. {
  401. if (!tryadd.Invoke(this, ref sk))
  402. {
  403. ret = false;
  404. }
  405. }
  406. }
  407. return ret;
  408. }
  409. /// <summary>
  410. /// 重置当前单位技能,当前单位技能重置为单位模板指定技能
  411. /// </summary>
  412. public void ResetSkills()
  413. {
  414. if (mFormula.TryResetSkill(this))
  415. {
  416. InitSkills(mInfo.BaseSkillID, mInfo.Skills.ToArray());
  417. }
  418. }
  419. /// <summary>
  420. /// 设置当前单位技能
  421. /// </summary>
  422. /// <param name="baseSkill"></param>
  423. /// <param name="skills"></param>
  424. public virtual void InitSkills(LaunchSkill baseSkill, LaunchSkill[] skills)
  425. {
  426. List<SkillState> exist = new List<SkillState>(mSkillStatus.Skills);
  427. mSyncSkillActivesChanged = true;
  428. mDefaultSkill = null;
  429. mSkillStatus.Clear();
  430. mAllSkills.Clear();
  431. foreach (SkillState st in exist)
  432. {
  433. if (mOnSkillRemoved != null) { mOnSkillRemoved.Invoke(this, st); }
  434. }
  435. if (baseSkill != null)
  436. {
  437. SkillTemplate st = Templates.getSkill(baseSkill.SkillID);
  438. if (DoTryAddSkill(ref st))
  439. {
  440. if (st != null && !mSkillStatus.ContainsKey(baseSkill.SkillID))
  441. {
  442. mDefaultSkill = new SkillState(st, baseSkill, this);
  443. mAllSkills.Add(st);
  444. mSkillStatus.Add(mDefaultSkill);
  445. if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, mDefaultSkill); }
  446. }
  447. }
  448. }
  449. foreach (LaunchSkill lsk in skills)
  450. {
  451. SkillTemplate stt = Templates.getSkill(lsk.SkillID);
  452. if (DoTryAddSkill(ref stt))
  453. {
  454. if (stt != null && !mSkillStatus.ContainsKey(lsk.SkillID))
  455. {
  456. SkillState sk = new SkillState(stt, lsk, this);
  457. mAllSkills.Add(stt);
  458. mSkillStatus.Add(sk);
  459. if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, sk); }
  460. }
  461. }
  462. }
  463. if (IsInZone)
  464. {
  465. Parent.queueObjectEvent(this, GetSkillEvent());
  466. }
  467. if (mOnSkillChanged != null)
  468. {
  469. mOnSkillChanged.Invoke(this, mDefaultSkill, mSkillStatus.SkillsArray);
  470. }
  471. }
  472. /// <summary>
  473. /// 设置当前单位技能
  474. /// </summary>
  475. /// <param name="baseSkill"></param>
  476. /// <param name="skills"></param>
  477. public void InitSkills(SkillTemplate baseSkill, SkillTemplate[] skills)
  478. {
  479. List<SkillState> exist = new List<SkillState>(mSkillStatus.Skills);
  480. mSyncSkillActivesChanged = true;
  481. mDefaultSkill = null;
  482. mSkillStatus.Clear();
  483. mAllSkills.Clear();
  484. foreach (SkillState st in exist)
  485. {
  486. if (mOnSkillRemoved != null) { mOnSkillRemoved.Invoke(this, st); }
  487. }
  488. if (baseSkill != null)
  489. {
  490. if (DoTryAddSkill(ref baseSkill))
  491. {
  492. if (baseSkill != null && !mSkillStatus.ContainsKey(baseSkill.ID))
  493. {
  494. mDefaultSkill = new SkillState(baseSkill, new LaunchSkill(baseSkill.ID), this);
  495. mAllSkills.Add(baseSkill);
  496. mSkillStatus.Add(mDefaultSkill);
  497. if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, mDefaultSkill); }
  498. }
  499. }
  500. }
  501. foreach (SkillTemplate ssk in skills)
  502. {
  503. SkillTemplate stt = ssk;
  504. if (DoTryAddSkill(ref stt))
  505. {
  506. if (stt != null && !mSkillStatus.ContainsKey(stt.ID))
  507. {
  508. LaunchSkill lsk = null;
  509. foreach(LaunchSkill _lsk in mInfo.Skills)
  510. {
  511. if (_lsk.SkillID == stt.ID)
  512. {
  513. lsk = _lsk;
  514. break;
  515. }
  516. }
  517. if (lsk == null)
  518. lsk = new LaunchSkill(stt.ID);
  519. SkillState state = new SkillState(stt, lsk, this);
  520. mAllSkills.Add(stt);
  521. mSkillStatus.Add(state);
  522. if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, state); }
  523. }
  524. }
  525. }
  526. if (IsInZone)
  527. {
  528. Parent.queueObjectEvent(this, GetSkillEvent());
  529. }
  530. if (mOnSkillChanged != null)
  531. {
  532. mOnSkillChanged.Invoke(this, mDefaultSkill, mSkillStatus.SkillsArray);
  533. }
  534. }
  535. public SkillState AddSkill(SkillTemplate st, bool autoLaunch, bool is_default = false)
  536. {
  537. if (DoTryAddSkill(ref st))
  538. {
  539. if (st != null && !mSkillStatus.ContainsKey(st.ID))
  540. {
  541. mSyncSkillActivesChanged = true;
  542. SkillState state = new SkillState(st, new LaunchSkill(st.ID, autoLaunch), this);
  543. mAllSkills.Add(st);
  544. mSkillStatus.Add(state);
  545. if (is_default)
  546. {
  547. mDefaultSkill = state;
  548. }
  549. if (IsInZone)
  550. {
  551. st.CurUseTimes = (short)state.OveryLayer;
  552. Parent.queueObjectEvent(this, new PlayerSkillAddedEvent(ID, st, is_default));
  553. }
  554. if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, state); }
  555. if (mOnSkillChanged != null) { mOnSkillChanged.Invoke(this, mDefaultSkill, mSkillStatus.SkillsArray); }
  556. return state;
  557. }
  558. }
  559. return null;
  560. }
  561. public SkillState RemoveSkill(int skillTemplateID)
  562. {
  563. SkillState state = mSkillStatus.RemoveByKey(skillTemplateID);
  564. if (state != null)
  565. {
  566. mSyncSkillActivesChanged = true;
  567. mAllSkills.RemoveAll((t) => { return (t.TemplateID == skillTemplateID); });
  568. if (state == mDefaultSkill)
  569. {
  570. mDefaultSkill = null;
  571. }
  572. if (IsInZone) { Parent.queueObjectEvent(this, new PlayerSkillRemovedEvent(ID, skillTemplateID)); }
  573. if (mOnSkillRemoved != null) { mOnSkillRemoved.Invoke(this, state); }
  574. if (mOnSkillChanged != null) { mOnSkillChanged.Invoke(this, mDefaultSkill, mSkillStatus.SkillsArray); }
  575. }
  576. return state;
  577. }
  578. public SkillTemplate DefaultSkill
  579. {
  580. get
  581. {
  582. if (mDefaultSkill != null)
  583. {
  584. return mDefaultSkill.Data;
  585. }
  586. return null;
  587. }
  588. }
  589. public SkillState DefaultSkillStatus()
  590. {
  591. return mDefaultSkill;
  592. }
  593. public IEnumerable<SkillState> SkillStatus
  594. {
  595. get
  596. {
  597. return mSkillStatus.Skills;
  598. }
  599. }
  600. public SkillTemplate getSkill(int skillID)
  601. {
  602. SkillState st = mSkillStatus.Get(skillID);
  603. if (st != null)
  604. {
  605. return st.Data;
  606. }
  607. return null;
  608. }
  609. public SkillState getSkillState(int skillID)
  610. {
  611. SkillState st = mSkillStatus.Get(skillID);
  612. if (st != null)
  613. {
  614. return st;
  615. }
  616. return null;
  617. }
  618. public void onSkillHitTarget()
  619. {
  620. if (mOnSkillHitTarger != null) mOnSkillHitTarger.Invoke();
  621. }
  622. /// <summary>
  623. /// 获得当前随机可释放的技能
  624. /// </summary>
  625. /// <param name="expectTarget"></param>
  626. /// <returns></returns>
  627. public virtual SkillState getRandomLaunchableExpectSkill(SkillTemplate.CastTarget expectTarget)
  628. {
  629. int rand = RandomN.Next(0, mAllSkills.Count);
  630. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  631. {
  632. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  633. if (st.ExpectTarget == expectTarget)
  634. {
  635. SkillState sst = mSkillStatus.Get(st.GetID());
  636. if (sst.LaunchSkill.AutoLaunch && sst.TryLaunch() && sst.CanAutoLaunch())
  637. {
  638. return sst;
  639. }
  640. }
  641. }
  642. return null;
  643. }
  644. /// <summary>
  645. /// 获得当前随机可释放的技能
  646. /// </summary>
  647. /// <param name="target"></param>
  648. /// <param name="expectTarget"></param>
  649. /// <param name="reason"></param>
  650. /// <param name="checkRange"></param>
  651. /// <returns></returns>
  652. public virtual SkillState getRandomLaunchableExpectSkill(InstanceUnit target, SkillTemplate.CastTarget expectTarget, AttackReason reason = AttackReason.Tracing, bool checkRange = false)
  653. {
  654. int rand = RandomN.Next(0, mAllSkills.Count);
  655. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  656. {
  657. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  658. if (st.ExpectTarget == expectTarget)
  659. {
  660. SkillState sst = mSkillStatus.Get(st.GetID());
  661. if (sst.LaunchSkill.AutoLaunch && sst.TryLaunch() && Parent.IsAttackable(this, target, expectTarget, reason, st))
  662. {
  663. if (!checkRange || sst.checkTargetRange(target))
  664. {
  665. return sst;
  666. }
  667. }
  668. }
  669. }
  670. return null;
  671. }
  672. /// <summary>
  673. /// 获得当前随机可释放的技能
  674. /// </summary>
  675. /// <returns></returns>
  676. public virtual SkillState getRandomLaunchableSkill()
  677. {
  678. int rand = RandomN.Next(0, mAllSkills.Count);
  679. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  680. {
  681. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  682. SkillState sst = mSkillStatus.Get(st.GetID());
  683. if (sst.TryLaunch())
  684. {
  685. return sst;
  686. }
  687. }
  688. return null;
  689. }
  690. public void getKeepSkills(List<int> keeps, List<LaunchSkill> ret)
  691. {
  692. foreach (SkillState st in mSkillStatus.Skills)
  693. {
  694. if (keeps.Contains(st.ID))
  695. {
  696. ret.Add(st.LaunchSkill);
  697. }
  698. }
  699. }
  700. private void updateSkills()
  701. {
  702. //int intervalMS = Parent.UpdateIntervalMS;
  703. //for (int i = 0; i < mSkillStatus.Count; i++)
  704. //{
  705. // SkillState st = mSkillStatus.GetAt(i);
  706. // st.Update(intervalMS);
  707. //}
  708. mSkillStatus.update(Parent.UpdateIntervalMS);
  709. }
  710. protected virtual void clearSkills()
  711. {
  712. this.mSkillStatus.Clear();
  713. }
  714. // Skills
  715. class SkillMap
  716. {
  717. private HashMap<int, SkillState> Map = new HashMap<int, SkillState>();
  718. private List<SkillState> For = new List<SkillState>();
  719. public int Count { get { return For.Count; } }
  720. public IEnumerable<SkillState> Skills { get { return For; } }
  721. public SkillState[] SkillsArray { get { return For.ToArray(); } }
  722. public SkillState GetAt(int i)
  723. {
  724. return For[i];
  725. }
  726. public SkillState Get(int id)
  727. {
  728. return Map.Get(id);
  729. }
  730. public void Add(SkillState state)
  731. {
  732. if (!Map.ContainsKey(state.ID))
  733. {
  734. Map.Put(state.ID, state);
  735. For.Add(state);
  736. }
  737. }
  738. public SkillState RemoveByKey(int id)
  739. {
  740. SkillState ret = Map.RemoveByKey(id);
  741. if (ret != null)
  742. {
  743. For.Remove(ret);
  744. }
  745. return ret;
  746. }
  747. public bool ContainsKey(int id)
  748. {
  749. return Map.ContainsKey(id);
  750. }
  751. public void Clear()
  752. {
  753. Map.Clear();
  754. For.Clear();
  755. }
  756. public void update(int intervalMS)
  757. {
  758. foreach (SkillState st in this.For)
  759. {
  760. st.Update(intervalMS);
  761. }
  762. }
  763. }
  764. public delegate void ISkillCDStatusChange(SkillState skill, bool iscd);
  765. public class SkillState
  766. {
  767. // 技能CD状态更改监听
  768. readonly private List<ISkillCDStatusChange> mSkillCDStatusChange = new List<ISkillCDStatusChange>();
  769. readonly public SkillTemplate Data;
  770. readonly public LaunchSkill LaunchSkill;
  771. readonly public InstanceUnit Owner;
  772. private SkillActiveState current_state = SkillActiveState.Active;
  773. private int total_cd_time;
  774. private int decrease_total_timems;
  775. private int trigger_decrease_times; //机制触发,减少CD
  776. // 叠层
  777. private int overLayer = -1;
  778. /// <summary>
  779. /// 从技能开始时的逝去时间
  780. /// </summary>
  781. private int pass_time;
  782. /// <summary>
  783. /// 最后CD完成时间
  784. /// </summary>
  785. private int stop_time;
  786. /// <summary>
  787. /// 如果是多段攻击,记录段数
  788. /// </summary>
  789. private int action_step = -1;
  790. private int lockActionStep = -1; //锁定执行序列号
  791. // 是否是多段式技能
  792. private bool mCanStoreTimes = false;
  793. /// <summary>
  794. /// 多段攻击自动下段
  795. /// </summary>
  796. private bool auto_increase_action_step = true;
  797. /// <summary>
  798. /// 当前是否在冷却
  799. /// </summary>
  800. private bool is_cd = false;
  801. /// <summary>
  802. /// 当前是否在多段攻击允许时间内
  803. /// </summary>
  804. private bool is_in_mutil_time = true;
  805. /// <summary>
  806. /// 当前释放技能时的状态机
  807. /// </summary>
  808. private StateSkill state;
  809. private float action_speed;
  810. public int ID { get { return Data.ID; } }
  811. /// <summary>
  812. /// 当前技能是否在转CD
  813. /// </summary>
  814. public bool IsCD { get { return is_cd; } }
  815. public SkillActiveState ActiveState { get { return current_state; } }
  816. public bool IsActive { get { return current_state == SkillActiveState.Active || current_state == SkillActiveState.ActiveAndHide; } }
  817. public bool IsPauseOnDeactive { get { return current_state == SkillActiveState.DeactiveAndPause; } }
  818. public XmdsSkillType GetSkillType() { return this.Data.Properties.GetSkillType(); }
  819. //是否是多段式技能
  820. private byte mMaxMutilStep = 0;
  821. //代码逻辑控制cd
  822. private bool mCodeControl_HasNext;
  823. /// <summary>
  824. /// 当前技能是否放完
  825. /// </summary>
  826. public bool IsDone
  827. {
  828. get
  829. {
  830. if (Data.IsCoolDownWithAction)
  831. {
  832. if (state == null)
  833. return true;
  834. if (state.IsDone)
  835. return true;
  836. if (state.IsCancelableBySkill)
  837. return true;
  838. return false;
  839. }
  840. else if (this.IsMutilAction() && this.IsHasNext())//多段式攻击
  841. {
  842. if (is_in_mutil_time)
  843. {
  844. return true;
  845. }
  846. }
  847. return !is_cd;
  848. }
  849. }
  850. public bool CanAutoLaunch()
  851. {
  852. return this.stop_time == 0 || this.GetSkillType() == XmdsSkillType.normalAtk || this.stop_time + 2000 < this.pass_time || this.IsInMutilTime;
  853. }
  854. public bool IsCodeConttrolCD()
  855. {
  856. return this.mCodeControl_HasNext;
  857. }
  858. /// <summary>
  859. /// CD 需要的总时间
  860. /// </summary>
  861. public int TotalCDTime
  862. {
  863. get { return total_cd_time - this.trigger_decrease_times; } set { total_cd_time = value; }
  864. }
  865. /// <summary>
  866. /// CD 需要的总时间
  867. /// </summary>
  868. public bool IsInMutilTime
  869. {
  870. get { return is_in_mutil_time; } set { is_in_mutil_time = value; }
  871. }
  872. public void ResetTotalTime()
  873. {
  874. this.total_cd_time = Data.ToUnitSkillTotalTime(Owner.mFastCastRate) - decrease_total_timems;
  875. this.total_cd_time = Math.Max(0, this.total_cd_time);
  876. }
  877. public int PassTime
  878. {
  879. get { return pass_time; }
  880. }
  881. public int OveryLayer
  882. {
  883. get { return this.overLayer; }
  884. }
  885. public void ResetPassTime()
  886. {
  887. this.pass_time = 0;
  888. }
  889. public void SetCDStates(bool cdStatus)
  890. {
  891. this.is_cd = cdStatus;
  892. for (int i = 0; i < mSkillCDStatusChange.Count; i++)
  893. {
  894. mSkillCDStatusChange[i].Invoke(this, this.is_cd);
  895. }
  896. }
  897. public void ResgistSkillCDChangeListen(ISkillCDStatusChange listen)
  898. {
  899. if(listen != null)
  900. {
  901. this.mSkillCDStatusChange.Add(listen);
  902. }
  903. }
  904. public bool IsMutilAction()
  905. {
  906. return Data.IsSingleAction && (Data.ActionQueue.Count > 1);
  907. }
  908. public bool IsHasNext()
  909. {
  910. bool isHasNext = this.ActionIndex < this.mMaxMutilStep;
  911. if(!isHasNext && this.Data.ActionQueue.Count > this.ActionIndex && this.Data.ActionQueue[this.ActionIndex].IsCancelBySkillNext)
  912. {
  913. isHasNext = true;
  914. }
  915. return isHasNext;
  916. }
  917. public byte GetMaxMutilStep()
  918. {
  919. return this.mMaxMutilStep;
  920. }
  921. public int GetMaxSteps()
  922. {
  923. return this.mMaxMutilStep > 0 ? Math.Min(this.mMaxMutilStep, this.Data.ActionQueue.Count-1) : (this.Data.ActionQueue.Count-1);
  924. }
  925. public UnitActionData GetNextAction()
  926. {
  927. if(this.ActionIndex + 1 >= this.Data.ActionQueue.Count)
  928. {
  929. return null;
  930. }
  931. return this.Data.ActionQueue[this.ActionIndex + 1];
  932. }
  933. public UnitActionData GetCurAction()
  934. {
  935. if (this.ActionIndex < 0 || this.ActionIndex >= this.Data.ActionQueue.Count)
  936. {
  937. return null;
  938. }
  939. return this.Data.ActionQueue[this.ActionIndex];
  940. }
  941. public float ActionSpeed { get { return action_speed; } }
  942. public int ActionIndex { get { return action_step; }}
  943. public int LockActionStep { get { return lockActionStep; } set { lockActionStep = value; } }
  944. public object Tag { get; set; }
  945. public bool CanStoreTimes { get { return this.mCanStoreTimes; }}
  946. internal SkillState(SkillTemplate data, LaunchSkill skill, InstanceUnit owner)
  947. {
  948. this.Owner = owner;
  949. this.Data = data;
  950. this.mCanStoreTimes = (this.Data.Properties.GetSkillUseTimes() > 1 && this.Data.Properties.GetSkillAddTimeInterval() > 0);
  951. //默认有最大层数
  952. if (this.mCanStoreTimes)
  953. {
  954. this.overLayer = this.Data.Properties.GetSkillUseTimes();
  955. }
  956. this.LaunchSkill = skill;
  957. this.total_cd_time = Data.ToUnitSkillTotalTime(Owner.mFastCastRate) - decrease_total_timems;
  958. this.pass_time = int.MaxValue / 2;
  959. this.action_speed = data.ActionSpeedRate + Owner.__mSkillActionSpeedRate;
  960. // 多段式技能,但是只能释放一段
  961. if(Data.IsSingleAction && (Data.ActionQueue.Count > 1))
  962. {
  963. for (int i = 0; i < Data.ActionQueue.Count; i++)
  964. {
  965. this.mMaxMutilStep = (byte)i;
  966. if (Data.ActionQueue[i].SigleActionType == ActionEnum.forbidNext || Data.ActionQueue[i].SigleActionType == ActionEnum.forbidNext_lock
  967. || Data.ActionQueue[i].SigleActionType == ActionEnum.forbidNext_showTime)
  968. {
  969. break;
  970. }
  971. }
  972. }
  973. }
  974. //设置自动释放
  975. public void setAutoLaunch(bool AutoLaunch)
  976. {
  977. this.LaunchSkill.AutoLaunch = AutoLaunch;
  978. }
  979. public void setMaxMutilStep(int vlaue)
  980. {
  981. this.mMaxMutilStep = (byte)vlaue;
  982. }
  983. public void SetActionIndex(int index)
  984. {
  985. this.action_step = index;
  986. }
  987. // 设置机制冷却缩减时间
  988. public void setTriggerDecreaseTime(int time, bool notify = false)
  989. {
  990. this.trigger_decrease_times = time;
  991. // time==0是开始释放时的重置
  992. if(notify)
  993. {
  994. PlayerCDEvent evt = new PlayerCDEvent(this.Owner.ID);
  995. evt.is_decrease_time = true;
  996. evt.is_all = false;
  997. evt.decrease_timeMS = time;
  998. evt.skill_template_id = this.LaunchSkill.SkillID;
  999. this.Owner.Parent.queueObjectEvent(this.Owner, evt);
  1000. }
  1001. }
  1002. /// <summary>
  1003. /// 检测目标距离
  1004. /// </summary>
  1005. /// <returns></returns>
  1006. public bool checkTargetRange(InstanceUnit targetUnit)
  1007. {
  1008. if (Data.AttackMustBeInRange && Data.AttackAngle == 0)
  1009. {
  1010. if (targetUnit != null)
  1011. {
  1012. float rg = Owner.GetSkillAttackRange(this.Data, targetUnit);
  1013. if (Collider.Object_HitBody_TouchRound(targetUnit, Owner.X, Owner.Y, rg))
  1014. {
  1015. return true;
  1016. }
  1017. }
  1018. return false;
  1019. }
  1020. return true;
  1021. }
  1022. /// <summary>
  1023. /// 检测目标距离
  1024. /// </summary>
  1025. /// <returns></returns>
  1026. public bool checkTargetInAttackRange(InstanceUnit targetUnit, LaunchSkillParam param)
  1027. {
  1028. //只处理怪物的自动战斗,玩家控制不了
  1029. if (this.Owner.IsMonster && this.Data.AttackKeepRange > 0)
  1030. {
  1031. targetUnit = targetUnit == null ? this.Owner.Parent.getUnit(param.TargetUnitID) : targetUnit;
  1032. if (targetUnit != null && CMath.getDistance(targetUnit.X, targetUnit.Y, this.Owner.X, this.Owner.Y) < Data.AttackKeepRange)
  1033. {
  1034. return false;
  1035. }
  1036. }
  1037. if (this.Data.AttackRange > 0)
  1038. {
  1039. targetUnit = targetUnit == null ? this.Owner.Parent.getUnit(param.TargetUnitID) : targetUnit;
  1040. if (targetUnit != null && CMath.getDistance(targetUnit.X, targetUnit.Y, this.Owner.X, this.Owner.Y) > (Owner.GetSkillAttackRange(this.Data) + targetUnit.BodyBlockSize))
  1041. {
  1042. return false;
  1043. }
  1044. }
  1045. return true;
  1046. }
  1047. public void SetDecreaseTotalTimeMS(int timeMS)
  1048. {
  1049. this.decrease_total_timems = timeMS;
  1050. }
  1051. public void AddDecreaseTotalTimeMS(int timeMS)
  1052. {
  1053. this.decrease_total_timems += timeMS;
  1054. }
  1055. public int GetDecreaseTotalTimeMS()
  1056. {
  1057. return this.decrease_total_timems;
  1058. }
  1059. /// <summary>
  1060. /// 指定技能动作
  1061. /// </summary>
  1062. /// <param name="step"></param>
  1063. public void Reset(bool init = false)
  1064. {
  1065. this.action_step = -1;
  1066. if (!init)
  1067. {
  1068. this.IsInMutilTime = false;
  1069. }
  1070. }
  1071. /// <summary>
  1072. /// 设置是否自动控制多段攻击(如果要手动指定播放技能,则设置为False)
  1073. /// </summary>
  1074. /// <param name="at"></param>
  1075. public void SetAutoIncreaseActionIndex(bool at)
  1076. {
  1077. this.auto_increase_action_step = at;
  1078. }
  1079. internal void Launch(StateSkill s, LaunchSkillParam param)
  1080. {
  1081. this.NextAction();
  1082. this.total_cd_time = this.GetDefTotalCDTime();
  1083. this.pass_time = 0;
  1084. this.state = s;
  1085. this.SetCDStates(true);
  1086. }
  1087. private int GetDefTotalCDTime()
  1088. {
  1089. int cd_time = 0;
  1090. cd_time = Data.ToUnitSkillTotalTime(Owner.mFastCastRate) - decrease_total_timems;
  1091. if(this.GetSkillType() == XmdsSkillType.normalAtk)
  1092. {
  1093. cd_time = (int)(cd_time / (Owner.GetAttackSpeed() * 0.0001f));
  1094. }
  1095. else
  1096. {
  1097. cd_time = (int)((1.0f - Math.Min(XmdsConstConfig.SKILL_CD_REDUCE_MAX, Owner.SkillCdReduce) * 0.0001f) * cd_time);
  1098. }
  1099. cd_time = Math.Max(0, cd_time);
  1100. return cd_time;
  1101. }
  1102. public void updateOverLayer(int value)
  1103. {
  1104. this.overLayer = value;
  1105. //通知叠层
  1106. var queData = new PlayerSkillUseTimeChangedEvent(this.Owner.ID, this.Data.ID, (byte)this.overLayer);
  1107. queData.hasNext = (byte)((this.OveryLayer >= this.Data.Properties.GetSkillUseTimes()) ? 0 : 1);
  1108. this.Owner.queueEvent(queData);
  1109. }
  1110. public void StartCD(int total_time = 0)
  1111. {
  1112. if (total_time > 0)
  1113. {
  1114. this.total_cd_time = Math.Max(0, total_time - decrease_total_timems);
  1115. }
  1116. else
  1117. {
  1118. //this.total_cd_time = Data.ToUnitSkillTotalTime(Owner.mFastCastRate) - decrease_total_timems;
  1119. //this.total_cd_time = Math.Max(0, this.total_cd_time);
  1120. this.total_cd_time = this.GetDefTotalCDTime();
  1121. }
  1122. this.pass_time = 0;
  1123. this.SetCDStates(true);
  1124. Owner.queueEvent(new PlayerSkillTimeChangedEvent(Owner.ID, ID, this.pass_time, this.TotalCDTime));
  1125. }
  1126. public void LogicToCD()
  1127. {
  1128. this.mCodeControl_HasNext = true;
  1129. this.StartCD();
  1130. }
  1131. public void SetCodeControlNext()
  1132. {
  1133. this.mCodeControl_HasNext = false;
  1134. }
  1135. public void ForceIntoCD()
  1136. {
  1137. this.is_in_mutil_time = false;
  1138. this.action_step = -1;
  1139. this.StartCD();
  1140. }
  1141. internal void Stop(StateSkill s)
  1142. {
  1143. this.state = null;
  1144. // CD 依靠动作结束
  1145. if (Data.IsCoolDownWithAction)
  1146. {
  1147. this.SetCDStates(false);
  1148. this.stop_time = this.pass_time;
  1149. }
  1150. }
  1151. internal void Update(int intervalMS)
  1152. {
  1153. if (!IsActive && IsPauseOnDeactive)
  1154. {
  1155. return;
  1156. }
  1157. this.pass_time += intervalMS;
  1158. if(this.CanStoreTimes)
  1159. {
  1160. if(this.overLayer < Data.Properties.GetSkillUseTimes() && this.pass_time >= Data.Properties.GetSkillAddTimeInterval())
  1161. {
  1162. this.pass_time = 0;
  1163. this.updateOverLayer(this.overLayer + 1);
  1164. }
  1165. }
  1166. else if (is_cd && !Data.IsCoolDownWithAction)
  1167. {
  1168. // 技能 CD
  1169. if (this.pass_time >= TotalCDTime)
  1170. {
  1171. if(this.IsMutilAction() && (this.is_in_mutil_time || (this.action_step >= Data.ActionQueue.Count)))
  1172. {
  1173. if(this.action_step < Data.ActionQueue.Count && this.action_step > 0 &&
  1174. (Data.ActionQueue[this.action_step].SigleActionType == ActionEnum.chargeAtk || Data.ActionQueue[this.action_step].SigleActionType == ActionEnum.IsAutoNext))
  1175. {
  1176. pass_time = 0;
  1177. return;
  1178. }
  1179. else
  1180. {
  1181. this.Reset();
  1182. this.StartCD();
  1183. }
  1184. }
  1185. else
  1186. {
  1187. this.is_in_mutil_time = true;
  1188. this.SetCDStates(false);
  1189. this.stop_time = this.pass_time;
  1190. //Console.WriteLine("out cd : " + TimeUtil.GetTimestampMS());
  1191. }
  1192. }
  1193. }
  1194. }
  1195. public bool TryLaunch()
  1196. {
  1197. if (!IsActive)
  1198. {
  1199. return false;
  1200. }
  1201. if (!this.Owner.IsCanLaunchSkill(Data.TemplateID))
  1202. {
  1203. return false;
  1204. }
  1205. //if (Owner.__mCurrentHP.Value >= Data.CostHP)
  1206. {
  1207. return IsDone;
  1208. }
  1209. //return false;
  1210. }
  1211. public int NextAction()
  1212. {
  1213. if (Data.IsSingleAction && auto_increase_action_step)
  1214. {
  1215. int action_time = this.TotalCDTime;
  1216. // 是放技能时,处于多段攻击连击冷却时间范围
  1217. //if (pass_time - stop_time < Data.SingleActionCoolDownMS || pass_time < (action_time + Data.SingleActionCoolDownMS))
  1218. if (this.action_step < 0)
  1219. {
  1220. this.action_step = 0;
  1221. }
  1222. else
  1223. {
  1224. if (Data.IsCoolDownWithAction)
  1225. {
  1226. action_time = Data.ActionQueue[action_step % Data.ActionQueue.Count].TotalTimeMS;
  1227. }
  1228. if (this.IsMutilAction())
  1229. {
  1230. if (this.IsInMutilTime || (this.Data.IsCoolDownWithAction && this.IsDone) || (!this.IsDone && this.action_step < this.mMaxMutilStep))
  1231. {
  1232. this.action_step += 1;
  1233. this.action_step = (byte)(action_step % Data.ActionQueue.Count);
  1234. //if (this.action_step >= Data.ActionQueue.Count || !IsCanLaunchNextStepByPlayer(Data.ActionQueue[this.action_step]))
  1235. //{
  1236. // this.action_step = 0;
  1237. // this.is_in_mutil_time = false;
  1238. //}
  1239. }
  1240. }
  1241. }
  1242. }
  1243. return action_step;
  1244. }
  1245. public bool IsCanLaunchNextStepByPlayer(UnitActionData actionData)
  1246. {
  1247. // = this.Data.ActionQueue[this.ActionIndex];
  1248. if (actionData.SigleActionType == ActionEnum.forbidNext ||
  1249. actionData.SigleActionType == ActionEnum.forbidNext_lock)
  1250. {
  1251. return false;
  1252. }
  1253. return true;
  1254. }
  1255. internal void ClearCD()
  1256. {
  1257. this.pass_time = TotalCDTime;
  1258. }
  1259. internal void DecreaseSkillCD(int timeMS)
  1260. {
  1261. if (!Data.IsCoolDownWithAction)
  1262. {
  1263. this.pass_time += timeMS;
  1264. }
  1265. }
  1266. internal void DecreaseSkillCD_Pct(float percent)
  1267. {
  1268. if (!Data.IsCoolDownWithAction)
  1269. {
  1270. this.pass_time += (int)(TotalCDTime * percent);
  1271. }
  1272. }
  1273. internal void SetPassTime(int passtime)
  1274. {
  1275. if (!Data.IsCoolDownWithAction)
  1276. {
  1277. if (passtime != this.pass_time)
  1278. {
  1279. this.pass_time = passtime;
  1280. Owner.queueEvent(new PlayerSkillTimeChangedEvent(Owner.ID, ID, this.pass_time, this.total_cd_time));
  1281. if (pass_time < TotalCDTime)
  1282. {
  1283. this.SetCDStates(true);
  1284. }
  1285. }
  1286. }
  1287. }
  1288. internal bool TrySetActive(SkillActiveState state)
  1289. {
  1290. if (state != current_state)
  1291. {
  1292. this.current_state = state;
  1293. this.Owner.mSyncSkillActivesChanged = true;
  1294. return true;
  1295. }
  1296. return false;
  1297. }
  1298. }
  1299. /// <summary>
  1300. /// 设置技能可用性
  1301. /// </summary>
  1302. /// <param name="skillTemplateID"></param>
  1303. /// <param name="active"></param>
  1304. /// <param name="pause_on_deactive"></param>
  1305. public void SetSkillActive(int skillTemplateID, bool active, bool pause_on_deactive = false)
  1306. {
  1307. SkillState st = getSkillState(skillTemplateID);
  1308. if (st != null)
  1309. {
  1310. if (active)
  1311. st.TrySetActive(SkillActiveState.Active);
  1312. else if (pause_on_deactive)
  1313. st.TrySetActive(SkillActiveState.DeactiveAndPause);
  1314. else
  1315. st.TrySetActive(SkillActiveState.Deactive);
  1316. }
  1317. //标记冻结
  1318. this.Virtual.SetSkillActive(skillTemplateID, active, pause_on_deactive);
  1319. }
  1320. public void SetSkillActive(int skillTemplateID, SkillActiveState state)
  1321. {
  1322. SkillState st = getSkillState(skillTemplateID);
  1323. if (st != null)
  1324. {
  1325. st.TrySetActive(state);
  1326. }
  1327. }
  1328. /// <summary>
  1329. /// 清除技能CD
  1330. /// </summary>
  1331. /// <param name="skillTemplateID"></param>
  1332. public void ClearSkillCD(int skillTemplateID)
  1333. {
  1334. SkillState ss = getSkillState(skillTemplateID);
  1335. if (ss != null)
  1336. {
  1337. ss.ClearCD();
  1338. PlayerCDEvent evt = new PlayerCDEvent(ID);
  1339. evt.is_clear = true;
  1340. evt.is_all = false;
  1341. evt.skill_template_id = skillTemplateID;
  1342. Parent.queueObjectEvent(this, evt);
  1343. }
  1344. }
  1345. /// <summary>
  1346. /// 清除所有技能CD
  1347. /// </summary>
  1348. public void ClearAllSkillCD()
  1349. {
  1350. foreach (SkillState ss in mSkillStatus.Skills)
  1351. {
  1352. ss.ClearCD();
  1353. }
  1354. PlayerCDEvent evt = new PlayerCDEvent(ID);
  1355. evt.is_clear = true;
  1356. evt.is_all = true;
  1357. Parent.queueObjectEvent(this, evt);
  1358. }
  1359. /// <summary>
  1360. /// 减少当前CD固定时间
  1361. /// </summary>
  1362. /// <param name="skillTemplateID"></param>
  1363. /// <param name="updateTimeMS"></param>
  1364. public void DecreaseSkillCD(int skillTemplateID, int updateTimeMS)
  1365. {
  1366. SkillState ss = getSkillState(skillTemplateID);
  1367. if (ss != null)
  1368. {
  1369. ss.DecreaseSkillCD(updateTimeMS);
  1370. PlayerCDEvent evt = new PlayerCDEvent(ID);
  1371. evt.is_decrease_time = true;
  1372. evt.is_all = false;
  1373. evt.decrease_timeMS = updateTimeMS;
  1374. evt.skill_template_id = skillTemplateID;
  1375. Parent.queueObjectEvent(this, evt);
  1376. }
  1377. }
  1378. /// <summary>
  1379. /// 减少当前CD固定时间
  1380. /// </summary>
  1381. /// <param name="updateTimeMS"></param>
  1382. public void DecreaseAllSkillCD(int updateTimeMS)
  1383. {
  1384. foreach (SkillState ss in mSkillStatus.Skills)
  1385. {
  1386. ss.DecreaseSkillCD(updateTimeMS);
  1387. }
  1388. PlayerCDEvent evt = new PlayerCDEvent(ID);
  1389. evt.is_decrease_time = true;
  1390. evt.is_all = true;
  1391. evt.decrease_timeMS = updateTimeMS;
  1392. Parent.queueObjectEvent(this, evt);
  1393. }
  1394. public void DecreaseSkillCD_Pct(int skillTemplateID, float percent)
  1395. {
  1396. SkillState ss = getSkillState(skillTemplateID);
  1397. if (ss != null)
  1398. {
  1399. ss.DecreaseSkillCD_Pct(percent);
  1400. PlayerCDEvent evt = new PlayerCDEvent(ID);
  1401. evt.is_decrease_pct = true;
  1402. evt.is_all = false;
  1403. evt.decrease_pct = percent;
  1404. evt.skill_template_id = skillTemplateID;
  1405. Parent.queueObjectEvent(this, evt);
  1406. }
  1407. }
  1408. public void DecreaseAllSkillCD_Pct(float percent)
  1409. {
  1410. foreach (SkillState ss in mSkillStatus.Skills)
  1411. {
  1412. ss.DecreaseSkillCD_Pct(percent);
  1413. }
  1414. PlayerCDEvent evt = new PlayerCDEvent(ID);
  1415. evt.is_decrease_pct = true;
  1416. evt.is_all = true;
  1417. evt.decrease_pct = percent;
  1418. Parent.queueObjectEvent(this, evt);
  1419. }
  1420. public void SetSkillPassTime(int skillID, int passTimeMS)
  1421. {
  1422. SkillState ss = this.getSkillState(skillID);
  1423. if (ss != null)
  1424. {
  1425. ss.SetPassTime(passTimeMS);
  1426. }
  1427. }
  1428. public void StartSkillCD(int skillID, int totalTimeMS = 0)
  1429. {
  1430. SkillState ss = this.getSkillState(skillID);
  1431. if (ss != null)
  1432. {
  1433. ss.StartCD(totalTimeMS);
  1434. }
  1435. }
  1436. #endregion
  1437. //-----------------------------------------------------------------------------------------------
  1438. #region _BUFF状态类_
  1439. public ClientStruct.UnitBuffStatus[] GetCurrentBuffStatus()
  1440. {
  1441. List<ClientStruct.UnitBuffStatus> ret = new List<ClientStruct.UnitBuffStatus>(mBuffs.Count);
  1442. mBuffs.ForEachRead((buff) =>
  1443. {
  1444. if (buff.Data.ClientVisible)
  1445. {
  1446. ClientStruct.UnitBuffStatus bf = new ClientStruct.UnitBuffStatus();
  1447. bf.BuffTemplateID = (buff.Data.TemplateID);
  1448. bf.SenderID = buff.SenderID;
  1449. bf.IsEquip = buff.IsEquip;
  1450. bf.TotalTime = buff.Data.LifeTimeMS;
  1451. bf.PassTime = (buff.PassTimeMS);
  1452. bf.OverlayLevel = buff.OverlayLevel;
  1453. ret.Add(bf);
  1454. }
  1455. return false;
  1456. });
  1457. return ret.ToArray();
  1458. }
  1459. public bool IshasControlBuff(Predicate<BuffTemplate> cb)
  1460. {
  1461. bool hasControlBuff = false;
  1462. mBuffs.ForEachReadTrueReturn((buff) =>
  1463. {
  1464. if (cb(buff.Data))
  1465. {
  1466. hasControlBuff = true;
  1467. return true;
  1468. }
  1469. return false;
  1470. });
  1471. return hasControlBuff;
  1472. }
  1473. public void GetAllBuffStatus(List<BuffState> ret)
  1474. {
  1475. mBuffs.ForEachRead((bf) =>
  1476. {
  1477. ret.Add(bf);
  1478. return false;
  1479. });
  1480. }
  1481. private class BuffMap
  1482. {
  1483. class BuffList : IEnumerable<InstanceUnit.BuffState>
  1484. {
  1485. readonly public BuffTemplate bufft;
  1486. private HashMap<uint, InstanceUnit.BuffState> list;
  1487. private InstanceUnit.BuffState last;
  1488. public BuffList(BuffTemplate bufft)
  1489. {
  1490. this.bufft = bufft;
  1491. this.list = new HashMap<uint, BuffState>(1);
  1492. }
  1493. public void Add(InstanceUnit.BuffState bs)
  1494. {
  1495. if (bs.Data.IsDuplicating)
  1496. {
  1497. list.Add(bs.SenderID, bs);
  1498. }
  1499. else
  1500. {
  1501. list.Add(0, bs);
  1502. }
  1503. last = bs;
  1504. }
  1505. public InstanceUnit.BuffState Remove(uint unit_id)
  1506. {
  1507. if (bufft.IsDuplicating)
  1508. {
  1509. InstanceUnit.BuffState bs = list.RemoveByKey(unit_id);
  1510. if (bs != null && bs == last)
  1511. {
  1512. if (list.Count > 0)
  1513. {
  1514. foreach (InstanceUnit.BuffState bf in list.Values)
  1515. {
  1516. last = bf;
  1517. break;
  1518. }
  1519. }
  1520. else
  1521. {
  1522. last = null;
  1523. }
  1524. }
  1525. return bs;
  1526. }
  1527. else
  1528. {
  1529. last = null;
  1530. return list.RemoveByKey(0);
  1531. }
  1532. }
  1533. public void Clear(List<InstanceUnit.BuffState> cleared)
  1534. {
  1535. last = null;
  1536. cleared.AddRange(list.Values);
  1537. list.Clear();
  1538. }
  1539. public InstanceUnit.BuffState Get(uint unit_id)
  1540. {
  1541. return list.Get(unit_id);
  1542. }
  1543. public int Count { get { return list.Count; } }
  1544. public InstanceUnit.BuffState Last { get { return last; } }
  1545. IEnumerator<BuffState> IEnumerable<BuffState>.GetEnumerator()
  1546. {
  1547. return list.Values.GetEnumerator();
  1548. }
  1549. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  1550. {
  1551. return list.Values.GetEnumerator();
  1552. }
  1553. }
  1554. private HashMap<int, BuffList> mObjects = new HashMap<int, BuffList>();
  1555. private List<BuffState> mObjectsList = new List<BuffState>();
  1556. private bool dirty = true;
  1557. public int Count { get { return mObjects.Count; } }
  1558. //-----------------------------------------------------------------------
  1559. // read
  1560. //-----------------------------------------------------------------------
  1561. public InstanceUnit.BuffState GetObject(int id)
  1562. {
  1563. BuffList list;
  1564. if (mObjects.TryGetValue(id, out list))
  1565. {
  1566. return list.Last;
  1567. }
  1568. return null;
  1569. }
  1570. public void GetObjects(int id, List<InstanceUnit.BuffState> ret)
  1571. {
  1572. BuffList list;
  1573. if (mObjects.TryGetValue(id, out list))
  1574. {
  1575. ret.AddRange(list);
  1576. }
  1577. }
  1578. public InstanceUnit.BuffState GetObject(int buff_id, uint unit_id)
  1579. {
  1580. BuffList list;
  1581. if (mObjects.TryGetValue(buff_id, out list))
  1582. {
  1583. return list.Get(unit_id);
  1584. }
  1585. return null;
  1586. }
  1587. //-----------------------------------------------------------------------
  1588. // write
  1589. //-----------------------------------------------------------------------
  1590. public bool RemoveObject(InstanceUnit.BuffState bs)
  1591. {
  1592. BuffList list;
  1593. if (mObjects.TryGetValue(bs.ID, out list))
  1594. {
  1595. dirty = true;
  1596. return list.Remove(bs.SenderID) != null;
  1597. }
  1598. return false;
  1599. }
  1600. public InstanceUnit.BuffState RemoveObject(int buffID, uint unit_id)
  1601. {
  1602. BuffList list;
  1603. if (mObjects.TryGetValue(buffID, out list))
  1604. {
  1605. dirty = true;
  1606. return list.Remove(unit_id);
  1607. }
  1608. return null;
  1609. }
  1610. public void RemoveObjects(int buffID, List<InstanceUnit.BuffState> removed)
  1611. {
  1612. BuffList list;
  1613. if (mObjects.TryGetValue(buffID, out list))
  1614. {
  1615. dirty = true;
  1616. list.Clear(removed);
  1617. }
  1618. }
  1619. public void AddObject(InstanceUnit.BuffState obj)
  1620. {
  1621. BuffList list = mObjects.Get(obj.ID);
  1622. if (list == null)
  1623. {
  1624. list = new BuffList(obj.Data);
  1625. mObjects.Add(obj.ID, list);
  1626. }
  1627. list.Add(obj);
  1628. dirty = true;
  1629. }
  1630. private IList<BuffState> Refresh()
  1631. {
  1632. if (dirty)
  1633. {
  1634. dirty = false;
  1635. mObjectsList.Clear();
  1636. foreach (BuffList list in mObjects.Values)
  1637. {
  1638. foreach (InstanceUnit.BuffState buff in list)
  1639. {
  1640. mObjectsList.Add(buff);
  1641. }
  1642. }
  1643. }
  1644. return mObjectsList;
  1645. }
  1646. private void ToList(List<BuffState> ret)
  1647. {
  1648. foreach (BuffList list in mObjects.Values)
  1649. {
  1650. foreach (InstanceUnit.BuffState buff in list)
  1651. {
  1652. ret.Add(buff);
  1653. }
  1654. }
  1655. }
  1656. //-----------------------------------------------------------------------
  1657. //
  1658. //-----------------------------------------------------------------------
  1659. /// <summary>
  1660. ///
  1661. /// </summary>
  1662. /// <param name="action">返回true终止迭代!</param>
  1663. public void ForEachRead(Predicate<InstanceUnit.BuffState> action)
  1664. {
  1665. foreach (BuffList list in mObjects.Values)
  1666. {
  1667. foreach (InstanceUnit.BuffState buff in list)
  1668. {
  1669. if (action(buff))
  1670. {
  1671. return;
  1672. }
  1673. }
  1674. }
  1675. }
  1676. public bool ForEachReadTrueReturn(Predicate<InstanceUnit.BuffState> action)
  1677. {
  1678. foreach (BuffList list in mObjects.Values)
  1679. {
  1680. foreach (InstanceUnit.BuffState buff in list)
  1681. {
  1682. if (action(buff))
  1683. {
  1684. return true;
  1685. }
  1686. }
  1687. }
  1688. return false;
  1689. }
  1690. /// <summary>
  1691. ///
  1692. /// </summary>
  1693. /// <param name="action">返回true终止迭代!</param>
  1694. public void ForEachWrite(Predicate<InstanceUnit.BuffState> action)
  1695. {
  1696. using (var list = ListObjectPool<BuffState>.AllocAutoRelease())
  1697. {
  1698. ToList(list);
  1699. for (int i = 0; i < list.Count; i++)
  1700. {
  1701. if (action(list[i]))
  1702. {
  1703. return;
  1704. }
  1705. }
  1706. }
  1707. }
  1708. public void Update(InstanceZone parent)
  1709. {
  1710. IList<BuffState> list = Refresh();
  1711. for (int i = 0; i < list.Count; i++)
  1712. {
  1713. BuffState bs = list[i];
  1714. bs.OnUpdate(parent);
  1715. if (bs.IsEnd() && !bs.MarkRemove)
  1716. {
  1717. this.RemoveObject(bs);
  1718. bs.OnEnd(BuffState.EndResult_ByTimeUp);
  1719. }
  1720. }
  1721. }
  1722. }
  1723. public class BuffState
  1724. {
  1725. public const string EndResult_ByTimeUp = "time_up";
  1726. public const string EndResult_ByReplaced = "replaced";
  1727. public const string EndResult_ByClientRemoved = "client_removed";
  1728. public const string EndResult_ByCatgoryExclusive = "catgory_exclusive";
  1729. public const string EndResult_ByCode = "code";
  1730. public const string ENDResult_HuDunBreak = "HuDunBreak";
  1731. readonly private BuffTemplate mData;
  1732. readonly private InstanceUnit mOwner;
  1733. readonly private InstanceUnit sender;
  1734. private TimeExpire<int> st_stun;
  1735. private TimeExpire<int> st_invisible;
  1736. private TimeExpire<int> st_invincible;
  1737. private TimeExpire<int> st_silent;
  1738. private TimeExpire<int> st_canmove;
  1739. private TimeExpire<int> st_ignoreControl;
  1740. private int mBuffExtData; //buff扩展字段
  1741. private TimeInterval<BuffTemplate.KeyFrame> interval;
  1742. private PopupKeyFrames<BuffTemplate.KeyFrame> keyframes;
  1743. private int total_time;
  1744. private int passtime;
  1745. private byte overlay_level;
  1746. public BuffTemplate Data { get { return mData; } }
  1747. public int ID { get { return mData.ID; } }
  1748. public byte OverlayLevel { get { return overlay_level; }}
  1749. public int PassTimeMS { get { return passtime; } }
  1750. public int LifeTimeMS { get { return total_time; } }
  1751. public bool IsEquip { get; private set; }
  1752. public uint SenderID { get { return sender.ID; } }
  1753. public int BuffExtData { get { return mBuffExtData; } set { mBuffExtData = value; } }
  1754. public InstanceZone Zone { get { return mOwner.Parent; } }
  1755. public InstanceUnit Owner { get { return mOwner; } }
  1756. public InstanceUnit Sender { get { return sender; } }
  1757. public object Tag { get; set; }
  1758. //释放时位置信息
  1759. private Vector2 mHitPos = new Vector2();
  1760. public bool MarkRemove { get; set; }
  1761. //set和get放在一起虽然爽,但是可读性真一般
  1762. public void SetLayerLevel(byte value)
  1763. {
  1764. this.overlay_level = value;
  1765. }
  1766. internal BuffState(BuffTemplate data, InstanceUnit unit, InstanceUnit sender, byte overlay_level, bool forever, bool isControlBuf)
  1767. {
  1768. this.mData = data;
  1769. this.mOwner = unit;
  1770. this.sender = sender;
  1771. this.IsEquip = forever;
  1772. this.overlay_level = overlay_level;
  1773. this.passtime = 0;
  1774. this.MarkRemove = false;
  1775. this.interval = new TimeInterval<BuffTemplate.KeyFrame>(data.HitIntervalMS);
  1776. this.interval.FirstTimeEnable = data.FirstTimeEnable;
  1777. this.interval.Tag = data.HitKeyFrame;
  1778. this.keyframes = new PopupKeyFrames<BuffTemplate.KeyFrame>();
  1779. this.keyframes.AddRange(data.KeyFrames);
  1780. this.total_time = data.LifeTimeMS;
  1781. //控制增幅
  1782. if (sender.__mControledTimeAdd != 0 && (mData.IsSilent || mData.MakeStun || isControlBuf))
  1783. {
  1784. this.total_time = (int)(Math.Max(0.0f, 1.0f + unit.__mControledTimeAdd * 0.0001f) * this.total_time);
  1785. }
  1786. //韧性,控制减
  1787. if (unit.ControledTimeReduce != 0 && (mData.IsSilent || mData.MakeStun || isControlBuf))
  1788. {
  1789. this.total_time = (int)(Math.Max(0.0f, 1.0f - unit.ControledTimeReduce * 0.0001f) * this.total_time);
  1790. }
  1791. }
  1792. internal void OnStart()
  1793. {
  1794. mHitPos.SetX(mOwner.X);
  1795. mHitPos.SetY(mOwner.Y);
  1796. mOwner.mFormula.OnBuffBegin(mOwner, this, sender);
  1797. if (mData.MakeStun)
  1798. {
  1799. this.st_stun = mOwner.SetStunTimeMS(total_time);
  1800. }
  1801. if (mData.IsInvisible)
  1802. {
  1803. this.st_invisible = mOwner.SetInvisibleTimeMS(total_time);
  1804. }
  1805. if (mData.IsInvincible)
  1806. {
  1807. this.st_invincible = mOwner.SetInvincibleTimeMS(total_time);
  1808. }
  1809. if (mData.IsSilent)
  1810. {
  1811. this.st_silent = mOwner.SetSilentTimeMS(total_time);
  1812. }
  1813. if(!mData.IsCanMove)
  1814. {
  1815. this.st_canmove = mOwner.SetCannotMoveTimeMS(total_time);
  1816. }
  1817. // 免疫控制
  1818. if(mData.IgnoreControl)
  1819. {
  1820. this.st_ignoreControl = mOwner.SetIgnoreControlTimeMS(total_time);
  1821. }
  1822. if (mData.UnitTrigger != null)
  1823. {
  1824. mOwner.addTrigger(mData.UnitTrigger.TriggerTemplateID);
  1825. }
  1826. if (mData.UnitChangeSkills)
  1827. {
  1828. if (mData.UnitKeepSkillsID != null && mData.UnitKeepSkillsID.Count > 0)
  1829. {
  1830. using (var keeps = ListObjectPool<LaunchSkill>.AllocAutoRelease())
  1831. {
  1832. mOwner.getKeepSkills(mData.UnitKeepSkillsID, keeps);
  1833. keeps.AddRange(mData.UnitSkills);
  1834. mOwner.InitSkills(mData.UnitBaseSkillID, keeps.ToArray());
  1835. }
  1836. }
  1837. else
  1838. {
  1839. mOwner.InitSkills(mData.UnitBaseSkillID, mData.UnitSkills.ToArray());
  1840. }
  1841. }
  1842. mOwner.doGotBuff(this);
  1843. if (mData.ClientVisible)
  1844. {
  1845. //Console.WriteLine("获得剑影 - " + this.BuffExtData);
  1846. mOwner.queueEvent(new UnitLaunchBuffEvent(mOwner.ID, this.ID, this.SenderID,
  1847. this.LifeTimeMS, this.IsEquip, overlay_level, this.BuffExtData));
  1848. }
  1849. }
  1850. internal void OnEnd(string result, bool replace = false)
  1851. {
  1852. this.passtime = total_time;
  1853. this.MarkRemove = true;
  1854. mOwner.doLostBuff(this);
  1855. if (this.st_stun != null)
  1856. {
  1857. mOwner.mStunTimeMS.Remove(st_stun);
  1858. }
  1859. if (this.st_invisible != null)
  1860. {
  1861. mOwner.mInvisibleTimeMS.Remove(st_invisible);
  1862. }
  1863. if (this.st_invincible != null)
  1864. {
  1865. mOwner.mInvisibleTimeMS.Remove(st_invincible);
  1866. }
  1867. if (this.st_silent != null)
  1868. {
  1869. mOwner.mSilentTimeMS.Remove(st_silent);
  1870. }
  1871. if(this.st_canmove != null)
  1872. {
  1873. mOwner.mCannotMoveTimeMS.Remove(st_canmove);
  1874. }
  1875. if(this.st_ignoreControl != null)
  1876. {
  1877. mOwner.mIgnoreControl.Remove(st_ignoreControl);
  1878. }
  1879. if (mData.UnitTrigger != null)
  1880. {
  1881. mOwner.removeTrigger(mData.UnitTrigger.TriggerTemplateID);
  1882. }
  1883. if (mData.UnitChangeSkills)
  1884. {
  1885. mOwner.ResetSkills();
  1886. }
  1887. if (mData.EndKeyFrame != null)
  1888. {
  1889. doKeyFrame(mData.EndKeyFrame);
  1890. }
  1891. mOwner.mFormula.OnBuffEnd(mOwner, this, result, replace);
  1892. if (this.Data.ClientVisible)
  1893. {
  1894. if (!replace)
  1895. {
  1896. mOwner.queueEvent(new UnitStopBuffEvent(mOwner.ID, this.ID, SenderID));
  1897. }
  1898. }
  1899. }
  1900. internal void OnUpdate(InstanceZone zone)
  1901. {
  1902. using (var kfs = ListObjectPool<BuffTemplate.KeyFrame>.AllocAutoRelease())
  1903. {
  1904. if (keyframes.PopKeyFrames(passtime, kfs) > 0)
  1905. {
  1906. foreach (BuffTemplate.KeyFrame kf in kfs)
  1907. {
  1908. doKeyFrame(kf);
  1909. }
  1910. }
  1911. }
  1912. if (interval.Update(zone.UpdateIntervalMS))
  1913. {
  1914. doKeyFrame(mData.HitKeyFrame);
  1915. mOwner.mFormula.OnBuffUpdate(mOwner, this, interval.TotalTickCount);
  1916. }
  1917. passtime += zone.UpdateIntervalMS;
  1918. }
  1919. private void doKeyFrame(BuffTemplate.KeyFrame kf)
  1920. {
  1921. if (kf != null)
  1922. {
  1923. if (kf.Attack != null)
  1924. {
  1925. mOwner.doHitAttack(sender, new AttackSource(this, kf.Attack));
  1926. }
  1927. if (kf.Spell != null)
  1928. {
  1929. if(kf.Spell.SenderUnit == LaunchSpllSenderUnit.AttackerUnit)
  1930. {
  1931. this.sender.Parent.unitLaunchSpell(XmdsSkillType.none, sender, kf.Spell,
  1932. this.sender.X, this.sender.Y, mOwner.ID, null);
  1933. }
  1934. else
  1935. {
  1936. mOwner.Parent.unitLaunchSpell(XmdsSkillType.none, sender, kf.Spell,
  1937. mOwner.X, mOwner.Y, mOwner.ID, null);
  1938. }
  1939. }
  1940. if (kf.Item != null)
  1941. {
  1942. mOwner.UseItem(kf.Item.ItemTemplateID);
  1943. }
  1944. if(kf.addItem != null)
  1945. {
  1946. this.BuffAddItem(kf.addItem);
  1947. }
  1948. }
  1949. }
  1950. /** buff生成道具 */
  1951. private void BuffAddItem(BuffAddItem addItem)
  1952. {
  1953. var itemInfo = Zone.Templates.getItem(addItem.ItemTemplateID);
  1954. if(itemInfo == null)
  1955. {
  1956. log.Warn("BuffAddItem找不到指定道具: " + addItem.ItemTemplateID);
  1957. return;
  1958. }
  1959. if(addItem.posType == CommonAI.Zone.BuffAddItem.PosType.HitBuffPos)
  1960. {
  1961. this.mOwner.Parent.AddItem(itemInfo, null, this.mHitPos.X, this.mHitPos.Y, 0, this.mOwner.Force, null, this.Owner);
  1962. }
  1963. else if(addItem.posType == CommonAI.Zone.BuffAddItem.PosType.BindUnitPos)
  1964. {
  1965. this.mOwner.Parent.AddItem(itemInfo, null, this.Owner.X, this.Owner.Y, 0, this.mOwner.Force, null, this.Owner);
  1966. }
  1967. else if(addItem.posType == CommonAI.Zone.BuffAddItem.PosType.PointPos)
  1968. {
  1969. this.mOwner.Parent.AddItem(itemInfo, null, addItem.pointX, addItem.pointY, 0, this.mOwner.Force, null, this.Owner);
  1970. }
  1971. }
  1972. public bool IsEnd()
  1973. {
  1974. if (Data.IsRemoveOnSenderRemoved)
  1975. {
  1976. if (!sender.Enable || sender.IsDead())
  1977. {
  1978. return true;
  1979. }
  1980. }
  1981. if(Data.IsRemoveOnDead && this.mOwner.IsDead())
  1982. {
  1983. return true;
  1984. }
  1985. if (IsEquip) return false;
  1986. return (passtime >= total_time);
  1987. }
  1988. public void AddLifeTimeMS(int timeMS)
  1989. {
  1990. this.total_time += timeMS;
  1991. }
  1992. //public void RefrushToMaxOverlayer()
  1993. //{
  1994. // this.overlay_level = this.mData.MaxOverlay;
  1995. //}
  1996. }
  1997. private BuffMap mBuffs = new BuffMap();
  1998. /// <summary>
  1999. /// 是否存在技能变身BUFF
  2000. /// </summary>
  2001. public bool IsBuffChangeSkills
  2002. {
  2003. get
  2004. {
  2005. return GetChangeSkillBuff() != null;
  2006. }
  2007. }
  2008. /// <summary>
  2009. /// 获得技能变身BUFF
  2010. /// </summary>
  2011. public BuffState GetChangeSkillBuff()
  2012. {
  2013. BuffState ret = null;
  2014. mBuffs.ForEachRead((bs) =>
  2015. {
  2016. if (bs.Data.UnitChangeSkills && !bs.IsEnd())
  2017. {
  2018. ret = bs;
  2019. return true;
  2020. }
  2021. return false;
  2022. });
  2023. return ret;
  2024. }
  2025. public BuffState GetBuffByID(int buffTemplateID)
  2026. {
  2027. return mBuffs.GetObject(buffTemplateID);
  2028. }
  2029. public BuffState GetBuffByIDAndSender(int buffTemplateID, uint senderUnitID)
  2030. {
  2031. return mBuffs.GetObject(buffTemplateID, senderUnitID);
  2032. }
  2033. public BuffState AddBuff(int buffID, int pointMaxOverLayer = 0, bool forever = false, bool bMaxOverlayer = false, bool bMaxReset = false)
  2034. {
  2035. BuffTemplate buff = Templates.getBuff(buffID);
  2036. if (buff != null)
  2037. {
  2038. return this.AddBuff(buff, this, pointMaxOverLayer, forever, bMaxOverlayer, 0, false, bMaxReset);
  2039. }
  2040. return null;
  2041. }
  2042. public BuffState AddBuff(int buffID, InstanceUnit sender, int pointMaxOverLayer = 0, bool forever = false,
  2043. bool bMaxOverlayer = false, bool bMaxReset = false, int buffExt = 0, int addLyers = 0)
  2044. {
  2045. BuffTemplate buff = Templates.getBuff(buffID);
  2046. if (buff != null)
  2047. {
  2048. return this.AddBuff(buff, sender, pointMaxOverLayer, forever, bMaxOverlayer, buffExt, false, bMaxReset, addLyers);
  2049. }
  2050. return null;
  2051. }
  2052. /** 编辑器直接编写增加buff接口 */
  2053. public BuffState AddBuff(LaunchBuff buff, InstanceUnit sender, bool forever = false)
  2054. {
  2055. //如果绑定了事件id,就表示去触发事件
  2056. if (buff.BuffID == 0 && buff.BindEventID > 0)
  2057. {
  2058. BattleFunction.GetInstance().TriggrBattleFunction(buff.BindEventID, this.Virtual, sender == null ? null : sender.Virtual);
  2059. return null;
  2060. }
  2061. return this.AddBuff(buff.BuffID, sender, 0, forever);
  2062. }
  2063. /// <summary>
  2064. /// 增加Buff
  2065. /// </summary>
  2066. /// <param name="forever">如果为True,则表示Buff不受时间CD控制</param>
  2067. public virtual BuffState AddBuff(BuffTemplate buff, InstanceUnit sender, int pointMaxOverLayer = 0, bool forever = false,
  2068. bool bMaxOverlayer = false, int buffExt = 0, bool isControlBuf = false, bool maxReset = false, int addLayers = 0)
  2069. {
  2070. object tag = null;
  2071. if (mFormula.TryAddBuff(this, sender, ref buff, out tag))
  2072. {
  2073. if (!forever && buff.LifeTimeMS <= 0)
  2074. {
  2075. return null;
  2076. }
  2077. //凌驾于一切之上的免控
  2078. if(isControlBuf && this.IsIgnoreControl)
  2079. {
  2080. return null;
  2081. }
  2082. if (!removeBuffByCatgory(buff.ID, buff.ExclusiveCatgory, buff.ExclusivePriority))
  2083. {
  2084. return null;
  2085. }
  2086. int finalMaxOverLay = (pointMaxOverLayer <= 0) ? buff.MaxOverlay : pointMaxOverLayer;
  2087. int overlay_level = bMaxOverlayer ? (finalMaxOverLay - 1) : 0;
  2088. BuffState bs = mBuffs.RemoveObject(buff.ID, sender.ID);
  2089. if (bs != null)
  2090. {
  2091. if (buff.IsOverlay)
  2092. {
  2093. int finalAddLayer = Math.Max(1, addLayers);
  2094. if(bs.OverlayLevel + finalAddLayer >= finalMaxOverLay && maxReset)
  2095. {
  2096. overlay_level = 0;
  2097. }
  2098. else
  2099. {
  2100. overlay_level = bMaxOverlayer ? (finalMaxOverLay - 1) : (Math.Min(bs.OverlayLevel + finalAddLayer, finalMaxOverLay - 1));
  2101. }
  2102. }
  2103. bs.OnEnd(BuffState.EndResult_ByReplaced, true);
  2104. }
  2105. else if(addLayers > 0 && buff.MaxOverlay > 0)
  2106. {
  2107. overlay_level = Math.Max(0, Math.Min(buff.MaxOverlay - 1, addLayers-1));
  2108. }
  2109. BuffState newbs = new BuffState(buff, this, sender, (byte)overlay_level, forever, isControlBuf);
  2110. newbs.Tag = tag;
  2111. newbs.BuffExtData = (bs == null) ? buffExt : bs.BuffExtData;
  2112. mBuffs.AddObject(newbs);
  2113. newbs.OnStart();
  2114. //System.Console.WriteLine("AddBuf - " + newbs.Data.ID + ", " + newbs.OverlayLevel);
  2115. mFormula.SendAddBuffEvent(this, sender, buff);
  2116. return newbs;
  2117. }
  2118. return null;
  2119. }
  2120. /// <summary>
  2121. /// 移除互斥技能,根据优先级
  2122. /// </summary>
  2123. /// <param name="buffID"></param>
  2124. /// <param name="catgory"></param>
  2125. /// <param name="priority"></param>
  2126. private bool removeBuffByCatgory(int buffID, int catgory, int priority)
  2127. {
  2128. if (catgory != 0)
  2129. {
  2130. // 检测当时有更高优先级 //
  2131. {
  2132. BuffState greater_buff = null;
  2133. mBuffs.ForEachRead((bs) =>
  2134. {
  2135. if ((bs.ID != buffID) && (bs.Data.ExclusiveCatgory == catgory) && (bs.Data.ExclusivePriority > priority))
  2136. {
  2137. greater_buff = bs;
  2138. return true;
  2139. }
  2140. return false;
  2141. });
  2142. if (greater_buff != null)
  2143. {
  2144. return false;
  2145. }
  2146. }
  2147. // 移除低优先级 //
  2148. mBuffs.ForEachWrite((bs) =>
  2149. {
  2150. if ((bs.ID != buffID) && (bs.Data.ExclusiveCatgory == catgory))
  2151. {
  2152. mBuffs.RemoveObject(bs);
  2153. bs.OnEnd(BuffState.EndResult_ByCatgoryExclusive);
  2154. }
  2155. return false;
  2156. });
  2157. }
  2158. return true;
  2159. }
  2160. public void removeBuff(int buffID, string result = BuffState.EndResult_ByCode)
  2161. {
  2162. using (var bss = ListObjectPool<BuffState>.AllocAutoRelease())
  2163. {
  2164. mBuffs.RemoveObjects(buffID, bss);
  2165. if (bss.Count > 0)
  2166. {
  2167. foreach (BuffState bs in bss)
  2168. {
  2169. bs.OnEnd(result);
  2170. }
  2171. }
  2172. }
  2173. }
  2174. public void removeBuffBySender(int buffID, uint senderUnitID, string result = BuffState.EndResult_ByCode)
  2175. {
  2176. BuffState bs = mBuffs.RemoveObject(buffID, senderUnitID);
  2177. if (bs != null)
  2178. {
  2179. bs.OnEnd(result);
  2180. }
  2181. }
  2182. public void clearBuffs(string result = BuffState.EndResult_ByCode)
  2183. {
  2184. mBuffs.ForEachWrite((bs) =>
  2185. {
  2186. if (!bs.Data.IsPassive && !bs.IsEquip)
  2187. {
  2188. mBuffs.RemoveObject(bs);
  2189. bs.OnEnd(result);
  2190. }
  2191. return false;
  2192. });
  2193. }
  2194. private void updateBuffs()
  2195. {
  2196. mBuffs.Update(Parent);
  2197. }
  2198. #endregion
  2199. //---------------------------------------------------------------------------------------------------------------
  2200. #region _被动系触发类_
  2201. private readonly List<TriggerState> mTriggerStatus = new List<TriggerState>();
  2202. private readonly UnitTriggerHelper mTriggerHelper;
  2203. /// <summary>
  2204. /// 保存被动状态
  2205. /// </summary>
  2206. public class TriggerState
  2207. {
  2208. readonly internal UnitTriggerTemplate mData;
  2209. readonly internal InstanceUnit mOwner;
  2210. public bool IsDamageTrigger { get; private set; }
  2211. public bool IsAttackTrigger { get; private set; }
  2212. public UnitTriggerTemplate Data { get { return mData; } }
  2213. public object Tag { get; set; }
  2214. private long launch_time;
  2215. internal TriggerState(UnitTriggerTemplate data, InstanceUnit unit)
  2216. {
  2217. this.mData = data;
  2218. this.mOwner = unit;
  2219. this.IsDamageTrigger = false;
  2220. this.IsAttackTrigger = false;
  2221. this.launch_time = -mData.CoolDownTimeMS;
  2222. foreach (BaseTriggerEvent evt in data.Triggers)
  2223. {
  2224. UnitTriggerEventTypeAttribute ut = PropertyUtil.GetAttribute<UnitTriggerEventTypeAttribute>(evt.GetType());
  2225. foreach (Type dtype in ut.DelegateTypes)
  2226. {
  2227. if (dtype == typeof(DamageHandler))
  2228. {
  2229. this.IsDamageTrigger = true;
  2230. }
  2231. else if (dtype == typeof(AttackHandler))
  2232. {
  2233. this.IsAttackTrigger = true;
  2234. }
  2235. }
  2236. }
  2237. }
  2238. internal void Start(InstanceUnit target)
  2239. {
  2240. if (IsActive)
  2241. {
  2242. this.launch_time = mOwner.Parent.PassTimeMS;
  2243. uint targetID = 0;
  2244. Vector2 targetPos = new Vector2(mOwner.X, mOwner.Y);
  2245. if (target != null)
  2246. {
  2247. targetPos.SetX(target.X);
  2248. targetPos.SetY(target.Y);
  2249. targetID = target.ID;
  2250. }
  2251. // 如果关键帧绑定特效
  2252. if (mData.DoEffect != null)
  2253. {
  2254. mOwner.queueEvent(new UnitEffectEvent(mOwner.ID, mData.DoEffect));
  2255. }
  2256. // 如果关键帧绑定释放法术
  2257. if (mData.DoSpell != null)
  2258. {
  2259. mOwner.Parent.unitLaunchSpell(XmdsSkillType.none, mOwner, mData.DoSpell, mOwner.X, mOwner.Y, 0, targetPos);
  2260. }
  2261. // 如果关键帧绑定自己释放BUFF
  2262. if (mData.DoBuff != null)
  2263. {
  2264. mOwner.AddBuff(mData.DoBuff, mOwner);
  2265. }
  2266. // 如果关键帧绑定单位位移
  2267. if (mData.DoSkill != null)
  2268. {
  2269. mOwner.launchSkill(mData.DoSkill.SkillID, new InstanceUnit.LaunchSkillParam(targetID, targetPos));
  2270. }
  2271. //召唤
  2272. if (mData.DoSummon != null)
  2273. {
  2274. mOwner.Parent.unitSummonUnit(mOwner, mData.DoSummon);
  2275. }
  2276. mOwner.mFormula.OnTriggerStart(mOwner, this, target);
  2277. }
  2278. }
  2279. public bool Test(AttackSource source)
  2280. {
  2281. if (IsActive)
  2282. {
  2283. foreach (BaseTriggerEvent evt in mData.Triggers)
  2284. {
  2285. if (!mOwner.mFormula.TryLaunchTrigger(mOwner, mData, evt, source) || !evt.Test(mOwner, source))
  2286. {
  2287. return false;
  2288. }
  2289. }
  2290. return true;
  2291. }
  2292. return false;
  2293. }
  2294. /// <summary>
  2295. /// 是否处于冷却状态
  2296. /// </summary>
  2297. public bool IsActive
  2298. {
  2299. get { return (launch_time + mData.CoolDownTimeMS) < mOwner.Parent.PassTimeMS; }
  2300. }
  2301. }
  2302. private void updateTriggers(bool slowRefresh)
  2303. {
  2304. this.mFormula.OnUpdateTriggerSkill(this, Parent.UpdateIntervalMS, slowRefresh);
  2305. }
  2306. public void addTriggers(ICollection<LaunchTrigger> triggers)
  2307. {
  2308. foreach (LaunchTrigger launch in triggers)
  2309. {
  2310. addTrigger(launch.TriggerTemplateID);
  2311. }
  2312. }
  2313. public bool containsTrigger(int tgID)
  2314. {
  2315. for (int i = mTriggerStatus.Count - 1; i >= 0; --i)
  2316. {
  2317. if (mTriggerStatus[i].mData.ID == tgID)
  2318. {
  2319. return true;
  2320. }
  2321. }
  2322. return false;
  2323. }
  2324. public TriggerState findTrigger(int tgID)
  2325. {
  2326. for (int i = mTriggerStatus.Count - 1; i >= 0; --i)
  2327. {
  2328. if (mTriggerStatus[i].mData.ID == tgID)
  2329. {
  2330. return mTriggerStatus[i];
  2331. }
  2332. }
  2333. return null;
  2334. }
  2335. public bool addTrigger(UnitTriggerTemplate trigger)
  2336. {
  2337. if (!containsTrigger(trigger.ID))
  2338. {
  2339. TriggerState ts = new TriggerState(trigger, this);
  2340. mTriggerStatus.Add(ts);
  2341. }
  2342. return false;
  2343. }
  2344. public bool addTrigger(int triggerID)
  2345. {
  2346. if (!containsTrigger(triggerID))
  2347. {
  2348. UnitTriggerTemplate tg = Templates.getUnitTrigger(triggerID);
  2349. if (tg != null)
  2350. {
  2351. TriggerState ts = new TriggerState(tg, this);
  2352. mTriggerStatus.Add(ts);
  2353. }
  2354. }
  2355. return false;
  2356. }
  2357. public void removeTrigger(int tgID)
  2358. {
  2359. TriggerState bs = findTrigger(tgID);
  2360. if (bs != null)
  2361. {
  2362. mTriggerStatus.Remove(bs);
  2363. }
  2364. }
  2365. public bool startTrigger(int tgID, InstanceUnit target)
  2366. {
  2367. TriggerState bs = findTrigger(tgID);
  2368. if (bs != null && bs.IsActive)
  2369. {
  2370. bs.Start(target);
  2371. return true;
  2372. }
  2373. return false;
  2374. }
  2375. public void clearTriggers()
  2376. {
  2377. mTriggerStatus.Clear();
  2378. }
  2379. /// <summary>
  2380. /// 如果被攻击,尝试启动单位触发器
  2381. /// </summary>
  2382. /// <param name="attacker"></param>
  2383. /// <param name="source"></param>
  2384. public void TryStartDamageTriggers(InstanceUnit attacker, AttackSource source)
  2385. {
  2386. foreach (TriggerState ts in this.mTriggerStatus)
  2387. {
  2388. if (ts.IsDamageTrigger && ts.Test(source))
  2389. {
  2390. ts.Start(attacker);
  2391. }
  2392. }
  2393. }
  2394. /// <summary>
  2395. /// 如果攻击别人,尝试启动触发器
  2396. /// </summary>
  2397. /// <param name="target"></param>
  2398. /// <param name="source"></param>
  2399. public void TryStartAttackTriggers(InstanceUnit target, AttackSource source)
  2400. {
  2401. foreach (TriggerState ts in this.mTriggerStatus)
  2402. {
  2403. if (ts.IsAttackTrigger && ts.Test(source))
  2404. {
  2405. ts.Start(target);
  2406. }
  2407. }
  2408. }
  2409. public class UnitTriggerHelper
  2410. {
  2411. private readonly InstanceUnit unit;
  2412. public UnitTriggerHelper(InstanceUnit unit)
  2413. {
  2414. this.unit = unit;
  2415. unit.OnDamage += this.onUnitDamage;
  2416. unit.OnAttack += this.onUnitAttack;
  2417. }
  2418. public void onUnitDamage(InstanceUnit obj, InstanceUnit attacker, int hp, AttackSource source)
  2419. {
  2420. unit.TryStartDamageTriggers(attacker, source);
  2421. }
  2422. public void onUnitAttack(InstanceUnit obj, InstanceUnit target, int hp, AttackSource source)
  2423. {
  2424. unit.TryStartAttackTriggers(target, source);
  2425. }
  2426. }
  2427. #endregion
  2428. //---------------------------------------------------------------------------------------------------------------
  2429. #region _背包和道具_
  2430. public ClientStruct.UnitItemStatus[] GetCurrentItemStatus()
  2431. {
  2432. ClientStruct.UnitItemStatus[] ret = new ClientStruct.UnitItemStatus[mInventorySlots.Count];
  2433. int i = 0;
  2434. foreach (InventorySlot item in mInventorySlots)
  2435. {
  2436. ret[i].ItemTemplateID = item.ItemTemplateID;
  2437. ret[i].Count = item.Count;
  2438. i++;
  2439. }
  2440. return ret;
  2441. }
  2442. public class InventorySlot
  2443. {
  2444. readonly public int Index;
  2445. readonly public InstanceUnit Owner;
  2446. public InventorySlot(int index, InstanceUnit owner)
  2447. {
  2448. this.Index = index;
  2449. this.Owner = owner;
  2450. }
  2451. public bool IsEmpty
  2452. {
  2453. get
  2454. {
  2455. return (Item == null);
  2456. }
  2457. }
  2458. public int ItemTemplateID
  2459. {
  2460. get
  2461. {
  2462. if (Item != null)
  2463. return Item.TemplateID;
  2464. return 0;
  2465. }
  2466. }
  2467. public ItemTemplate Item { get; private set; }
  2468. public int Count { get; private set; }
  2469. internal int AddItem(ItemTemplate item, int count)
  2470. {
  2471. if (item == null) return 0;
  2472. if (count <= 0) return 0;
  2473. if (this.IsEmpty)
  2474. {
  2475. // 背包为空 //
  2476. this.Item = item;
  2477. this.Count = Math.Min(this.Item.MaxStackCount, count);
  2478. this.Owner.doGotInventoryItem(this, item, Index, count);
  2479. return this.Count;
  2480. }
  2481. else if (item.TemplateID == this.ItemTemplateID)
  2482. {
  2483. int oldcount = this.Count;
  2484. // 道具类型一致, 堆叠 //
  2485. this.Count = Math.Min(this.Item.MaxStackCount, this.Count + count);
  2486. if (oldcount != this.Count)
  2487. {
  2488. this.Owner.doGotInventoryItem(this, item, Index, count);
  2489. return this.Count - oldcount;
  2490. }
  2491. return 0;
  2492. }
  2493. return 0;
  2494. }
  2495. internal int TryUse(int count)
  2496. {
  2497. if (IsEmpty) return 0;
  2498. if (!Item.IsInventoryUseable) return 0;
  2499. if (Owner.tryUseItem(Item, Owner))
  2500. {
  2501. return Math.Min(count, this.Count);
  2502. }
  2503. return 0;
  2504. }
  2505. internal int Use(int count)
  2506. {
  2507. if (IsEmpty) return 0;
  2508. if (!Item.IsInventoryUseable) return 0;
  2509. if (Owner.tryUseItem(Item, Owner))
  2510. {
  2511. int used = 0;
  2512. count = Math.Min(count, this.Count);
  2513. for (int i = 0; i < count; i++)
  2514. {
  2515. Owner.UseItem(Item);
  2516. this.Count--;
  2517. used++;
  2518. if (Count == 0)
  2519. {
  2520. break;
  2521. }
  2522. }
  2523. this.Owner.doLostInventoryItem(this, Item, Index, count);
  2524. if (Count == 0)
  2525. {
  2526. this.Item = null;
  2527. }
  2528. return used;
  2529. }
  2530. return 0;
  2531. }
  2532. internal int Drop(int count)
  2533. {
  2534. if (IsEmpty) return 0;
  2535. count = Math.Min(count, this.Count);
  2536. this.Count -= count;
  2537. if (this.Count <= 0)
  2538. {
  2539. this.Count = 0;
  2540. }
  2541. this.Owner.doLostInventoryItem(this, Item, Index, count);
  2542. if (this.Count == 0)
  2543. {
  2544. this.Item = null;
  2545. }
  2546. return count;
  2547. }
  2548. internal int Clean()
  2549. {
  2550. return Drop(this.Count);
  2551. }
  2552. }
  2553. private List<InventorySlot> mInventorySlots = new List<InventorySlot>();
  2554. private HashMap<int, TimeExpire<ItemTemplate>> mCoolDownItems = new HashMap<int, TimeExpire<ItemTemplate>>();
  2555. private List<TimeExpire<ItemTemplate>> mRemoved = new List<TimeExpire<ItemTemplate>>();
  2556. private void InitBagSlots()
  2557. {
  2558. for (int i = 0; i < mInfo.InventorySize; i++)
  2559. {
  2560. mInventorySlots.Add(new InventorySlot(i, this));
  2561. }
  2562. }
  2563. private bool tryUseItem(ItemTemplate item, InstanceUnit item_creater)
  2564. {
  2565. if (mFormula.TryUseItem(this, item, item_creater))
  2566. {
  2567. if (item.UseCoolDownTimeMS > 0)
  2568. {
  2569. if (IsItemCoolDown(item.ID))
  2570. {
  2571. return false;
  2572. }
  2573. return true;
  2574. }
  2575. return true;
  2576. }
  2577. return false;
  2578. }
  2579. private void beginUseItem(ItemTemplate item)
  2580. {
  2581. if (item.UseCoolDownTimeMS > 0)
  2582. {
  2583. mCoolDownItems.Put(item.ID, new TimeExpire<ItemTemplate>(item, item.UseCoolDownTimeMS));
  2584. }
  2585. }
  2586. private void updateItems()
  2587. {
  2588. int intervalMS = Parent.UpdateIntervalMS;
  2589. /*
  2590. using (var removed = ListObjectPool<TimeExpire<ItemTemplate>>.AllocAutoRelease())
  2591. {
  2592. foreach (TimeExpire<ItemTemplate> expire in mCoolDownItems.Values)
  2593. {
  2594. if (expire.Update(intervalMS))
  2595. {
  2596. removed.Add(expire);
  2597. }
  2598. }
  2599. if (removed.Count > 0)
  2600. {
  2601. foreach (TimeExpire<ItemTemplate> expire in removed)
  2602. {
  2603. if (expire.IsEnd)
  2604. {
  2605. mCoolDownItems.RemoveByKey(expire.Tag.ID);
  2606. }
  2607. }
  2608. }
  2609. }*/
  2610. if(mCoolDownItems.Count <= 0)
  2611. {
  2612. return;
  2613. }
  2614. //换一个写法
  2615. mRemoved.Clear();
  2616. foreach (TimeExpire<ItemTemplate> expire in mCoolDownItems.Values)
  2617. {
  2618. if (expire.Update(intervalMS))
  2619. {
  2620. mRemoved.Add(expire);
  2621. }
  2622. }
  2623. if (mRemoved.Count > 0)
  2624. {
  2625. foreach (TimeExpire<ItemTemplate> expire in mRemoved)
  2626. {
  2627. if (expire.IsEnd)
  2628. {
  2629. mCoolDownItems.RemoveByKey(expire.Tag.ID);
  2630. }
  2631. }
  2632. mRemoved.Clear();
  2633. }
  2634. }
  2635. protected virtual void clearItemSlots()
  2636. {
  2637. foreach (var slot in mInventorySlots)
  2638. {
  2639. slot.Clean();
  2640. }
  2641. mInventorySlots.Clear();
  2642. mCoolDownItems.Clear();
  2643. }
  2644. /// <summary>
  2645. /// 检测此道具是否在CD
  2646. /// </summary>
  2647. /// <param name="itemTemplateID"></param>
  2648. /// <returns></returns>
  2649. public bool IsItemCoolDown(int itemTemplateID)
  2650. {
  2651. if (mCoolDownItems.ContainsKey(itemTemplateID))
  2652. {
  2653. return true;
  2654. }
  2655. return false;
  2656. }
  2657. /// <summary>
  2658. /// 添加道具到白空的背包
  2659. /// </summary>
  2660. /// <param name="item"></param>
  2661. /// <param name="count"></param>
  2662. /// <returns></returns>
  2663. public int AddItemToEmptyInventory(ItemTemplate item, int count = 1)
  2664. {
  2665. if (item == null) return 0;
  2666. if (count <= 0) return 0;
  2667. if (!item.IsDuplicateInventory || item.HoldingLimit > 0)
  2668. {
  2669. int holding = GetItemCountInInventory(item.ID);
  2670. if (!item.IsDuplicateInventory)
  2671. {
  2672. if (holding > 0)
  2673. {
  2674. return 0;
  2675. }
  2676. count = 1;
  2677. }
  2678. if (item.HoldingLimit > 0)
  2679. {
  2680. if (holding >= item.HoldingLimit)
  2681. {
  2682. return 0;
  2683. }
  2684. if (holding + count > item.HoldingLimit)
  2685. {
  2686. count = item.HoldingLimit - holding;
  2687. }
  2688. if (count <= 0) return 0;
  2689. }
  2690. }
  2691. int added = 0;
  2692. //优先堆叠//
  2693. for (int i = 0; i < mInventorySlots.Count; i++)
  2694. {
  2695. InventorySlot slot = mInventorySlots[i];
  2696. if (slot.ItemTemplateID == item.ID)
  2697. {
  2698. added += slot.AddItem(item, count - added);
  2699. if (added >= count)
  2700. {
  2701. break;
  2702. }
  2703. }
  2704. }
  2705. //使用空闲格子//
  2706. for (int i = 0; i < mInventorySlots.Count; i++)
  2707. {
  2708. InventorySlot slot = mInventorySlots[i];
  2709. added += slot.AddItem(item, count - added);
  2710. if (added >= count)
  2711. {
  2712. break;
  2713. }
  2714. }
  2715. return added;
  2716. }
  2717. /// <summary>
  2718. /// 添加道具到指定背包
  2719. /// </summary>
  2720. /// <param name="item"></param>
  2721. /// <param name="index"></param>
  2722. /// <param name="count"></param>
  2723. /// <returns></returns>
  2724. public int AddItemToInventory(ItemTemplate item, int index, int count = 1)
  2725. {
  2726. if (item == null) return 0;
  2727. if (count <= 0) return 0;
  2728. if (!item.IsDuplicateInventory || item.HoldingLimit > 0)
  2729. {
  2730. int holding = GetItemCountInInventory(item.ID);
  2731. if (!item.IsDuplicateInventory)
  2732. {
  2733. if (holding > 0)
  2734. {
  2735. return 0;
  2736. }
  2737. count = 1;
  2738. }
  2739. if (item.HoldingLimit > 0)
  2740. {
  2741. if (holding >= item.HoldingLimit)
  2742. {
  2743. return 0;
  2744. }
  2745. if (holding + count > item.HoldingLimit)
  2746. {
  2747. count = item.HoldingLimit - holding;
  2748. }
  2749. if (count <= 0) return 0;
  2750. }
  2751. }
  2752. if (index >= 0 && index < mInventorySlots.Count)
  2753. {
  2754. InventorySlot slot = mInventorySlots[index];
  2755. return slot.AddItem(item, count);
  2756. }
  2757. return 0;
  2758. }
  2759. /// <summary>
  2760. /// 添加道具到指定背包
  2761. /// </summary>
  2762. /// <param name="itemTemplateID"></param>
  2763. /// <param name="index"></param>
  2764. /// <param name="count"></param>
  2765. /// <returns></returns>
  2766. public int AddItemToInventory(int itemTemplateID, int index, int count = 1)
  2767. {
  2768. if (count <= 0) return 0;
  2769. return AddItemToInventory(Templates.getItem(itemTemplateID), index, count);
  2770. }
  2771. private int UseItemInternal(InventorySlot slot, int count)
  2772. {
  2773. if (slot.Item != null)
  2774. {
  2775. if (slot.Item.UseInProgressTimeMS > 0)
  2776. {
  2777. count = slot.TryUse(count);
  2778. if (count > 0)
  2779. {
  2780. this.startPickProgressSelf(slot.Item.UseInProgressTimeMS, (s, p) =>
  2781. {
  2782. slot.Use(count);
  2783. });
  2784. }
  2785. return count;
  2786. }
  2787. else
  2788. {
  2789. return slot.Use(count);
  2790. }
  2791. }
  2792. return 0;
  2793. }
  2794. /// <summary>
  2795. /// 使用背包的道具
  2796. /// </summary>
  2797. /// <param name="index"></param>
  2798. /// <param name="count"></param>
  2799. /// <returns></returns>
  2800. public int UseInventoryItem(int index, int count = 1)
  2801. {
  2802. if (count <= 0) return 0;
  2803. if (index >= 0 && index < mInventorySlots.Count)
  2804. {
  2805. InventorySlot slot = mInventorySlots[index];
  2806. return UseItemInternal(slot, count);
  2807. }
  2808. return 0;
  2809. }
  2810. public int UseInventoryItemByType(int itemTemplateID, int count = 1)
  2811. {
  2812. if (count <= 0) return 0;
  2813. for (int i = 0; i < mInventorySlots.Count; i++)
  2814. {
  2815. InventorySlot slot = mInventorySlots[i];
  2816. if (slot.ItemTemplateID == itemTemplateID)
  2817. {
  2818. return UseItemInternal(slot, count);
  2819. }
  2820. }
  2821. return 0;
  2822. }
  2823. public int DropInventoryItemByIndex(int index, int count = 1)
  2824. {
  2825. if (count <= 0) return 0;
  2826. if (index >= 0 && index < mInventorySlots.Count)
  2827. {
  2828. InventorySlot slot = mInventorySlots[index];
  2829. return slot.Drop(count);
  2830. }
  2831. return 0;
  2832. }
  2833. public int DropInventoryItemByType(int itemTemplateID, int count = 1)
  2834. {
  2835. if (count <= 0) return 0;
  2836. int droped = 0;
  2837. for (int i = 0; i < mInventorySlots.Count; i++)
  2838. {
  2839. InventorySlot slot = mInventorySlots[i];
  2840. if (slot.ItemTemplateID == itemTemplateID)
  2841. {
  2842. droped += slot.Drop(count);
  2843. if (droped >= count)
  2844. {
  2845. break;
  2846. }
  2847. }
  2848. }
  2849. return droped;
  2850. }
  2851. public int ClearInventoryItemByIndex(int index)
  2852. {
  2853. if (index >= 0 && index < mInventorySlots.Count)
  2854. {
  2855. InventorySlot slot = mInventorySlots[index];
  2856. return slot.Clean();
  2857. }
  2858. return 0;
  2859. }
  2860. public int ClearInventoryItemByType(int itemTemplateID)
  2861. {
  2862. int droped = 0;
  2863. for (int i = 0; i < mInventorySlots.Count; i++)
  2864. {
  2865. InventorySlot slot = mInventorySlots[i];
  2866. if (slot.ItemTemplateID == itemTemplateID)
  2867. {
  2868. droped += slot.Clean();
  2869. }
  2870. }
  2871. return droped;
  2872. }
  2873. /// <summary>
  2874. /// 背包内是否有此道具
  2875. /// </summary>
  2876. /// <param name="itemTemplateID"></param>
  2877. /// <returns></returns>
  2878. public bool ContainsItemInInventory(int itemTemplateID)
  2879. {
  2880. for (int i = 0; i < mInventorySlots.Count; i++)
  2881. {
  2882. InventorySlot slot = mInventorySlots[i];
  2883. if (slot.ItemTemplateID == itemTemplateID)
  2884. {
  2885. return true;
  2886. }
  2887. }
  2888. return false;
  2889. }
  2890. public int GetItemCountInInventory(int itemTemplateID)
  2891. {
  2892. int ret = 0;
  2893. for (int i = 0; i < mInventorySlots.Count; i++)
  2894. {
  2895. InventorySlot slot = mInventorySlots[i];
  2896. if (slot.ItemTemplateID == itemTemplateID)
  2897. {
  2898. ret += slot.Count;
  2899. }
  2900. }
  2901. return ret;
  2902. }
  2903. #endregion
  2904. //---------------------------------------------------------------------------------------------------------------
  2905. #region _物理运动_
  2906. public bool IsFallingDown
  2907. {
  2908. get
  2909. {
  2910. if (mCurrentFallingDown != null)
  2911. return !mCurrentFallingDown.IsEnd;
  2912. return false;
  2913. }
  2914. }
  2915. public bool IsDamageFallingDown
  2916. {
  2917. get
  2918. {
  2919. StateDamage damage = (this.CurrentState as StateDamage);
  2920. if (damage != null)
  2921. {
  2922. return damage.IsFallingDown;
  2923. }
  2924. return false;
  2925. }
  2926. }
  2927. public bool IsPhysicalMove
  2928. {
  2929. get
  2930. {
  2931. if (mCurrentStartMove != null)
  2932. return !mCurrentStartMove.IsEnd;
  2933. return false;
  2934. }
  2935. }
  2936. private FallingDown mCurrentFallingDown;
  2937. private HitMoveSpeed mCurrentStartMove;
  2938. private void updatePhysical()
  2939. {
  2940. if (mCurrentFallingDown != null && mCurrentFallingDown.Update(Parent.UpdateIntervalMS))
  2941. {
  2942. mCurrentFallingDown = null;
  2943. }
  2944. if (mCurrentStartMove != null && mCurrentStartMove.Update(Parent.UpdateIntervalMS))
  2945. {
  2946. mCurrentStartMove = null;
  2947. }
  2948. }
  2949. /// <summary>
  2950. /// 开始自由落体
  2951. /// </summary>
  2952. /// <param name="launcher"></param>
  2953. /// <param name="speedZ"></param>
  2954. /// <param name="gravity"></param>
  2955. /// <param name="zlimit"></param>
  2956. /// <returns></returns>
  2957. public virtual FallingDown StartFly(object launcher, float speedZ, float gravity, float zlimit)
  2958. {
  2959. mCurrentFallingDown = new FallingDown(this, speedZ, gravity, zlimit);
  2960. return mCurrentFallingDown;
  2961. }
  2962. /// <summary>
  2963. /// 开始复杂移动
  2964. /// </summary>
  2965. /// <param name="launcher"></param>
  2966. /// <param name="direction"></param>
  2967. /// <param name="rotateSpeedSEC"></param>
  2968. /// <param name="expectlTimeMS"></param>
  2969. /// <param name="moveSpeedSEC"></param>
  2970. /// <param name="moveSpeedAdd"></param>
  2971. /// <param name="moveSpeedAcc"></param>
  2972. /// <param name="isNoneTouch"></param>
  2973. public virtual HitMoveSpeed StartHitMove(
  2974. object launcher,
  2975. float direction,
  2976. float rotateSpeedSEC,
  2977. int expectlTimeMS,
  2978. float moveSpeedSEC,
  2979. float moveSpeedAdd,
  2980. float moveSpeedAcc,
  2981. bool isNoneTouch)
  2982. {
  2983. if (mCurrentStartMove != null)
  2984. {
  2985. mCurrentStartMove.Stop();
  2986. }
  2987. HitMoveSpeed move = new HitMoveSpeed(
  2988. this,
  2989. direction,
  2990. rotateSpeedSEC,
  2991. expectlTimeMS,
  2992. moveSpeedSEC,
  2993. moveSpeedAdd,
  2994. moveSpeedAcc,
  2995. isNoneTouch);
  2996. mCurrentStartMove = move;
  2997. return move;
  2998. }
  2999. /// <summary>
  3000. /// 落体运动
  3001. /// </summary>
  3002. public class FallingDown
  3003. {
  3004. private readonly InstanceUnit unit;
  3005. private readonly float gravity;
  3006. private readonly float z_limit;
  3007. private float z_speed;
  3008. public FallingDown(InstanceUnit unit, float zspeed, float zgravity, float zlimit)
  3009. {
  3010. this.unit = unit;
  3011. this.z_limit = zlimit;
  3012. this.z_speed = zspeed;
  3013. this.gravity = zgravity == 0 ? unit.Templates.CFG.GLOBAL_GRAVITY : zgravity;
  3014. this.ExpectTimeMS = MoveHelper.CalculateFlyTimeMS(unit.Z, zspeed, zlimit, gravity, 10);
  3015. this.IsEnd = false;
  3016. }
  3017. public bool Update(int intervalMS)
  3018. {
  3019. //向上受到ZLimit限制//
  3020. unit.Z += MoveHelper.GetDistance(intervalMS, z_speed);
  3021. unit.Z = Math.Max(0, unit.Z);
  3022. this.IsEnd = unit.Z <= 0;
  3023. //骤停//
  3024. if (z_limit != 0 && z_speed > 0 && unit.Z > z_limit)
  3025. {
  3026. z_speed = 0;
  3027. }
  3028. this.z_speed -= MoveHelper.GetDistance(intervalMS, gravity);
  3029. return IsEnd;
  3030. }
  3031. public bool IsEnd { get; private set; }
  3032. public int ExpectTimeMS { get; private set; }
  3033. public float ZSpeedSEC { get { return z_speed; } }
  3034. public float ZLimit { get { return z_limit; } }
  3035. }
  3036. public class HitMoveSpeed
  3037. {
  3038. private readonly InstanceUnit mOwner;
  3039. private readonly float mStartDirection;
  3040. private readonly float mMoveSpeedAdd;
  3041. private readonly float mMoveSpeedAcc;
  3042. private readonly float mRotateSpeedSEC;
  3043. private float moveSpeedSEC;
  3044. private TimeExpire<int> hitMoveTime;
  3045. private FallingDown hasFly;
  3046. private IPositionObject moveTarget;
  3047. private bool moveTargetBody;
  3048. private bool isNoneTouch = false;
  3049. public HitMoveSpeed(
  3050. InstanceUnit owner,
  3051. float direction,
  3052. float rotateSpeedSEC,
  3053. int expectlTimeMS,
  3054. float moveSpeedSEC,
  3055. float moveSpeedAdd,
  3056. float moveSpeedAccPct,
  3057. bool isNoneTouch)
  3058. {
  3059. this.mOwner = owner;
  3060. this.mStartDirection = direction;
  3061. this.moveSpeedSEC = moveSpeedSEC;
  3062. this.mMoveSpeedAdd = moveSpeedAdd;
  3063. this.mMoveSpeedAcc = moveSpeedAccPct / 100f;
  3064. this.mRotateSpeedSEC = rotateSpeedSEC;
  3065. this.isNoneTouch = isNoneTouch;
  3066. this.TotalTimeMS = expectlTimeMS;
  3067. this.hitMoveTime = new TimeExpire<int>(TotalTimeMS);
  3068. this.IsEnd = false;
  3069. this.PrevX = owner.X;
  3070. this.PrevY = owner.Y;
  3071. }
  3072. public void SetFly(object launcher, float moveZSpeed, float gravity, float zlimit)
  3073. {
  3074. if (moveZSpeed != 0)
  3075. {
  3076. this.hasFly = mOwner.StartFly(launcher, moveZSpeed, gravity, zlimit);
  3077. this.TotalTimeMS = hasFly.ExpectTimeMS;
  3078. this.hitMoveTime = new TimeExpire<int>(TotalTimeMS);
  3079. }
  3080. }
  3081. public void SetMoveTarget(IPositionObject target, bool targetBodyBlock)
  3082. {
  3083. moveTarget = target;
  3084. moveTargetBody = targetBodyBlock;
  3085. }
  3086. public void Stop()
  3087. {
  3088. this.IsEnd = true;
  3089. }
  3090. public bool Update(int intervalMS)
  3091. {
  3092. int intervalMS_move = intervalMS;
  3093. if (hitMoveTime.PassTimeMS + intervalMS >= hitMoveTime.TotalTimeMS)
  3094. {
  3095. intervalMS_move = hitMoveTime.TotalTimeMS - hitMoveTime.PassTimeMS;
  3096. }
  3097. if (mRotateSpeedSEC != 0)
  3098. {
  3099. mOwner.turn(MoveHelper.GetDistance(intervalMS_move, mRotateSpeedSEC));
  3100. }
  3101. this.PrevX = mOwner.X;
  3102. this.PrevY = mOwner.Y;
  3103. // 移动 //
  3104. if (moveTarget != null)
  3105. {
  3106. move(intervalMS_move, moveTarget);
  3107. }
  3108. else
  3109. {
  3110. move(intervalMS_move, mStartDirection);
  3111. }
  3112. // 递增 //
  3113. {
  3114. //每秒递减速度绝对值//
  3115. MoveHelper.UpdateSpeed(intervalMS_move, ref moveSpeedSEC, mMoveSpeedAdd, mMoveSpeedAcc);
  3116. }
  3117. if (hitMoveTime.PassTimeMS + intervalMS >= hitMoveTime.TotalTimeMS || hitMoveTime.Update(intervalMS))
  3118. {
  3119. IsEnd = true;
  3120. }
  3121. return IsEnd;
  3122. }
  3123. private void move(int intervalMS, IPositionObject target)
  3124. {
  3125. if (moveTargetBody)
  3126. {
  3127. float distance = MoveHelper.GetDistance(intervalMS, moveSpeedSEC);
  3128. if (CMath.includeRoundPoint(mOwner.X, mOwner.Y, target.RadiusSize + mOwner.RadiusSize + distance, target.X, target.Y))
  3129. {
  3130. return;
  3131. }
  3132. }
  3133. if (hasFly != null)
  3134. {
  3135. mOwner.jumpToTarget(target.X, target.Y, moveSpeedSEC, intervalMS, IsNoneTouch);
  3136. }
  3137. else
  3138. {
  3139. if (IsNoneTouch)
  3140. {
  3141. mOwner.jumpToTarget(target.X, target.Y, moveSpeedSEC, intervalMS, IsNoneTouch);
  3142. }
  3143. else if (mMoveSpeedAdd != 0 || mMoveSpeedAcc != 0)
  3144. {
  3145. mOwner.moveImpactTo(target.X, target.Y, moveSpeedSEC, intervalMS);
  3146. }
  3147. else
  3148. {
  3149. mOwner.moveBlockTo(target.X, target.Y, moveSpeedSEC, intervalMS);
  3150. }
  3151. }
  3152. }
  3153. private void move(int intervalMS, float direction)
  3154. {
  3155. if (hasFly != null)
  3156. {
  3157. mOwner.jumpTo(direction, moveSpeedSEC, intervalMS, IsNoneTouch);
  3158. }
  3159. else
  3160. {
  3161. if (IsNoneTouch)
  3162. {
  3163. mOwner.jumpTo(direction, moveSpeedSEC, intervalMS, IsNoneTouch);
  3164. }
  3165. else if (mMoveSpeedAdd != 0 || mMoveSpeedAcc != 0)
  3166. {
  3167. mOwner.moveImpact(direction, moveSpeedSEC, intervalMS);
  3168. }
  3169. else
  3170. {
  3171. mOwner.moveBlockTo(direction, moveSpeedSEC, intervalMS);
  3172. }
  3173. }
  3174. }
  3175. public float PrevX { get; private set; }
  3176. public float PrevY { get; private set; }
  3177. public bool IsFly { get { return hasFly != null; } }
  3178. public int TotalTimeMS { get; private set; }
  3179. public bool IsEnd { get; private set; }
  3180. public bool IsNoneTouch
  3181. {
  3182. get { return isNoneTouch; }
  3183. set { this.isNoneTouch = value; }
  3184. }
  3185. public float ZSpeedSEC
  3186. {
  3187. get
  3188. {
  3189. if (hasFly != null)
  3190. return hasFly.ZSpeedSEC;
  3191. return 0;
  3192. }
  3193. }
  3194. }
  3195. #endregion
  3196. //---------------------------------------------------------------------------------------------------------------
  3197. }
  3198. }