InstanceUnit.Status.cs 113 KB

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