InstanceUnit.Status.cs 113 KB

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