InstanceUnit.StateMachine.cs 158 KB

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