InstanceUnit.StateMachine.cs 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonAI.RTS;
  5. using CommonLang.Vector;
  6. using CommonAI.Zone;
  7. using CommonAI.Zone.Instance;
  8. using CommonAI.RTS.Manhattan;
  9. using CommonLang;
  10. using CommonLang.Log;
  11. using CommonAI.Zone.Formula;
  12. using CommonAI.ZoneClient;
  13. using CommonAI.Zone.Helper;
  14. using CommonAI.Zone.Attributes;
  15. using CommonLang.Property;
  16. using static CommonAI.Zone.AttackProp;
  17. using CommonAI.Data;
  18. namespace CommonAI.Zone.Instance
  19. {
  20. partial class InstanceUnit
  21. {
  22. //-----------------------------------------------------------------------------------------------------//
  23. #region SEND_EVENT
  24. private UnitForceSyncPosEvent mForceSync = new UnitForceSyncPosEvent();
  25. private UnitActionStatus mCurrentActionMainState = UnitActionStatus.Idle;
  26. //子状态状态
  27. private int mCurrentActionSubState = 0;
  28. //子状态扩展信息
  29. private short mSubStateExt = 0;
  30. private UnitActionStatus mLastActionStatus = 0;
  31. private int mLastActionSubstate = 0;
  32. //蓄力状态
  33. //private byte chargeAtkStatus = 0;
  34. /// <summary>
  35. /// 当前动做主状态
  36. /// </summary>
  37. public UnitActionStatus CurrentActionStatus
  38. {
  39. get { return mCurrentActionMainState; }
  40. }
  41. /// <summary>
  42. /// 当前动做子状态, 0-正常,1-隐身
  43. /// </summary>
  44. public int CurrentActionSubstate
  45. {
  46. get { return mCurrentActionSubState; }
  47. }
  48. //蓄力状态
  49. public byte ChargeAtkStatus { get; set; }
  50. public bool IsActionStatusChanged
  51. {
  52. get; private set;
  53. }
  54. public void SetActionStatus(UnitActionStatus st)
  55. {
  56. if (st == UnitActionStatus.Move && !this.Moveable)
  57. {
  58. // nothing //
  59. }
  60. else
  61. {
  62. this.mCurrentActionMainState = st;
  63. }
  64. }
  65. public void AddActionSubState(UnitActionSubStatus substate, short ext = 0)
  66. {
  67. this.mCurrentActionSubState = this.mCurrentActionSubState | (int)substate;
  68. this.mSubStateExt = ext;
  69. // 特殊状态额外信息通知
  70. if (this.IsPlayer && substate == UnitActionSubStatus.Stealth)
  71. {
  72. UnitStealthInfo stealInfo = new UnitStealthInfo(this.ID, 1, (short)ext);
  73. this.queueEvent(stealInfo);
  74. }
  75. //Console.WriteLine("add - sub - " + this.mCurrentActionSubState);
  76. }
  77. public void RemoveActionSubState(UnitActionSubStatus substate)
  78. {
  79. //有这个状态,才能进行移除
  80. if (this.IsHasSubState(substate))
  81. {
  82. this.mCurrentActionSubState = this.mCurrentActionSubState ^ (int)substate;
  83. // 特殊状态额外信息通知
  84. if (this.IsPlayer && substate == UnitActionSubStatus.Stealth)
  85. {
  86. UnitStealthInfo stealInfo = new UnitStealthInfo(this.ID, 0, 0);
  87. this.queueEvent(stealInfo);
  88. }
  89. }
  90. //Console.WriteLine("remove - sub - " + this.mCurrentActionSubState);
  91. }
  92. //是否拥有该子状态
  93. public bool IsHasSubState(UnitActionSubStatus state)
  94. {
  95. return ((this.mCurrentActionSubState & (int)state) > 0);
  96. }
  97. public void queueEvent(ObjectEvent evt, bool force = false)
  98. {
  99. Parent.queueObjectEvent(this, evt, force);
  100. }
  101. public void queueEvent(Event evt)
  102. {
  103. Parent.queueEventInternal(evt);
  104. }
  105. public virtual bool DoAutoPick(bool ignoreInterval = false)
  106. {
  107. return false;
  108. }
  109. //通知拾取消息
  110. public virtual void NotifyRemoveDropItemB2C(InstanceItem item)
  111. {
  112. }
  113. public override void SendForceSync()
  114. {
  115. mForceSync.object_id = this.ID;
  116. mForceSync.X = this.X;
  117. mForceSync.Y = this.Y;
  118. mForceSync.Direction = this.Direction;
  119. mForceSync.UnitMainState = (byte)mCurrentActionMainState;
  120. this.mLastActionStatus = mCurrentActionMainState;
  121. mForceSync.UnitSubState = (byte)mCurrentActionSubState;
  122. Parent.queueObjectEvent(this, mForceSync);
  123. //Console.WriteLine("--------SendForceSync x:" + mForceSync.X + ", y : " + mForceSync.Y);
  124. }
  125. internal override bool updatePos(InstanceZone zone)
  126. {
  127. this.IsActionStatusChanged = (mLastActionStatus != mCurrentActionMainState) || (mLastActionSubstate != mCurrentActionSubState);
  128. this.mLastActionStatus = mCurrentActionMainState;
  129. this.mLastActionSubstate = mCurrentActionSubState;
  130. if (base.updatePos(zone) || IsActionStatusChanged)
  131. {
  132. return true;
  133. }
  134. return false;
  135. }
  136. protected internal virtual bool GenSyncState(ref SyncPosEvent.UnitState ret)
  137. {
  138. if (IsActionStatusChanged)
  139. {
  140. ret.SetObject(this);
  141. return true;
  142. }
  143. return false;
  144. }
  145. public SyncPosEvent.UnitState GetSyncState()
  146. {
  147. SyncPosEvent.UnitState ret = new SyncPosEvent.UnitState();
  148. ret.SetObject(this);
  149. return ret;
  150. }
  151. //-------------------------------------------------------------------------------------
  152. #endregion
  153. //-----------------------------------------------------------------------------------------------------//
  154. public abstract class State : IDisposable
  155. {
  156. private bool m_started = false;
  157. public InstanceUnit unit { get; private set; }
  158. public InstanceZone zone { get { return unit.Parent; } }
  159. public bool IsStarted { get { return m_started; } }
  160. public State(InstanceUnit unit)
  161. {
  162. this.unit = unit;
  163. }
  164. internal void start()
  165. {
  166. if (!m_started)
  167. {
  168. m_started = true;
  169. onStart();
  170. if (this.mOnStart != null)
  171. {
  172. this.mOnStart(unit, this);
  173. }
  174. }
  175. }
  176. internal void update()
  177. {
  178. onUpdate();
  179. }
  180. internal void stop()
  181. {
  182. if (m_started)
  183. {
  184. m_started = false;
  185. onStop();
  186. if (this.mStopOnce != null)
  187. {
  188. var invoke = mStopOnce;
  189. this.mStopOnce = null;
  190. invoke.Invoke(unit, this);
  191. }
  192. if (this.mOnStop != null)
  193. {
  194. this.mOnStop.Invoke(unit, this);
  195. }
  196. }
  197. }
  198. public virtual void MarkRemove() { }
  199. /// 当前状态是否可以被新状态打断
  200. public abstract bool onBlock(State new_state);
  201. protected abstract void onStart();
  202. protected abstract void onUpdate();
  203. protected abstract void onStop();
  204. public virtual void Dispose()
  205. {
  206. mOnStart = null;
  207. mOnStop = null;
  208. mStopOnce = null;
  209. }
  210. public delegate void StateStartHandler(InstanceUnit obj, State state);
  211. public delegate void StateStopHandler(InstanceUnit obj, State state);
  212. private StateStartHandler mOnStart;
  213. private StateStopHandler mOnStop;
  214. private StateStopHandler mStopOnce;
  215. [EventTriggerDescAttribute("状态机每次开始时触发")]
  216. public event StateStartHandler OnStart { add { mOnStart += value; } remove { mOnStart -= value; } }
  217. [EventTriggerDescAttribute("状态机每次结束时触发")]
  218. public event StateStopHandler OnStop { add { mOnStop += value; } remove { mOnStop -= value; } }
  219. [DescAttribute("状态机结束时触发,停止后自动清理所有监听")]
  220. public void AddStopOnce(StateStopHandler stop)
  221. {
  222. mStopOnce += stop;
  223. }
  224. }
  225. //-----------------------------------------------------------------------------------------------------//
  226. public class StateSpawn : State
  227. {
  228. private TimeExpire<int> timer;
  229. public StateSpawn(InstanceUnit unit, int timeMS)
  230. : base(unit)
  231. {
  232. timer = new TimeExpire<int>(timeMS);
  233. }
  234. override public bool onBlock(State new_state)
  235. {
  236. if(new_state is StateDuJie)
  237. {
  238. return true;
  239. }
  240. return timer.IsEnd;
  241. }
  242. override protected void onStart()
  243. {
  244. unit.SetActionStatus(UnitActionStatus.Spawn);
  245. }
  246. override protected void onUpdate()
  247. {
  248. if (timer.Update(zone.UpdateIntervalMS))
  249. {
  250. unit.doSomething();
  251. }
  252. }
  253. override protected void onStop()
  254. {
  255. unit.doActivated();
  256. }
  257. }
  258. //-----------------------------------------------------------------------------------------------------//
  259. /// <summary>
  260. /// 待机状态
  261. /// </summary>
  262. public class StateIdle : State
  263. {
  264. public StateIdle(InstanceUnit unit)
  265. : base(unit)
  266. {
  267. }
  268. override public bool onBlock(State new_state)
  269. {
  270. if (new_state is StateIdle)
  271. {
  272. return false;
  273. }
  274. return true;
  275. }
  276. override protected void onStart()
  277. {
  278. unit.SetActionStatus(UnitActionStatus.Idle);
  279. }
  280. override protected void onUpdate()
  281. {
  282. unit.doSomething();
  283. //unit.SetActionStatus(UnitActionStatus.Idle);
  284. }
  285. override protected void onStop()
  286. {
  287. }
  288. }
  289. /// <summary>
  290. ///
  291. /// </summary>
  292. public class StateIdleMove : State
  293. {
  294. private readonly TimeExpire<int> mExpire;
  295. private readonly MoveAI mMoveAI;
  296. private readonly Vector2 mOrginPos;
  297. private readonly float mRange;
  298. private MoveBlockResult mLastResult;
  299. public MoveBlockResult LastMoveResult { get { return mLastResult; } }
  300. public StateIdleMove(InstanceUnit unit, Vector2 orginPos, int timeMS, float range)
  301. : base(unit)
  302. {
  303. this.mExpire = new TimeExpire<int>(timeMS);
  304. this.mMoveAI = new MoveAI(unit, false);
  305. this.mRange = Math.Abs(range);
  306. this.mOrginPos = orginPos;
  307. }
  308. public override bool onBlock(State new_state)
  309. {
  310. return true;
  311. }
  312. protected override void onStart()
  313. {
  314. Vector2 target = this.FindTargetPos();
  315. this.mMoveAI.FindPath(target.X, target.Y);
  316. unit.SetActionStatus(UnitActionStatus.Move);
  317. }
  318. protected override void onStop()
  319. {
  320. }
  321. protected override void onUpdate()
  322. {
  323. if (mExpire.Update(zone.UpdateIntervalMS))
  324. {
  325. unit.doSomething();
  326. }
  327. else
  328. {
  329. mLastResult = mMoveAI.Update();
  330. if ((mLastResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  331. {
  332. Vector2 target = this.FindTargetPos();
  333. this.mMoveAI.FindPath(target.X, target.Y);
  334. }
  335. }
  336. }
  337. /// <summary>
  338. /// 搜索要去的地方
  339. /// </summary>
  340. /// <returns></returns>
  341. protected virtual Vector2 FindTargetPos()
  342. {
  343. var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, mOrginPos.X, mOrginPos.Y, mRange);
  344. if (node != null)
  345. {
  346. return new Vector2(node.PosX, node.PosY);
  347. }
  348. return new Vector2(mOrginPos.X, mOrginPos.Y);
  349. }
  350. }
  351. /// <summary>
  352. /// 移动状态
  353. /// </summary>
  354. public class StateMove : State
  355. {
  356. private Vector2 target;
  357. private int minStepTime;
  358. private bool moveEnd = false;
  359. private MoveBlockResult mLastMoveResult;
  360. public Vector2 Target { get { return target; } }
  361. public bool IsMoveEnd { get { return moveEnd; } }
  362. public MoveBlockResult LastMoveResult { get { return mLastMoveResult; } }
  363. public int MinStepCheckCount { get; set; }
  364. public bool StopOnTouchMap { get; set; }
  365. public Predicate<StateMove> EndMoveAction { get; set; }
  366. public StateMove(InstanceUnit unit, float x, float y)
  367. : base(unit)
  368. {
  369. this.StopOnTouchMap = false;
  370. this.MinStepCheckCount = 10;
  371. this.target = new Vector2(x, y);
  372. }
  373. override public bool onBlock(State new_state)
  374. {
  375. return true;
  376. }
  377. override protected void onStart()
  378. {
  379. unit.SetActionStatus(UnitActionStatus.Move);
  380. }
  381. override protected void onUpdate()
  382. {
  383. unit.SetActionStatus(UnitActionStatus.Move);
  384. unit.faceTo(target.X, target.Y);
  385. mLastMoveResult = unit.moveBlockTo(target.X, target.Y, unit.MoveSpeedSEC, zone.UpdateIntervalMS);
  386. if ((mLastMoveResult.result & MoveResult.MOVE_RESULT_BLOCK_MAP) != 0 && StopOnTouchMap)
  387. {
  388. moveEnd = true;
  389. if (EndMoveAction == null || !EndMoveAction(this))
  390. {
  391. unit.doSomething();
  392. }
  393. }
  394. else if ((mLastMoveResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  395. {
  396. moveEnd = true;
  397. if (EndMoveAction == null || !EndMoveAction(this))
  398. {
  399. unit.doSomething();
  400. }
  401. }
  402. else if ((mLastMoveResult.result & MoveResult.MOVE_RESULT_MIN_STEP) != 0)
  403. {
  404. moveEnd = true;
  405. minStepTime++;
  406. if (minStepTime > MinStepCheckCount)
  407. {
  408. if (EndMoveAction == null || !EndMoveAction(this))
  409. {
  410. unit.doSomething();
  411. }
  412. }
  413. }
  414. else
  415. {
  416. minStepTime = 0;
  417. }
  418. }
  419. override protected void onStop()
  420. {
  421. }
  422. }
  423. /// <summary>
  424. /// 释放技能状态
  425. /// </summary>
  426. public class StateSkill : State
  427. {
  428. readonly private InstanceUnit.SkillState skill;
  429. readonly private SkillLaunched launched;
  430. readonly private AttackRangeHelper attack_range;
  431. private Queue<UnitActionData> action_queue;
  432. private LaunchSkillParam param;
  433. private InstanceUnit targetUnit;
  434. private bool is_done = false;
  435. private UnitActionData current_action = null;
  436. private int curActionTotalTimes = 0;
  437. private PopupKeyFrames<UnitActionData.KeyFrame> current_frames = new PopupKeyFrames<UnitActionData.KeyFrame>();
  438. private BitSet8 current_action_status = new BitSet8();
  439. private int current_pass_time = 0;
  440. private Vector2 move_to;
  441. private HitMoveSpeed start_move;
  442. private Vector2 body_hited_last_pos = new Vector2();
  443. private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
  444. private TimeExpire<int> chantExpire;
  445. private AstarManhattan.MWayPoint move_to_target_path;
  446. private float jump_to_target_speed_sec;
  447. private TVector2 jump_to_target_pos;
  448. // 技能加速,攻速
  449. private float mSkillSpeed = 0.0f;
  450. public InstanceUnit.SkillState Skill { get { return skill; } }
  451. public SkillTemplate SkillData { get { return skill.Data; } }
  452. public InstanceUnit TargetUnit { get { return targetUnit; } }
  453. public Queue<UnitActionData> ActionQqueue { get { return action_queue; } }
  454. public bool IsControlMoveable { get { return current_action_status.Get(0); } private set { current_action_status.Set(0, value); } }
  455. public bool IsControlFaceable { get { return current_action_status.Get(1); } private set { current_action_status.Set(1, value); } }
  456. public bool IsCancelableBySkill { get { return current_action_status.Get(2); } private set { current_action_status.Set(2, value); } }
  457. public bool IsCancelableByMove { get { return current_action_status.Get(3); } private set { current_action_status.Set(3, value); } }
  458. public bool IsNoneBlock { get { return current_action_status.Get(4); } private set { current_action_status.Set(4, value); } }
  459. public bool IsNoneTouch { get { return current_action_status.Get(5); } private set { current_action_status.Set(5, value); } }
  460. public bool IsFaceToTarget { get { return current_action_status.Get(6); } private set { current_action_status.Set(6, value); } }
  461. public Vector2 GetMoveToPos() { return this.move_to; }
  462. // public bool IsCancelableBySkill { get; private set; }
  463. // public bool IsCancelableByMove { get; private set; }
  464. // public bool IsNoneBlock { get; private set; }
  465. // public bool IsNoneTouch { get; private set; }
  466. // public bool IsFaceToTarget { get; private set; }
  467. // public bool IsControlMoveable { get; private set; }
  468. // public bool IsControlFaceable { get; private set; }
  469. /// <summary>
  470. /// 技能是否完结
  471. /// </summary>
  472. public bool IsDone { get { return is_done; } }
  473. /// <summary>
  474. /// 是否在吟唱中
  475. /// </summary>
  476. public bool IsChanting { get { return chantExpire != null; } }
  477. /// <summary>
  478. /// 动作加速因子
  479. /// </summary>
  480. public float ActionSpeed { get; private set; }
  481. /// <summary>
  482. /// 技能释放结束后的朝向
  483. /// </summary>
  484. public Vector2 StopFaceTo { set; get; }
  485. /// <summary>
  486. /// 动作总时间
  487. /// </summary>
  488. public int ActionTotalTimeMS { get; private set; }
  489. public int CurrentActionIndex
  490. {
  491. get
  492. {
  493. if (current_action != null)
  494. {
  495. return SkillData.ActionQueue.IndexOf(current_action);
  496. }
  497. return 0;
  498. }
  499. }
  500. public StateSkill(InstanceUnit unit, InstanceUnit.SkillState skill, LaunchSkillParam param, float actionSpeed, SkillLaunched skillLaunched)
  501. : base(unit)
  502. {
  503. this.skill = skill;
  504. this.param = param;
  505. this.ActionSpeed = actionSpeed;
  506. this.ActionTotalTimeMS = skill.Data.ActionQueueTimeMS;
  507. this.launched = skillLaunched;
  508. this.targetUnit = zone.getUnit(param.TargetUnitID);
  509. this.attack_range = new AttackRangeHelper(unit);
  510. //if (SkillData.ChantTimeMS > 0)
  511. //{
  512. // this.chantExpire = new TimeExpire<int>(SkillData.ChantTimeMS);
  513. // this.IsCancelableByMove = true;
  514. //}
  515. //this.beginLaunch();
  516. }
  517. /// <summary>
  518. /// 释放前判断
  519. /// </summary>
  520. /// <returns></returns>
  521. public bool tryLaunch()
  522. {
  523. if (SkillData.ActionQueue.Count > 0)
  524. {
  525. if (!checkTargetRange())
  526. {
  527. return false;
  528. }
  529. if (!checkMoveToTarget(SkillData.ActionQueue[0]))
  530. {
  531. return false;
  532. }
  533. return true;
  534. }
  535. return false;
  536. }
  537. public bool CanBlockByNewSkill(SkillTemplate sk)
  538. {
  539. // 反击
  540. if (sk.IsCounter)
  541. {
  542. return true;
  543. }
  544. // 高优先级技能打断老技能, 0普通,-1标识位移技能
  545. if (sk.ActionPriority > 0 && sk.ActionPriority > this.SkillData.ActionPriority)
  546. {
  547. return true;
  548. }
  549. // 当前动作是否可被打断
  550. if (this.IsCancelableBySkill)
  551. {
  552. return true;
  553. }
  554. return false;
  555. }
  556. override public bool onBlock(State new_state)
  557. {
  558. // 吟唱中 //
  559. if (IsChanting)
  560. {
  561. return true;
  562. }
  563. // 技能完结 //
  564. if (is_done)
  565. {
  566. return true;
  567. }
  568. // 被死亡或控制类技能中断 //
  569. if (new_state is StateDead || new_state is StateStun)
  570. {
  571. skill.Reset();
  572. return true;
  573. }
  574. // 如果死亡则受击 //
  575. else if (new_state is StateDamage)
  576. {
  577. if (IsNoneBlock)
  578. {
  579. return false;
  580. }
  581. skill.ForceIntoCD();
  582. return true;
  583. }
  584. else if (new_state is StateSkill)
  585. {
  586. StateSkill ns = new_state as StateSkill;
  587. if (CanBlockByNewSkill(ns.SkillData))
  588. {
  589. return true;
  590. }
  591. else if (SkillData.ID == ns.SkillData.ID && SkillData.ActionQueue.Count > CurrentActionIndex)
  592. {
  593. if (SkillData.ActionQueue[this.CurrentActionIndex].IsCancelBySkillNext)
  594. {
  595. return true;
  596. }
  597. }
  598. }
  599. else
  600. {
  601. // 当前动作是否可被打断 //
  602. if (this.IsCancelableByMove)
  603. {
  604. return true;
  605. }
  606. }
  607. return false;
  608. }
  609. override protected void onStart()
  610. {
  611. unit.SetActionStatus(UnitActionStatus.Skill);
  612. if (IsChanting)
  613. {
  614. unit.queueEvent(new UnitChantSkillEvent(unit.ID, SkillData));
  615. }
  616. else
  617. {
  618. beginLaunch();
  619. }
  620. }
  621. override protected void onStop()
  622. {
  623. move_to_target_path = null;
  624. if (start_move != null)
  625. {
  626. start_move.Stop();
  627. start_move = null;
  628. }
  629. unit.Z = 0;
  630. is_done = true;
  631. if (!IsChanting)
  632. {
  633. if (skill != null)
  634. {
  635. skill.Stop(this);
  636. }
  637. if (unit.IsSkillControllableByServer)
  638. {
  639. if (StopFaceTo != null)
  640. {
  641. //unit.Direction = MathVector.getDegree(StopFaceTo);
  642. unit.faceTo(StopFaceTo.X, StopFaceTo.Y);
  643. }
  644. }
  645. }
  646. PlayerSkillStopEvent evt = new PlayerSkillStopEvent(unit.ID, skill.ID);
  647. unit.queueEvent(evt);
  648. unit.Virtual.DispatchSkillBlockEvent(this, null);
  649. }
  650. override protected void onUpdate()
  651. {
  652. if (chantExpire != null)
  653. {
  654. if (!checkTargetRange())
  655. {
  656. is_done = true;
  657. unit.doSomething();
  658. return;
  659. }
  660. if (chantExpire.Update(zone.UpdateIntervalMS))
  661. {
  662. beginLaunch();
  663. chantExpire = null;
  664. }
  665. }
  666. if (IsChanting) return;
  667. if (is_done) return;
  668. this.current_pass_time += (int)(zone.UpdateIntervalMS * ActionSpeed);
  669. if (current_action == null)
  670. {
  671. nextAction();
  672. }
  673. if (current_action == null)
  674. {
  675. is_done = true;
  676. unit.doSomething();
  677. }
  678. else
  679. {
  680. body_hited_last_pos.SetX(unit.X);
  681. body_hited_last_pos.SetY(unit.Y);
  682. if (IsFaceToTarget)
  683. {
  684. float tx = 0;
  685. float ty = 0;
  686. if (getTargetPos(out tx, out ty))
  687. {
  688. unit.faceTo(tx, ty);
  689. unit.SendForceSync();
  690. }
  691. }
  692. // 关键帧 //
  693. List<UnitActionData.KeyFrame> kfs = new List<UnitActionData.KeyFrame>();
  694. // 技能加速
  695. int timePassVirtual = current_pass_time;
  696. if (this.Skill.GetSkillType() == XmdsSkillType.normalAtk && this.mSkillSpeed != 0)
  697. {
  698. timePassVirtual = (int)(timePassVirtual * this.mSkillSpeed);
  699. }
  700. if (current_frames.PopKeyFrames(timePassVirtual, kfs) > 0)
  701. {
  702. for (int i = 0; i < kfs.Count; i++)
  703. {
  704. doKeyFrame(kfs[i]);
  705. }
  706. }
  707. if (current_action != null)
  708. {
  709. var action = current_action;
  710. // 技能位移 //
  711. // 移动到目标时,不切换动作 //
  712. if (action.IsMoveToTarget)
  713. {
  714. // 冲到目标,立即下段 //
  715. doMoveToTarget(action);
  716. }
  717. else if (action.IsJumpToTarget)
  718. {
  719. doJumpToTarget(action);
  720. }
  721. else
  722. {
  723. if (unit.IsSkillControllableByServer)
  724. {
  725. if (start_move != null)
  726. {
  727. doMove();
  728. }
  729. if (move_to != null)
  730. {
  731. if (IsControlMoveable && !unit.IsCannotMove)
  732. {
  733. unit.moveImpactTo(move_to.X, move_to.Y, unit.MoveSpeedSEC, zone.UpdateIntervalMS);
  734. }
  735. else
  736. {
  737. move_to = null;
  738. }
  739. }
  740. }
  741. // 身体攻击 //
  742. if (action.BodyHit != null)
  743. {
  744. doBodyHit(action);
  745. }
  746. // 防止技能位移导致单位重合 //
  747. if (action.BodyBlockOnAttackRange)
  748. {
  749. doBodyBlock(action);
  750. }
  751. if (IsFaceToTarget)
  752. {
  753. float tx = 0;
  754. float ty = 0;
  755. if (getTargetPos(out tx, out ty))
  756. {
  757. unit.faceTo(tx, ty);
  758. unit.SendForceSync();
  759. }
  760. }
  761. }
  762. }
  763. //ADD 6/21 By Zou 加入自动战斗取消后摇
  764. if (current_action != null)
  765. {
  766. var isFinish = current_pass_time >= this.curActionTotalTimes;
  767. if (!isFinish && (IsCancelableBySkill) && unit is InstancePlayer)
  768. {
  769. isFinish = (unit as InstancePlayer).IsGuard;
  770. }
  771. if (isFinish && !nextAction())
  772. {
  773. is_done = true;
  774. unit.doSomething();
  775. }
  776. }
  777. }
  778. }
  779. private void beginLaunch()
  780. {
  781. //是否使用多段中字段时间作为cd
  782. bool isUseActionQueuetime = false;
  783. int launchIndex = 0;
  784. skill.setTriggerDecreaseTime(0);
  785. skill.Launch(this, param);
  786. {
  787. launchIndex = skill.ActionIndex;
  788. int maxCount = skill.GetMaxSteps();// skill.Data.ActionQueue.Count;
  789. if (skill.Data.IsSingleAction)
  790. {
  791. if (launchIndex > 0 && launchIndex < skill.Data.ActionQueue.Count
  792. //&& skill.Data.ActionQueue[actionIndex].SigleActionType == ActionEnum.forbidNext_lock)
  793. && !skill.IsCanLaunchNextStepByPlayer(skill.Data.ActionQueue[launchIndex]))
  794. {
  795. skill.Reset();
  796. }
  797. this.action_queue = new Queue<UnitActionData>(1);
  798. if (0 <= skill.LockActionStep && skill.LockActionStep <= maxCount)
  799. {
  800. this.action_queue = new Queue<UnitActionData>(1);
  801. launchIndex = skill.LockActionStep;
  802. skill.Reset();
  803. this.action_queue.Enqueue(skill.Data.ActionQueue[skill.LockActionStep]);
  804. }
  805. else
  806. {
  807. skill.LockActionStep = -1;
  808. for (int i = launchIndex; i <= maxCount; i++)
  809. {
  810. UnitActionData data = skill.Data.ActionQueue[i];
  811. this.action_queue.Enqueue(data);
  812. if (data.SigleActionType == ActionEnum.chargeAtk
  813. || data.SigleActionType == ActionEnum.showLeftTime)
  814. {
  815. isUseActionQueuetime = true;
  816. }
  817. else if (data.SigleActionType == ActionEnum.forbidNext_showTime)
  818. {
  819. isUseActionQueuetime = true;
  820. break;
  821. }
  822. else if (data.SigleActionType == ActionEnum.IsAutoNext)
  823. {
  824. continue;
  825. }
  826. else
  827. {
  828. break;
  829. }
  830. }
  831. }
  832. }
  833. else
  834. {
  835. //if (skill.OveryLayer > 0 && maxCount > 1)
  836. if(skill.CanStoreTimes)
  837. {
  838. int maxIndex = Math.Min(skill.OveryLayer, maxCount);
  839. this.action_queue = new Queue<UnitActionData>(maxIndex);
  840. for (int i = 0; i < maxIndex; i++)
  841. {
  842. UnitActionData temp = skill.Data.ActionQueue[i];
  843. this.action_queue.Enqueue(temp);
  844. if (temp.SigleActionType == ActionEnum.forbidNext
  845. || temp.SigleActionType == ActionEnum.forbidNext_lock
  846. || temp.SigleActionType == ActionEnum.forbidNext_showTime)
  847. {
  848. break;
  849. }
  850. }
  851. //技能释放
  852. if (skill.CanStoreTimes)
  853. {
  854. skill.updateOverLayer(0);
  855. }
  856. }
  857. else
  858. {
  859. this.action_queue = new Queue<UnitActionData>(skill.Data.ActionQueue);
  860. }
  861. }
  862. launched.Invoke(this);
  863. if (mSkillLaunched != null)
  864. {
  865. mSkillLaunched.Invoke(this);
  866. mSkillLaunched = null;
  867. }
  868. }
  869. if (unit.IsSkillControllableByServer && param.AutoFocusNearTarget && (targetUnit == null || !targetUnit.IsActive))
  870. {
  871. //自动锁定目标//
  872. bool directionChange = false;
  873. this.targetUnit = unit.getSkillAttackableFirstTarget(this.SkillData, AttackReason.Look, ref directionChange);
  874. if (targetUnit != null && targetUnit != unit && directionChange)
  875. {
  876. unit.faceTo(targetUnit.X, targetUnit.Y);
  877. }
  878. }
  879. else
  880. {
  881. if (param.SpellTargetPos != null)
  882. {
  883. unit.faceTo(param.SpellTargetPos.X, param.SpellTargetPos.Y);
  884. unit.SendForceSync();
  885. }
  886. }
  887. {
  888. if (skill.IsMutilAction())
  889. {
  890. //最后一段取消连招标记
  891. if (launchIndex + 1 == skill.Data.ActionQueue.Count &&
  892. skill.Data.ActionQueue[launchIndex].SigleActionType != ActionEnum.showLeftTime)
  893. {
  894. skill.Reset();
  895. skill.ResetTotalTime();
  896. }
  897. else if(!skill.IsCodeConttrolCD())
  898. {
  899. if (isUseActionQueuetime)
  900. {
  901. skill.TotalCDTime = skill.Data.ActionQueue[launchIndex].TotalTimeMS;
  902. }
  903. else if (launchIndex + 1 != skill.Data.ActionQueue.Count &&
  904. skill.IsCanLaunchNextStepByPlayer(skill.Data.ActionQueue[launchIndex]))//非最后一段才用这个时间
  905. {
  906. skill.TotalCDTime = skill.Data.SingleActionCoolDownMS;
  907. }
  908. }
  909. }
  910. //Console.WriteLine("skill.TotalCDTime - " + launchIndex);
  911. PushLaunchSkillEvent(launchIndex);
  912. }
  913. nextAction();
  914. beginMoveToTarget();
  915. }
  916. private void PushLaunchSkillEvent(int launchIndex)
  917. {
  918. float speed = ActionSpeed;
  919. if (this.Skill.GetSkillType() == XmdsSkillType.normalAtk)
  920. {
  921. speed = speed * this.unit.GetAttackSpeed() / 10000;
  922. }
  923. UnitLaunchSkillEvent evt = new UnitLaunchSkillEvent(
  924. unit.ID,
  925. skill.Data,
  926. (byte)launchIndex,
  927. speed,
  928. skill.TotalCDTime,
  929. param.AutoFocusNearTarget,
  930. targetUnit != unit ? param.SpellTargetPos : null,
  931. targetUnit != null ? targetUnit.ID : 0,
  932. skill.LockActionStep);
  933. unit.queueEvent(evt);
  934. }
  935. private bool beginMoveToTarget()
  936. {
  937. if (current_action != null && current_action.IsMoveToTarget)
  938. {
  939. if (targetUnit != null && zone.TouchObject2(unit, targetUnit))
  940. {
  941. if (nextAction())
  942. {
  943. unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
  944. }
  945. return true;
  946. }
  947. }
  948. return false;
  949. }
  950. private bool nextAction()
  951. {
  952. move_to_target_path = null;
  953. if (start_move != null)
  954. {
  955. start_move.Stop();
  956. start_move = null;
  957. }
  958. if (action_queue != null && action_queue.Count > 0)
  959. {
  960. bool isNeedNotifyClient = (this.current_action != null && (this.current_action.SigleActionType == ActionEnum.IsAutoNext
  961. || this.current_action.SigleActionType == ActionEnum.chargeAtk));
  962. //bool perChargeAtk = (this.current_action != null && this.current_action.SigleActionType == ActionEnum.chargeAtk);
  963. this.current_action = action_queue.Dequeue();
  964. if (this.Skill.GetSkillType() == XmdsSkillType.normalAtk && this.unit.GetAttackSpeed() != 0)
  965. {
  966. this.mSkillSpeed = this.unit.GetAttackSpeed() * 0.0001f;
  967. this.curActionTotalTimes = (int)(this.current_action.TotalTimeMS / this.mSkillSpeed);
  968. }
  969. else
  970. {
  971. this.mSkillSpeed = 0;
  972. this.curActionTotalTimes = this.current_action.TotalTimeMS;
  973. }
  974. //如果为蓄力技能,通知客户端状态
  975. if (this.current_action.SigleActionType == ActionEnum.chargeAtk)
  976. {
  977. this.skill.Owner.AddActionSubState(UnitActionSubStatus.ChargeAtkIdle);
  978. }
  979. else
  980. {
  981. this.skill.Owner.RemoveActionSubState(UnitActionSubStatus.ChargeAtkIdle);
  982. this.skill.Owner.RemoveActionSubState(UnitActionSubStatus.ChargeAtkMove);
  983. }
  984. this.current_frames.AddRange(current_action.KeyFrames);
  985. this.current_pass_time = 0;
  986. this.IsCancelableByMove = current_action.IsCancelable;
  987. this.IsCancelableBySkill = current_action.IsCancelableBySkill;
  988. this.IsNoneBlock = current_action.IsNoneBlock;
  989. this.IsNoneTouch = current_action.IsNoneTouch;
  990. this.IsFaceToTarget = current_action.IsFaceToTarget;
  991. this.IsControlFaceable = current_action.IsControlFaceable;
  992. this.IsControlMoveable = current_action.IsControlMoveable;
  993. if (isNeedNotifyClient)
  994. {
  995. skill.ResetPassTime();
  996. skill.NextAction();
  997. //程序自动连接每一段动作,时间默认为totalMS
  998. UnitActionData actionData = skill.Data.ActionQueue[skill.ActionIndex];
  999. if(!skill.IsCodeConttrolCD())
  1000. {
  1001. if (skill.Data.IsSingleAction && actionData.SigleActionType == ActionEnum.None)
  1002. {
  1003. skill.TotalCDTime = skill.Data.SingleActionCoolDownMS;
  1004. }
  1005. else
  1006. {
  1007. skill.TotalCDTime = actionData.TotalTimeMS;
  1008. }
  1009. }
  1010. if(this.current_action.SigleActionType != ActionEnum.IsAutoNext)
  1011. {
  1012. this.PushLaunchSkillEvent(skill.ActionIndex);
  1013. }
  1014. }
  1015. if (IsNoneBlock)
  1016. {
  1017. unit.SetNoneBlockTimeMS(this.curActionTotalTimes);
  1018. }
  1019. if (current_action.IsMoveToTarget)
  1020. {
  1021. //先寻路//
  1022. checkMoveToTarget(current_action);
  1023. }
  1024. else if (current_action.IsJumpToTarget)
  1025. {
  1026. //计算跳跃距离//
  1027. checkJumpToTarget(current_action);
  1028. }
  1029. else
  1030. {
  1031. if (unit.IsSkillControllableByServer)
  1032. {
  1033. if (param.AutoFocusNearTarget && (targetUnit == null || !targetUnit.IsActive))
  1034. {
  1035. //自动锁定目标//
  1036. bool directionChange = false;
  1037. targetUnit = unit.getSkillAttackableFirstTarget(this.SkillData, AttackReason.Look, ref directionChange);
  1038. if (targetUnit != null && targetUnit != unit && directionChange)
  1039. {
  1040. unit.faceTo(targetUnit.X, targetUnit.Y);
  1041. }
  1042. }
  1043. }
  1044. }
  1045. return true;
  1046. }
  1047. //如果为蓄力技能,通知客户端状态
  1048. if (this.current_action != null && this.current_action.SigleActionType == ActionEnum.chargeAtk)
  1049. {
  1050. this.skill.Owner.RemoveActionSubState(UnitActionSubStatus.ChargeAtkIdle);
  1051. }
  1052. current_action = null;
  1053. return false;
  1054. }
  1055. public void doKeyFrame(UnitActionData.KeyFrame kf)
  1056. {
  1057. // 关键帧改变状态
  1058. if (kf.ChangeStatus != null)
  1059. {
  1060. this.IsNoneBlock = kf.ChangeStatus.IsNoneBlock;
  1061. this.IsNoneTouch = kf.ChangeStatus.IsNoneTouch;
  1062. this.IsFaceToTarget = kf.ChangeStatus.IsFaceToTarget;
  1063. this.IsCancelableByMove = kf.ChangeStatus.IsCancelable;
  1064. this.IsCancelableBySkill = kf.ChangeStatus.IsCancelableBySkill;
  1065. this.IsControlFaceable = kf.ChangeStatus.IsControlFaceable;
  1066. this.IsControlMoveable = kf.ChangeStatus.IsControlMoveable;
  1067. }
  1068. // 如果关键帧绑定特效
  1069. if (kf.Effect != null)
  1070. {
  1071. unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Effect));
  1072. }
  1073. // 如果关键帧绑定近战攻击
  1074. if (kf.Attack != null)
  1075. {
  1076. doHitAttack(kf.Attack);
  1077. }
  1078. // 如果关键帧绑定释放法术
  1079. if (kf.Spell != null)
  1080. {
  1081. float tx = unit.X;
  1082. float ty = unit.Y;
  1083. if (param.SpellTargetPos != null)
  1084. {
  1085. float td = MathVector.getDistance(unit.X, unit.Y, param.SpellTargetPos.X, param.SpellTargetPos.Y);
  1086. // TargetPos超出技能范围 //
  1087. if (td > SkillData.AttackRange)
  1088. {
  1089. // 把TargetPos拉回 //
  1090. MathVector.movePolar(param.SpellTargetPos,
  1091. MathVector.getDegree(unit.X, unit.Y, param.SpellTargetPos.X, param.SpellTargetPos.Y),
  1092. SkillData.AttackRange - td);
  1093. }
  1094. // 设置法术出生点 (非自身坐标发射,比如Cannon) //
  1095. if (!SkillData.IsLaunchBody)
  1096. {
  1097. tx = param.SpellTargetPos.X;
  1098. ty = param.SpellTargetPos.Y;
  1099. }
  1100. }
  1101. //else if(this.targetUnit != null)
  1102. //{
  1103. // tx = this.targetUnit.X;
  1104. // ty = this.targetUnit.Y;
  1105. //}
  1106. zone.unitLaunchSpell(this.skill == null ?XmdsSkillType.none : this.skill.GetSkillType(), unit, kf.Spell,
  1107. tx, ty, param.TargetUnitID, param.SpellTargetPos, this.CurrentActionIndex);
  1108. }
  1109. // 如果关键帧绑定自己释放BUFF
  1110. if (kf.SelfBuff != null)
  1111. {
  1112. unit.AddBuff(kf.SelfBuff, unit);
  1113. }
  1114. // 如果关键帧绑定单位位移
  1115. if (kf.Move != null && (targetUnit == null || kf.hitTargetMoveEnable))
  1116. {
  1117. StartMove action_move = kf.Move;
  1118. //if (unit.IsSkillControllableByServer)
  1119. {
  1120. this.start_move = unit.StartHitMove(this,
  1121. unit.Direction + action_move.Direction,
  1122. action_move.RotateSpeedSEC,
  1123. action_move.KeepTimeMS,
  1124. action_move.SpeedSEC,
  1125. action_move.SpeedAdd,
  1126. action_move.SpeedAcc,
  1127. action_move.IsNoneTouch);
  1128. this.start_move.SetFly(this,
  1129. action_move.ZSpeedSEC,
  1130. action_move.ZLimit,
  1131. 0);
  1132. }
  1133. if (action_move.HasFly && !zone.IsSyncZ)
  1134. {
  1135. unit.queueEvent(new UnitJumpEvent(unit.ID, action_move.ZSpeedSEC, action_move.OverrideGravity, action_move.ZLimit));
  1136. }
  1137. }
  1138. if (kf.Blink != null)
  1139. {
  1140. if (kf.Blink.BeginEffect != null)
  1141. {
  1142. unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Blink.BeginEffect));
  1143. }
  1144. unit.moveBlink(kf.Blink, param.SpellTargetPos, targetUnit);
  1145. unit.SendForceSync();
  1146. if (kf.Blink.TargetEffect != null)
  1147. {
  1148. unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Blink.TargetEffect));
  1149. }
  1150. }
  1151. }
  1152. /// <summary>
  1153. /// 近战攻击
  1154. /// </summary>
  1155. /// <param name="attack"></param>
  1156. private void doHitAttack(AttackProp attack)
  1157. {
  1158. float rg = unit.GetSkillAttackRange(skill.Data);
  1159. if (current_action.OverrideAttackShape != null)
  1160. {
  1161. attack_range.Shape = (AttackShape)current_action.OverrideAttackShape.AShape;
  1162. attack_range.Direction = unit.Direction;
  1163. attack_range.ExpectTarget = SkillData.ExpectTarget;
  1164. attack_range.BodySize = current_action.OverrideAttackShape.AttackRange;
  1165. attack_range.Distance = current_action.OverrideAttackShape.AttackRange;
  1166. attack_range.FanAngle = current_action.OverrideAttackShape.AttackAngle;
  1167. attack_range.StripWide = current_action.OverrideAttackShape.StripWide;
  1168. float dx = unit.X;
  1169. float dy = unit.Y;
  1170. if (current_action.OverrideAttackShape.OffsetRadius != 0)
  1171. {
  1172. MathVector.movePolar(ref dx, ref dy, unit.Direction, current_action.OverrideAttackShape.OffsetRadius);
  1173. }
  1174. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1175. {
  1176. attack_range.GetShapeAttackable(list, AttackReason.Attack, SkillData, dx, dy, dx, dy, 0, unit.AoiStatus);
  1177. if (list.Count > 0)
  1178. {
  1179. zone.unitAttackDirect(unit, new AttackSource(skill, attack), list);
  1180. }
  1181. }
  1182. }
  1183. else if (skill.Data.AttackAngle == 0)
  1184. {
  1185. if (targetUnit != null)
  1186. {
  1187. if (checkTargetRange())
  1188. {
  1189. zone.unitAttackSingle(
  1190. unit,
  1191. new AttackSource(skill, attack),
  1192. targetUnit,
  1193. SkillData.ExpectTarget);
  1194. }
  1195. }
  1196. }
  1197. else
  1198. {
  1199. int hitcount = zone.unitAttackFan(
  1200. unit,
  1201. new AttackSource(skill, attack),
  1202. unit.Direction,
  1203. rg,
  1204. skill.Data.AttackAngle,
  1205. SkillData.ExpectTarget);
  1206. }
  1207. }
  1208. /// <summary>
  1209. /// 身体攻击
  1210. /// </summary>
  1211. private void doBodyHit(UnitActionData current_action)
  1212. {
  1213. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1214. {
  1215. float line_r = (current_action.BodyHitSize > 0) ? current_action.BodyHitSize : unit.BodyHitSize;
  1216. zone.getObjectsRoundLineRange<InstanceUnit>(
  1217. Collider.Object_HitBody_TouchRoundLine,
  1218. body_hited_last_pos.X,
  1219. body_hited_last_pos.Y,
  1220. unit.X,
  1221. unit.Y,
  1222. line_r,
  1223. list,
  1224. unit.AoiStatus);
  1225. if (list.Count > 0)
  1226. {
  1227. CUtils.RemoveAll(list, body_hited.Values);
  1228. zone.unitAttack(unit, new AttackSource(skill, current_action.BodyHit), list, SkillData.ExpectTarget);
  1229. if (list.Count > 0)
  1230. {
  1231. for (int i = 0; i < list.Count; i++)
  1232. {
  1233. InstanceUnit o = list[i];
  1234. body_hited.Put(o.ID, o);
  1235. }
  1236. if (current_action.BodyHitNextAction)
  1237. {
  1238. if (nextAction())
  1239. {
  1240. unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
  1241. }
  1242. unit.SendForceSync();
  1243. }
  1244. }
  1245. }
  1246. }
  1247. }
  1248. private void doMove()
  1249. {
  1250. start_move.IsNoneTouch = IsNoneTouch;
  1251. if (start_move.IsEnd)
  1252. {
  1253. start_move = null;
  1254. }
  1255. }
  1256. /// <summary>
  1257. /// 防止技能位移导致单位重合
  1258. /// </summary>
  1259. private void doBodyBlock(UnitActionData current_action)
  1260. {
  1261. if (start_move != null || move_to != null)
  1262. {
  1263. if (unit.ElasticOtherObjects())
  1264. {
  1265. unit.SendForceSync();
  1266. }
  1267. }
  1268. }
  1269. /// <summary>
  1270. /// 移动到目标面前
  1271. /// </summary>
  1272. private bool doMoveToTarget(UnitActionData current_action)
  1273. {
  1274. if (this.move_to_target_path == null)
  1275. {
  1276. if (nextAction())
  1277. {
  1278. unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
  1279. }
  1280. return true;
  1281. }
  1282. else if (targetUnit != null && zone.TouchObject2(unit, targetUnit))
  1283. {
  1284. this.move_to_target_path = null;
  1285. unit.ElasticOtherObjects();
  1286. if (nextAction())
  1287. {
  1288. unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
  1289. }
  1290. return true;
  1291. }
  1292. else
  1293. {
  1294. float tx, ty;
  1295. tx = move_to_target_path.PosX;
  1296. ty = move_to_target_path.PosY;
  1297. unit.faceTo(tx, ty);// = MathVector.getDegree(tx - unit.X, ty - unit.Y);
  1298. if (unit.jumpToTarget(tx, ty, current_action.MoveToTargetSpeedSEC, zone.UpdateIntervalMS))
  1299. {
  1300. move_to_target_path = move_to_target_path.Next;
  1301. }
  1302. unit.SendForceSync();
  1303. return false;
  1304. }
  1305. }
  1306. private bool doJumpToTarget(UnitActionData action)
  1307. {
  1308. unit.Z = MoveHelper.CalulateParabolicHeight(action.JumpToTargetHeightZ, action.TotalTimeMS, this.current_pass_time);
  1309. unit.jumpToTarget(jump_to_target_pos.X, jump_to_target_pos.Y, jump_to_target_speed_sec, zone.UpdateIntervalMS);
  1310. unit.SendForceSync();
  1311. return false;
  1312. }
  1313. public bool setMoveTo(float x, float y)
  1314. {
  1315. //if(Math.Abs(this.unit.X - x) < 0.5 && Math.Abs(this.unit.Y - y) < 0.5)
  1316. //{
  1317. // return true;
  1318. //}
  1319. if (unit.IsSkillControllableByServer)
  1320. {
  1321. if (IsControlMoveable && !unit.IsCannotMove)
  1322. {
  1323. if (move_to == null)
  1324. {
  1325. move_to = new Vector2(x, y);
  1326. }
  1327. else
  1328. {
  1329. move_to.SetX(x);
  1330. move_to.SetY(y);
  1331. }
  1332. }
  1333. if (IsControlFaceable)
  1334. {
  1335. unit.faceTo(x, y);
  1336. }
  1337. this.StopFaceTo = new Vector2(x - unit.X, y - unit.Y);
  1338. }
  1339. return false;
  1340. }
  1341. public void setMoveTo(Vector2 pos)
  1342. {
  1343. if (unit.IsSkillControllableByServer)
  1344. {
  1345. if (pos == null)
  1346. {
  1347. this.move_to = null;
  1348. return;
  1349. }
  1350. if (IsControlMoveable && !unit.IsCannotMove)
  1351. {
  1352. move_to = pos;
  1353. }
  1354. if (IsControlFaceable)
  1355. {
  1356. unit.faceTo(pos.X, pos.Y);
  1357. }
  1358. this.StopFaceTo = new Vector2(pos.X - unit.X, pos.Y - unit.Y);
  1359. }
  1360. }
  1361. public void block(State newState = null)
  1362. {
  1363. bool old_done = is_done;
  1364. is_done = true;
  1365. if (newState != null)
  1366. {
  1367. unit.changeState(newState);
  1368. }
  1369. else if (!old_done)
  1370. {
  1371. unit.doSomething();
  1372. }
  1373. }
  1374. /// <summary>
  1375. /// 检测目标距离
  1376. /// </summary>
  1377. /// <returns></returns>
  1378. private bool checkTargetRange()
  1379. {
  1380. return skill.checkTargetRange(targetUnit);
  1381. }
  1382. /// <summary>
  1383. /// 检测冲锋距离
  1384. /// </summary>
  1385. /// <returns></returns>
  1386. private bool checkMoveToTarget(UnitActionData action)
  1387. {
  1388. if (action.IsMoveToTarget)
  1389. {
  1390. float tx; float ty;
  1391. if (getTargetPos(out tx, out ty))
  1392. {
  1393. this.move_to_target_path = zone.findPath(unit.X, unit.Y, tx, ty);
  1394. if (this.move_to_target_path != null)
  1395. {
  1396. float max = MoveHelper.GetDistance(action.TotalTimeMS, action.MoveToTargetSpeedSEC);
  1397. float total = this.move_to_target_path.GetTotalDistance();
  1398. if (total > max)
  1399. {
  1400. move_to_target_path = null;
  1401. return false;
  1402. }
  1403. return true;
  1404. }
  1405. }
  1406. return false;
  1407. }
  1408. return true;
  1409. }
  1410. /// <summary>
  1411. /// 检测跳跃速度
  1412. /// </summary>
  1413. /// <param name="action"></param>
  1414. /// <returns></returns>
  1415. private bool checkJumpToTarget(UnitActionData action)
  1416. {
  1417. if (action.IsJumpToTarget)
  1418. {
  1419. float tx; float ty;
  1420. if (getTargetPos(out tx, out ty))
  1421. {
  1422. float distance = CMath.getDistance(unit.X, unit.Y, tx, ty);
  1423. this.jump_to_target_pos = new TVector2(tx, ty);
  1424. this.jump_to_target_speed_sec = distance / (action.TotalTimeMS * 0.001f);
  1425. }
  1426. }
  1427. return true;
  1428. }
  1429. private bool getTargetPos(out float x, out float y)
  1430. {
  1431. x = unit.X;
  1432. y = unit.Y;
  1433. if (param.SpellTargetPos != null)
  1434. {
  1435. x = param.SpellTargetPos.X;
  1436. y = param.SpellTargetPos.Y;
  1437. return true;
  1438. }
  1439. else if (targetUnit != null && targetUnit != unit)
  1440. {
  1441. x = targetUnit.X;
  1442. y = targetUnit.Y;
  1443. return true;
  1444. }
  1445. else return false;
  1446. }
  1447. public void doUpdatePosByClient(UnitUpdatePosAction act)
  1448. {
  1449. if (!unit.IsSkillControllableByServer)
  1450. {
  1451. if(this.skill.GetSkillType() == XmdsSkillType.petGiveAcitve)
  1452. {
  1453. unit.setPos(act.x, act.y, unit.IntersectMap);
  1454. unit.faceTo(act.d);
  1455. }
  1456. else
  1457. {
  1458. //暂时只管方向,不管pos
  1459. if (this.IsControlMoveable && !unit.IsCannotMove)
  1460. {
  1461. unit.setPos(act.x, act.y, unit.IntersectMap);
  1462. }
  1463. if (this.IsControlFaceable)
  1464. {
  1465. unit.faceTo(act.d);
  1466. }
  1467. else
  1468. {
  1469. unit.SendForceSync();
  1470. }
  1471. }
  1472. }
  1473. }
  1474. public delegate void SkillLaunched(StateSkill state);
  1475. private SkillLaunched mSkillLaunched;
  1476. public event SkillLaunched OnSkillLaunched { add { mSkillLaunched += value; } remove { mSkillLaunched -= value; } }
  1477. }
  1478. internal void SetActionSubState(UnitActionStatus unitSubState)
  1479. {
  1480. throw new NotImplementedException();
  1481. }
  1482. //--------------------------------------------------------------------------------------------------------
  1483. //---------------------------------------------------------------------------------------
  1484. public class StatePickObject : State
  1485. {
  1486. readonly private TimeExpire<int> mTimer;
  1487. readonly private int mTotalTimeMS;
  1488. readonly private InstanceZoneObject mPickable;
  1489. private bool mIsDone = false;
  1490. private string mStopReason;
  1491. private string mStatus;
  1492. private OnPickDone mOnDone;
  1493. private OnCheckPickable mCheckTargetActive;
  1494. public string StopReason { get { return mStopReason; } }
  1495. /// <summary>
  1496. /// 设置目标检测状态方法
  1497. /// </summary>
  1498. /// <param name="check">check返回True,终止状态机。</param>
  1499. public void SetCheckTargetAcvite(OnCheckPickable check)
  1500. {
  1501. this.mCheckTargetActive = check;
  1502. }
  1503. public StatePickObject(InstanceUnit unit, InstanceZoneObject pickable, int timeMS, string status, OnPickDone done)
  1504. : base(unit)
  1505. {
  1506. this.mStatus = status;
  1507. this.mTimer = new TimeExpire<int>(timeMS);
  1508. this.mPickable = pickable;
  1509. this.mTotalTimeMS = timeMS;
  1510. this.mOnDone = done;
  1511. }
  1512. public override bool onBlock(State new_state)
  1513. {
  1514. if (new_state is StateIdle)
  1515. return mIsDone;
  1516. //block by anything//
  1517. return true;
  1518. }
  1519. protected override void onStart()
  1520. {
  1521. unit.SetActionStatus(UnitActionStatus.Pick);
  1522. unit.queueEvent(new UnitStartPickObjectEvent(unit.ID, mTotalTimeMS, mPickable.ID, mStatus));
  1523. }
  1524. protected override void onStop()
  1525. {
  1526. unit.queueEvent(new UnitStopPickObjectEvent(unit.ID, mStopReason));
  1527. this.mOnDone = null;
  1528. mCheckTargetActive = null;
  1529. }
  1530. protected override void onUpdate()
  1531. {
  1532. if (!mPickable.Enable)
  1533. {
  1534. mIsDone = true;
  1535. unit.doSomething();
  1536. return;
  1537. }
  1538. if (mCheckTargetActive != null && mCheckTargetActive(unit, mPickable, ref mStopReason))
  1539. {
  1540. mIsDone = true;
  1541. unit.doSomething();
  1542. return;
  1543. }
  1544. if (mTimer.Update(zone.UpdateIntervalMS))
  1545. {
  1546. mIsDone = true;
  1547. unit.doSomething();
  1548. if (mOnDone != null)
  1549. {
  1550. mOnDone.Invoke(unit, mPickable);
  1551. }
  1552. }
  1553. }
  1554. public delegate void OnPickDone(InstanceUnit unit, InstanceZoneObject pickable);
  1555. public delegate bool OnCheckPickable(InstanceUnit unit, InstanceZoneObject pickable, ref string reason);
  1556. }
  1557. //---------------------------------------------------------------------------------------
  1558. #region __状态限制__
  1559. //---------------------------------------------------------------------------------------
  1560. public interface IStateNoneControllable
  1561. {
  1562. State AsState();
  1563. }
  1564. /// <summary>
  1565. /// 被击中受击过程
  1566. /// </summary>
  1567. public class StateDamage : State, IStateNoneControllable
  1568. {
  1569. private readonly AttackSource source;
  1570. private readonly InstanceUnit attacker;
  1571. private float startDirection;
  1572. private float rotateSpeedSEC;
  1573. private bool isEnd = false;
  1574. private HitMoveSpeed hitMoveSpeed;
  1575. private TimeExpire<object> damageExpire;
  1576. private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
  1577. private IStateNoneControllable nextState;
  1578. public void SetNextNoneControllable(IStateNoneControllable state)
  1579. {
  1580. nextState = state;
  1581. }
  1582. public State AsState()
  1583. {
  1584. return this;
  1585. }
  1586. public bool IsFallingDown
  1587. {
  1588. get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
  1589. }
  1590. public bool IsKnockDown
  1591. {
  1592. get { return source.OutHasKnockDown; }
  1593. }
  1594. public bool IsDamageProtect
  1595. {
  1596. get { return source.Attack.IsDamageProtect; }
  1597. }
  1598. public int DamageTimeMS
  1599. {
  1600. get { return damageExpire.TotalTimeMS; }
  1601. }
  1602. public bool IsHitMove
  1603. {
  1604. get { return hitMoveSpeed != null; }
  1605. }
  1606. public StateDamage(InstanceUnit unit, AttackSource source, InstanceUnit attacker)
  1607. : base(unit)
  1608. {
  1609. this.source = source;
  1610. this.attacker = attacker;
  1611. this.body_hited.Put(unit.ID, unit);
  1612. AttackProp.HitMoveType mtype = source.Attack.HitMoveMType;
  1613. if (source.FromSpell != null)
  1614. {
  1615. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
  1616. }
  1617. else if (source.FromSkill != null)
  1618. {
  1619. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
  1620. }
  1621. else
  1622. {
  1623. this.startDirection = unit.Direction + CMath.PI_F;
  1624. }
  1625. if (source.OutHitMove != null)
  1626. {
  1627. this.startDirection += source.OutHitMove.Direction;
  1628. this.rotateSpeedSEC = source.OutHitMove.RotateSpeedSEC * ((unit.RandomN.Next() % 2) == 0 ? -1 : 1);
  1629. }
  1630. // 计算受击时间 //
  1631. int damageTime = unit.Info.DamageTimeMS;
  1632. if (source.OutKnockDownTimeMS > 0)
  1633. {
  1634. damageTime = source.OutKnockDownTimeMS;
  1635. }
  1636. else if (unit.mInfo.DamageTimeMS > 0)
  1637. {
  1638. damageTime = unit.mInfo.DamageTimeMS;
  1639. }
  1640. else
  1641. {
  1642. damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
  1643. }
  1644. this.damageExpire = new TimeExpire<object>(damageTime);
  1645. }
  1646. override public bool onBlock(State new_state)
  1647. {
  1648. if (isEnd)
  1649. {
  1650. return true;
  1651. }
  1652. if (new_state is StateDead)
  1653. {
  1654. return true;
  1655. }
  1656. if (new_state is StateDamage)
  1657. {
  1658. return onBlockNewDamage(new_state as StateDamage);
  1659. }
  1660. if (new_state is IStateNoneControllable)
  1661. {
  1662. this.SetNextNoneControllable(new_state as IStateNoneControllable);
  1663. }
  1664. if (unit.IsDead())
  1665. {
  1666. return false;
  1667. }
  1668. if (new_state is StateSkill)
  1669. {
  1670. StateSkill ss = new_state as StateSkill;
  1671. // 反击或者状态解除//
  1672. if (ss.SkillData.IsCounter)
  1673. {
  1674. return true;
  1675. }
  1676. }
  1677. return isEnd;
  1678. }
  1679. private bool onBlockNewDamage(StateDamage new_damage)
  1680. {
  1681. //击飞只能被击飞中断//
  1682. if (this.IsFallingDown)
  1683. {
  1684. if (!new_damage.source.OutHasFly)
  1685. {
  1686. return false;
  1687. }
  1688. }
  1689. //击倒只能被击飞或击倒中断//
  1690. if (this.IsKnockDown)
  1691. {
  1692. if (!new_damage.source.OutHasFly)
  1693. {
  1694. return false;
  1695. }
  1696. if (!new_damage.source.OutHasKnockDown)
  1697. {
  1698. return false;
  1699. }
  1700. }
  1701. //如果正在位移,需要更高优先级//
  1702. if (hitMoveSpeed != null)
  1703. {
  1704. if (new_damage.source.OutHasFly || new_damage.source.OutHasKnockDown || new_damage.source.OutHitMove != null)
  1705. {
  1706. //带位移的受击,相等优先级也可以打断当前位移//
  1707. if (new_damage.source.OutWeight >= this.source.OutWeight)
  1708. {
  1709. return true;
  1710. }
  1711. }
  1712. else
  1713. {
  1714. //普通受击,更高优先级才能打断当前位移//
  1715. if (new_damage.source.OutWeight > this.source.OutWeight)
  1716. {
  1717. return true;
  1718. }
  1719. }
  1720. return false;
  1721. }
  1722. return true;
  1723. }
  1724. override protected void onStart()
  1725. {
  1726. float zgravity = 0;
  1727. float zlimit = 0;
  1728. bool hasHitMove = false;
  1729. if (source.OutHitMove != null)
  1730. {
  1731. int keepTimeMs = source.OutHitMove.KeepTimeMS;
  1732. float speedSEC = source.OutHitMove.SpeedSEC;
  1733. if (source.Attack.HitMoveMType == AttackProp.HitMoveType.BySenderRange)
  1734. {
  1735. float distance = CMath.getDistance(attacker.X, attacker.Y, unit.X, unit.Y);
  1736. if (distance > source.FromSpell.BodySize)
  1737. {
  1738. keepTimeMs = 0;
  1739. speedSEC = 0;
  1740. }
  1741. else
  1742. {
  1743. float moveDistance = source.FromSpell.BodySize - distance;
  1744. keepTimeMs = (int)(moveDistance * 1000 / source.OutHitMove.SpeedSEC);
  1745. }
  1746. }
  1747. hasHitMove = true;
  1748. this.hitMoveSpeed = unit.StartHitMove(this,
  1749. startDirection,
  1750. source.OutHitMove.RotateSpeedSEC,
  1751. keepTimeMs,
  1752. speedSEC,
  1753. source.OutHitMove.SpeedAdd,
  1754. source.OutHitMove.SpeedAcc,
  1755. source.OutHitMove.IsNoneTouch);
  1756. if (source.OutHasFly)
  1757. {
  1758. this.hitMoveSpeed.SetFly(this,
  1759. source.OutHitMove.ZSpeedSEC,
  1760. source.OutHitMove.OverrideGravity,
  1761. source.OutHitMove.ZLimit);
  1762. zgravity = source.OutHitMove.OverrideGravity;
  1763. zlimit = source.OutHitMove.ZLimit;
  1764. }
  1765. }
  1766. else if (source.OutHasFly)
  1767. {
  1768. hasHitMove = true;
  1769. this.hitMoveSpeed = unit.StartHitMove(this,
  1770. startDirection,
  1771. 0,
  1772. 0, // 如果击飞,则计算按落地时间 //
  1773. zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_SEC,
  1774. zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_ADD,
  1775. zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_ACC,
  1776. false);
  1777. this.hitMoveSpeed.SetFly(this,
  1778. zone.Templates.CFG.OBJECT_DAMAGE_FLY_ZSPEED_SEC, 0, 0);
  1779. }
  1780. int moveTime = 0;
  1781. float zspeed = 0;
  1782. if (hitMoveSpeed != null)
  1783. {
  1784. moveTime = hitMoveSpeed.TotalTimeMS;
  1785. zspeed = hitMoveSpeed.ZSpeedSEC;
  1786. if (source.Attack.HitMoveMType == AttackProp.HitMoveType.ToSenderCenter)
  1787. {
  1788. if (source.FromSpellUnit != null)
  1789. {
  1790. this.hitMoveSpeed.SetMoveTarget(source.FromSpellUnit, false);
  1791. }
  1792. else if (source.FromSkill != null)
  1793. {
  1794. this.hitMoveSpeed.SetMoveTarget(this.attacker, false);
  1795. }
  1796. }
  1797. else if (source.Attack.HitMoveMType == AttackProp.HitMoveType.ToSenderBodySize)
  1798. {
  1799. if (source.FromSpellUnit != null)
  1800. {
  1801. this.hitMoveSpeed.SetMoveTarget(source.FromSpellUnit, true);
  1802. }
  1803. else if (source.FromSkill != null)
  1804. {
  1805. this.hitMoveSpeed.SetMoveTarget(this.attacker, true);
  1806. }
  1807. }
  1808. }
  1809. unit.queueEvent(new UnitDamageEvent(
  1810. unit.ID,
  1811. DamageTimeMS,
  1812. moveTime,
  1813. zspeed,
  1814. zgravity,
  1815. zlimit,
  1816. source.OutHasKnockDown,
  1817. source.Attack));
  1818. if (hasHitMove)
  1819. {
  1820. unit.SetActionStatus(UnitActionStatus.HitMove);
  1821. }
  1822. else
  1823. {
  1824. unit.SetActionStatus(UnitActionStatus.Damage);
  1825. }
  1826. }
  1827. override protected void onUpdate()
  1828. {
  1829. if (hitMoveSpeed != null)
  1830. {
  1831. if (hitMoveSpeed.IsEnd)
  1832. {
  1833. hitMoveSpeed = null;
  1834. if (source.Attack.FlyFallenDownAttack != null)
  1835. {
  1836. doFlyDownAttack();
  1837. }
  1838. }
  1839. else if (source.Attack.HitMoveBodyAttack != null)
  1840. {
  1841. doBodyAttack();
  1842. }
  1843. }
  1844. else if (damageExpire != null)
  1845. {
  1846. if (damageExpire.Update(zone.UpdateIntervalMS))
  1847. {
  1848. damageExpire = null;
  1849. }
  1850. }
  1851. else
  1852. {
  1853. end();
  1854. }
  1855. }
  1856. override protected void onStop()
  1857. {
  1858. if (unit.IsStun)
  1859. {
  1860. unit.changeState(new StateStun(unit));
  1861. }
  1862. else if (nextState != null)
  1863. {
  1864. unit.changeState(nextState.AsState());
  1865. }
  1866. }
  1867. private void end()
  1868. {
  1869. if (unit.IsDead())
  1870. {
  1871. isEnd = true;
  1872. unit.changeState(new StateDead(unit, attacker));
  1873. }
  1874. else if (unit.IsStun)
  1875. {
  1876. unit.SetActionStatus(UnitActionStatus.Stun);
  1877. }
  1878. else
  1879. {
  1880. isEnd = true;
  1881. unit.doSomething();
  1882. }
  1883. }
  1884. /// <summary>
  1885. /// 自己落地摔一下
  1886. /// </summary>
  1887. private void doFlyDownAttack()
  1888. {
  1889. unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
  1890. }
  1891. /// <summary>
  1892. /// 飞行过程中打别人
  1893. /// </summary>
  1894. private void doBodyAttack()
  1895. {
  1896. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1897. {
  1898. zone.getObjectsRoundLineRange<InstanceUnit>(
  1899. Collider.Object_HitBody_TouchRoundLine,
  1900. hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
  1901. unit.X, unit.Y,
  1902. unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
  1903. list, unit.AoiStatus);
  1904. if (list.Count > 0)
  1905. {
  1906. CUtils.RemoveAll(list, body_hited.Values);
  1907. zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
  1908. if (list.Count > 0)
  1909. {
  1910. for (int i = 0; i < list.Count; i++)
  1911. {
  1912. InstanceUnit o = list[i];
  1913. body_hited.Put(o.ID, o);
  1914. }
  1915. }
  1916. }
  1917. }
  1918. }
  1919. }
  1920. /// <summary>
  1921. /// 死了,而且是被艹翻的,暴击打死然后击退
  1922. /// </summary>
  1923. public class StateDeadFuckFuck : State, IStateNoneControllable
  1924. {
  1925. private readonly AttackSource source;
  1926. private readonly InstanceUnit attacker;
  1927. private float startDirection;
  1928. private bool isEnd = false;
  1929. private HitMoveSpeed hitMoveSpeed;
  1930. private TimeExpire<object> damageExpire;
  1931. private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
  1932. private IStateNoneControllable nextState;
  1933. public void SetNextNoneControllable(IStateNoneControllable state)
  1934. {
  1935. nextState = state;
  1936. }
  1937. public State AsState()
  1938. {
  1939. return this;
  1940. }
  1941. public bool IsFallingDown
  1942. {
  1943. get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
  1944. }
  1945. public bool IsKnockDown
  1946. {
  1947. get { return source.OutHasKnockDown; }
  1948. }
  1949. public bool IsDamageProtect
  1950. {
  1951. get { return source.Attack.IsDamageProtect; }
  1952. }
  1953. public int DamageTimeMS
  1954. {
  1955. get { return damageExpire.TotalTimeMS; }
  1956. }
  1957. public bool IsHitMove
  1958. {
  1959. get { return hitMoveSpeed != null; }
  1960. }
  1961. public StateDeadFuckFuck(InstanceUnit unit, AttackSource source, InstanceUnit attacker, HitMoveType movetype)
  1962. : base(unit)
  1963. {
  1964. this.source = source;
  1965. this.attacker = attacker;
  1966. this.body_hited.Put(unit.ID, unit);
  1967. HitMoveType mtype = movetype;
  1968. if (source.FromSpell != null)
  1969. {
  1970. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
  1971. }
  1972. else if (source.FromSkill != null)
  1973. {
  1974. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
  1975. }
  1976. else
  1977. {
  1978. this.startDirection = unit.Direction + CMath.PI_F;
  1979. }
  1980. // 计算受击时间 //
  1981. int damageTime = unit.Info.DamageTimeMS;
  1982. if (source.OutKnockDownTimeMS > 0)
  1983. {
  1984. damageTime = source.OutKnockDownTimeMS;
  1985. }
  1986. else if (unit.mInfo.DamageTimeMS > 0)
  1987. {
  1988. damageTime = unit.mInfo.DamageTimeMS;
  1989. }
  1990. else
  1991. {
  1992. damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
  1993. }
  1994. this.damageExpire = new TimeExpire<object>(damageTime);
  1995. }
  1996. override public bool onBlock(State new_state)
  1997. {
  1998. if (isEnd)
  1999. {
  2000. return true;
  2001. }
  2002. if (new_state is StateDead)
  2003. {
  2004. return true;
  2005. }
  2006. if (new_state is IStateNoneControllable)
  2007. {
  2008. this.SetNextNoneControllable(new_state as IStateNoneControllable);
  2009. }
  2010. if (unit.IsDead())
  2011. {
  2012. return false;
  2013. }
  2014. if (new_state is StateDamage)
  2015. {
  2016. return unit.CanWhiplashDeadBody;
  2017. }
  2018. if (new_state is StateRebirth)
  2019. {
  2020. return true;
  2021. }
  2022. return isEnd;
  2023. }
  2024. override protected void onStart()
  2025. {
  2026. unit.SetActionStatus(UnitActionStatus.Dead);
  2027. float zgravity = 0;
  2028. float zlimit = 0;
  2029. if (true)
  2030. {
  2031. int keepTimeMs = 0;
  2032. int moveDistance = new Random().Next(2, 12);
  2033. float speedSEC = 15;
  2034. keepTimeMs = (int)(moveDistance * 1000 / speedSEC);
  2035. //speedSEC = new Random().Next(0, 11);
  2036. this.hitMoveSpeed = unit.StartHitMove(this,
  2037. startDirection,
  2038. 0,
  2039. keepTimeMs,
  2040. speedSEC,
  2041. 0,
  2042. 0,
  2043. false);
  2044. }
  2045. int moveTime = 0;
  2046. float zspeed = 0;
  2047. if (hitMoveSpeed != null)
  2048. {
  2049. moveTime = hitMoveSpeed.TotalTimeMS;
  2050. zspeed = hitMoveSpeed.ZSpeedSEC;
  2051. }
  2052. unit.queueEvent(new UnitDamageEvent(
  2053. unit.ID,
  2054. DamageTimeMS,
  2055. moveTime,
  2056. zspeed,
  2057. zgravity,
  2058. zlimit,
  2059. source.OutHasKnockDown,
  2060. source.Attack));
  2061. //unit.SetActionStatus(UnitActionStatus.Dead);
  2062. }
  2063. override protected void onUpdate()
  2064. {
  2065. if (hitMoveSpeed != null)
  2066. {
  2067. if (hitMoveSpeed.IsEnd)
  2068. {
  2069. hitMoveSpeed = null;
  2070. if (source.Attack.FlyFallenDownAttack != null)
  2071. {
  2072. doFlyDownAttack();
  2073. }
  2074. }
  2075. else if (source.Attack.HitMoveBodyAttack != null)
  2076. {
  2077. doBodyAttack();
  2078. }
  2079. }
  2080. else if (damageExpire != null)
  2081. {
  2082. if (damageExpire.Update(zone.UpdateIntervalMS))
  2083. {
  2084. damageExpire = null;
  2085. }
  2086. }
  2087. else
  2088. {
  2089. end();
  2090. }
  2091. }
  2092. override protected void onStop()
  2093. {
  2094. if (unit.IsStun)
  2095. {
  2096. unit.changeState(new StateStun(unit));
  2097. }
  2098. else if (nextState != null)
  2099. {
  2100. unit.changeState(nextState.AsState());
  2101. }
  2102. }
  2103. private void end()
  2104. {
  2105. isEnd = true;
  2106. unit.changeState(new StateDead(unit, attacker));
  2107. }
  2108. /// <summary>
  2109. /// 自己落地摔一下
  2110. /// </summary>
  2111. private void doFlyDownAttack()
  2112. {
  2113. unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
  2114. }
  2115. /// <summary>
  2116. /// 飞行过程中打别人
  2117. /// </summary>
  2118. private void doBodyAttack()
  2119. {
  2120. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  2121. {
  2122. zone.getObjectsRoundLineRange<InstanceUnit>(
  2123. Collider.Object_HitBody_TouchRoundLine,
  2124. hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
  2125. unit.X, unit.Y,
  2126. unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
  2127. list, unit.AoiStatus);
  2128. if (list.Count > 0)
  2129. {
  2130. CUtils.RemoveAll(list, body_hited.Values);
  2131. zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
  2132. if (list.Count > 0)
  2133. {
  2134. for (int i = 0; i < list.Count; i++)
  2135. {
  2136. InstanceUnit o = list[i];
  2137. body_hited.Put(o.ID, o);
  2138. }
  2139. }
  2140. }
  2141. }
  2142. }
  2143. }
  2144. /// <summary>
  2145. /// 死了,而且是被艹翻的,暴击打死然后击退
  2146. /// </summary>
  2147. public class StateDeadFuck : State, IStateNoneControllable
  2148. {
  2149. private readonly AttackSource source;
  2150. private readonly InstanceUnit attacker;
  2151. private float startDirection;
  2152. private bool isEnd = false;
  2153. private HitMoveSpeed hitMoveSpeed;
  2154. private TimeExpire<object> damageExpire;
  2155. private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
  2156. private IStateNoneControllable nextState;
  2157. public void SetNextNoneControllable(IStateNoneControllable state)
  2158. {
  2159. nextState = state;
  2160. }
  2161. public State AsState()
  2162. {
  2163. return this;
  2164. }
  2165. public bool IsFallingDown
  2166. {
  2167. get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
  2168. }
  2169. public bool IsKnockDown
  2170. {
  2171. get { return source.OutHasKnockDown; }
  2172. }
  2173. public bool IsDamageProtect
  2174. {
  2175. get { return source.Attack.IsDamageProtect; }
  2176. }
  2177. public int DamageTimeMS
  2178. {
  2179. get { return damageExpire.TotalTimeMS; }
  2180. }
  2181. public bool IsHitMove
  2182. {
  2183. get { return hitMoveSpeed != null; }
  2184. }
  2185. public StateDeadFuck(InstanceUnit unit, AttackSource source, InstanceUnit attacker)
  2186. : base(unit)
  2187. {
  2188. this.source = source;
  2189. this.attacker = attacker;
  2190. this.body_hited.Put(unit.ID, unit);
  2191. AttackProp.HitMoveType mtype = AttackProp.HitMoveType.BySenderPosition;
  2192. if (source.FromSpell != null)
  2193. {
  2194. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
  2195. }
  2196. else if (source.FromSkill != null)
  2197. {
  2198. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
  2199. }
  2200. else
  2201. {
  2202. this.startDirection = unit.Direction + CMath.PI_F;
  2203. }
  2204. // 计算受击时间 //
  2205. int damageTime = unit.Info.DamageTimeMS;
  2206. if (source.OutKnockDownTimeMS > 0)
  2207. {
  2208. damageTime = source.OutKnockDownTimeMS;
  2209. }
  2210. else if (unit.mInfo.DamageTimeMS > 0)
  2211. {
  2212. damageTime = unit.mInfo.DamageTimeMS;
  2213. }
  2214. else
  2215. {
  2216. damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
  2217. }
  2218. this.damageExpire = new TimeExpire<object>(damageTime);
  2219. }
  2220. override public bool onBlock(State new_state)
  2221. {
  2222. if (isEnd)
  2223. {
  2224. return true;
  2225. }
  2226. if (new_state is StateDead)
  2227. {
  2228. return true;
  2229. }
  2230. if (new_state is IStateNoneControllable)
  2231. {
  2232. this.SetNextNoneControllable(new_state as IStateNoneControllable);
  2233. }
  2234. if (unit.IsDead())
  2235. {
  2236. return false;
  2237. }
  2238. if (new_state is StateDamage)
  2239. {
  2240. return unit.CanWhiplashDeadBody;
  2241. }
  2242. if (new_state is StateRebirth)
  2243. {
  2244. return true;
  2245. }
  2246. return isEnd;
  2247. }
  2248. override protected void onStart()
  2249. {
  2250. unit.SetActionStatus(UnitActionStatus.Dead);
  2251. float zgravity = 0;
  2252. float zlimit = 0;
  2253. if (true)
  2254. {
  2255. int keepTimeMs = 0;
  2256. float speedSEC = 3;//这个数字要随机1-10
  2257. speedSEC = new Random().Next(0, 11);
  2258. this.hitMoveSpeed = unit.StartHitMove(this,
  2259. startDirection,
  2260. 0,
  2261. keepTimeMs,
  2262. speedSEC,
  2263. 0,
  2264. 0,
  2265. false);
  2266. if (true)
  2267. {
  2268. this.hitMoveSpeed.SetFly(this,
  2269. 15,//source.OutHitMove.ZSpeedSEC
  2270. 0,
  2271. 0);
  2272. zgravity = 0;
  2273. zlimit = 0;
  2274. }
  2275. }
  2276. int moveTime = 0;
  2277. float zspeed = 0;
  2278. if (hitMoveSpeed != null)
  2279. {
  2280. moveTime = hitMoveSpeed.TotalTimeMS;
  2281. zspeed = hitMoveSpeed.ZSpeedSEC;
  2282. }
  2283. unit.queueEvent(new UnitDamageEvent(
  2284. unit.ID,
  2285. DamageTimeMS,
  2286. moveTime,
  2287. zspeed,
  2288. zgravity,
  2289. zlimit,
  2290. source.OutHasKnockDown,
  2291. source.Attack));
  2292. unit.SetActionStatus(UnitActionStatus.Dead);
  2293. }
  2294. override protected void onUpdate()
  2295. {
  2296. if (hitMoveSpeed != null)
  2297. {
  2298. if (hitMoveSpeed.IsEnd)
  2299. {
  2300. hitMoveSpeed = null;
  2301. if (source.Attack.FlyFallenDownAttack != null)
  2302. {
  2303. doFlyDownAttack();
  2304. }
  2305. }
  2306. else if (source.Attack.HitMoveBodyAttack != null)
  2307. {
  2308. doBodyAttack();
  2309. }
  2310. }
  2311. else if (damageExpire != null)
  2312. {
  2313. if (damageExpire.Update(zone.UpdateIntervalMS))
  2314. {
  2315. damageExpire = null;
  2316. }
  2317. }
  2318. else
  2319. {
  2320. end();
  2321. }
  2322. }
  2323. override protected void onStop()
  2324. {
  2325. if (unit.IsStun)
  2326. {
  2327. unit.changeState(new StateStun(unit));
  2328. }
  2329. else if (nextState != null)
  2330. {
  2331. unit.changeState(nextState.AsState());
  2332. }
  2333. }
  2334. private void end()
  2335. {
  2336. isEnd = true;
  2337. unit.changeState(new StateDead(unit, attacker));
  2338. }
  2339. /// <summary>
  2340. /// 自己落地摔一下
  2341. /// </summary>
  2342. private void doFlyDownAttack()
  2343. {
  2344. unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
  2345. }
  2346. /// <summary>
  2347. /// 飞行过程中打别人
  2348. /// </summary>
  2349. private void doBodyAttack()
  2350. {
  2351. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  2352. {
  2353. zone.getObjectsRoundLineRange<InstanceUnit>(
  2354. Collider.Object_HitBody_TouchRoundLine,
  2355. hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
  2356. unit.X, unit.Y,
  2357. unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
  2358. list, unit.AoiStatus);
  2359. if (list.Count > 0)
  2360. {
  2361. CUtils.RemoveAll(list, body_hited.Values);
  2362. zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
  2363. if (list.Count > 0)
  2364. {
  2365. for (int i = 0; i < list.Count; i++)
  2366. {
  2367. InstanceUnit o = list[i];
  2368. body_hited.Put(o.ID, o);
  2369. }
  2370. }
  2371. }
  2372. }
  2373. }
  2374. }
  2375. /// <summary>
  2376. /// 死亡状态
  2377. /// </summary>
  2378. public class StateDead : State, IStateNoneControllable
  2379. {
  2380. private TimeExpire<int> dead_time;
  2381. private TimeExpire<int> rebirth_time;
  2382. private InstanceUnit attacker;
  2383. private bool crushed;
  2384. private List<TimeExpire<int>> dead_delayTimes = new List<TimeExpire<int>>();
  2385. public StateDead(InstanceUnit unit, InstanceUnit attacker, bool crush = false)
  2386. : base(unit)
  2387. {
  2388. this.attacker = attacker;
  2389. this.crushed = crush;
  2390. }
  2391. public State AsState()
  2392. {
  2393. return this;
  2394. }
  2395. override public bool onBlock(State new_state)
  2396. {
  2397. if (new_state is StateDamage)
  2398. {
  2399. return unit.CanWhiplashDeadBody;
  2400. }
  2401. if (new_state is StateRebirth)
  2402. {
  2403. return true;
  2404. }
  2405. return !unit.IsDead();
  2406. }
  2407. override protected void onStart()
  2408. {
  2409. //某些状态帧下死亡调用漏掉了
  2410. if (unit.IsNeedProcessDead())
  2411. {
  2412. if (unit.IsPlayer)
  2413. {
  2414. log.Warn("手动调用死亡逻辑1:" + unit.PlayerUUID + ", 攻击方:" + (attacker == null ? "null" : attacker.PlayerUUID));
  2415. }
  2416. unit.Parent.cb_unitDeadCallBack(unit, this.attacker);
  2417. }
  2418. unit.SetActionStatus(UnitActionStatus.Dead);
  2419. this.rebirth_time = new TimeExpire<int>(unit.mInfo.RebirthTimeMS);
  2420. this.dead_time = new TimeExpire<int>(unit.mInfo.DeadTimeMS);
  2421. if (unit.Info.DeadLaunchSpell != null)
  2422. {
  2423. // 自爆法术
  2424. zone.unitLaunchSpell(XmdsSkillType.none, unit, unit.Info.DeadLaunchSpell, unit.X, unit.Y);
  2425. }
  2426. // Boss 死亡之后灵魂飘到击杀者身上
  2427. if (unit.mInfo.DeadDelayTimeMS > 0)
  2428. {
  2429. dead_delayTimes.Clear();
  2430. for (var i = 0; i < unit.Info.DeadDelaySpellNumber; i++)
  2431. {
  2432. this.dead_delayTimes.Add(new TimeExpire<int>(
  2433. unit.mInfo.DeadDelayTimeMS + i * unit.Info.DeadDelaySpellIntervalMS));
  2434. }
  2435. }
  2436. }
  2437. override protected void onUpdate()
  2438. {
  2439. if (dead_time.Update(zone.UpdateIntervalMS))
  2440. {
  2441. if (unit.mInfo.RebirthTimeMS <= 0)
  2442. {
  2443. zone.RemoveObject(unit);
  2444. }
  2445. ////跨服场景中,单位死亡,需要移除单位,然后才能复活
  2446. //if (unit.IsDead() && unit.IsPlayer && unit.GetSceneType() >= CommonAI.Data.SceneType.CROSS_SERVER_NEW)
  2447. //{
  2448. // // zone.RemoveObject(unit);
  2449. // unit.DeadHide = true;
  2450. //}
  2451. }
  2452. if (rebirth_time.Update(zone.UpdateIntervalMS))
  2453. {
  2454. if (unit.mInfo.RebirthTimeMS > 0)
  2455. {
  2456. unit.startRebirth();
  2457. }
  2458. }
  2459. for (var i = 0; i < this.dead_delayTimes.Count; i++)
  2460. {
  2461. if (dead_delayTimes[i] != null && dead_delayTimes[i].Update(zone.UpdateIntervalMS))
  2462. {
  2463. zone.unitLaunchSpell(XmdsSkillType.none, unit, unit.Info.DeadDelayLaunchSpell,
  2464. unit.X, unit.Y, attacker == null ? 0 : ((InstanceUnit)attacker).mSyncInfo.ObjectID);
  2465. dead_delayTimes[i] = null;
  2466. }
  2467. }
  2468. }
  2469. override protected void onStop()
  2470. {
  2471. //unit.SetActionStatus(UnitActionStatus.Idle);
  2472. }
  2473. }
  2474. /// <summary>
  2475. /// 复活
  2476. /// </summary>
  2477. public class StateRebirth : State, IStateNoneControllable
  2478. {
  2479. private bool is_done = false;
  2480. private int max_hp = 0;
  2481. private int max_mp = 0;
  2482. public StateRebirth(InstanceUnit unit, int max_hp, int max_mp)
  2483. : base(unit)
  2484. {
  2485. this.max_hp = max_hp;
  2486. this.max_mp = max_mp;
  2487. }
  2488. public State AsState()
  2489. {
  2490. return this;
  2491. }
  2492. override public bool onBlock(State new_state)
  2493. {
  2494. return is_done;
  2495. }
  2496. override protected void onStart()
  2497. {
  2498. unit.SetActionStatus(UnitActionStatus.Rebirth);
  2499. }
  2500. override protected void onUpdate()
  2501. {
  2502. this.is_done = true;
  2503. unit.doRebirth(max_hp, max_mp);
  2504. unit.doSomething();
  2505. }
  2506. override protected void onStop()
  2507. {
  2508. }
  2509. }
  2510. //---------------------------------------------------------------------------------------
  2511. /// <summary>
  2512. /// 眩晕状态
  2513. /// </summary>
  2514. public class StateStun : State, IStateNoneControllable
  2515. {
  2516. public StateStun(InstanceUnit unit)
  2517. : base(unit)
  2518. {
  2519. }
  2520. public State AsState()
  2521. {
  2522. return this;
  2523. }
  2524. override public bool onBlock(State new_state)
  2525. {
  2526. if (new_state is StateDamage)
  2527. {
  2528. return true;
  2529. }
  2530. if (unit.IsDead() && new_state is StateDead)
  2531. {
  2532. return true;
  2533. }
  2534. if (new_state is StateSkill)
  2535. {
  2536. StateSkill ss = new_state as StateSkill;
  2537. // 反击或者状态解除//
  2538. if (ss.SkillData.IsCounter)
  2539. {
  2540. return true;
  2541. }
  2542. }
  2543. return !unit.IsStun;
  2544. }
  2545. override protected void onStart()
  2546. {
  2547. unit.SetActionStatus(UnitActionStatus.Stun);
  2548. }
  2549. override protected void onUpdate()
  2550. {
  2551. if (!unit.IsStun)
  2552. {
  2553. unit.doSomething();
  2554. }
  2555. }
  2556. override protected void onStop()
  2557. {
  2558. //unit.SetActionStatus(UnitActionStatus.Idle);
  2559. }
  2560. }
  2561. /// <summary>
  2562. /// 不可移动状态
  2563. /// </summary>
  2564. public class StateChuanGongA : State, IStateNoneControllable
  2565. {
  2566. private bool markRemove;
  2567. public StateChuanGongA(InstanceUnit unit)
  2568. : base(unit)
  2569. {
  2570. }
  2571. public State AsState()
  2572. {
  2573. return this;
  2574. }
  2575. override public bool onBlock(State new_state)
  2576. {
  2577. if (this.markRemove)
  2578. {
  2579. return true;
  2580. }
  2581. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2582. }
  2583. public override void MarkRemove()
  2584. {
  2585. this.markRemove = true;
  2586. }
  2587. override protected void onStart()
  2588. {
  2589. this.markRemove = false;
  2590. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 60000;
  2591. unit.SetActionStatus(UnitActionStatus.ChuanGongA);
  2592. }
  2593. override protected void onUpdate()
  2594. {
  2595. if (this.markRemove)
  2596. {
  2597. unit.startIdle();
  2598. }
  2599. else if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2600. {
  2601. unit.doSomething();
  2602. }
  2603. }
  2604. override protected void onStop()
  2605. {
  2606. unit.SetActionStatus(UnitActionStatus.Idle);
  2607. }
  2608. }
  2609. /// <summary>
  2610. /// 不可移动状态
  2611. /// </summary>
  2612. public class StateChuanGongB : State, IStateNoneControllable
  2613. {
  2614. private bool markRemove;
  2615. public StateChuanGongB(InstanceUnit unit)
  2616. : base(unit)
  2617. {
  2618. }
  2619. public State AsState()
  2620. {
  2621. return this;
  2622. }
  2623. override public bool onBlock(State new_state)
  2624. {
  2625. if (this.markRemove)
  2626. {
  2627. return true;
  2628. }
  2629. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2630. }
  2631. public override void MarkRemove()
  2632. {
  2633. this.markRemove = true;
  2634. }
  2635. override protected void onStart()
  2636. {
  2637. this.markRemove = false;
  2638. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 60000;
  2639. unit.SetActionStatus(UnitActionStatus.ChuanGongB);
  2640. }
  2641. override protected void onUpdate()
  2642. {
  2643. if (this.markRemove)
  2644. {
  2645. unit.startIdle();
  2646. }
  2647. else if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2648. {
  2649. unit.doSomething();
  2650. }
  2651. }
  2652. override protected void onStop()
  2653. {
  2654. unit.SetActionStatus(UnitActionStatus.Idle);
  2655. }
  2656. }
  2657. /// <summary>
  2658. /// 不可移动状态 净妖,和传功一样锁定4秒,时间变量用传功的
  2659. /// </summary>
  2660. public class StateClearYaoQi : State, IStateNoneControllable
  2661. {
  2662. public StateClearYaoQi(InstanceUnit unit)
  2663. : base(unit)
  2664. {
  2665. }
  2666. public State AsState()
  2667. {
  2668. return this;
  2669. }
  2670. override public bool onBlock(State new_state)
  2671. {
  2672. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2673. }
  2674. override protected void onStart()
  2675. {
  2676. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 4000;
  2677. unit.SetActionStatus(UnitActionStatus.ClearYaoQi);
  2678. }
  2679. override protected void onUpdate()
  2680. {
  2681. if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2682. {
  2683. unit.doSomething();
  2684. }
  2685. }
  2686. override protected void onStop()
  2687. {
  2688. unit.SetActionStatus(UnitActionStatus.Idle);
  2689. }
  2690. }
  2691. /// <summary>
  2692. /// 打坐
  2693. /// </summary>
  2694. public class StateDaZuo : State
  2695. {
  2696. private float startX;
  2697. private float startY;
  2698. private long lockEndTime;
  2699. private bool markRemove;
  2700. public StateDaZuo(InstanceUnit unit, int time)
  2701. : base(unit)
  2702. {
  2703. int lockTime = Math.Max(1, time);
  2704. //锁定短暂时间,避免一打坐就被客户端取消了。
  2705. lockEndTime = 0;// CommonLang.CUtils.localTimeMS + lockTime * 1000;
  2706. }
  2707. public override void MarkRemove()
  2708. {
  2709. this.markRemove = true;
  2710. }
  2711. override public bool onBlock(State new_state)
  2712. {
  2713. if (this.markRemove)
  2714. {
  2715. return true;
  2716. }
  2717. if (CommonLang.CUtils.localTimeMS < lockEndTime)
  2718. {
  2719. return false;
  2720. }
  2721. if (new_state is StateIdle)
  2722. {
  2723. return false;
  2724. }
  2725. return true;
  2726. }
  2727. override protected void onStart()
  2728. {
  2729. unit.SetActionStatus(UnitActionStatus.DaZuo);
  2730. }
  2731. override protected void onUpdate()
  2732. {
  2733. if(this.markRemove)
  2734. {
  2735. unit.startIdle();
  2736. }
  2737. else if(lockEndTime > CommonLang.CUtils.localTimeMS)
  2738. {
  2739. startX = unit.X;
  2740. startY = unit.Y;
  2741. }
  2742. else if (unit.X != startX || unit.Y != startY)
  2743. {
  2744. unit.doSomething();
  2745. }
  2746. }
  2747. override protected void onStop()
  2748. {
  2749. unit.SetActionStatus(UnitActionStatus.Idle);
  2750. }
  2751. }
  2752. /// <summary>
  2753. /// 打坐恢复血量
  2754. /// </summary>
  2755. public class StateDaZuoRecoveryAttr : State
  2756. {
  2757. private long endTime;//回血结束时间
  2758. private bool markRemove;
  2759. private int sustainTime; //持续时间
  2760. private long nextRecoveryTime; //下一次恢复时间
  2761. public StateDaZuoRecoveryAttr(InstanceUnit unit, int time)
  2762. : base(unit)
  2763. {
  2764. endTime = CommonLang.CUtils.CurrentTimeMS + time*1000;
  2765. sustainTime = time-3; //减去3是因为等待坐下去后才开始回血
  2766. nextRecoveryTime = CommonLang.CUtils.CurrentTimeMS+2350; //2350是客户端坐下去整个动作的时间,等待动作播放完成,开始回血
  2767. }
  2768. public override void MarkRemove()
  2769. {
  2770. this.markRemove = true;
  2771. }
  2772. override public bool onBlock(State new_state)
  2773. {
  2774. return true;
  2775. }
  2776. override protected void onStart()
  2777. {
  2778. unit.SetActionStatus(UnitActionStatus.DaZuoRecoveryAttr);
  2779. }
  2780. override protected void onUpdate()
  2781. {
  2782. if(this.markRemove)
  2783. {
  2784. unit.startIdle();
  2785. }
  2786. else
  2787. {
  2788. if (unit.Virtual.GetBattleStatus() != BattleStatus.None)
  2789. {
  2790. unit.startIdle();
  2791. return;
  2792. }
  2793. if (CommonLang.CUtils.CurrentTimeMS > endTime)
  2794. {
  2795. unit.startIdle();
  2796. return;
  2797. }
  2798. if (nextRecoveryTime < CommonLang.CUtils.CurrentTimeMS)
  2799. {
  2800. int Recover = 0;
  2801. //恢复主角血量
  2802. // if (unit.CurrentHP < unit.MaxHP)
  2803. //{
  2804. Recover = (int)(unit.MaxHP / sustainTime);
  2805. /*if (unit.CurrentHP + Recover > unit.MaxHP)
  2806. {
  2807. Recover = unit.MaxHP - unit.CurrentHP;
  2808. }*/
  2809. unit.add_hp(Recover);
  2810. // }
  2811. //恢复宠物血量
  2812. InstanceUnit petUnit = unit.Virtual.GetPetUnit();
  2813. if (petUnit != null)
  2814. {
  2815. if (petUnit.CurrentHP < petUnit.MaxHP && petUnit.Virtual.GetBattleStatus() == BattleStatus.None)
  2816. {
  2817. Recover = (int)(petUnit.MaxHP / sustainTime);
  2818. if (petUnit.CurrentHP + Recover > petUnit.MaxHP)
  2819. {
  2820. Recover = petUnit.MaxHP - petUnit.CurrentHP;
  2821. }
  2822. petUnit.add_hp(Recover);
  2823. }
  2824. }
  2825. nextRecoveryTime = CommonLang.CUtils.CurrentTimeMS + 1 * 1000;
  2826. }
  2827. }
  2828. }
  2829. override protected void onStop()
  2830. {
  2831. unit.SetActionStatus(UnitActionStatus.Idle);
  2832. }
  2833. }
  2834. /// <summary>
  2835. /// 随从向玩家瞬移
  2836. /// </summary>
  2837. public class PetLockState : State
  2838. {
  2839. private long endTime;//瞬移后定住时间
  2840. private bool markRemove;
  2841. public PetLockState(InstanceUnit unit)
  2842. : base(unit)
  2843. {
  2844. endTime = CommonLang.CUtils.CurrentTimeMS + 1*1000; //持续时间1秒
  2845. }
  2846. public override void MarkRemove()
  2847. {
  2848. this.markRemove = true;
  2849. }
  2850. override public bool onBlock(State new_state)
  2851. {
  2852. return true;
  2853. }
  2854. override protected void onStart()
  2855. {
  2856. unit.SetActionStatus(UnitActionStatus.PetLock);
  2857. }
  2858. override protected void onUpdate()
  2859. {
  2860. if(this.markRemove)
  2861. {
  2862. unit.startIdle();
  2863. }
  2864. else
  2865. {
  2866. if (CommonLang.CUtils.CurrentTimeMS > endTime)
  2867. {
  2868. unit.startIdle();
  2869. return;
  2870. }
  2871. }
  2872. }
  2873. override protected void onStop()
  2874. {
  2875. unit.SetActionStatus(UnitActionStatus.Idle);
  2876. }
  2877. }
  2878. /// <summary>
  2879. /// 渡劫场景,不能移动
  2880. /// </summary>
  2881. public class StateDuJie : State, IStateNoneControllable
  2882. {
  2883. public StateDuJie(InstanceUnit unit)
  2884. : base(unit)
  2885. {
  2886. }
  2887. public State AsState()
  2888. {
  2889. return this;
  2890. }
  2891. override public bool onBlock(State new_state)
  2892. {
  2893. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2894. }
  2895. override protected void onStart()
  2896. {
  2897. // 管他多长时间,锁一个小时再说
  2898. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 3600000;
  2899. unit.SetActionStatus(UnitActionStatus.CrossRobbery);
  2900. }
  2901. override protected void onUpdate()
  2902. {
  2903. //if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2904. //{
  2905. // unit.doSomething();
  2906. //}
  2907. }
  2908. override protected void onStop()
  2909. {
  2910. unit.SetActionStatus(UnitActionStatus.Idle);
  2911. }
  2912. }
  2913. /// <summary>
  2914. /// 渡劫场景,不能移动
  2915. /// </summary>
  2916. public class StateTransport : State, IStateNoneControllable
  2917. {
  2918. private long mLockEndTime = 0;
  2919. public StateTransport(InstanceUnit unit)
  2920. : base(unit)
  2921. {
  2922. }
  2923. public State AsState()
  2924. {
  2925. return this;
  2926. }
  2927. override public bool onBlock(State new_state)
  2928. {
  2929. if(new_state is StateSkill)
  2930. {
  2931. return true;
  2932. }
  2933. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2934. }
  2935. override protected void onStart()
  2936. {
  2937. // 管他多长时间,锁定300ms
  2938. this.mLockEndTime = CommonLang.CUtils.localTimeMS + 250;
  2939. unit.SetActionStatus(UnitActionStatus.Transport);
  2940. //System.Console.WriteLine("开始传送状态:" + CommonLang.CUtils.localTimeMS);
  2941. }
  2942. override protected void onUpdate()
  2943. {
  2944. if (this.mLockEndTime <= CommonLang.CUtils.localTimeMS)
  2945. {
  2946. unit.doSomething();
  2947. }
  2948. }
  2949. override protected void onStop()
  2950. {
  2951. unit.SetActionStatus(UnitActionStatus.Idle);
  2952. //System.Console.WriteLine("end传送状态:" + CommonLang.CUtils.localTimeMS);
  2953. }
  2954. }
  2955. //---------------------------------------------------------------------------------------
  2956. /// <summary>
  2957. /// 状态限制移动
  2958. /// </summary>
  2959. abstract public class StateMoveNoneControllable : State, IStateNoneControllable
  2960. {
  2961. private TimeExpire<int> mExpire;
  2962. private MoveAI mMoveAI;
  2963. private MoveBlockResult mLastResult;
  2964. public MoveBlockResult LastMoveResult { get { return mLastResult; } }
  2965. public State AsState()
  2966. {
  2967. return this;
  2968. }
  2969. public StateMoveNoneControllable(InstanceUnit unit, int timeMS)
  2970. : base(unit)
  2971. {
  2972. this.mExpire = new TimeExpire<int>(timeMS);
  2973. this.mMoveAI = new MoveAI(unit, false);
  2974. }
  2975. /// <summary>
  2976. /// 搜索要去的地方
  2977. /// </summary>
  2978. /// <returns></returns>
  2979. protected abstract Vector2 FindTargetPos();
  2980. public override bool onBlock(State new_state)
  2981. {
  2982. if (new_state is StateDead)
  2983. {
  2984. return true;
  2985. }
  2986. if (new_state is StateDamage)
  2987. {
  2988. (new_state as StateDamage).SetNextNoneControllable(this);
  2989. return true;
  2990. }
  2991. if (new_state is StateSkill)
  2992. {
  2993. StateSkill ss = new_state as StateSkill;
  2994. // 反击或者状态解除//
  2995. if (ss.SkillData.IsCounter)
  2996. {
  2997. return true;
  2998. }
  2999. }
  3000. return mExpire.IsEnd;
  3001. }
  3002. protected override void onStart()
  3003. {
  3004. Vector2 target = this.FindTargetPos();
  3005. this.mMoveAI.FindPath(target.X, target.Y);
  3006. }
  3007. protected override void onUpdate()
  3008. {
  3009. if (mExpire.Update(zone.UpdateIntervalMS))
  3010. {
  3011. unit.doSomething();
  3012. }
  3013. else
  3014. {
  3015. mLastResult = mMoveAI.Update();
  3016. if ((mLastResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  3017. {
  3018. Vector2 target = this.FindTargetPos();
  3019. this.mMoveAI.FindPath(target.X, target.Y);
  3020. }
  3021. }
  3022. }
  3023. protected override void onStop()
  3024. {
  3025. }
  3026. }
  3027. /// <summary>
  3028. /// 拾取状态
  3029. /// </summary>
  3030. public class StateAutoPick : State
  3031. {
  3032. private InstanceItem mPickItem; //要自动拾取的物品
  3033. private MoveAI mMoveAi;
  3034. bool mIsEnd;
  3035. public StateAutoPick(InstanceUnit unit, InstanceItem item)
  3036. : base(unit)
  3037. {
  3038. this.mPickItem = item;
  3039. this.mMoveAi = new MoveAI(unit);
  3040. this.mMoveAi.FindPath(item.X, item.Y);
  3041. }
  3042. override public bool onBlock(State new_state)
  3043. {
  3044. if (unit.IsDead())
  3045. {
  3046. return true;
  3047. }
  3048. return true;
  3049. }
  3050. override protected void onStart()
  3051. {
  3052. //unit.SetActionStatus(UnitActionStatus.AutoPick);
  3053. }
  3054. override protected void onUpdate()
  3055. {
  3056. if (mIsEnd || this.mPickItem.IsDead() || !this.mPickItem.Enable || this.unit.Virtual.GetBattleStatus() != BattleStatus.None)
  3057. {
  3058. unit.doSomething();
  3059. }
  3060. else
  3061. {
  3062. MoveBlockResult result = mMoveAi.Update();
  3063. if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
  3064. {
  3065. mIsEnd = true;
  3066. unit.doSomething();
  3067. }
  3068. else
  3069. {
  3070. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  3071. if (CMath.includeRoundPoint(unit.X, unit.Y, r, mPickItem.X, mPickItem.Y))
  3072. {
  3073. mIsEnd = true;
  3074. unit.doSomething();
  3075. }
  3076. }
  3077. }
  3078. }
  3079. override protected void onStop()
  3080. {
  3081. unit.SetActionStatus(UnitActionStatus.Idle);
  3082. }
  3083. }
  3084. //---------------------------------------------------------------------------------------
  3085. /// <summary>
  3086. /// 逃跑状态
  3087. /// </summary>
  3088. public class StateEscape : StateMoveNoneControllable
  3089. {
  3090. private float mDistance;
  3091. public StateEscape(InstanceUnit unit, int timeMS, float distance = 0) : base(unit, timeMS)
  3092. {
  3093. if (distance > 0)
  3094. {
  3095. this.mDistance = distance;
  3096. }
  3097. else
  3098. {
  3099. this.mDistance = unit.Info.GuardRange;
  3100. }
  3101. }
  3102. protected override void onStart()
  3103. {
  3104. base.onStart();
  3105. unit.SetActionStatus(UnitActionStatus.Escape);
  3106. }
  3107. protected override Vector2 FindTargetPos()
  3108. {
  3109. var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, unit.X, unit.Y, mDistance);
  3110. return new Vector2(node.BX, node.BY);
  3111. }
  3112. }
  3113. //---------------------------------------------------------------------------------------
  3114. /// <summary>
  3115. /// 混乱状态
  3116. /// </summary>
  3117. public class StateChaos : StateMoveNoneControllable
  3118. {
  3119. public StateChaos(InstanceUnit unit, int timeMS) : base(unit, timeMS)
  3120. {
  3121. }
  3122. protected override void onStart()
  3123. {
  3124. base.onStart();
  3125. unit.SetActionStatus(UnitActionStatus.Chaos);
  3126. }
  3127. protected override void onStop()
  3128. {
  3129. base.onStop();
  3130. }
  3131. protected override Vector2 FindTargetPos()
  3132. {
  3133. var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, unit.X, unit.Y, unit.Info.GuardRange);
  3134. return new Vector2(node.BX, node.BY);
  3135. }
  3136. }
  3137. //---------------------------------------------------------------------------------------
  3138. /// <summary>
  3139. /// 破定
  3140. /// </summary>
  3141. public class StateBreakShield : State
  3142. {
  3143. private long mStateEndTime;
  3144. public StateBreakShield(InstanceUnit unit) : base(unit)
  3145. {
  3146. this.mStateEndTime = CommonLang.CUtils.localTimeMS + GlobalData.SHIELD_BREAK_HOLDTIME;
  3147. }
  3148. protected override void onStart()
  3149. {
  3150. unit.SetActionStatus(UnitActionStatus.BreakShield);
  3151. unit.AddBuff(GlobalData.SHIELD_BREAK_BUFF, 0, true);
  3152. }
  3153. protected override void onUpdate()
  3154. {
  3155. if(CommonLang.CUtils.localTimeMS > this.mStateEndTime)
  3156. {
  3157. unit.startIdle();
  3158. }
  3159. }
  3160. public override bool onBlock(State new_state)
  3161. {
  3162. if (CommonLang.CUtils.localTimeMS > this.mStateEndTime)
  3163. {
  3164. return true;
  3165. }
  3166. else if (new_state is StateDead)
  3167. {
  3168. return true;
  3169. }
  3170. return false;
  3171. }
  3172. protected override void onStop()
  3173. {
  3174. unit.SetActionStatus(UnitActionStatus.Idle);
  3175. //unit.removeBuff(GlobalData.SHIELD_BREAK_BUFF);
  3176. unit.Virtual.StartRecoverMP();
  3177. }
  3178. }
  3179. //---------------------------------------------------------------------------------------
  3180. #endregion
  3181. #region __AI行为__
  3182. //---------------------------------------------------------------------------------------
  3183. /// <summary>
  3184. /// 移动状态
  3185. /// </summary>
  3186. abstract public class StateFollow : State
  3187. {
  3188. public enum MoveState
  3189. {
  3190. Hold,
  3191. Move,
  3192. }
  3193. // 被追目标
  3194. protected IPositionObject target;
  3195. // 如果被单位碰撞,则向左或右挪动//
  3196. readonly private MoveAI moveAI;
  3197. // 检测切换走停的间隔时间 //
  3198. readonly private State<MoveState> state = new State<MoveState>(MoveState.Move);
  3199. readonly private bool begin_in_min_range;
  3200. // 检测切换走停的间隔时间 //
  3201. private TimeInterval<int> start_move_hold_time;
  3202. public abstract bool IsActive { get; }
  3203. public MoveAI MoveAI
  3204. {
  3205. get
  3206. {
  3207. return moveAI;
  3208. }
  3209. private set { }
  3210. }
  3211. public bool IsNoWay { get { if (moveAI != null) { return moveAI.IsNoWay; } return false; } }
  3212. public IPositionObject Target { get { return target; } }
  3213. public MoveState FollowState { get { return state.Value; } }
  3214. /// <summary>
  3215. /// Hold到Move之间的检测间隔
  3216. /// </summary>
  3217. public int StartMoveHoldTimeMS
  3218. {
  3219. get { return (start_move_hold_time != null) ? start_move_hold_time.IntervalTimeMS : 0; }
  3220. set
  3221. {
  3222. if (value > 0 && value != StartMoveHoldTimeMS)
  3223. {
  3224. start_move_hold_time = new TimeInterval<int>(value);
  3225. }
  3226. }
  3227. }
  3228. public StateFollow(InstanceUnit unit, InstanceZoneObject target, bool beginInMinRange = true, int holdTime = 0) : base(unit)
  3229. {
  3230. this.begin_in_min_range = beginInMinRange;
  3231. this.target = target;
  3232. this.moveAI = new MoveAI(unit, true, holdTime);
  3233. }
  3234. public override bool onBlock(State new_state)
  3235. {
  3236. return true;
  3237. }
  3238. protected override void onStart()
  3239. {
  3240. if (this.unit.Info.SumType == SummonType.AwaitMaster || cheekBeginInRange())
  3241. {
  3242. state.ChangeState(MoveState.Hold);
  3243. unit.SetActionStatus(UnitActionStatus.Idle);
  3244. }
  3245. else
  3246. {
  3247. state.ChangeState(MoveState.Move);
  3248. unit.SetActionStatus(UnitActionStatus.Move);
  3249. moveAI.FindPath(target);
  3250. }
  3251. }
  3252. protected virtual void OnMoveAiNoWay() { }
  3253. protected override void onUpdate()
  3254. {
  3255. if (!IsActive)
  3256. {
  3257. unit.doSomething();
  3258. }
  3259. else if ((unit.CurrentActionSubstate & (int)UnitActionSubStatus.CanNotMove) > 0)
  3260. {
  3261. //不能移动,啥事也不干
  3262. }
  3263. else
  3264. {
  3265. switch (FollowState)
  3266. {
  3267. case MoveState.Move:
  3268. // 进入最小追踪距离 //
  3269. if (CheckTargetInMinRange() == true)
  3270. {
  3271. changeToHold();
  3272. }
  3273. else
  3274. {
  3275. if (moveAI.Target == null)
  3276. {
  3277. moveAI.FindPath(target);
  3278. }
  3279. moveAI.Update();
  3280. if (moveAI.IsNoWay)
  3281. {
  3282. this.OnMoveAiNoWay();
  3283. if (CheckTargetInMaxRange() == true)
  3284. {
  3285. changeToHold();
  3286. }
  3287. else
  3288. {
  3289. unit.doSomething();
  3290. }
  3291. }
  3292. }
  3293. break;
  3294. case MoveState.Hold:
  3295. // 超过最大追踪范围 //
  3296. if (unit.IsMoveAble() && (start_move_hold_time == null || start_move_hold_time.Update(zone.UpdateIntervalMS)) && !CheckTargetInMaxRange())
  3297. {
  3298. changeToMove();
  3299. }
  3300. else
  3301. {
  3302. if (unit.CurrentActionStatus != UnitActionStatus.Idle)
  3303. {
  3304. unit.SetActionStatus(UnitActionStatus.Idle);
  3305. }
  3306. }
  3307. onUpdateFollowed(target);
  3308. break;
  3309. }
  3310. }
  3311. }
  3312. protected override void onStop() { }
  3313. private bool cheekBeginInRange()
  3314. {
  3315. if (begin_in_min_range)
  3316. {
  3317. return CheckTargetInMinRange();
  3318. }
  3319. else
  3320. {
  3321. return CheckTargetInMaxRange();
  3322. }
  3323. }
  3324. private void changeToMove()
  3325. {
  3326. state.ChangeState(MoveState.Move);
  3327. unit.SetActionStatus(UnitActionStatus.Move);
  3328. moveAI.FindPath(target);
  3329. onInRangeChanged();
  3330. onChangedToMove(target);
  3331. }
  3332. private void changeToHold()
  3333. {
  3334. state.ChangeState(MoveState.Hold);
  3335. unit.SetActionStatus(UnitActionStatus.Idle);
  3336. moveAI.Pause();
  3337. onInRangeChanged();
  3338. onChangedToHold(target);
  3339. }
  3340. /// <summary>
  3341. /// 检查目标是否在跟随范围内。
  3342. /// 检测为True,停止移动。
  3343. /// </summary>
  3344. protected abstract bool CheckTargetInMinRange();
  3345. /// <summary>
  3346. /// 检查目标是否在跟随范围内。
  3347. /// 检测为False,开始移动。
  3348. /// </summary>
  3349. protected abstract bool CheckTargetInMaxRange();
  3350. /// <summary>
  3351. /// 行为改变
  3352. /// </summary>
  3353. protected virtual void onInRangeChanged()
  3354. {
  3355. }
  3356. /// <summary>
  3357. /// 目标已经被追踪到
  3358. /// </summary>
  3359. /// <param name="target"></param>
  3360. protected virtual void onUpdateFollowed(IPositionObject target)
  3361. {
  3362. }
  3363. /// <summary>
  3364. /// 开始移动
  3365. /// </summary>
  3366. /// <param name="target"></param>
  3367. protected virtual void onChangedToMove(IPositionObject target)
  3368. {
  3369. }
  3370. /// <summary>
  3371. /// changeToHold
  3372. /// </summary>
  3373. /// <param name="target"></param>
  3374. protected virtual void onChangedToHold(IPositionObject target)
  3375. {
  3376. }
  3377. }
  3378. /// <summary>
  3379. ///
  3380. /// </summary>
  3381. public class StateFollowObject : StateFollow
  3382. {
  3383. protected float distance;
  3384. //protected InstanceZoneObject target;
  3385. public override bool IsActive
  3386. {
  3387. get
  3388. {
  3389. var t = target as InstanceZoneObject;
  3390. if (t != null)
  3391. {
  3392. return t.Enable;
  3393. }
  3394. return false;
  3395. }
  3396. }
  3397. public float Distance
  3398. {
  3399. set
  3400. {
  3401. this.distance = value;
  3402. }
  3403. get
  3404. {
  3405. return this.distance;
  3406. }
  3407. }
  3408. public StateFollowObject(InstanceUnit unit, InstanceZoneObject target, int holdTime = 0)
  3409. : base(unit, target, true, holdTime)
  3410. {
  3411. //this.target = target;
  3412. this.distance = Target.RadiusSize + unit.BodyBlockSize * 2;
  3413. }
  3414. protected override bool CheckTargetInMaxRange()
  3415. {
  3416. return CMath.includeRoundPoint(
  3417. unit.X, unit.Y,
  3418. distance * 2,
  3419. Target.X, Target.Y);
  3420. }
  3421. protected override bool CheckTargetInMinRange()
  3422. {
  3423. return CMath.includeRoundPoint(
  3424. unit.X, unit.Y,
  3425. distance,
  3426. Target.X, Target.Y);
  3427. }
  3428. }
  3429. public class VStateFollowLeader : StateFollowObject
  3430. {
  3431. public VStateFollowLeader(InstanceUnit unit, InstanceZoneObject target, int holdTime = 0) : base(unit, target, holdTime)
  3432. {
  3433. }
  3434. }
  3435. /// <summary>
  3436. ///
  3437. /// </summary>
  3438. public class StateFollowAndGuard : StateFollow
  3439. {
  3440. private readonly float distance_min;
  3441. private readonly float distance_max;
  3442. private readonly InstanceUnit targetUnit;
  3443. public InstanceUnit TargetUnit { get { return targetUnit; } }
  3444. public override bool IsActive { get { return targetUnit.Enable; } }
  3445. public StateFollowAndGuard(InstanceUnit unit, InstanceUnit target, float minDistance, float maxDistance)
  3446. : base(unit, target)
  3447. {
  3448. this.targetUnit = target;
  3449. this.distance_min = Math.Max(minDistance, target.BodyBlockSize + unit.BodyBlockSize * 2);
  3450. this.distance_max = Math.Max(maxDistance, distance_min);
  3451. }
  3452. protected override bool CheckTargetInMinRange()
  3453. {
  3454. return CMath.includeRoundPoint(
  3455. unit.X, unit.Y,
  3456. distance_min,
  3457. Target.X, Target.Y);
  3458. }
  3459. protected override bool CheckTargetInMaxRange()
  3460. {
  3461. return CMath.includeRoundPoint(
  3462. unit.X, unit.Y,
  3463. distance_max,
  3464. Target.X, Target.Y);
  3465. }
  3466. }
  3467. /// <summary>
  3468. ///
  3469. /// </summary>
  3470. public class StateFollowAndAttack : StateFollow
  3471. {
  3472. /*readonly private*/
  3473. InstanceUnit targetUnit;
  3474. private SkillTemplate.CastTarget expectTarget;
  3475. private TimeInterval<int> checkAdjustLaunchSkillTime;
  3476. private SkillState launchSkill;
  3477. private bool can_auto_focus_near_target;
  3478. private bool can_random_skill = true;
  3479. private bool can_attack = true;
  3480. private float min_distance;
  3481. private float max_distance;
  3482. /// <summary>
  3483. /// 是否追踪
  3484. /// </summary>
  3485. /// <returns></returns>
  3486. override public bool IsActive
  3487. {
  3488. get { return can_attack && targetUnit.IsActive; }
  3489. }
  3490. public SkillState ExpectSkillState
  3491. {
  3492. get { return launchSkill; }
  3493. set
  3494. {
  3495. launchSkill = value;
  3496. expectTarget = value.Data.ExpectTarget;
  3497. }
  3498. }
  3499. public SkillTemplate ExpectSkill
  3500. {
  3501. get { return (launchSkill != null) ? launchSkill.Data : unit.DefaultSkill; }
  3502. }
  3503. public InstanceUnit TargetUnit
  3504. {
  3505. get { return targetUnit; }
  3506. set { targetUnit = value; }
  3507. }
  3508. public SkillTemplate.CastTarget ExpectTarget
  3509. {
  3510. get { return expectTarget; }
  3511. }
  3512. public bool IsLaunchRandomSkill
  3513. {
  3514. get { return can_random_skill; }
  3515. set { can_random_skill = value; }
  3516. }
  3517. public bool IsAutoFocusNearTarget
  3518. {
  3519. get { return can_auto_focus_near_target; }
  3520. set { can_auto_focus_near_target = value; }
  3521. }
  3522. public StateFollowAndAttack(
  3523. InstanceUnit unit,
  3524. InstanceUnit target,
  3525. SkillTemplate.CastTarget expectTarget = SkillTemplate.CastTarget.Enemy,
  3526. bool autoFocusNearTarget = false)
  3527. : base(unit, target, false)
  3528. {
  3529. this.targetUnit = target;
  3530. // Console.WriteLine(" - - - - StateFollowAndAttack - - - - " + this.TargetUnit.Name);
  3531. this.expectTarget = expectTarget;
  3532. this.can_auto_focus_near_target = autoFocusNearTarget;
  3533. this.StartMoveHoldTimeMS = zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS;
  3534. }
  3535. protected override void onStart()
  3536. {
  3537. this.checkAdjustLaunchSkillTime = new TimeInterval<int>(zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS);
  3538. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  3539. base.onStart();
  3540. }
  3541. protected override void onUpdateFollowed(IPositionObject target)
  3542. {
  3543. this.can_attack = zone.IsAttackable(unit, targetUnit, expectTarget, AttackReason.Tracing, this.ExpectSkill);
  3544. if (can_attack)
  3545. {
  3546. unit.faceTo(target.X, target.Y);
  3547. if (TryLaunchSkill() == null)
  3548. {
  3549. SkillTemplate expect_skill = ExpectSkill;
  3550. if (expect_skill != null && expect_skill.AttackKeepRange > 0)
  3551. {
  3552. if (checkAdjustLaunchSkillTime.IntervalTimeMS == 0 || checkAdjustLaunchSkillTime.Update(zone.UpdateIntervalMS))
  3553. {
  3554. if (CUtils.RandomPercent(zone.RandomN, zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_ADJUST_ESCAPE_PCT))
  3555. {
  3556. unit.startAdjustLaunchSkill(expect_skill, targetUnit, this.OnEndAdjustKeepRange);
  3557. }
  3558. }
  3559. }
  3560. }
  3561. }
  3562. }
  3563. protected override void onUpdate()
  3564. {
  3565. if (IsActive)
  3566. {
  3567. resetMaxMinRange();
  3568. }
  3569. base.onUpdate();
  3570. }
  3571. private void resetMaxMinRange()
  3572. {
  3573. var expect_skill = ExpectSkillState;
  3574. min_distance = unit.BodyBlockSize + targetUnit.BodyBlockSize;
  3575. max_distance = Math.Max(min_distance, unit.BodyBlockSize + targetUnit.BodyHitSize);
  3576. bool needCheck = true;
  3577. if (expect_skill == null)
  3578. {
  3579. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  3580. if (new_skill != null)
  3581. {
  3582. ExpectSkillState = new_skill;
  3583. expect_skill = new_skill;
  3584. needCheck = false;
  3585. }
  3586. }
  3587. if (expect_skill != null)
  3588. {
  3589. if (can_random_skill)
  3590. {
  3591. if (needCheck == true && !expect_skill.checkTargetRange(targetUnit))
  3592. {
  3593. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  3594. if (new_skill != null)
  3595. {
  3596. ExpectSkillState = new_skill;
  3597. expect_skill = new_skill;
  3598. }
  3599. }
  3600. }
  3601. float skill_distance = unit.GetSkillAttackRange(expect_skill.Data);
  3602. float half = (skill_distance - min_distance) / 2;
  3603. if (expect_skill.Data.AttackKeepRange > 0 && expect_skill.Data.AttackRange > expect_skill.Data.AttackKeepRange)
  3604. {
  3605. min_distance = Math.Max(min_distance, unit.GetSkillAttackRange(expect_skill.Data.AttackKeepRange) + targetUnit.BodyBlockSize);
  3606. max_distance = Math.Max(max_distance, min_distance);
  3607. }
  3608. else if (half > 0)
  3609. {
  3610. min_distance = min_distance + half;
  3611. max_distance = Math.Max(max_distance, min_distance);
  3612. }
  3613. else
  3614. {
  3615. min_distance = Math.Max(min_distance, skill_distance + targetUnit.BodyBlockSize);
  3616. max_distance = Math.Max(max_distance, min_distance);
  3617. }
  3618. max_distance = Math.Max(max_distance, skill_distance + targetUnit.BodyHitSize);
  3619. }
  3620. }
  3621. protected override bool CheckTargetInMaxRange()
  3622. {
  3623. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  3624. }
  3625. protected override bool CheckTargetInMinRange()
  3626. {
  3627. return CMath.includeRoundPoint(unit.X, unit.Y, min_distance, targetUnit.X, targetUnit.Y);
  3628. }
  3629. protected virtual bool OnEndAdjustKeepRange(StateMove m)
  3630. {
  3631. unit.faceTo(Target.X, Target.Y);
  3632. unit.changeState(this);
  3633. return true;
  3634. }
  3635. protected virtual SkillTemplate TryLaunchSkill()
  3636. {
  3637. if (launchSkill == null)
  3638. {
  3639. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  3640. }
  3641. StateSkill ret = null;
  3642. Vector2 targetPos = null;
  3643. if (launchSkill != null)
  3644. {
  3645. ret = unit.launchSkill(launchSkill, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target));
  3646. if (ret != null)
  3647. {
  3648. return ret.SkillData;
  3649. }
  3650. }
  3651. if (can_random_skill)
  3652. {
  3653. ret = unit.launchRandomSkill(expectTarget, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target));
  3654. if (ret != null)
  3655. {
  3656. return ret.SkillData;
  3657. }
  3658. }
  3659. return null;
  3660. }
  3661. }
  3662. /// <summary>
  3663. ///
  3664. /// </summary>
  3665. public class StateFollowAndPickObject : StateFollow
  3666. {
  3667. private readonly InstanceZoneObject targetObject;
  3668. private readonly int pickTimeMS;
  3669. private readonly StatePickObject.OnPickDone doneEvent;
  3670. private readonly string status;
  3671. public StateFollowAndPickObject(InstanceUnit unit, InstanceZoneObject target, int timeMS, StatePickObject.OnPickDone done, string status = null)
  3672. : base(unit, target, false)
  3673. {
  3674. this.targetObject = target;
  3675. this.pickTimeMS = timeMS;
  3676. this.doneEvent = done;
  3677. this.status = status;
  3678. }
  3679. public override bool IsActive
  3680. {
  3681. get { return targetObject.Enable; }
  3682. }
  3683. protected override bool CheckTargetInMinRange()
  3684. {
  3685. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, targetObject.X, targetObject.Y);
  3686. }
  3687. protected override bool CheckTargetInMaxRange()
  3688. {
  3689. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize + targetObject.BodyBlockSize, targetObject.X, targetObject.Y);
  3690. }
  3691. protected override void onUpdateFollowed(IPositionObject target)
  3692. {
  3693. unit.startPickProgressObject(targetObject, pickTimeMS, doneEvent, status);
  3694. }
  3695. }
  3696. /// <summary>
  3697. ///
  3698. /// </summary>
  3699. public class StateFollowAndPickItem : StateFollow
  3700. {
  3701. public readonly InstanceItem targetObject;
  3702. private long mLockEndTime;
  3703. private bool mDoPick;
  3704. private bool mMarkBlock;
  3705. public StateFollowAndPickItem(InstanceUnit unit, InstanceItem target, bool doPick)
  3706. : base(unit, target, false)
  3707. {
  3708. this.targetObject = target;
  3709. this.mDoPick = doPick;
  3710. }
  3711. public override bool IsActive
  3712. {
  3713. get { return (targetObject.Enable && targetObject.IsPosValid()) || mLockEndTime > CommonLang.CUtils.localTimeMS; }
  3714. }
  3715. public bool IsBlock()
  3716. {
  3717. return this.mMarkBlock;
  3718. }
  3719. protected override bool CheckTargetInMinRange()
  3720. {
  3721. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, targetObject.X, targetObject.Y);
  3722. }
  3723. protected override bool CheckTargetInMaxRange()
  3724. {
  3725. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize + targetObject.BodyBlockSize, targetObject.X, targetObject.Y);
  3726. }
  3727. protected override void onUpdateFollowed(IPositionObject target)
  3728. {
  3729. if (this.mDoPick && this.targetObject.Enable)
  3730. {
  3731. targetObject.PickItem(unit);
  3732. }
  3733. }
  3734. public void CheckAndLockPickTime()
  3735. {
  3736. if(!this.targetObject.Enable && mLockEndTime == 0)
  3737. {
  3738. mLockEndTime = CommonLang.CUtils.localTimeMS + 350;
  3739. }
  3740. }
  3741. protected override void onUpdate()
  3742. {
  3743. this.CheckAndLockPickTime();
  3744. if (!this.IsActive && unit.IsPlayer)
  3745. {
  3746. if (unit.DoAutoPick(true))
  3747. {
  3748. return;
  3749. }
  3750. }
  3751. base.onUpdate();
  3752. }
  3753. protected override void OnMoveAiNoWay()
  3754. {
  3755. this.mLockEndTime = 0;
  3756. targetObject.setPosNotValid();
  3757. }
  3758. public override bool onBlock(State new_state)
  3759. {
  3760. if(/*!(new_state is StateFollowAndPickItem) && */mLockEndTime > CommonLang.CUtils.localTimeMS)
  3761. {
  3762. return false;
  3763. }
  3764. //把当前拾取弄完,再去跟随
  3765. if (new_state is VStateFollowLeader && unit.DoAutoPick(true))
  3766. {
  3767. return false;
  3768. }
  3769. return true;
  3770. }
  3771. protected override void onStop()
  3772. {
  3773. this.mMarkBlock = true;
  3774. }
  3775. }
  3776. /// <summary>
  3777. ///
  3778. /// </summary>
  3779. public abstract class StateAttackTo : State
  3780. {
  3781. public IPositionObject Target { get { return target; } }
  3782. public MoveBlockResult LastMovingResult { get; private set; }
  3783. public MoveAI UnitMoveAI { get { return moveAI; } }
  3784. // 如果被地图碰撞,则寻路
  3785. private IPositionObject target;
  3786. // 如果被单位碰撞,则向左或右挪动
  3787. private MoveAI moveAI;
  3788. private int holdTimeMS;
  3789. private long mPeaceEndTime;
  3790. public StateAttackTo(InstanceUnit unit, int move_ai_hold_time_ms = -1, int peaceTime = 0)
  3791. : base(unit)
  3792. {
  3793. this.holdTimeMS = move_ai_hold_time_ms;
  3794. this.mPeaceEndTime = CommonLang.CUtils.CurrentTimeMS + peaceTime;
  3795. }
  3796. override public bool onBlock(State new_state)
  3797. {
  3798. if(this.mPeaceEndTime > CommonLang.CUtils.CurrentTimeMS)
  3799. {
  3800. return false;
  3801. }
  3802. return true;
  3803. }
  3804. override protected void onStart()
  3805. {
  3806. if (target == null)
  3807. {
  3808. target = PopNextPos();
  3809. }
  3810. this.moveAI = new MoveAI(unit, true, holdTimeMS);
  3811. if (target != null)
  3812. {
  3813. this.moveAI.FindPath(target);
  3814. unit.SetActionStatus(UnitActionStatus.Move);
  3815. }
  3816. else
  3817. {
  3818. unit.SetActionStatus(UnitActionStatus.Idle);
  3819. }
  3820. }
  3821. override protected void onUpdate()
  3822. {
  3823. if (target == null)
  3824. {
  3825. unit.doSomething();
  3826. }
  3827. else
  3828. {
  3829. LastMovingResult = this.moveAI.Update();
  3830. if (TestPopNext())
  3831. {
  3832. this.target = PopNextPos();
  3833. if (target != null)
  3834. {
  3835. this.moveAI.FindPath(target);
  3836. }
  3837. }
  3838. }
  3839. }
  3840. override protected void onStop()
  3841. {
  3842. }
  3843. abstract protected bool HasNextPos();
  3844. abstract protected IPositionObject PopNextPos();
  3845. protected virtual bool TestPopNext()
  3846. {
  3847. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  3848. {
  3849. return true;
  3850. }
  3851. return false;
  3852. }
  3853. public virtual bool IsDone
  3854. {
  3855. get
  3856. {
  3857. if (IsStarted)
  3858. {
  3859. if (target == null)
  3860. {
  3861. return true;
  3862. }
  3863. if (HasNextPos())
  3864. {
  3865. return false;
  3866. }
  3867. return TestPopNext();
  3868. }
  3869. return false;
  3870. }
  3871. }
  3872. }
  3873. public class StateAttackToZoneWayPoint : StateAttackTo
  3874. {
  3875. // 如果被地图碰撞,则寻路
  3876. private ZoneWayPoint paths;
  3877. private ZoneWayPoint prev;
  3878. protected ZoneWayPoint Paths { get { return paths; } }
  3879. public StateAttackToZoneWayPoint(InstanceUnit unit, ZoneWayPoint wps, int peaceTime)
  3880. : base(unit, -1, peaceTime)
  3881. {
  3882. this.paths = wps;
  3883. }
  3884. protected override bool HasNextPos()
  3885. {
  3886. return paths != null;
  3887. }
  3888. protected override IPositionObject PopNextPos()
  3889. {
  3890. if (paths != null)
  3891. {
  3892. ZoneWayPoint ret = paths;
  3893. ZoneWayPoint prv = prev;
  3894. prev = paths;
  3895. paths = paths.GetRandomNext(prv);
  3896. return ret;
  3897. }
  3898. return null;
  3899. }
  3900. protected override bool TestPopNext()
  3901. {
  3902. if (LastMovingResult.obj != null)
  3903. {
  3904. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, Target.X, Target.Y))
  3905. {
  3906. return true;
  3907. }
  3908. }
  3909. else
  3910. {
  3911. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, Target.X, Target.Y))
  3912. {
  3913. return true;
  3914. }
  3915. }
  3916. return false;
  3917. }
  3918. }
  3919. public class StatePatrolWayPoint : StateAttackTo
  3920. {
  3921. // 如果被地图碰撞,则寻路
  3922. private ZoneWayPoint paths;
  3923. private ZoneWayPoint prev;
  3924. private TimeExpire<int> hold_time;
  3925. private int hold_max_time_ms, hold_min_time_ms;
  3926. protected ZoneWayPoint Paths { get { return paths; } }
  3927. public StatePatrolWayPoint(InstanceUnit unit, ZoneWayPoint wps, int hold_max_time_ms, int hold_min_time_ms, int peaceTime)
  3928. : base(unit, -1, peaceTime)
  3929. {
  3930. this.paths = wps;
  3931. this.hold_max_time_ms = Math.Max(hold_max_time_ms, hold_min_time_ms);
  3932. this.hold_min_time_ms = Math.Min(hold_max_time_ms, hold_min_time_ms);
  3933. }
  3934. protected override void onUpdate()
  3935. {
  3936. if (this.hold_time != null)
  3937. {
  3938. if (this.hold_time.Update(zone.UpdateIntervalMS))
  3939. {
  3940. this.hold_time = null;
  3941. }
  3942. }
  3943. else
  3944. {
  3945. base.onUpdate();
  3946. }
  3947. }
  3948. protected override bool HasNextPos()
  3949. {
  3950. return paths != null;
  3951. }
  3952. private void Hold()
  3953. {
  3954. if (hold_max_time_ms > 0)
  3955. {
  3956. unit.SetActionStatus(UnitActionStatus.Idle);
  3957. this.hold_time = new TimeExpire<int>(unit.RandomN.Next(hold_min_time_ms, hold_max_time_ms));
  3958. }
  3959. }
  3960. protected override IPositionObject PopNextPos()
  3961. {
  3962. if (paths != null)
  3963. {
  3964. ZoneWayPoint ret = paths;
  3965. ZoneWayPoint prv = prev;
  3966. prev = paths;
  3967. paths = paths.GetRandomNext(prv);
  3968. Hold();
  3969. return ret;
  3970. }
  3971. return null;
  3972. }
  3973. protected override bool TestPopNext()
  3974. {
  3975. if (LastMovingResult.obj != null)
  3976. {
  3977. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, Target.X, Target.Y))
  3978. {
  3979. return true;
  3980. }
  3981. }
  3982. else
  3983. {
  3984. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, Target.X, Target.Y))
  3985. {
  3986. return true;
  3987. }
  3988. }
  3989. return false;
  3990. }
  3991. }
  3992. /// <summary>
  3993. ///
  3994. /// </summary>
  3995. public class StateGuardInPosition : State
  3996. {
  3997. public Vector2 Target { get { return target; } }
  3998. public MoveBlockResult LastMovingResult { get; private set; }
  3999. // 如果被地图碰撞,则寻路
  4000. private Vector2 target;
  4001. // 如果被单位碰撞,则向左或右挪动
  4002. private MoveAI moveAI;
  4003. public StateGuardInPosition(InstanceUnit unit, Vector2 target)
  4004. : base(unit)
  4005. {
  4006. this.target = target;
  4007. this.moveAI = new MoveAI(unit);
  4008. }
  4009. override public bool onBlock(State new_state)
  4010. {
  4011. return true;
  4012. }
  4013. override protected void onStart()
  4014. {
  4015. this.moveAI.FindPath(target.X, target.Y);
  4016. if (moveAI.IsNoWay)
  4017. {
  4018. unit.SetActionStatus(UnitActionStatus.Idle);
  4019. }
  4020. else
  4021. {
  4022. unit.SetActionStatus(UnitActionStatus.Move);
  4023. }
  4024. }
  4025. override protected void onUpdate()
  4026. {
  4027. this.LastMovingResult = this.moveAI.Update();
  4028. if ((LastMovingResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  4029. {
  4030. if ((LastMovingResult.result & MoveResult.MOVE_RESULT_BLOCK_MAP) != 0)
  4031. {
  4032. moveAI.Pause();
  4033. unit.SetActionStatus(UnitActionStatus.Idle);
  4034. }
  4035. else if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  4036. {
  4037. moveAI.Pause();
  4038. unit.SetActionStatus(UnitActionStatus.Idle);
  4039. }
  4040. }
  4041. }
  4042. override protected void onStop()
  4043. {
  4044. }
  4045. }
  4046. /// <summary>
  4047. ///
  4048. /// </summary>
  4049. public class StateBackToPosition : State
  4050. {
  4051. public Vector2 Target { get { return target; } }
  4052. public MoveBlockResult LastMovingResult { get; private set; }
  4053. public bool IsDone { get { return isDone; } }
  4054. // 如果被地图碰撞,则寻路
  4055. private Vector2 target;
  4056. // 如果被单位碰撞,则向左或右挪动
  4057. private MoveAI moveAI;
  4058. private bool isDone = false;
  4059. public StateBackToPosition(InstanceUnit unit, Vector2 target)
  4060. : base(unit)
  4061. {
  4062. this.target = target;
  4063. this.moveAI = new MoveAI(unit);
  4064. }
  4065. override public bool onBlock(State new_state)
  4066. {
  4067. if (new_state is StateDamage)
  4068. {
  4069. return true;
  4070. }
  4071. if (new_state is StateDead)
  4072. {
  4073. return true;
  4074. }
  4075. return isDone;
  4076. }
  4077. override protected void onStart()
  4078. {
  4079. this.unit.onBack2PositionStart();
  4080. this.moveAI.FindPath(target.X, target.Y);
  4081. if (moveAI.IsNoWay)
  4082. {
  4083. unit.SetActionStatus(UnitActionStatus.Idle);
  4084. }
  4085. else
  4086. {
  4087. unit.SetActionStatus(UnitActionStatus.Move);
  4088. }
  4089. }
  4090. override protected void onUpdate()
  4091. {
  4092. this.LastMovingResult = this.moveAI.Update();
  4093. if ((LastMovingResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  4094. {
  4095. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  4096. {
  4097. isDone = true;
  4098. unit.doSomething();
  4099. }
  4100. else
  4101. {
  4102. moveAI.FindPath(target.X, target.Y);
  4103. }
  4104. }
  4105. }
  4106. override protected void onStop()
  4107. {
  4108. this.unit.onBack2PositionEnd();
  4109. }
  4110. }
  4111. protected virtual void onBack2PositionEnd()
  4112. {
  4113. }
  4114. protected virtual void onBack2PositionStart()
  4115. {
  4116. }
  4117. #endregion
  4118. //---------------------------------------------------------------------------------------
  4119. #region __强制剧情播放__
  4120. public abstract class ForceState : State
  4121. {
  4122. public ForceState(InstanceUnit unit)
  4123. : base(unit)
  4124. {
  4125. }
  4126. override public bool onBlock(State new_state)
  4127. {
  4128. if (new_state is StateDead) return true;
  4129. if (new_state is StateRebirth) return true;
  4130. if (new_state is StateStun) return true;
  4131. return unit.IsDead();
  4132. }
  4133. }
  4134. /// <summary>
  4135. /// 强制移动到目标点,一般用于剧情动画
  4136. /// </summary>
  4137. public class ForceStateMoveTo : ForceState
  4138. {
  4139. private readonly float targetX;
  4140. private readonly float targetY;
  4141. private bool isEnd = false;
  4142. private MoveAI moveAI;
  4143. public ForceStateMoveTo(InstanceUnit unit, float x, float y)
  4144. : base(unit)
  4145. {
  4146. this.targetX = x;
  4147. this.targetY = y;
  4148. }
  4149. override public bool onBlock(State new_state)
  4150. {
  4151. if (base.onBlock(new_state)) return true;
  4152. return isEnd;
  4153. }
  4154. override protected void onStart()
  4155. {
  4156. unit.SetActionStatus(UnitActionStatus.Move);
  4157. this.moveAI = new MoveAI(unit);
  4158. this.moveAI.FindPath(targetX, targetY);
  4159. }
  4160. override protected void onUpdate()
  4161. {
  4162. if (!isEnd)
  4163. {
  4164. unit.faceTo(targetX, targetY);
  4165. MoveBlockResult result = moveAI.Update();
  4166. if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
  4167. {
  4168. isEnd = true;
  4169. unit.doSomething();
  4170. }
  4171. else if ((result.result & MoveResult.RESULTS_MOVE_END) != 0)
  4172. {
  4173. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  4174. if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
  4175. {
  4176. isEnd = true;
  4177. unit.doSomething();
  4178. }
  4179. }
  4180. else
  4181. {
  4182. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  4183. if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
  4184. {
  4185. isEnd = true;
  4186. unit.doSomething();
  4187. }
  4188. }
  4189. }
  4190. }
  4191. override protected void onStop()
  4192. {
  4193. }
  4194. }
  4195. public class ForceStateLaunchSkill : ForceState
  4196. {
  4197. private readonly int SkillID;
  4198. private readonly bool IsRandom;
  4199. private readonly StateStopHandler SkillOver;
  4200. private StateSkill mSkillState;
  4201. public ForceStateLaunchSkill(InstanceUnit unit, int skillID, bool random, StateStopHandler over = null)
  4202. : base(unit)
  4203. {
  4204. this.SkillID = skillID;
  4205. this.IsRandom = random;
  4206. this.SkillOver = over;
  4207. }
  4208. public override bool onBlock(State new_state)
  4209. {
  4210. if (base.onBlock(new_state)) return true;
  4211. if (new_state is StateSkill)
  4212. {
  4213. return true;
  4214. }
  4215. return mSkillState != null;
  4216. }
  4217. protected override void onStart()
  4218. {
  4219. }
  4220. protected override void onUpdate()
  4221. {
  4222. if (IsRandom)
  4223. {
  4224. mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
  4225. }
  4226. else
  4227. {
  4228. mSkillState = unit.launchSkill(SkillID, new InstanceUnit.LaunchSkillParam());
  4229. }
  4230. if (mSkillState == null)
  4231. {
  4232. mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
  4233. }
  4234. if (mSkillState != null && SkillOver != null)
  4235. {
  4236. mSkillState.AddStopOnce(SkillOver);
  4237. }
  4238. }
  4239. protected override void onStop()
  4240. {
  4241. if (mSkillState == null && SkillOver != null)
  4242. {
  4243. SkillOver.Invoke(unit, this);
  4244. }
  4245. }
  4246. }
  4247. public class ForceStateIdleTime : ForceState
  4248. {
  4249. private readonly TimeExpire<int> mIdleTime;
  4250. public ForceStateIdleTime(InstanceUnit unit, float timeSEC)
  4251. : base(unit)
  4252. {
  4253. mIdleTime = new TimeExpire<int>((int)(timeSEC * 1000));
  4254. }
  4255. override public bool onBlock(State new_state)
  4256. {
  4257. if (base.onBlock(new_state)) return true;
  4258. return mIdleTime.IsEnd;
  4259. }
  4260. override protected void onStart()
  4261. {
  4262. unit.SetActionStatus(UnitActionStatus.Idle);
  4263. }
  4264. override protected void onUpdate()
  4265. {
  4266. if (mIdleTime.Update(zone.UpdateIntervalMS))
  4267. {
  4268. unit.doSomething();
  4269. }
  4270. }
  4271. override protected void onStop() { }
  4272. }
  4273. public class ForceStateActionTime : ForceStateIdleTime
  4274. {
  4275. private readonly string ActionName;
  4276. public ForceStateActionTime(InstanceUnit unit, float timeSEC, string actionName)
  4277. : base(unit, timeSEC)
  4278. {
  4279. this.ActionName = actionName;
  4280. }
  4281. override protected void onStart()
  4282. {
  4283. unit.queueEvent(new UnitDoActionEvent(unit.ID, ActionName));
  4284. }
  4285. }
  4286. #endregion
  4287. }
  4288. }