InstanceUnit.Status.cs 113 KB

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