InstanceUnit.StateMachine.cs 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691
  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. log.Warn("手动调用死亡逻辑1:" + unit.PlayerUUID + ", 攻击方:" + (attacker == null ? "null" : attacker.PlayerUUID) + ", " + this.zone.GetSceneID());
  2413. unit.Parent.cb_unitDeadCallBack(unit, this.attacker);
  2414. }
  2415. unit.SetActionStatus(UnitActionStatus.Dead);
  2416. this.rebirth_time = new TimeExpire<int>(unit.mInfo.RebirthTimeMS);
  2417. this.dead_time = new TimeExpire<int>(unit.mInfo.DeadTimeMS);
  2418. if (unit.Info.DeadLaunchSpell != null)
  2419. {
  2420. // 自爆法术
  2421. zone.unitLaunchSpell(XmdsSkillType.none, unit, unit.Info.DeadLaunchSpell, unit.X, unit.Y);
  2422. }
  2423. // Boss 死亡之后灵魂飘到击杀者身上
  2424. if (unit.mInfo.DeadDelayTimeMS > 0)
  2425. {
  2426. dead_delayTimes.Clear();
  2427. for (var i = 0; i < unit.Info.DeadDelaySpellNumber; i++)
  2428. {
  2429. this.dead_delayTimes.Add(new TimeExpire<int>(
  2430. unit.mInfo.DeadDelayTimeMS + i * unit.Info.DeadDelaySpellIntervalMS));
  2431. }
  2432. }
  2433. }
  2434. override protected void onUpdate()
  2435. {
  2436. if (dead_time.Update(zone.UpdateIntervalMS))
  2437. {
  2438. if (unit.mInfo.RebirthTimeMS <= 0)
  2439. {
  2440. zone.RemoveObject(unit);
  2441. }
  2442. ////跨服场景中,单位死亡,需要移除单位,然后才能复活
  2443. //if (unit.IsDead() && unit.IsPlayer && unit.GetSceneType() >= CommonAI.Data.SceneType.CROSS_SERVER_NEW)
  2444. //{
  2445. // // zone.RemoveObject(unit);
  2446. // unit.DeadHide = true;
  2447. //}
  2448. }
  2449. if (rebirth_time.Update(zone.UpdateIntervalMS))
  2450. {
  2451. if (unit.mInfo.RebirthTimeMS > 0)
  2452. {
  2453. unit.startRebirth();
  2454. }
  2455. }
  2456. for (var i = 0; i < this.dead_delayTimes.Count; i++)
  2457. {
  2458. if (dead_delayTimes[i] != null && dead_delayTimes[i].Update(zone.UpdateIntervalMS))
  2459. {
  2460. zone.unitLaunchSpell(XmdsSkillType.none, unit, unit.Info.DeadDelayLaunchSpell,
  2461. unit.X, unit.Y, attacker == null ? 0 : ((InstanceUnit)attacker).mSyncInfo.ObjectID);
  2462. dead_delayTimes[i] = null;
  2463. }
  2464. }
  2465. }
  2466. override protected void onStop()
  2467. {
  2468. //unit.SetActionStatus(UnitActionStatus.Idle);
  2469. }
  2470. }
  2471. /// <summary>
  2472. /// 复活
  2473. /// </summary>
  2474. public class StateRebirth : State, IStateNoneControllable
  2475. {
  2476. private bool is_done = false;
  2477. private int max_hp = 0;
  2478. private int max_mp = 0;
  2479. public StateRebirth(InstanceUnit unit, int max_hp, int max_mp)
  2480. : base(unit)
  2481. {
  2482. this.max_hp = max_hp;
  2483. this.max_mp = max_mp;
  2484. }
  2485. public State AsState()
  2486. {
  2487. return this;
  2488. }
  2489. override public bool onBlock(State new_state)
  2490. {
  2491. return is_done;
  2492. }
  2493. override protected void onStart()
  2494. {
  2495. unit.SetActionStatus(UnitActionStatus.Rebirth);
  2496. }
  2497. override protected void onUpdate()
  2498. {
  2499. this.is_done = true;
  2500. unit.doRebirth(max_hp, max_mp);
  2501. unit.doSomething();
  2502. }
  2503. override protected void onStop()
  2504. {
  2505. }
  2506. }
  2507. //---------------------------------------------------------------------------------------
  2508. /// <summary>
  2509. /// 眩晕状态
  2510. /// </summary>
  2511. public class StateStun : State, IStateNoneControllable
  2512. {
  2513. public StateStun(InstanceUnit unit)
  2514. : base(unit)
  2515. {
  2516. }
  2517. public State AsState()
  2518. {
  2519. return this;
  2520. }
  2521. override public bool onBlock(State new_state)
  2522. {
  2523. if (new_state is StateDamage)
  2524. {
  2525. return true;
  2526. }
  2527. if (unit.IsDead() && new_state is StateDead)
  2528. {
  2529. return true;
  2530. }
  2531. if (new_state is StateSkill)
  2532. {
  2533. StateSkill ss = new_state as StateSkill;
  2534. // 反击或者状态解除//
  2535. if (ss.SkillData.IsCounter)
  2536. {
  2537. return true;
  2538. }
  2539. }
  2540. return !unit.IsStun;
  2541. }
  2542. override protected void onStart()
  2543. {
  2544. unit.SetActionStatus(UnitActionStatus.Stun);
  2545. }
  2546. override protected void onUpdate()
  2547. {
  2548. if (!unit.IsStun)
  2549. {
  2550. unit.doSomething();
  2551. }
  2552. }
  2553. override protected void onStop()
  2554. {
  2555. //unit.SetActionStatus(UnitActionStatus.Idle);
  2556. }
  2557. }
  2558. /// <summary>
  2559. /// 不可移动状态
  2560. /// </summary>
  2561. public class StateChuanGongA : State, IStateNoneControllable
  2562. {
  2563. private bool markRemove;
  2564. public StateChuanGongA(InstanceUnit unit)
  2565. : base(unit)
  2566. {
  2567. }
  2568. public State AsState()
  2569. {
  2570. return this;
  2571. }
  2572. override public bool onBlock(State new_state)
  2573. {
  2574. if (this.markRemove)
  2575. {
  2576. return true;
  2577. }
  2578. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2579. }
  2580. public override void MarkRemove()
  2581. {
  2582. this.markRemove = true;
  2583. }
  2584. override protected void onStart()
  2585. {
  2586. this.markRemove = false;
  2587. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 60000;
  2588. unit.SetActionStatus(UnitActionStatus.ChuanGongA);
  2589. }
  2590. override protected void onUpdate()
  2591. {
  2592. if (this.markRemove)
  2593. {
  2594. unit.startIdle();
  2595. }
  2596. else if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2597. {
  2598. unit.doSomething();
  2599. }
  2600. }
  2601. override protected void onStop()
  2602. {
  2603. unit.SetActionStatus(UnitActionStatus.Idle);
  2604. }
  2605. }
  2606. /// <summary>
  2607. /// 不可移动状态
  2608. /// </summary>
  2609. public class StateChuanGongB : State, IStateNoneControllable
  2610. {
  2611. private bool markRemove;
  2612. public StateChuanGongB(InstanceUnit unit)
  2613. : base(unit)
  2614. {
  2615. }
  2616. public State AsState()
  2617. {
  2618. return this;
  2619. }
  2620. override public bool onBlock(State new_state)
  2621. {
  2622. if (this.markRemove)
  2623. {
  2624. return true;
  2625. }
  2626. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2627. }
  2628. public override void MarkRemove()
  2629. {
  2630. this.markRemove = true;
  2631. }
  2632. override protected void onStart()
  2633. {
  2634. this.markRemove = false;
  2635. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 60000;
  2636. unit.SetActionStatus(UnitActionStatus.ChuanGongB);
  2637. }
  2638. override protected void onUpdate()
  2639. {
  2640. if (this.markRemove)
  2641. {
  2642. unit.startIdle();
  2643. }
  2644. else if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2645. {
  2646. unit.doSomething();
  2647. }
  2648. }
  2649. override protected void onStop()
  2650. {
  2651. unit.SetActionStatus(UnitActionStatus.Idle);
  2652. }
  2653. }
  2654. /// <summary>
  2655. /// 不可移动状态 净妖,和传功一样锁定4秒,时间变量用传功的
  2656. /// </summary>
  2657. public class StateClearYaoQi : State, IStateNoneControllable
  2658. {
  2659. public StateClearYaoQi(InstanceUnit unit)
  2660. : base(unit)
  2661. {
  2662. }
  2663. public State AsState()
  2664. {
  2665. return this;
  2666. }
  2667. override public bool onBlock(State new_state)
  2668. {
  2669. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2670. }
  2671. override protected void onStart()
  2672. {
  2673. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 4000;
  2674. unit.SetActionStatus(UnitActionStatus.ClearYaoQi);
  2675. }
  2676. override protected void onUpdate()
  2677. {
  2678. if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2679. {
  2680. unit.doSomething();
  2681. }
  2682. }
  2683. override protected void onStop()
  2684. {
  2685. unit.SetActionStatus(UnitActionStatus.Idle);
  2686. }
  2687. }
  2688. /// <summary>
  2689. /// 打坐
  2690. /// </summary>
  2691. public class StateDaZuo : State
  2692. {
  2693. private float startX;
  2694. private float startY;
  2695. private long lockEndTime;
  2696. private bool markRemove;
  2697. public StateDaZuo(InstanceUnit unit, int time)
  2698. : base(unit)
  2699. {
  2700. int lockTime = Math.Max(1, time);
  2701. //锁定短暂时间,避免一打坐就被客户端取消了。
  2702. lockEndTime = 0;// CommonLang.CUtils.localTimeMS + lockTime * 1000;
  2703. }
  2704. public override void MarkRemove()
  2705. {
  2706. this.markRemove = true;
  2707. }
  2708. override public bool onBlock(State new_state)
  2709. {
  2710. if (this.markRemove)
  2711. {
  2712. return true;
  2713. }
  2714. if (CommonLang.CUtils.localTimeMS < lockEndTime)
  2715. {
  2716. return false;
  2717. }
  2718. if (new_state is StateIdle)
  2719. {
  2720. return false;
  2721. }
  2722. return true;
  2723. }
  2724. override protected void onStart()
  2725. {
  2726. unit.SetActionStatus(UnitActionStatus.DaZuo);
  2727. }
  2728. override protected void onUpdate()
  2729. {
  2730. if(this.markRemove)
  2731. {
  2732. unit.startIdle();
  2733. }
  2734. else if(lockEndTime > CommonLang.CUtils.localTimeMS)
  2735. {
  2736. startX = unit.X;
  2737. startY = unit.Y;
  2738. }
  2739. else if (unit.X != startX || unit.Y != startY)
  2740. {
  2741. unit.doSomething();
  2742. }
  2743. }
  2744. override protected void onStop()
  2745. {
  2746. unit.SetActionStatus(UnitActionStatus.Idle);
  2747. }
  2748. }
  2749. /// <summary>
  2750. /// 打坐恢复血量
  2751. /// </summary>
  2752. public class StateDaZuoRecoveryAttr : State
  2753. {
  2754. private long endTime;//回血结束时间
  2755. private bool markRemove;
  2756. private int sustainTime; //持续时间
  2757. private long nextRecoveryTime; //下一次恢复时间
  2758. public StateDaZuoRecoveryAttr(InstanceUnit unit, int time)
  2759. : base(unit)
  2760. {
  2761. endTime = CommonLang.CUtils.CurrentTimeMS + time*1000;
  2762. sustainTime = time-3; //减去3是因为等待坐下去后才开始回血
  2763. nextRecoveryTime = CommonLang.CUtils.CurrentTimeMS+2350; //2350是客户端坐下去整个动作的时间,等待动作播放完成,开始回血
  2764. }
  2765. public override void MarkRemove()
  2766. {
  2767. this.markRemove = true;
  2768. }
  2769. override public bool onBlock(State new_state)
  2770. {
  2771. return true;
  2772. }
  2773. override protected void onStart()
  2774. {
  2775. unit.SetActionStatus(UnitActionStatus.DaZuoRecoveryAttr);
  2776. }
  2777. override protected void onUpdate()
  2778. {
  2779. if(this.markRemove)
  2780. {
  2781. unit.startIdle();
  2782. }
  2783. else
  2784. {
  2785. if (unit.Virtual.GetBattleStatus() != BattleStatus.None)
  2786. {
  2787. unit.startIdle();
  2788. return;
  2789. }
  2790. if (CommonLang.CUtils.CurrentTimeMS > endTime)
  2791. {
  2792. unit.startIdle();
  2793. return;
  2794. }
  2795. if (nextRecoveryTime < CommonLang.CUtils.CurrentTimeMS)
  2796. {
  2797. int Recover = 0;
  2798. //恢复主角血量
  2799. // if (unit.CurrentHP < unit.MaxHP)
  2800. //{
  2801. Recover = (int)(unit.MaxHP / sustainTime);
  2802. /*if (unit.CurrentHP + Recover > unit.MaxHP)
  2803. {
  2804. Recover = unit.MaxHP - unit.CurrentHP;
  2805. }*/
  2806. unit.add_hp(Recover);
  2807. // }
  2808. //恢复宠物血量
  2809. InstanceUnit petUnit = unit.Virtual.GetPetUnit();
  2810. if (petUnit != null)
  2811. {
  2812. if (petUnit.CurrentHP < petUnit.MaxHP && petUnit.Virtual.GetBattleStatus() == BattleStatus.None)
  2813. {
  2814. Recover = (int)(petUnit.MaxHP / sustainTime);
  2815. if (petUnit.CurrentHP + Recover > petUnit.MaxHP)
  2816. {
  2817. Recover = petUnit.MaxHP - petUnit.CurrentHP;
  2818. }
  2819. petUnit.add_hp(Recover);
  2820. }
  2821. }
  2822. nextRecoveryTime = CommonLang.CUtils.CurrentTimeMS + 1 * 1000;
  2823. }
  2824. }
  2825. }
  2826. override protected void onStop()
  2827. {
  2828. unit.SetActionStatus(UnitActionStatus.Idle);
  2829. }
  2830. }
  2831. /// <summary>
  2832. /// 随从向玩家瞬移
  2833. /// </summary>
  2834. public class PetLockState : State
  2835. {
  2836. private long endTime;//瞬移后定住时间
  2837. private bool markRemove;
  2838. public PetLockState(InstanceUnit unit)
  2839. : base(unit)
  2840. {
  2841. endTime = CommonLang.CUtils.CurrentTimeMS + 1*1000; //持续时间1秒
  2842. }
  2843. public override void MarkRemove()
  2844. {
  2845. this.markRemove = true;
  2846. }
  2847. override public bool onBlock(State new_state)
  2848. {
  2849. return true;
  2850. }
  2851. override protected void onStart()
  2852. {
  2853. unit.SetActionStatus(UnitActionStatus.PetLock);
  2854. }
  2855. override protected void onUpdate()
  2856. {
  2857. if(this.markRemove)
  2858. {
  2859. unit.startIdle();
  2860. }
  2861. else
  2862. {
  2863. if (CommonLang.CUtils.CurrentTimeMS > endTime)
  2864. {
  2865. unit.startIdle();
  2866. return;
  2867. }
  2868. }
  2869. }
  2870. override protected void onStop()
  2871. {
  2872. unit.SetActionStatus(UnitActionStatus.Idle);
  2873. }
  2874. }
  2875. /// <summary>
  2876. /// 渡劫场景,不能移动
  2877. /// </summary>
  2878. public class StateDuJie : State, IStateNoneControllable
  2879. {
  2880. public StateDuJie(InstanceUnit unit)
  2881. : base(unit)
  2882. {
  2883. }
  2884. public State AsState()
  2885. {
  2886. return this;
  2887. }
  2888. override public bool onBlock(State new_state)
  2889. {
  2890. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2891. }
  2892. override protected void onStart()
  2893. {
  2894. // 管他多长时间,锁一个小时再说
  2895. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 3600000;
  2896. unit.SetActionStatus(UnitActionStatus.CrossRobbery);
  2897. }
  2898. override protected void onUpdate()
  2899. {
  2900. //if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2901. //{
  2902. // unit.doSomething();
  2903. //}
  2904. }
  2905. override protected void onStop()
  2906. {
  2907. unit.SetActionStatus(UnitActionStatus.Idle);
  2908. }
  2909. }
  2910. /// <summary>
  2911. /// 渡劫场景,不能移动
  2912. /// </summary>
  2913. public class StateTransport : State, IStateNoneControllable
  2914. {
  2915. private long mLockEndTime = 0;
  2916. public StateTransport(InstanceUnit unit)
  2917. : base(unit)
  2918. {
  2919. }
  2920. public State AsState()
  2921. {
  2922. return this;
  2923. }
  2924. override public bool onBlock(State new_state)
  2925. {
  2926. if(new_state is StateSkill)
  2927. {
  2928. return true;
  2929. }
  2930. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2931. }
  2932. override protected void onStart()
  2933. {
  2934. // 管他多长时间,锁定300ms
  2935. this.mLockEndTime = CommonLang.CUtils.localTimeMS + 250;
  2936. unit.SetActionStatus(UnitActionStatus.Transport);
  2937. //System.Console.WriteLine("开始传送状态:" + CommonLang.CUtils.localTimeMS);
  2938. }
  2939. override protected void onUpdate()
  2940. {
  2941. if (this.mLockEndTime <= CommonLang.CUtils.localTimeMS)
  2942. {
  2943. unit.doSomething();
  2944. }
  2945. }
  2946. override protected void onStop()
  2947. {
  2948. unit.SetActionStatus(UnitActionStatus.Idle);
  2949. //System.Console.WriteLine("end传送状态:" + CommonLang.CUtils.localTimeMS);
  2950. }
  2951. }
  2952. //---------------------------------------------------------------------------------------
  2953. /// <summary>
  2954. /// 状态限制移动
  2955. /// </summary>
  2956. abstract public class StateMoveNoneControllable : State, IStateNoneControllable
  2957. {
  2958. private TimeExpire<int> mExpire;
  2959. private MoveAI mMoveAI;
  2960. private MoveBlockResult mLastResult;
  2961. public MoveBlockResult LastMoveResult { get { return mLastResult; } }
  2962. public State AsState()
  2963. {
  2964. return this;
  2965. }
  2966. public StateMoveNoneControllable(InstanceUnit unit, int timeMS)
  2967. : base(unit)
  2968. {
  2969. this.mExpire = new TimeExpire<int>(timeMS);
  2970. this.mMoveAI = new MoveAI(unit, false);
  2971. }
  2972. /// <summary>
  2973. /// 搜索要去的地方
  2974. /// </summary>
  2975. /// <returns></returns>
  2976. protected abstract Vector2 FindTargetPos();
  2977. public override bool onBlock(State new_state)
  2978. {
  2979. if (new_state is StateDead)
  2980. {
  2981. return true;
  2982. }
  2983. if (new_state is StateDamage)
  2984. {
  2985. (new_state as StateDamage).SetNextNoneControllable(this);
  2986. return true;
  2987. }
  2988. if (new_state is StateSkill)
  2989. {
  2990. StateSkill ss = new_state as StateSkill;
  2991. // 反击或者状态解除//
  2992. if (ss.SkillData.IsCounter)
  2993. {
  2994. return true;
  2995. }
  2996. }
  2997. return mExpire.IsEnd;
  2998. }
  2999. protected override void onStart()
  3000. {
  3001. Vector2 target = this.FindTargetPos();
  3002. this.mMoveAI.FindPath(target.X, target.Y);
  3003. }
  3004. protected override void onUpdate()
  3005. {
  3006. if (mExpire.Update(zone.UpdateIntervalMS))
  3007. {
  3008. unit.doSomething();
  3009. }
  3010. else
  3011. {
  3012. mLastResult = mMoveAI.Update();
  3013. if ((mLastResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  3014. {
  3015. Vector2 target = this.FindTargetPos();
  3016. this.mMoveAI.FindPath(target.X, target.Y);
  3017. }
  3018. }
  3019. }
  3020. protected override void onStop()
  3021. {
  3022. }
  3023. }
  3024. /// <summary>
  3025. /// 拾取状态
  3026. /// </summary>
  3027. public class StateAutoPick : State
  3028. {
  3029. private InstanceItem mPickItem; //要自动拾取的物品
  3030. private MoveAI mMoveAi;
  3031. bool mIsEnd;
  3032. public StateAutoPick(InstanceUnit unit, InstanceItem item)
  3033. : base(unit)
  3034. {
  3035. this.mPickItem = item;
  3036. this.mMoveAi = new MoveAI(unit);
  3037. this.mMoveAi.FindPath(item.X, item.Y);
  3038. }
  3039. override public bool onBlock(State new_state)
  3040. {
  3041. if (unit.IsDead())
  3042. {
  3043. return true;
  3044. }
  3045. return true;
  3046. }
  3047. override protected void onStart()
  3048. {
  3049. //unit.SetActionStatus(UnitActionStatus.AutoPick);
  3050. }
  3051. override protected void onUpdate()
  3052. {
  3053. if (mIsEnd || this.mPickItem.IsDead() || !this.mPickItem.Enable || this.unit.Virtual.GetBattleStatus() != BattleStatus.None)
  3054. {
  3055. unit.doSomething();
  3056. }
  3057. else
  3058. {
  3059. MoveBlockResult result = mMoveAi.Update();
  3060. if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
  3061. {
  3062. mIsEnd = true;
  3063. unit.doSomething();
  3064. }
  3065. else
  3066. {
  3067. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  3068. if (CMath.includeRoundPoint(unit.X, unit.Y, r, mPickItem.X, mPickItem.Y))
  3069. {
  3070. mIsEnd = true;
  3071. unit.doSomething();
  3072. }
  3073. }
  3074. }
  3075. }
  3076. override protected void onStop()
  3077. {
  3078. unit.SetActionStatus(UnitActionStatus.Idle);
  3079. }
  3080. }
  3081. //---------------------------------------------------------------------------------------
  3082. /// <summary>
  3083. /// 逃跑状态
  3084. /// </summary>
  3085. public class StateEscape : StateMoveNoneControllable
  3086. {
  3087. private float mDistance;
  3088. public StateEscape(InstanceUnit unit, int timeMS, float distance = 0) : base(unit, timeMS)
  3089. {
  3090. if (distance > 0)
  3091. {
  3092. this.mDistance = distance;
  3093. }
  3094. else
  3095. {
  3096. this.mDistance = unit.Info.GuardRange;
  3097. }
  3098. }
  3099. protected override void onStart()
  3100. {
  3101. base.onStart();
  3102. unit.SetActionStatus(UnitActionStatus.Escape);
  3103. }
  3104. protected override Vector2 FindTargetPos()
  3105. {
  3106. var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, unit.X, unit.Y, mDistance);
  3107. return new Vector2(node.BX, node.BY);
  3108. }
  3109. }
  3110. //---------------------------------------------------------------------------------------
  3111. /// <summary>
  3112. /// 混乱状态
  3113. /// </summary>
  3114. public class StateChaos : StateMoveNoneControllable
  3115. {
  3116. public StateChaos(InstanceUnit unit, int timeMS) : base(unit, timeMS)
  3117. {
  3118. }
  3119. protected override void onStart()
  3120. {
  3121. base.onStart();
  3122. unit.SetActionStatus(UnitActionStatus.Chaos);
  3123. }
  3124. protected override void onStop()
  3125. {
  3126. base.onStop();
  3127. }
  3128. protected override Vector2 FindTargetPos()
  3129. {
  3130. var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, unit.X, unit.Y, unit.Info.GuardRange);
  3131. return new Vector2(node.BX, node.BY);
  3132. }
  3133. }
  3134. //---------------------------------------------------------------------------------------
  3135. /// <summary>
  3136. /// 破定
  3137. /// </summary>
  3138. public class StateBreakShield : State
  3139. {
  3140. private long mStateEndTime;
  3141. public StateBreakShield(InstanceUnit unit) : base(unit)
  3142. {
  3143. this.mStateEndTime = CommonLang.CUtils.localTimeMS + GlobalData.SHIELD_BREAK_HOLDTIME;
  3144. }
  3145. protected override void onStart()
  3146. {
  3147. unit.SetActionStatus(UnitActionStatus.BreakShield);
  3148. unit.AddBuff(GlobalData.SHIELD_BREAK_BUFF, 0, true);
  3149. }
  3150. protected override void onUpdate()
  3151. {
  3152. if(CommonLang.CUtils.localTimeMS > this.mStateEndTime)
  3153. {
  3154. unit.startIdle();
  3155. }
  3156. }
  3157. public override bool onBlock(State new_state)
  3158. {
  3159. if (CommonLang.CUtils.localTimeMS > this.mStateEndTime)
  3160. {
  3161. return true;
  3162. }
  3163. else if (new_state is StateDead)
  3164. {
  3165. return true;
  3166. }
  3167. return false;
  3168. }
  3169. protected override void onStop()
  3170. {
  3171. unit.SetActionStatus(UnitActionStatus.Idle);
  3172. //unit.removeBuff(GlobalData.SHIELD_BREAK_BUFF);
  3173. unit.Virtual.StartRecoverMP();
  3174. }
  3175. }
  3176. //---------------------------------------------------------------------------------------
  3177. #endregion
  3178. #region __AI行为__
  3179. //---------------------------------------------------------------------------------------
  3180. /// <summary>
  3181. /// 移动状态
  3182. /// </summary>
  3183. abstract public class StateFollow : State
  3184. {
  3185. public enum MoveState
  3186. {
  3187. Hold,
  3188. Move,
  3189. }
  3190. // 被追目标
  3191. protected IPositionObject target;
  3192. // 如果被单位碰撞,则向左或右挪动//
  3193. readonly private MoveAI moveAI;
  3194. // 检测切换走停的间隔时间 //
  3195. readonly private State<MoveState> state = new State<MoveState>(MoveState.Move);
  3196. readonly private bool begin_in_min_range;
  3197. // 检测切换走停的间隔时间 //
  3198. private TimeInterval<int> start_move_hold_time;
  3199. public abstract bool IsActive { get; }
  3200. public MoveAI MoveAI
  3201. {
  3202. get
  3203. {
  3204. return moveAI;
  3205. }
  3206. private set { }
  3207. }
  3208. public bool IsNoWay { get { if (moveAI != null) { return moveAI.IsNoWay; } return false; } }
  3209. public IPositionObject Target { get { return target; } }
  3210. public MoveState FollowState { get { return state.Value; } }
  3211. /// <summary>
  3212. /// Hold到Move之间的检测间隔
  3213. /// </summary>
  3214. public int StartMoveHoldTimeMS
  3215. {
  3216. get { return (start_move_hold_time != null) ? start_move_hold_time.IntervalTimeMS : 0; }
  3217. set
  3218. {
  3219. if (value > 0 && value != StartMoveHoldTimeMS)
  3220. {
  3221. start_move_hold_time = new TimeInterval<int>(value);
  3222. }
  3223. }
  3224. }
  3225. public StateFollow(InstanceUnit unit, InstanceZoneObject target, bool beginInMinRange = true, int holdTime = 0) : base(unit)
  3226. {
  3227. this.begin_in_min_range = beginInMinRange;
  3228. this.target = target;
  3229. this.moveAI = new MoveAI(unit, true, holdTime);
  3230. }
  3231. public override bool onBlock(State new_state)
  3232. {
  3233. return true;
  3234. }
  3235. protected override void onStart()
  3236. {
  3237. if (this.unit.Info.SumType == SummonType.AwaitMaster || cheekBeginInRange())
  3238. {
  3239. state.ChangeState(MoveState.Hold);
  3240. unit.SetActionStatus(UnitActionStatus.Idle);
  3241. }
  3242. else
  3243. {
  3244. state.ChangeState(MoveState.Move);
  3245. unit.SetActionStatus(UnitActionStatus.Move);
  3246. moveAI.FindPath(target);
  3247. }
  3248. }
  3249. protected virtual void OnMoveAiNoWay() { }
  3250. protected override void onUpdate()
  3251. {
  3252. if (!IsActive)
  3253. {
  3254. unit.doSomething();
  3255. }
  3256. else if ((unit.CurrentActionSubstate & (int)UnitActionSubStatus.CanNotMove) > 0)
  3257. {
  3258. //不能移动,啥事也不干
  3259. }
  3260. else
  3261. {
  3262. switch (FollowState)
  3263. {
  3264. case MoveState.Move:
  3265. // 进入最小追踪距离 //
  3266. if (CheckTargetInMinRange() == true)
  3267. {
  3268. changeToHold();
  3269. }
  3270. else
  3271. {
  3272. if (moveAI.Target == null)
  3273. {
  3274. moveAI.FindPath(target);
  3275. }
  3276. moveAI.Update();
  3277. if (moveAI.IsNoWay)
  3278. {
  3279. this.OnMoveAiNoWay();
  3280. if (CheckTargetInMaxRange() == true)
  3281. {
  3282. changeToHold();
  3283. }
  3284. else
  3285. {
  3286. unit.doSomething();
  3287. }
  3288. }
  3289. }
  3290. break;
  3291. case MoveState.Hold:
  3292. // 超过最大追踪范围 //
  3293. if (unit.IsMoveAble() && (start_move_hold_time == null || start_move_hold_time.Update(zone.UpdateIntervalMS)) && !CheckTargetInMaxRange())
  3294. {
  3295. changeToMove();
  3296. }
  3297. else
  3298. {
  3299. if (unit.CurrentActionStatus != UnitActionStatus.Idle)
  3300. {
  3301. unit.SetActionStatus(UnitActionStatus.Idle);
  3302. }
  3303. }
  3304. onUpdateFollowed(target);
  3305. break;
  3306. }
  3307. }
  3308. }
  3309. protected override void onStop() { }
  3310. private bool cheekBeginInRange()
  3311. {
  3312. if (begin_in_min_range)
  3313. {
  3314. return CheckTargetInMinRange();
  3315. }
  3316. else
  3317. {
  3318. return CheckTargetInMaxRange();
  3319. }
  3320. }
  3321. private void changeToMove()
  3322. {
  3323. state.ChangeState(MoveState.Move);
  3324. unit.SetActionStatus(UnitActionStatus.Move);
  3325. moveAI.FindPath(target);
  3326. onInRangeChanged();
  3327. onChangedToMove(target);
  3328. }
  3329. private void changeToHold()
  3330. {
  3331. state.ChangeState(MoveState.Hold);
  3332. unit.SetActionStatus(UnitActionStatus.Idle);
  3333. moveAI.Pause();
  3334. onInRangeChanged();
  3335. onChangedToHold(target);
  3336. }
  3337. /// <summary>
  3338. /// 检查目标是否在跟随范围内。
  3339. /// 检测为True,停止移动。
  3340. /// </summary>
  3341. protected abstract bool CheckTargetInMinRange();
  3342. /// <summary>
  3343. /// 检查目标是否在跟随范围内。
  3344. /// 检测为False,开始移动。
  3345. /// </summary>
  3346. protected abstract bool CheckTargetInMaxRange();
  3347. /// <summary>
  3348. /// 行为改变
  3349. /// </summary>
  3350. protected virtual void onInRangeChanged()
  3351. {
  3352. }
  3353. /// <summary>
  3354. /// 目标已经被追踪到
  3355. /// </summary>
  3356. /// <param name="target"></param>
  3357. protected virtual void onUpdateFollowed(IPositionObject target)
  3358. {
  3359. }
  3360. /// <summary>
  3361. /// 开始移动
  3362. /// </summary>
  3363. /// <param name="target"></param>
  3364. protected virtual void onChangedToMove(IPositionObject target)
  3365. {
  3366. }
  3367. /// <summary>
  3368. /// changeToHold
  3369. /// </summary>
  3370. /// <param name="target"></param>
  3371. protected virtual void onChangedToHold(IPositionObject target)
  3372. {
  3373. }
  3374. }
  3375. /// <summary>
  3376. ///
  3377. /// </summary>
  3378. public class StateFollowObject : StateFollow
  3379. {
  3380. protected float distance;
  3381. //protected InstanceZoneObject target;
  3382. public override bool IsActive
  3383. {
  3384. get
  3385. {
  3386. var t = target as InstanceZoneObject;
  3387. if (t != null)
  3388. {
  3389. return t.Enable;
  3390. }
  3391. return false;
  3392. }
  3393. }
  3394. public float Distance
  3395. {
  3396. set
  3397. {
  3398. this.distance = value;
  3399. }
  3400. get
  3401. {
  3402. return this.distance;
  3403. }
  3404. }
  3405. public StateFollowObject(InstanceUnit unit, InstanceZoneObject target, int holdTime = 0)
  3406. : base(unit, target, true, holdTime)
  3407. {
  3408. //this.target = target;
  3409. this.distance = Target.RadiusSize + unit.BodyBlockSize * 2;
  3410. }
  3411. protected override bool CheckTargetInMaxRange()
  3412. {
  3413. return CMath.includeRoundPoint(
  3414. unit.X, unit.Y,
  3415. distance * 2,
  3416. Target.X, Target.Y);
  3417. }
  3418. protected override bool CheckTargetInMinRange()
  3419. {
  3420. return CMath.includeRoundPoint(
  3421. unit.X, unit.Y,
  3422. distance,
  3423. Target.X, Target.Y);
  3424. }
  3425. }
  3426. public class VStateFollowLeader : StateFollowObject
  3427. {
  3428. public VStateFollowLeader(InstanceUnit unit, InstanceZoneObject target, int holdTime = 0) : base(unit, target, holdTime)
  3429. {
  3430. }
  3431. }
  3432. /// <summary>
  3433. ///
  3434. /// </summary>
  3435. public class StateFollowAndGuard : StateFollow
  3436. {
  3437. private readonly float distance_min;
  3438. private readonly float distance_max;
  3439. private readonly InstanceUnit targetUnit;
  3440. public InstanceUnit TargetUnit { get { return targetUnit; } }
  3441. public override bool IsActive { get { return targetUnit.Enable; } }
  3442. public StateFollowAndGuard(InstanceUnit unit, InstanceUnit target, float minDistance, float maxDistance)
  3443. : base(unit, target)
  3444. {
  3445. this.targetUnit = target;
  3446. this.distance_min = Math.Max(minDistance, target.BodyBlockSize + unit.BodyBlockSize * 2);
  3447. this.distance_max = Math.Max(maxDistance, distance_min);
  3448. }
  3449. protected override bool CheckTargetInMinRange()
  3450. {
  3451. return CMath.includeRoundPoint(
  3452. unit.X, unit.Y,
  3453. distance_min,
  3454. Target.X, Target.Y);
  3455. }
  3456. protected override bool CheckTargetInMaxRange()
  3457. {
  3458. return CMath.includeRoundPoint(
  3459. unit.X, unit.Y,
  3460. distance_max,
  3461. Target.X, Target.Y);
  3462. }
  3463. }
  3464. /// <summary>
  3465. ///
  3466. /// </summary>
  3467. public class StateFollowAndAttack : StateFollow
  3468. {
  3469. /*readonly private*/
  3470. InstanceUnit targetUnit;
  3471. private SkillTemplate.CastTarget expectTarget;
  3472. private TimeInterval<int> checkAdjustLaunchSkillTime;
  3473. private SkillState launchSkill;
  3474. private bool can_auto_focus_near_target;
  3475. private bool can_random_skill = true;
  3476. private bool can_attack = true;
  3477. private float min_distance = -1;
  3478. private float max_distance = -1;
  3479. /// <summary>
  3480. /// 是否追踪
  3481. /// </summary>
  3482. /// <returns></returns>
  3483. override public bool IsActive
  3484. {
  3485. get { return can_attack && targetUnit.IsActive; }
  3486. }
  3487. public SkillState ExpectSkillState
  3488. {
  3489. get { return launchSkill; }
  3490. set
  3491. {
  3492. launchSkill = value;
  3493. expectTarget = value.Data.ExpectTarget;
  3494. }
  3495. }
  3496. public SkillTemplate ExpectSkill
  3497. {
  3498. get { return (launchSkill != null) ? launchSkill.Data : unit.DefaultSkill; }
  3499. }
  3500. public InstanceUnit TargetUnit
  3501. {
  3502. get { return targetUnit; }
  3503. set { targetUnit = value; }
  3504. }
  3505. public SkillTemplate.CastTarget ExpectTarget
  3506. {
  3507. get { return expectTarget; }
  3508. }
  3509. public bool IsLaunchRandomSkill
  3510. {
  3511. get { return can_random_skill; }
  3512. set { can_random_skill = value; }
  3513. }
  3514. public bool IsAutoFocusNearTarget
  3515. {
  3516. get { return can_auto_focus_near_target; }
  3517. set { can_auto_focus_near_target = value; }
  3518. }
  3519. public StateFollowAndAttack(
  3520. InstanceUnit unit,
  3521. InstanceUnit target,
  3522. SkillTemplate.CastTarget expectTarget = SkillTemplate.CastTarget.Enemy,
  3523. bool autoFocusNearTarget = false)
  3524. : base(unit, target, false)
  3525. {
  3526. this.targetUnit = target;
  3527. // Console.WriteLine(" - - - - StateFollowAndAttack - - - - " + this.TargetUnit.Name);
  3528. this.expectTarget = expectTarget;
  3529. this.can_auto_focus_near_target = autoFocusNearTarget;
  3530. this.StartMoveHoldTimeMS = zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS;
  3531. }
  3532. protected override void onStart()
  3533. {
  3534. this.checkAdjustLaunchSkillTime = new TimeInterval<int>(zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS);
  3535. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  3536. base.onStart();
  3537. }
  3538. protected override void onUpdateFollowed(IPositionObject target)
  3539. {
  3540. this.can_attack = zone.IsAttackable(unit, targetUnit, expectTarget, AttackReason.Tracing, this.ExpectSkill);
  3541. if (can_attack)
  3542. {
  3543. unit.faceTo(target.X, target.Y);
  3544. if (TryLaunchSkill() == null)
  3545. {
  3546. SkillTemplate expect_skill = ExpectSkill;
  3547. if (expect_skill != null && expect_skill.AttackKeepRange > 0)
  3548. {
  3549. if (checkAdjustLaunchSkillTime.IntervalTimeMS == 0 || checkAdjustLaunchSkillTime.Update(zone.UpdateIntervalMS))
  3550. {
  3551. if (CUtils.RandomPercent(zone.RandomN, zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_ADJUST_ESCAPE_PCT))
  3552. {
  3553. unit.startAdjustLaunchSkill(expect_skill, targetUnit, this.OnEndAdjustKeepRange);
  3554. }
  3555. }
  3556. }
  3557. }
  3558. }
  3559. }
  3560. protected override void onUpdate()
  3561. {
  3562. if (IsActive)
  3563. {
  3564. resetMaxMinRange();
  3565. }
  3566. base.onUpdate();
  3567. }
  3568. private void resetMaxMinRange()
  3569. {
  3570. var expect_skill = ExpectSkillState;
  3571. bool needCheck = true;
  3572. if (expect_skill == null)
  3573. {
  3574. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  3575. if (new_skill != null)
  3576. {
  3577. ExpectSkillState = new_skill;
  3578. expect_skill = new_skill;
  3579. needCheck = false;
  3580. }
  3581. }
  3582. if(expect_skill == null )
  3583. {
  3584. expect_skill = unit.DefaultSkillStatus();
  3585. }
  3586. if (expect_skill != null)
  3587. {
  3588. min_distance = unit.BodyBlockSize + targetUnit.BodyBlockSize;
  3589. max_distance = Math.Max(min_distance, unit.BodyBlockSize + targetUnit.BodyHitSize);
  3590. if (can_random_skill)
  3591. {
  3592. if (needCheck == true && !expect_skill.checkTargetRange(targetUnit))
  3593. {
  3594. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  3595. if (new_skill != null)
  3596. {
  3597. ExpectSkillState = new_skill;
  3598. expect_skill = new_skill;
  3599. }
  3600. }
  3601. }
  3602. float skill_distance = unit.GetSkillAttackRange(expect_skill.Data);
  3603. float half = (skill_distance - min_distance) / 2;
  3604. if (expect_skill.Data.AttackKeepRange > 0 && expect_skill.Data.AttackRange > expect_skill.Data.AttackKeepRange)
  3605. {
  3606. min_distance = Math.Max(min_distance, unit.GetSkillAttackRange(expect_skill.Data.AttackKeepRange) + targetUnit.BodyBlockSize);
  3607. max_distance = Math.Max(max_distance, min_distance);
  3608. }
  3609. else if (half > 0)
  3610. {
  3611. min_distance = min_distance + half;
  3612. max_distance = Math.Max(skill_distance, min_distance);
  3613. }
  3614. else
  3615. {
  3616. min_distance = Math.Max(min_distance, skill_distance + targetUnit.BodyBlockSize);
  3617. max_distance = Math.Max(max_distance, min_distance);
  3618. }
  3619. max_distance = Math.Max(max_distance, skill_distance + targetUnit.BodyHitSize);
  3620. }
  3621. }
  3622. protected override bool CheckTargetInMaxRange()
  3623. {
  3624. if (max_distance < 0) return true;
  3625. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  3626. }
  3627. protected override bool CheckTargetInMinRange()
  3628. {
  3629. if(min_distance < 0) return true;
  3630. return CMath.includeRoundPoint(unit.X, unit.Y, min_distance, targetUnit.X, targetUnit.Y);
  3631. }
  3632. protected virtual bool OnEndAdjustKeepRange(StateMove m)
  3633. {
  3634. unit.faceTo(Target.X, Target.Y);
  3635. unit.changeState(this);
  3636. return true;
  3637. }
  3638. protected virtual SkillTemplate TryLaunchSkill()
  3639. {
  3640. if (launchSkill == null)
  3641. {
  3642. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  3643. }
  3644. StateSkill ret = null;
  3645. Vector2 targetPos = null;
  3646. if (launchSkill != null)
  3647. {
  3648. ret = unit.launchSkill(launchSkill, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target));
  3649. if (ret != null)
  3650. {
  3651. return ret.SkillData;
  3652. }
  3653. }
  3654. if (can_random_skill)
  3655. {
  3656. ret = unit.launchRandomSkill(expectTarget, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target));
  3657. if (ret != null)
  3658. {
  3659. return ret.SkillData;
  3660. }
  3661. }
  3662. return null;
  3663. }
  3664. }
  3665. /// <summary>
  3666. ///
  3667. /// </summary>
  3668. public class StateFollowAndPickObject : StateFollow
  3669. {
  3670. private readonly InstanceZoneObject targetObject;
  3671. private readonly int pickTimeMS;
  3672. private readonly StatePickObject.OnPickDone doneEvent;
  3673. private readonly string status;
  3674. public StateFollowAndPickObject(InstanceUnit unit, InstanceZoneObject target, int timeMS, StatePickObject.OnPickDone done, string status = null)
  3675. : base(unit, target, false)
  3676. {
  3677. this.targetObject = target;
  3678. this.pickTimeMS = timeMS;
  3679. this.doneEvent = done;
  3680. this.status = status;
  3681. }
  3682. public override bool IsActive
  3683. {
  3684. get { return targetObject.Enable; }
  3685. }
  3686. protected override bool CheckTargetInMinRange()
  3687. {
  3688. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, targetObject.X, targetObject.Y);
  3689. }
  3690. protected override bool CheckTargetInMaxRange()
  3691. {
  3692. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize + targetObject.BodyBlockSize, targetObject.X, targetObject.Y);
  3693. }
  3694. protected override void onUpdateFollowed(IPositionObject target)
  3695. {
  3696. unit.startPickProgressObject(targetObject, pickTimeMS, doneEvent, status);
  3697. }
  3698. }
  3699. /// <summary>
  3700. ///
  3701. /// </summary>
  3702. public class StateFollowAndPickItem : StateFollow
  3703. {
  3704. public readonly InstanceItem targetObject;
  3705. private long mLockEndTime;
  3706. private bool mDoPick;
  3707. private bool mMarkBlock;
  3708. public StateFollowAndPickItem(InstanceUnit unit, InstanceItem target, bool doPick)
  3709. : base(unit, target, false)
  3710. {
  3711. this.targetObject = target;
  3712. this.mDoPick = doPick;
  3713. }
  3714. public override bool IsActive
  3715. {
  3716. get { return (targetObject.Enable && targetObject.IsPosValid()) || mLockEndTime > CommonLang.CUtils.localTimeMS; }
  3717. }
  3718. public bool IsBlock()
  3719. {
  3720. return this.mMarkBlock;
  3721. }
  3722. protected override bool CheckTargetInMinRange()
  3723. {
  3724. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, targetObject.X, targetObject.Y);
  3725. }
  3726. protected override bool CheckTargetInMaxRange()
  3727. {
  3728. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize + targetObject.BodyBlockSize, targetObject.X, targetObject.Y);
  3729. }
  3730. protected override void onUpdateFollowed(IPositionObject target)
  3731. {
  3732. if (this.mDoPick && this.targetObject.Enable)
  3733. {
  3734. targetObject.PickItem(unit);
  3735. }
  3736. }
  3737. public void CheckAndLockPickTime()
  3738. {
  3739. if(!this.targetObject.Enable && mLockEndTime == 0)
  3740. {
  3741. mLockEndTime = CommonLang.CUtils.localTimeMS + 350;
  3742. }
  3743. }
  3744. protected override void onUpdate()
  3745. {
  3746. this.CheckAndLockPickTime();
  3747. if (!this.IsActive && unit.IsPlayer)
  3748. {
  3749. if (unit.DoAutoPick(true))
  3750. {
  3751. return;
  3752. }
  3753. }
  3754. base.onUpdate();
  3755. }
  3756. protected override void OnMoveAiNoWay()
  3757. {
  3758. this.mLockEndTime = 0;
  3759. targetObject.setPosNotValid();
  3760. }
  3761. public override bool onBlock(State new_state)
  3762. {
  3763. if(/*!(new_state is StateFollowAndPickItem) && */mLockEndTime > CommonLang.CUtils.localTimeMS)
  3764. {
  3765. return false;
  3766. }
  3767. //把当前拾取弄完,再去跟随
  3768. if (new_state is VStateFollowLeader && unit.DoAutoPick(true))
  3769. {
  3770. return false;
  3771. }
  3772. return true;
  3773. }
  3774. protected override void onStop()
  3775. {
  3776. this.mMarkBlock = true;
  3777. }
  3778. }
  3779. /// <summary>
  3780. ///
  3781. /// </summary>
  3782. public abstract class StateAttackTo : State
  3783. {
  3784. public IPositionObject Target { get { return target; } }
  3785. public MoveBlockResult LastMovingResult { get; private set; }
  3786. public MoveAI UnitMoveAI { get { return moveAI; } }
  3787. // 如果被地图碰撞,则寻路
  3788. private IPositionObject target;
  3789. // 如果被单位碰撞,则向左或右挪动
  3790. private MoveAI moveAI;
  3791. private int holdTimeMS;
  3792. private long mPeaceEndTime;
  3793. public StateAttackTo(InstanceUnit unit, int move_ai_hold_time_ms = -1, int peaceTime = 0)
  3794. : base(unit)
  3795. {
  3796. this.holdTimeMS = move_ai_hold_time_ms;
  3797. this.mPeaceEndTime = CommonLang.CUtils.CurrentTimeMS + peaceTime;
  3798. }
  3799. override public bool onBlock(State new_state)
  3800. {
  3801. if(this.mPeaceEndTime > CommonLang.CUtils.CurrentTimeMS)
  3802. {
  3803. return false;
  3804. }
  3805. return true;
  3806. }
  3807. override protected void onStart()
  3808. {
  3809. if (target == null)
  3810. {
  3811. target = PopNextPos();
  3812. }
  3813. this.moveAI = new MoveAI(unit, true, holdTimeMS);
  3814. if (target != null)
  3815. {
  3816. this.moveAI.FindPath(target);
  3817. unit.SetActionStatus(UnitActionStatus.Move);
  3818. }
  3819. else
  3820. {
  3821. unit.SetActionStatus(UnitActionStatus.Idle);
  3822. }
  3823. }
  3824. override protected void onUpdate()
  3825. {
  3826. if (target == null)
  3827. {
  3828. unit.doSomething();
  3829. }
  3830. else
  3831. {
  3832. LastMovingResult = this.moveAI.Update();
  3833. if (TestPopNext())
  3834. {
  3835. this.target = PopNextPos();
  3836. if (target != null)
  3837. {
  3838. this.moveAI.FindPath(target);
  3839. }
  3840. }
  3841. }
  3842. }
  3843. override protected void onStop()
  3844. {
  3845. }
  3846. abstract protected bool HasNextPos();
  3847. abstract protected IPositionObject PopNextPos();
  3848. protected virtual bool TestPopNext()
  3849. {
  3850. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  3851. {
  3852. return true;
  3853. }
  3854. return false;
  3855. }
  3856. public virtual bool IsDone
  3857. {
  3858. get
  3859. {
  3860. if (IsStarted)
  3861. {
  3862. if (target == null)
  3863. {
  3864. return true;
  3865. }
  3866. if (HasNextPos())
  3867. {
  3868. return false;
  3869. }
  3870. return TestPopNext();
  3871. }
  3872. return false;
  3873. }
  3874. }
  3875. }
  3876. public class StateAttackToZoneWayPoint : StateAttackTo
  3877. {
  3878. // 如果被地图碰撞,则寻路
  3879. private ZoneWayPoint paths;
  3880. private ZoneWayPoint prev;
  3881. protected ZoneWayPoint Paths { get { return paths; } }
  3882. public StateAttackToZoneWayPoint(InstanceUnit unit, ZoneWayPoint wps, int peaceTime)
  3883. : base(unit, -1, peaceTime)
  3884. {
  3885. this.paths = wps;
  3886. }
  3887. protected override bool HasNextPos()
  3888. {
  3889. return paths != null;
  3890. }
  3891. protected override IPositionObject PopNextPos()
  3892. {
  3893. if (paths != null)
  3894. {
  3895. ZoneWayPoint ret = paths;
  3896. ZoneWayPoint prv = prev;
  3897. prev = paths;
  3898. paths = paths.GetRandomNext(prv);
  3899. return ret;
  3900. }
  3901. return null;
  3902. }
  3903. protected override bool TestPopNext()
  3904. {
  3905. if (LastMovingResult.obj != null)
  3906. {
  3907. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, Target.X, Target.Y))
  3908. {
  3909. return true;
  3910. }
  3911. }
  3912. else
  3913. {
  3914. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, Target.X, Target.Y))
  3915. {
  3916. return true;
  3917. }
  3918. }
  3919. return false;
  3920. }
  3921. }
  3922. public class StatePatrolWayPoint : StateAttackTo
  3923. {
  3924. // 如果被地图碰撞,则寻路
  3925. private ZoneWayPoint paths;
  3926. private ZoneWayPoint prev;
  3927. private TimeExpire<int> hold_time;
  3928. private int hold_max_time_ms, hold_min_time_ms;
  3929. protected ZoneWayPoint Paths { get { return paths; } }
  3930. public StatePatrolWayPoint(InstanceUnit unit, ZoneWayPoint wps, int hold_max_time_ms, int hold_min_time_ms, int peaceTime)
  3931. : base(unit, -1, peaceTime)
  3932. {
  3933. this.paths = wps;
  3934. this.hold_max_time_ms = Math.Max(hold_max_time_ms, hold_min_time_ms);
  3935. this.hold_min_time_ms = Math.Min(hold_max_time_ms, hold_min_time_ms);
  3936. }
  3937. protected override void onUpdate()
  3938. {
  3939. if (this.hold_time != null)
  3940. {
  3941. if (this.hold_time.Update(zone.UpdateIntervalMS))
  3942. {
  3943. this.hold_time = null;
  3944. }
  3945. }
  3946. else
  3947. {
  3948. base.onUpdate();
  3949. }
  3950. }
  3951. protected override bool HasNextPos()
  3952. {
  3953. return paths != null;
  3954. }
  3955. private void Hold()
  3956. {
  3957. if (hold_max_time_ms > 0)
  3958. {
  3959. unit.SetActionStatus(UnitActionStatus.Idle);
  3960. this.hold_time = new TimeExpire<int>(unit.RandomN.Next(hold_min_time_ms, hold_max_time_ms));
  3961. }
  3962. }
  3963. protected override IPositionObject PopNextPos()
  3964. {
  3965. if (paths != null)
  3966. {
  3967. ZoneWayPoint ret = paths;
  3968. ZoneWayPoint prv = prev;
  3969. prev = paths;
  3970. paths = paths.GetRandomNext(prv);
  3971. Hold();
  3972. return ret;
  3973. }
  3974. return null;
  3975. }
  3976. protected override bool TestPopNext()
  3977. {
  3978. if (LastMovingResult.obj != null)
  3979. {
  3980. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, Target.X, Target.Y))
  3981. {
  3982. return true;
  3983. }
  3984. }
  3985. else
  3986. {
  3987. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, Target.X, Target.Y))
  3988. {
  3989. return true;
  3990. }
  3991. }
  3992. return false;
  3993. }
  3994. }
  3995. /// <summary>
  3996. ///
  3997. /// </summary>
  3998. public class StateGuardInPosition : State
  3999. {
  4000. public Vector2 Target { get { return target; } }
  4001. public MoveBlockResult LastMovingResult { get; private set; }
  4002. // 如果被地图碰撞,则寻路
  4003. private Vector2 target;
  4004. // 如果被单位碰撞,则向左或右挪动
  4005. private MoveAI moveAI;
  4006. public StateGuardInPosition(InstanceUnit unit, Vector2 target)
  4007. : base(unit)
  4008. {
  4009. this.target = target;
  4010. this.moveAI = new MoveAI(unit);
  4011. }
  4012. override public bool onBlock(State new_state)
  4013. {
  4014. return true;
  4015. }
  4016. override protected void onStart()
  4017. {
  4018. this.moveAI.FindPath(target.X, target.Y);
  4019. if (moveAI.IsNoWay)
  4020. {
  4021. unit.SetActionStatus(UnitActionStatus.Idle);
  4022. }
  4023. else
  4024. {
  4025. unit.SetActionStatus(UnitActionStatus.Move);
  4026. }
  4027. }
  4028. override protected void onUpdate()
  4029. {
  4030. this.LastMovingResult = this.moveAI.Update();
  4031. if ((LastMovingResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  4032. {
  4033. if ((LastMovingResult.result & MoveResult.MOVE_RESULT_BLOCK_MAP) != 0)
  4034. {
  4035. moveAI.Pause();
  4036. unit.SetActionStatus(UnitActionStatus.Idle);
  4037. }
  4038. else if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  4039. {
  4040. moveAI.Pause();
  4041. unit.SetActionStatus(UnitActionStatus.Idle);
  4042. }
  4043. }
  4044. }
  4045. override protected void onStop()
  4046. {
  4047. }
  4048. }
  4049. /// <summary>
  4050. ///
  4051. /// </summary>
  4052. public class StateBackToPosition : State
  4053. {
  4054. public Vector2 Target { get { return target; } }
  4055. public MoveBlockResult LastMovingResult { get; private set; }
  4056. public bool IsDone { get { return isDone; } }
  4057. // 如果被地图碰撞,则寻路
  4058. private Vector2 target;
  4059. // 如果被单位碰撞,则向左或右挪动
  4060. private MoveAI moveAI;
  4061. private bool isDone = false;
  4062. public StateBackToPosition(InstanceUnit unit, Vector2 target)
  4063. : base(unit)
  4064. {
  4065. this.target = target;
  4066. this.moveAI = new MoveAI(unit);
  4067. }
  4068. override public bool onBlock(State new_state)
  4069. {
  4070. if (new_state is StateDamage)
  4071. {
  4072. return true;
  4073. }
  4074. if (new_state is StateDead)
  4075. {
  4076. return true;
  4077. }
  4078. return isDone;
  4079. }
  4080. override protected void onStart()
  4081. {
  4082. this.unit.onBack2PositionStart();
  4083. this.moveAI.FindPath(target.X, target.Y);
  4084. if (moveAI.IsNoWay)
  4085. {
  4086. unit.SetActionStatus(UnitActionStatus.Idle);
  4087. }
  4088. else
  4089. {
  4090. unit.SetActionStatus(UnitActionStatus.Move);
  4091. }
  4092. }
  4093. override protected void onUpdate()
  4094. {
  4095. this.LastMovingResult = this.moveAI.Update();
  4096. if ((LastMovingResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  4097. {
  4098. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  4099. {
  4100. isDone = true;
  4101. unit.doSomething();
  4102. }
  4103. else
  4104. {
  4105. moveAI.FindPath(target.X, target.Y);
  4106. }
  4107. }
  4108. }
  4109. override protected void onStop()
  4110. {
  4111. this.unit.onBack2PositionEnd();
  4112. }
  4113. }
  4114. protected virtual void onBack2PositionEnd()
  4115. {
  4116. }
  4117. protected virtual void onBack2PositionStart()
  4118. {
  4119. }
  4120. #endregion
  4121. //---------------------------------------------------------------------------------------
  4122. #region __强制剧情播放__
  4123. public abstract class ForceState : State
  4124. {
  4125. public ForceState(InstanceUnit unit)
  4126. : base(unit)
  4127. {
  4128. }
  4129. override public bool onBlock(State new_state)
  4130. {
  4131. if (new_state is StateDead) return true;
  4132. if (new_state is StateRebirth) return true;
  4133. if (new_state is StateStun) return true;
  4134. return unit.IsDead();
  4135. }
  4136. }
  4137. /// <summary>
  4138. /// 强制移动到目标点,一般用于剧情动画
  4139. /// </summary>
  4140. public class ForceStateMoveTo : ForceState
  4141. {
  4142. private readonly float targetX;
  4143. private readonly float targetY;
  4144. private bool isEnd = false;
  4145. private MoveAI moveAI;
  4146. public ForceStateMoveTo(InstanceUnit unit, float x, float y)
  4147. : base(unit)
  4148. {
  4149. this.targetX = x;
  4150. this.targetY = y;
  4151. }
  4152. override public bool onBlock(State new_state)
  4153. {
  4154. if (base.onBlock(new_state)) return true;
  4155. return isEnd;
  4156. }
  4157. override protected void onStart()
  4158. {
  4159. unit.SetActionStatus(UnitActionStatus.Move);
  4160. this.moveAI = new MoveAI(unit);
  4161. this.moveAI.FindPath(targetX, targetY);
  4162. }
  4163. override protected void onUpdate()
  4164. {
  4165. if (!isEnd)
  4166. {
  4167. unit.faceTo(targetX, targetY);
  4168. MoveBlockResult result = moveAI.Update();
  4169. if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
  4170. {
  4171. isEnd = true;
  4172. unit.doSomething();
  4173. }
  4174. else if ((result.result & MoveResult.RESULTS_MOVE_END) != 0)
  4175. {
  4176. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  4177. if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
  4178. {
  4179. isEnd = true;
  4180. unit.doSomething();
  4181. }
  4182. }
  4183. else
  4184. {
  4185. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  4186. if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
  4187. {
  4188. isEnd = true;
  4189. unit.doSomething();
  4190. }
  4191. }
  4192. }
  4193. }
  4194. override protected void onStop()
  4195. {
  4196. }
  4197. }
  4198. public class ForceStateLaunchSkill : ForceState
  4199. {
  4200. private readonly int SkillID;
  4201. private readonly bool IsRandom;
  4202. private readonly StateStopHandler SkillOver;
  4203. private StateSkill mSkillState;
  4204. public ForceStateLaunchSkill(InstanceUnit unit, int skillID, bool random, StateStopHandler over = null)
  4205. : base(unit)
  4206. {
  4207. this.SkillID = skillID;
  4208. this.IsRandom = random;
  4209. this.SkillOver = over;
  4210. }
  4211. public override bool onBlock(State new_state)
  4212. {
  4213. if (base.onBlock(new_state)) return true;
  4214. if (new_state is StateSkill)
  4215. {
  4216. return true;
  4217. }
  4218. return mSkillState != null;
  4219. }
  4220. protected override void onStart()
  4221. {
  4222. }
  4223. protected override void onUpdate()
  4224. {
  4225. if (IsRandom)
  4226. {
  4227. mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
  4228. }
  4229. else
  4230. {
  4231. mSkillState = unit.launchSkill(SkillID, new InstanceUnit.LaunchSkillParam());
  4232. }
  4233. if (mSkillState == null)
  4234. {
  4235. mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
  4236. }
  4237. if (mSkillState != null && SkillOver != null)
  4238. {
  4239. mSkillState.AddStopOnce(SkillOver);
  4240. }
  4241. }
  4242. protected override void onStop()
  4243. {
  4244. if (mSkillState == null && SkillOver != null)
  4245. {
  4246. SkillOver.Invoke(unit, this);
  4247. }
  4248. }
  4249. }
  4250. public class ForceStateIdleTime : ForceState
  4251. {
  4252. private readonly TimeExpire<int> mIdleTime;
  4253. public ForceStateIdleTime(InstanceUnit unit, float timeSEC)
  4254. : base(unit)
  4255. {
  4256. mIdleTime = new TimeExpire<int>((int)(timeSEC * 1000));
  4257. }
  4258. override public bool onBlock(State new_state)
  4259. {
  4260. if (base.onBlock(new_state)) return true;
  4261. return mIdleTime.IsEnd;
  4262. }
  4263. override protected void onStart()
  4264. {
  4265. unit.SetActionStatus(UnitActionStatus.Idle);
  4266. }
  4267. override protected void onUpdate()
  4268. {
  4269. if (mIdleTime.Update(zone.UpdateIntervalMS))
  4270. {
  4271. unit.doSomething();
  4272. }
  4273. }
  4274. override protected void onStop() { }
  4275. }
  4276. public class ForceStateActionTime : ForceStateIdleTime
  4277. {
  4278. private readonly string ActionName;
  4279. public ForceStateActionTime(InstanceUnit unit, float timeSEC, string actionName)
  4280. : base(unit, timeSEC)
  4281. {
  4282. this.ActionName = actionName;
  4283. }
  4284. override protected void onStart()
  4285. {
  4286. unit.queueEvent(new UnitDoActionEvent(unit.ID, ActionName));
  4287. }
  4288. }
  4289. #endregion
  4290. }
  4291. }