InstanceUnit.StateMachine.cs 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669
  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. this.PushLaunchSkillEvent(skill.ActionIndex);
  1011. }
  1012. if (IsNoneBlock)
  1013. {
  1014. unit.SetNoneBlockTimeMS(this.curActionTotalTimes);
  1015. }
  1016. if (current_action.IsMoveToTarget)
  1017. {
  1018. //先寻路//
  1019. checkMoveToTarget(current_action);
  1020. }
  1021. else if (current_action.IsJumpToTarget)
  1022. {
  1023. //计算跳跃距离//
  1024. checkJumpToTarget(current_action);
  1025. }
  1026. else
  1027. {
  1028. if (unit.IsSkillControllableByServer)
  1029. {
  1030. if (param.AutoFocusNearTarget && (targetUnit == null || !targetUnit.IsActive))
  1031. {
  1032. //自动锁定目标//
  1033. bool directionChange = false;
  1034. targetUnit = unit.getSkillAttackableFirstTarget(this.SkillData, AttackReason.Look, ref directionChange);
  1035. if (targetUnit != null && targetUnit != unit && directionChange)
  1036. {
  1037. unit.faceTo(targetUnit.X, targetUnit.Y);
  1038. }
  1039. }
  1040. }
  1041. }
  1042. return true;
  1043. }
  1044. //如果为蓄力技能,通知客户端状态
  1045. if (this.current_action != null && this.current_action.SigleActionType == ActionEnum.chargeAtk)
  1046. {
  1047. this.skill.Owner.RemoveActionSubState(UnitActionSubStatus.ChargeAtkIdle);
  1048. }
  1049. current_action = null;
  1050. return false;
  1051. }
  1052. public void doKeyFrame(UnitActionData.KeyFrame kf)
  1053. {
  1054. // 关键帧改变状态
  1055. if (kf.ChangeStatus != null)
  1056. {
  1057. this.IsNoneBlock = kf.ChangeStatus.IsNoneBlock;
  1058. this.IsNoneTouch = kf.ChangeStatus.IsNoneTouch;
  1059. this.IsFaceToTarget = kf.ChangeStatus.IsFaceToTarget;
  1060. this.IsCancelableByMove = kf.ChangeStatus.IsCancelable;
  1061. this.IsCancelableBySkill = kf.ChangeStatus.IsCancelableBySkill;
  1062. this.IsControlFaceable = kf.ChangeStatus.IsControlFaceable;
  1063. this.IsControlMoveable = kf.ChangeStatus.IsControlMoveable;
  1064. }
  1065. // 如果关键帧绑定特效
  1066. if (kf.Effect != null)
  1067. {
  1068. unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Effect));
  1069. }
  1070. // 如果关键帧绑定近战攻击
  1071. if (kf.Attack != null)
  1072. {
  1073. doHitAttack(kf.Attack);
  1074. }
  1075. // 如果关键帧绑定释放法术
  1076. if (kf.Spell != null)
  1077. {
  1078. float tx = unit.X;
  1079. float ty = unit.Y;
  1080. if (param.SpellTargetPos != null)
  1081. {
  1082. float td = MathVector.getDistance(unit.X, unit.Y, param.SpellTargetPos.X, param.SpellTargetPos.Y);
  1083. // TargetPos超出技能范围 //
  1084. if (td > SkillData.AttackRange)
  1085. {
  1086. // 把TargetPos拉回 //
  1087. MathVector.movePolar(param.SpellTargetPos,
  1088. MathVector.getDegree(unit.X, unit.Y, param.SpellTargetPos.X, param.SpellTargetPos.Y),
  1089. SkillData.AttackRange - td);
  1090. }
  1091. // 设置法术出生点 (非自身坐标发射,比如Cannon) //
  1092. if (!SkillData.IsLaunchBody)
  1093. {
  1094. tx = param.SpellTargetPos.X;
  1095. ty = param.SpellTargetPos.Y;
  1096. }
  1097. }
  1098. //else if(this.targetUnit != null)
  1099. //{
  1100. // tx = this.targetUnit.X;
  1101. // ty = this.targetUnit.Y;
  1102. //}
  1103. zone.unitLaunchSpell(this.skill == null ?XmdsSkillType.none : this.skill.GetSkillType(), unit, kf.Spell,
  1104. tx, ty, param.TargetUnitID, param.SpellTargetPos, this.CurrentActionIndex);
  1105. }
  1106. // 如果关键帧绑定自己释放BUFF
  1107. if (kf.SelfBuff != null)
  1108. {
  1109. unit.AddBuff(kf.SelfBuff, unit);
  1110. }
  1111. // 如果关键帧绑定单位位移
  1112. if (kf.Move != null && (targetUnit == null || kf.hitTargetMoveEnable))
  1113. {
  1114. StartMove action_move = kf.Move;
  1115. //if (unit.IsSkillControllableByServer)
  1116. {
  1117. this.start_move = unit.StartHitMove(this,
  1118. unit.Direction + action_move.Direction,
  1119. action_move.RotateSpeedSEC,
  1120. action_move.KeepTimeMS,
  1121. action_move.SpeedSEC,
  1122. action_move.SpeedAdd,
  1123. action_move.SpeedAcc,
  1124. action_move.IsNoneTouch);
  1125. this.start_move.SetFly(this,
  1126. action_move.ZSpeedSEC,
  1127. action_move.ZLimit,
  1128. 0);
  1129. }
  1130. if (action_move.HasFly && !zone.IsSyncZ)
  1131. {
  1132. unit.queueEvent(new UnitJumpEvent(unit.ID, action_move.ZSpeedSEC, action_move.OverrideGravity, action_move.ZLimit));
  1133. }
  1134. }
  1135. if (kf.Blink != null)
  1136. {
  1137. if (kf.Blink.BeginEffect != null)
  1138. {
  1139. unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Blink.BeginEffect));
  1140. }
  1141. unit.moveBlink(kf.Blink, param.SpellTargetPos, targetUnit);
  1142. unit.SendForceSync();
  1143. if (kf.Blink.TargetEffect != null)
  1144. {
  1145. unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Blink.TargetEffect));
  1146. }
  1147. }
  1148. }
  1149. /// <summary>
  1150. /// 近战攻击
  1151. /// </summary>
  1152. /// <param name="attack"></param>
  1153. private void doHitAttack(AttackProp attack)
  1154. {
  1155. float rg = unit.GetSkillAttackRange(skill.Data);
  1156. if (current_action.OverrideAttackShape != null)
  1157. {
  1158. attack_range.Shape = (AttackShape)current_action.OverrideAttackShape.AShape;
  1159. attack_range.Direction = unit.Direction;
  1160. attack_range.ExpectTarget = SkillData.ExpectTarget;
  1161. attack_range.BodySize = current_action.OverrideAttackShape.AttackRange;
  1162. attack_range.Distance = current_action.OverrideAttackShape.AttackRange;
  1163. attack_range.FanAngle = current_action.OverrideAttackShape.AttackAngle;
  1164. attack_range.StripWide = current_action.OverrideAttackShape.StripWide;
  1165. float dx = unit.X;
  1166. float dy = unit.Y;
  1167. if (current_action.OverrideAttackShape.OffsetRadius != 0)
  1168. {
  1169. MathVector.movePolar(ref dx, ref dy, unit.Direction, current_action.OverrideAttackShape.OffsetRadius);
  1170. }
  1171. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1172. {
  1173. attack_range.GetShapeAttackable(list, AttackReason.Attack, SkillData, dx, dy, dx, dy, 0, unit.AoiStatus);
  1174. if (list.Count > 0)
  1175. {
  1176. zone.unitAttackDirect(unit, new AttackSource(skill, attack), list);
  1177. }
  1178. }
  1179. }
  1180. else if (skill.Data.AttackAngle == 0)
  1181. {
  1182. if (targetUnit != null)
  1183. {
  1184. if (checkTargetRange())
  1185. {
  1186. zone.unitAttackSingle(
  1187. unit,
  1188. new AttackSource(skill, attack),
  1189. targetUnit,
  1190. SkillData.ExpectTarget);
  1191. }
  1192. }
  1193. }
  1194. else
  1195. {
  1196. int hitcount = zone.unitAttackFan(
  1197. unit,
  1198. new AttackSource(skill, attack),
  1199. unit.Direction,
  1200. rg,
  1201. skill.Data.AttackAngle,
  1202. SkillData.ExpectTarget);
  1203. }
  1204. }
  1205. /// <summary>
  1206. /// 身体攻击
  1207. /// </summary>
  1208. private void doBodyHit(UnitActionData current_action)
  1209. {
  1210. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1211. {
  1212. float line_r = (current_action.BodyHitSize > 0) ? current_action.BodyHitSize : unit.BodyHitSize;
  1213. zone.getObjectsRoundLineRange<InstanceUnit>(
  1214. Collider.Object_HitBody_TouchRoundLine,
  1215. body_hited_last_pos.X,
  1216. body_hited_last_pos.Y,
  1217. unit.X,
  1218. unit.Y,
  1219. line_r,
  1220. list,
  1221. unit.AoiStatus);
  1222. if (list.Count > 0)
  1223. {
  1224. CUtils.RemoveAll(list, body_hited.Values);
  1225. zone.unitAttack(unit, new AttackSource(skill, current_action.BodyHit), list, SkillData.ExpectTarget);
  1226. if (list.Count > 0)
  1227. {
  1228. for (int i = 0; i < list.Count; i++)
  1229. {
  1230. InstanceUnit o = list[i];
  1231. body_hited.Put(o.ID, o);
  1232. }
  1233. if (current_action.BodyHitNextAction)
  1234. {
  1235. if (nextAction())
  1236. {
  1237. unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
  1238. }
  1239. unit.SendForceSync();
  1240. }
  1241. }
  1242. }
  1243. }
  1244. }
  1245. private void doMove()
  1246. {
  1247. start_move.IsNoneTouch = IsNoneTouch;
  1248. if (start_move.IsEnd)
  1249. {
  1250. start_move = null;
  1251. }
  1252. }
  1253. /// <summary>
  1254. /// 防止技能位移导致单位重合
  1255. /// </summary>
  1256. private void doBodyBlock(UnitActionData current_action)
  1257. {
  1258. if (start_move != null || move_to != null)
  1259. {
  1260. if (unit.ElasticOtherObjects())
  1261. {
  1262. unit.SendForceSync();
  1263. }
  1264. }
  1265. }
  1266. /// <summary>
  1267. /// 移动到目标面前
  1268. /// </summary>
  1269. private bool doMoveToTarget(UnitActionData current_action)
  1270. {
  1271. if (this.move_to_target_path == null)
  1272. {
  1273. if (nextAction())
  1274. {
  1275. unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
  1276. }
  1277. return true;
  1278. }
  1279. else if (targetUnit != null && zone.TouchObject2(unit, targetUnit))
  1280. {
  1281. this.move_to_target_path = null;
  1282. unit.ElasticOtherObjects();
  1283. if (nextAction())
  1284. {
  1285. unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
  1286. }
  1287. return true;
  1288. }
  1289. else
  1290. {
  1291. float tx, ty;
  1292. tx = move_to_target_path.PosX;
  1293. ty = move_to_target_path.PosY;
  1294. unit.faceTo(tx, ty);// = MathVector.getDegree(tx - unit.X, ty - unit.Y);
  1295. if (unit.jumpToTarget(tx, ty, current_action.MoveToTargetSpeedSEC, zone.UpdateIntervalMS))
  1296. {
  1297. move_to_target_path = move_to_target_path.Next;
  1298. }
  1299. unit.SendForceSync();
  1300. return false;
  1301. }
  1302. }
  1303. private bool doJumpToTarget(UnitActionData action)
  1304. {
  1305. unit.Z = MoveHelper.CalulateParabolicHeight(action.JumpToTargetHeightZ, action.TotalTimeMS, this.current_pass_time);
  1306. unit.jumpToTarget(jump_to_target_pos.X, jump_to_target_pos.Y, jump_to_target_speed_sec, zone.UpdateIntervalMS);
  1307. unit.SendForceSync();
  1308. return false;
  1309. }
  1310. public bool setMoveTo(float x, float y)
  1311. {
  1312. //if(Math.Abs(this.unit.X - x) < 0.5 && Math.Abs(this.unit.Y - y) < 0.5)
  1313. //{
  1314. // return true;
  1315. //}
  1316. if (unit.IsSkillControllableByServer)
  1317. {
  1318. if (IsControlMoveable && !unit.IsCannotMove)
  1319. {
  1320. if (move_to == null)
  1321. {
  1322. move_to = new Vector2(x, y);
  1323. }
  1324. else
  1325. {
  1326. move_to.SetX(x);
  1327. move_to.SetY(y);
  1328. }
  1329. }
  1330. if (IsControlFaceable)
  1331. {
  1332. unit.faceTo(x, y);
  1333. }
  1334. this.StopFaceTo = new Vector2(x - unit.X, y - unit.Y);
  1335. }
  1336. return false;
  1337. }
  1338. public void setMoveTo(Vector2 pos)
  1339. {
  1340. if (unit.IsSkillControllableByServer)
  1341. {
  1342. if (pos == null)
  1343. {
  1344. this.move_to = null;
  1345. return;
  1346. }
  1347. if (IsControlMoveable && !unit.IsCannotMove)
  1348. {
  1349. move_to = pos;
  1350. }
  1351. if (IsControlFaceable)
  1352. {
  1353. unit.faceTo(pos.X, pos.Y);
  1354. }
  1355. this.StopFaceTo = new Vector2(pos.X - unit.X, pos.Y - unit.Y);
  1356. }
  1357. }
  1358. public void block(State newState = null)
  1359. {
  1360. bool old_done = is_done;
  1361. is_done = true;
  1362. if (newState != null)
  1363. {
  1364. unit.changeState(newState);
  1365. }
  1366. else if (!old_done)
  1367. {
  1368. unit.doSomething();
  1369. }
  1370. }
  1371. /// <summary>
  1372. /// 检测目标距离
  1373. /// </summary>
  1374. /// <returns></returns>
  1375. private bool checkTargetRange()
  1376. {
  1377. return skill.checkTargetRange(targetUnit);
  1378. }
  1379. /// <summary>
  1380. /// 检测冲锋距离
  1381. /// </summary>
  1382. /// <returns></returns>
  1383. private bool checkMoveToTarget(UnitActionData action)
  1384. {
  1385. if (action.IsMoveToTarget)
  1386. {
  1387. float tx; float ty;
  1388. if (getTargetPos(out tx, out ty))
  1389. {
  1390. this.move_to_target_path = zone.findPath(unit.X, unit.Y, tx, ty);
  1391. if (this.move_to_target_path != null)
  1392. {
  1393. float max = MoveHelper.GetDistance(action.TotalTimeMS, action.MoveToTargetSpeedSEC);
  1394. float total = this.move_to_target_path.GetTotalDistance();
  1395. if (total > max)
  1396. {
  1397. move_to_target_path = null;
  1398. return false;
  1399. }
  1400. return true;
  1401. }
  1402. }
  1403. return false;
  1404. }
  1405. return true;
  1406. }
  1407. /// <summary>
  1408. /// 检测跳跃速度
  1409. /// </summary>
  1410. /// <param name="action"></param>
  1411. /// <returns></returns>
  1412. private bool checkJumpToTarget(UnitActionData action)
  1413. {
  1414. if (action.IsJumpToTarget)
  1415. {
  1416. float tx; float ty;
  1417. if (getTargetPos(out tx, out ty))
  1418. {
  1419. float distance = CMath.getDistance(unit.X, unit.Y, tx, ty);
  1420. this.jump_to_target_pos = new TVector2(tx, ty);
  1421. this.jump_to_target_speed_sec = distance / (action.TotalTimeMS * 0.001f);
  1422. }
  1423. }
  1424. return true;
  1425. }
  1426. private bool getTargetPos(out float x, out float y)
  1427. {
  1428. x = unit.X;
  1429. y = unit.Y;
  1430. if (param.SpellTargetPos != null)
  1431. {
  1432. x = param.SpellTargetPos.X;
  1433. y = param.SpellTargetPos.Y;
  1434. return true;
  1435. }
  1436. else if (targetUnit != null && targetUnit != unit)
  1437. {
  1438. x = targetUnit.X;
  1439. y = targetUnit.Y;
  1440. return true;
  1441. }
  1442. else return false;
  1443. }
  1444. public void doUpdatePosByClient(UnitUpdatePosAction act)
  1445. {
  1446. if (!unit.IsSkillControllableByServer)
  1447. {
  1448. if(this.skill.GetSkillType() == XmdsSkillType.petGiveAcitve)
  1449. {
  1450. unit.setPos(act.x, act.y, unit.IntersectMap);
  1451. unit.faceTo(act.d);
  1452. }
  1453. else
  1454. {
  1455. //暂时只管方向,不管pos
  1456. if (this.IsControlMoveable && !unit.IsCannotMove)
  1457. {
  1458. unit.setPos(act.x, act.y, unit.IntersectMap);
  1459. }
  1460. if (this.IsControlFaceable)
  1461. {
  1462. unit.faceTo(act.d);
  1463. }
  1464. else
  1465. {
  1466. unit.SendForceSync();
  1467. }
  1468. }
  1469. }
  1470. }
  1471. public delegate void SkillLaunched(StateSkill state);
  1472. private SkillLaunched mSkillLaunched;
  1473. public event SkillLaunched OnSkillLaunched { add { mSkillLaunched += value; } remove { mSkillLaunched -= value; } }
  1474. }
  1475. internal void SetActionSubState(UnitActionStatus unitSubState)
  1476. {
  1477. throw new NotImplementedException();
  1478. }
  1479. //--------------------------------------------------------------------------------------------------------
  1480. //---------------------------------------------------------------------------------------
  1481. public class StatePickObject : State
  1482. {
  1483. readonly private TimeExpire<int> mTimer;
  1484. readonly private int mTotalTimeMS;
  1485. readonly private InstanceZoneObject mPickable;
  1486. private bool mIsDone = false;
  1487. private string mStopReason;
  1488. private string mStatus;
  1489. private OnPickDone mOnDone;
  1490. private OnCheckPickable mCheckTargetActive;
  1491. public string StopReason { get { return mStopReason; } }
  1492. /// <summary>
  1493. /// 设置目标检测状态方法
  1494. /// </summary>
  1495. /// <param name="check">check返回True,终止状态机。</param>
  1496. public void SetCheckTargetAcvite(OnCheckPickable check)
  1497. {
  1498. this.mCheckTargetActive = check;
  1499. }
  1500. public StatePickObject(InstanceUnit unit, InstanceZoneObject pickable, int timeMS, string status, OnPickDone done)
  1501. : base(unit)
  1502. {
  1503. this.mStatus = status;
  1504. this.mTimer = new TimeExpire<int>(timeMS);
  1505. this.mPickable = pickable;
  1506. this.mTotalTimeMS = timeMS;
  1507. this.mOnDone = done;
  1508. }
  1509. public override bool onBlock(State new_state)
  1510. {
  1511. if (new_state is StateIdle)
  1512. return mIsDone;
  1513. //block by anything//
  1514. return true;
  1515. }
  1516. protected override void onStart()
  1517. {
  1518. unit.SetActionStatus(UnitActionStatus.Pick);
  1519. unit.queueEvent(new UnitStartPickObjectEvent(unit.ID, mTotalTimeMS, mPickable.ID, mStatus));
  1520. }
  1521. protected override void onStop()
  1522. {
  1523. unit.queueEvent(new UnitStopPickObjectEvent(unit.ID, mStopReason));
  1524. this.mOnDone = null;
  1525. mCheckTargetActive = null;
  1526. }
  1527. protected override void onUpdate()
  1528. {
  1529. if (!mPickable.Enable)
  1530. {
  1531. mIsDone = true;
  1532. unit.doSomething();
  1533. return;
  1534. }
  1535. if (mCheckTargetActive != null && mCheckTargetActive(unit, mPickable, ref mStopReason))
  1536. {
  1537. mIsDone = true;
  1538. unit.doSomething();
  1539. return;
  1540. }
  1541. if (mTimer.Update(zone.UpdateIntervalMS))
  1542. {
  1543. mIsDone = true;
  1544. unit.doSomething();
  1545. if (mOnDone != null)
  1546. {
  1547. mOnDone.Invoke(unit, mPickable);
  1548. }
  1549. }
  1550. }
  1551. public delegate void OnPickDone(InstanceUnit unit, InstanceZoneObject pickable);
  1552. public delegate bool OnCheckPickable(InstanceUnit unit, InstanceZoneObject pickable, ref string reason);
  1553. }
  1554. //---------------------------------------------------------------------------------------
  1555. #region __状态限制__
  1556. //---------------------------------------------------------------------------------------
  1557. public interface IStateNoneControllable
  1558. {
  1559. State AsState();
  1560. }
  1561. /// <summary>
  1562. /// 被击中受击过程
  1563. /// </summary>
  1564. public class StateDamage : State, IStateNoneControllable
  1565. {
  1566. private readonly AttackSource source;
  1567. private readonly InstanceUnit attacker;
  1568. private float startDirection;
  1569. private float rotateSpeedSEC;
  1570. private bool isEnd = false;
  1571. private HitMoveSpeed hitMoveSpeed;
  1572. private TimeExpire<object> damageExpire;
  1573. private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
  1574. private IStateNoneControllable nextState;
  1575. public void SetNextNoneControllable(IStateNoneControllable state)
  1576. {
  1577. nextState = state;
  1578. }
  1579. public State AsState()
  1580. {
  1581. return this;
  1582. }
  1583. public bool IsFallingDown
  1584. {
  1585. get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
  1586. }
  1587. public bool IsKnockDown
  1588. {
  1589. get { return source.OutHasKnockDown; }
  1590. }
  1591. public bool IsDamageProtect
  1592. {
  1593. get { return source.Attack.IsDamageProtect; }
  1594. }
  1595. public int DamageTimeMS
  1596. {
  1597. get { return damageExpire.TotalTimeMS; }
  1598. }
  1599. public bool IsHitMove
  1600. {
  1601. get { return hitMoveSpeed != null; }
  1602. }
  1603. public StateDamage(InstanceUnit unit, AttackSource source, InstanceUnit attacker)
  1604. : base(unit)
  1605. {
  1606. this.source = source;
  1607. this.attacker = attacker;
  1608. this.body_hited.Put(unit.ID, unit);
  1609. AttackProp.HitMoveType mtype = source.Attack.HitMoveMType;
  1610. if (source.FromSpell != null)
  1611. {
  1612. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
  1613. }
  1614. else if (source.FromSkill != null)
  1615. {
  1616. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
  1617. }
  1618. else
  1619. {
  1620. this.startDirection = unit.Direction + CMath.PI_F;
  1621. }
  1622. if (source.OutHitMove != null)
  1623. {
  1624. this.startDirection += source.OutHitMove.Direction;
  1625. this.rotateSpeedSEC = source.OutHitMove.RotateSpeedSEC * ((unit.RandomN.Next() % 2) == 0 ? -1 : 1);
  1626. }
  1627. // 计算受击时间 //
  1628. int damageTime = unit.Info.DamageTimeMS;
  1629. if (source.OutKnockDownTimeMS > 0)
  1630. {
  1631. damageTime = source.OutKnockDownTimeMS;
  1632. }
  1633. else if (unit.mInfo.DamageTimeMS > 0)
  1634. {
  1635. damageTime = unit.mInfo.DamageTimeMS;
  1636. }
  1637. else
  1638. {
  1639. damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
  1640. }
  1641. this.damageExpire = new TimeExpire<object>(damageTime);
  1642. }
  1643. override public bool onBlock(State new_state)
  1644. {
  1645. if (isEnd)
  1646. {
  1647. return true;
  1648. }
  1649. if (new_state is StateDead)
  1650. {
  1651. return true;
  1652. }
  1653. if (new_state is StateDamage)
  1654. {
  1655. return onBlockNewDamage(new_state as StateDamage);
  1656. }
  1657. if (new_state is IStateNoneControllable)
  1658. {
  1659. this.SetNextNoneControllable(new_state as IStateNoneControllable);
  1660. }
  1661. if (unit.IsDead())
  1662. {
  1663. return false;
  1664. }
  1665. if (new_state is StateSkill)
  1666. {
  1667. StateSkill ss = new_state as StateSkill;
  1668. // 反击或者状态解除//
  1669. if (ss.SkillData.IsCounter)
  1670. {
  1671. return true;
  1672. }
  1673. }
  1674. return isEnd;
  1675. }
  1676. private bool onBlockNewDamage(StateDamage new_damage)
  1677. {
  1678. //击飞只能被击飞中断//
  1679. if (this.IsFallingDown)
  1680. {
  1681. if (!new_damage.source.OutHasFly)
  1682. {
  1683. return false;
  1684. }
  1685. }
  1686. //击倒只能被击飞或击倒中断//
  1687. if (this.IsKnockDown)
  1688. {
  1689. if (!new_damage.source.OutHasFly)
  1690. {
  1691. return false;
  1692. }
  1693. if (!new_damage.source.OutHasKnockDown)
  1694. {
  1695. return false;
  1696. }
  1697. }
  1698. //如果正在位移,需要更高优先级//
  1699. if (hitMoveSpeed != null)
  1700. {
  1701. if (new_damage.source.OutHasFly || new_damage.source.OutHasKnockDown || new_damage.source.OutHitMove != null)
  1702. {
  1703. //带位移的受击,相等优先级也可以打断当前位移//
  1704. if (new_damage.source.OutWeight >= this.source.OutWeight)
  1705. {
  1706. return true;
  1707. }
  1708. }
  1709. else
  1710. {
  1711. //普通受击,更高优先级才能打断当前位移//
  1712. if (new_damage.source.OutWeight > this.source.OutWeight)
  1713. {
  1714. return true;
  1715. }
  1716. }
  1717. return false;
  1718. }
  1719. return true;
  1720. }
  1721. override protected void onStart()
  1722. {
  1723. float zgravity = 0;
  1724. float zlimit = 0;
  1725. bool hasHitMove = false;
  1726. if (source.OutHitMove != null)
  1727. {
  1728. int keepTimeMs = source.OutHitMove.KeepTimeMS;
  1729. float speedSEC = source.OutHitMove.SpeedSEC;
  1730. if (source.Attack.HitMoveMType == AttackProp.HitMoveType.BySenderRange)
  1731. {
  1732. float distance = CMath.getDistance(attacker.X, attacker.Y, unit.X, unit.Y);
  1733. if (distance > source.FromSpell.BodySize)
  1734. {
  1735. keepTimeMs = 0;
  1736. speedSEC = 0;
  1737. }
  1738. else
  1739. {
  1740. float moveDistance = source.FromSpell.BodySize - distance;
  1741. keepTimeMs = (int)(moveDistance * 1000 / source.OutHitMove.SpeedSEC);
  1742. }
  1743. }
  1744. hasHitMove = true;
  1745. this.hitMoveSpeed = unit.StartHitMove(this,
  1746. startDirection,
  1747. source.OutHitMove.RotateSpeedSEC,
  1748. keepTimeMs,
  1749. speedSEC,
  1750. source.OutHitMove.SpeedAdd,
  1751. source.OutHitMove.SpeedAcc,
  1752. source.OutHitMove.IsNoneTouch);
  1753. if (source.OutHasFly)
  1754. {
  1755. this.hitMoveSpeed.SetFly(this,
  1756. source.OutHitMove.ZSpeedSEC,
  1757. source.OutHitMove.OverrideGravity,
  1758. source.OutHitMove.ZLimit);
  1759. zgravity = source.OutHitMove.OverrideGravity;
  1760. zlimit = source.OutHitMove.ZLimit;
  1761. }
  1762. }
  1763. else if (source.OutHasFly)
  1764. {
  1765. hasHitMove = true;
  1766. this.hitMoveSpeed = unit.StartHitMove(this,
  1767. startDirection,
  1768. 0,
  1769. 0, // 如果击飞,则计算按落地时间 //
  1770. zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_SEC,
  1771. zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_ADD,
  1772. zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_ACC,
  1773. false);
  1774. this.hitMoveSpeed.SetFly(this,
  1775. zone.Templates.CFG.OBJECT_DAMAGE_FLY_ZSPEED_SEC, 0, 0);
  1776. }
  1777. int moveTime = 0;
  1778. float zspeed = 0;
  1779. if (hitMoveSpeed != null)
  1780. {
  1781. moveTime = hitMoveSpeed.TotalTimeMS;
  1782. zspeed = hitMoveSpeed.ZSpeedSEC;
  1783. if (source.Attack.HitMoveMType == AttackProp.HitMoveType.ToSenderCenter)
  1784. {
  1785. if (source.FromSpellUnit != null)
  1786. {
  1787. this.hitMoveSpeed.SetMoveTarget(source.FromSpellUnit, false);
  1788. }
  1789. else if (source.FromSkill != null)
  1790. {
  1791. this.hitMoveSpeed.SetMoveTarget(this.attacker, false);
  1792. }
  1793. }
  1794. else if (source.Attack.HitMoveMType == AttackProp.HitMoveType.ToSenderBodySize)
  1795. {
  1796. if (source.FromSpellUnit != null)
  1797. {
  1798. this.hitMoveSpeed.SetMoveTarget(source.FromSpellUnit, true);
  1799. }
  1800. else if (source.FromSkill != null)
  1801. {
  1802. this.hitMoveSpeed.SetMoveTarget(this.attacker, true);
  1803. }
  1804. }
  1805. }
  1806. unit.queueEvent(new UnitDamageEvent(
  1807. unit.ID,
  1808. DamageTimeMS,
  1809. moveTime,
  1810. zspeed,
  1811. zgravity,
  1812. zlimit,
  1813. source.OutHasKnockDown,
  1814. source.Attack));
  1815. if (hasHitMove)
  1816. {
  1817. unit.SetActionStatus(UnitActionStatus.HitMove);
  1818. }
  1819. else
  1820. {
  1821. unit.SetActionStatus(UnitActionStatus.Damage);
  1822. }
  1823. }
  1824. override protected void onUpdate()
  1825. {
  1826. if (hitMoveSpeed != null)
  1827. {
  1828. if (hitMoveSpeed.IsEnd)
  1829. {
  1830. hitMoveSpeed = null;
  1831. if (source.Attack.FlyFallenDownAttack != null)
  1832. {
  1833. doFlyDownAttack();
  1834. }
  1835. }
  1836. else if (source.Attack.HitMoveBodyAttack != null)
  1837. {
  1838. doBodyAttack();
  1839. }
  1840. }
  1841. else if (damageExpire != null)
  1842. {
  1843. if (damageExpire.Update(zone.UpdateIntervalMS))
  1844. {
  1845. damageExpire = null;
  1846. }
  1847. }
  1848. else
  1849. {
  1850. end();
  1851. }
  1852. }
  1853. override protected void onStop()
  1854. {
  1855. if (unit.IsStun)
  1856. {
  1857. unit.changeState(new StateStun(unit));
  1858. }
  1859. else if (nextState != null)
  1860. {
  1861. unit.changeState(nextState.AsState());
  1862. }
  1863. }
  1864. private void end()
  1865. {
  1866. if (unit.IsDead())
  1867. {
  1868. isEnd = true;
  1869. unit.changeState(new StateDead(unit, attacker));
  1870. }
  1871. else if (unit.IsStun)
  1872. {
  1873. unit.SetActionStatus(UnitActionStatus.Stun);
  1874. }
  1875. else
  1876. {
  1877. isEnd = true;
  1878. unit.doSomething();
  1879. }
  1880. }
  1881. /// <summary>
  1882. /// 自己落地摔一下
  1883. /// </summary>
  1884. private void doFlyDownAttack()
  1885. {
  1886. unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
  1887. }
  1888. /// <summary>
  1889. /// 飞行过程中打别人
  1890. /// </summary>
  1891. private void doBodyAttack()
  1892. {
  1893. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1894. {
  1895. zone.getObjectsRoundLineRange<InstanceUnit>(
  1896. Collider.Object_HitBody_TouchRoundLine,
  1897. hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
  1898. unit.X, unit.Y,
  1899. unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
  1900. list, unit.AoiStatus);
  1901. if (list.Count > 0)
  1902. {
  1903. CUtils.RemoveAll(list, body_hited.Values);
  1904. zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
  1905. if (list.Count > 0)
  1906. {
  1907. for (int i = 0; i < list.Count; i++)
  1908. {
  1909. InstanceUnit o = list[i];
  1910. body_hited.Put(o.ID, o);
  1911. }
  1912. }
  1913. }
  1914. }
  1915. }
  1916. }
  1917. /// <summary>
  1918. /// 死了,而且是被艹翻的,暴击打死然后击退
  1919. /// </summary>
  1920. public class StateDeadFuckFuck : State, IStateNoneControllable
  1921. {
  1922. private readonly AttackSource source;
  1923. private readonly InstanceUnit attacker;
  1924. private float startDirection;
  1925. private bool isEnd = false;
  1926. private HitMoveSpeed hitMoveSpeed;
  1927. private TimeExpire<object> damageExpire;
  1928. private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
  1929. private IStateNoneControllable nextState;
  1930. public void SetNextNoneControllable(IStateNoneControllable state)
  1931. {
  1932. nextState = state;
  1933. }
  1934. public State AsState()
  1935. {
  1936. return this;
  1937. }
  1938. public bool IsFallingDown
  1939. {
  1940. get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
  1941. }
  1942. public bool IsKnockDown
  1943. {
  1944. get { return source.OutHasKnockDown; }
  1945. }
  1946. public bool IsDamageProtect
  1947. {
  1948. get { return source.Attack.IsDamageProtect; }
  1949. }
  1950. public int DamageTimeMS
  1951. {
  1952. get { return damageExpire.TotalTimeMS; }
  1953. }
  1954. public bool IsHitMove
  1955. {
  1956. get { return hitMoveSpeed != null; }
  1957. }
  1958. public StateDeadFuckFuck(InstanceUnit unit, AttackSource source, InstanceUnit attacker, HitMoveType movetype)
  1959. : base(unit)
  1960. {
  1961. this.source = source;
  1962. this.attacker = attacker;
  1963. this.body_hited.Put(unit.ID, unit);
  1964. HitMoveType mtype = movetype;
  1965. if (source.FromSpell != null)
  1966. {
  1967. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
  1968. }
  1969. else if (source.FromSkill != null)
  1970. {
  1971. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
  1972. }
  1973. else
  1974. {
  1975. this.startDirection = unit.Direction + CMath.PI_F;
  1976. }
  1977. // 计算受击时间 //
  1978. int damageTime = unit.Info.DamageTimeMS;
  1979. if (source.OutKnockDownTimeMS > 0)
  1980. {
  1981. damageTime = source.OutKnockDownTimeMS;
  1982. }
  1983. else if (unit.mInfo.DamageTimeMS > 0)
  1984. {
  1985. damageTime = unit.mInfo.DamageTimeMS;
  1986. }
  1987. else
  1988. {
  1989. damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
  1990. }
  1991. this.damageExpire = new TimeExpire<object>(damageTime);
  1992. }
  1993. override public bool onBlock(State new_state)
  1994. {
  1995. if (isEnd)
  1996. {
  1997. return true;
  1998. }
  1999. if (new_state is StateDead)
  2000. {
  2001. return true;
  2002. }
  2003. if (new_state is IStateNoneControllable)
  2004. {
  2005. this.SetNextNoneControllable(new_state as IStateNoneControllable);
  2006. }
  2007. if (unit.IsDead())
  2008. {
  2009. return false;
  2010. }
  2011. if (new_state is StateDamage)
  2012. {
  2013. return unit.CanWhiplashDeadBody;
  2014. }
  2015. if (new_state is StateRebirth)
  2016. {
  2017. return true;
  2018. }
  2019. return isEnd;
  2020. }
  2021. override protected void onStart()
  2022. {
  2023. unit.SetActionStatus(UnitActionStatus.Dead);
  2024. float zgravity = 0;
  2025. float zlimit = 0;
  2026. if (true)
  2027. {
  2028. int keepTimeMs = 0;
  2029. int moveDistance = new Random().Next(2, 12);
  2030. float speedSEC = 15;
  2031. keepTimeMs = (int)(moveDistance * 1000 / speedSEC);
  2032. //speedSEC = new Random().Next(0, 11);
  2033. this.hitMoveSpeed = unit.StartHitMove(this,
  2034. startDirection,
  2035. 0,
  2036. keepTimeMs,
  2037. speedSEC,
  2038. 0,
  2039. 0,
  2040. false);
  2041. }
  2042. int moveTime = 0;
  2043. float zspeed = 0;
  2044. if (hitMoveSpeed != null)
  2045. {
  2046. moveTime = hitMoveSpeed.TotalTimeMS;
  2047. zspeed = hitMoveSpeed.ZSpeedSEC;
  2048. }
  2049. unit.queueEvent(new UnitDamageEvent(
  2050. unit.ID,
  2051. DamageTimeMS,
  2052. moveTime,
  2053. zspeed,
  2054. zgravity,
  2055. zlimit,
  2056. source.OutHasKnockDown,
  2057. source.Attack));
  2058. //unit.SetActionStatus(UnitActionStatus.Dead);
  2059. }
  2060. override protected void onUpdate()
  2061. {
  2062. if (hitMoveSpeed != null)
  2063. {
  2064. if (hitMoveSpeed.IsEnd)
  2065. {
  2066. hitMoveSpeed = null;
  2067. if (source.Attack.FlyFallenDownAttack != null)
  2068. {
  2069. doFlyDownAttack();
  2070. }
  2071. }
  2072. else if (source.Attack.HitMoveBodyAttack != null)
  2073. {
  2074. doBodyAttack();
  2075. }
  2076. }
  2077. else if (damageExpire != null)
  2078. {
  2079. if (damageExpire.Update(zone.UpdateIntervalMS))
  2080. {
  2081. damageExpire = null;
  2082. }
  2083. }
  2084. else
  2085. {
  2086. end();
  2087. }
  2088. }
  2089. override protected void onStop()
  2090. {
  2091. if (unit.IsStun)
  2092. {
  2093. unit.changeState(new StateStun(unit));
  2094. }
  2095. else if (nextState != null)
  2096. {
  2097. unit.changeState(nextState.AsState());
  2098. }
  2099. }
  2100. private void end()
  2101. {
  2102. isEnd = true;
  2103. unit.changeState(new StateDead(unit, attacker));
  2104. }
  2105. /// <summary>
  2106. /// 自己落地摔一下
  2107. /// </summary>
  2108. private void doFlyDownAttack()
  2109. {
  2110. unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
  2111. }
  2112. /// <summary>
  2113. /// 飞行过程中打别人
  2114. /// </summary>
  2115. private void doBodyAttack()
  2116. {
  2117. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  2118. {
  2119. zone.getObjectsRoundLineRange<InstanceUnit>(
  2120. Collider.Object_HitBody_TouchRoundLine,
  2121. hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
  2122. unit.X, unit.Y,
  2123. unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
  2124. list, unit.AoiStatus);
  2125. if (list.Count > 0)
  2126. {
  2127. CUtils.RemoveAll(list, body_hited.Values);
  2128. zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
  2129. if (list.Count > 0)
  2130. {
  2131. for (int i = 0; i < list.Count; i++)
  2132. {
  2133. InstanceUnit o = list[i];
  2134. body_hited.Put(o.ID, o);
  2135. }
  2136. }
  2137. }
  2138. }
  2139. }
  2140. }
  2141. /// <summary>
  2142. /// 死了,而且是被艹翻的,暴击打死然后击退
  2143. /// </summary>
  2144. public class StateDeadFuck : State, IStateNoneControllable
  2145. {
  2146. private readonly AttackSource source;
  2147. private readonly InstanceUnit attacker;
  2148. private float startDirection;
  2149. private bool isEnd = false;
  2150. private HitMoveSpeed hitMoveSpeed;
  2151. private TimeExpire<object> damageExpire;
  2152. private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
  2153. private IStateNoneControllable nextState;
  2154. public void SetNextNoneControllable(IStateNoneControllable state)
  2155. {
  2156. nextState = state;
  2157. }
  2158. public State AsState()
  2159. {
  2160. return this;
  2161. }
  2162. public bool IsFallingDown
  2163. {
  2164. get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
  2165. }
  2166. public bool IsKnockDown
  2167. {
  2168. get { return source.OutHasKnockDown; }
  2169. }
  2170. public bool IsDamageProtect
  2171. {
  2172. get { return source.Attack.IsDamageProtect; }
  2173. }
  2174. public int DamageTimeMS
  2175. {
  2176. get { return damageExpire.TotalTimeMS; }
  2177. }
  2178. public bool IsHitMove
  2179. {
  2180. get { return hitMoveSpeed != null; }
  2181. }
  2182. public StateDeadFuck(InstanceUnit unit, AttackSource source, InstanceUnit attacker)
  2183. : base(unit)
  2184. {
  2185. this.source = source;
  2186. this.attacker = attacker;
  2187. this.body_hited.Put(unit.ID, unit);
  2188. AttackProp.HitMoveType mtype = AttackProp.HitMoveType.BySenderPosition;
  2189. if (source.FromSpell != null)
  2190. {
  2191. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
  2192. }
  2193. else if (source.FromSkill != null)
  2194. {
  2195. this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
  2196. }
  2197. else
  2198. {
  2199. this.startDirection = unit.Direction + CMath.PI_F;
  2200. }
  2201. // 计算受击时间 //
  2202. int damageTime = unit.Info.DamageTimeMS;
  2203. if (source.OutKnockDownTimeMS > 0)
  2204. {
  2205. damageTime = source.OutKnockDownTimeMS;
  2206. }
  2207. else if (unit.mInfo.DamageTimeMS > 0)
  2208. {
  2209. damageTime = unit.mInfo.DamageTimeMS;
  2210. }
  2211. else
  2212. {
  2213. damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
  2214. }
  2215. this.damageExpire = new TimeExpire<object>(damageTime);
  2216. }
  2217. override public bool onBlock(State new_state)
  2218. {
  2219. if (isEnd)
  2220. {
  2221. return true;
  2222. }
  2223. if (new_state is StateDead)
  2224. {
  2225. return true;
  2226. }
  2227. if (new_state is IStateNoneControllable)
  2228. {
  2229. this.SetNextNoneControllable(new_state as IStateNoneControllable);
  2230. }
  2231. if (unit.IsDead())
  2232. {
  2233. return false;
  2234. }
  2235. if (new_state is StateDamage)
  2236. {
  2237. return unit.CanWhiplashDeadBody;
  2238. }
  2239. if (new_state is StateRebirth)
  2240. {
  2241. return true;
  2242. }
  2243. return isEnd;
  2244. }
  2245. override protected void onStart()
  2246. {
  2247. unit.SetActionStatus(UnitActionStatus.Dead);
  2248. float zgravity = 0;
  2249. float zlimit = 0;
  2250. if (true)
  2251. {
  2252. int keepTimeMs = 0;
  2253. float speedSEC = 3;//这个数字要随机1-10
  2254. speedSEC = new Random().Next(0, 11);
  2255. this.hitMoveSpeed = unit.StartHitMove(this,
  2256. startDirection,
  2257. 0,
  2258. keepTimeMs,
  2259. speedSEC,
  2260. 0,
  2261. 0,
  2262. false);
  2263. if (true)
  2264. {
  2265. this.hitMoveSpeed.SetFly(this,
  2266. 15,//source.OutHitMove.ZSpeedSEC
  2267. 0,
  2268. 0);
  2269. zgravity = 0;
  2270. zlimit = 0;
  2271. }
  2272. }
  2273. int moveTime = 0;
  2274. float zspeed = 0;
  2275. if (hitMoveSpeed != null)
  2276. {
  2277. moveTime = hitMoveSpeed.TotalTimeMS;
  2278. zspeed = hitMoveSpeed.ZSpeedSEC;
  2279. }
  2280. unit.queueEvent(new UnitDamageEvent(
  2281. unit.ID,
  2282. DamageTimeMS,
  2283. moveTime,
  2284. zspeed,
  2285. zgravity,
  2286. zlimit,
  2287. source.OutHasKnockDown,
  2288. source.Attack));
  2289. unit.SetActionStatus(UnitActionStatus.Dead);
  2290. }
  2291. override protected void onUpdate()
  2292. {
  2293. if (hitMoveSpeed != null)
  2294. {
  2295. if (hitMoveSpeed.IsEnd)
  2296. {
  2297. hitMoveSpeed = null;
  2298. if (source.Attack.FlyFallenDownAttack != null)
  2299. {
  2300. doFlyDownAttack();
  2301. }
  2302. }
  2303. else if (source.Attack.HitMoveBodyAttack != null)
  2304. {
  2305. doBodyAttack();
  2306. }
  2307. }
  2308. else if (damageExpire != null)
  2309. {
  2310. if (damageExpire.Update(zone.UpdateIntervalMS))
  2311. {
  2312. damageExpire = null;
  2313. }
  2314. }
  2315. else
  2316. {
  2317. end();
  2318. }
  2319. }
  2320. override protected void onStop()
  2321. {
  2322. if (unit.IsStun)
  2323. {
  2324. unit.changeState(new StateStun(unit));
  2325. }
  2326. else if (nextState != null)
  2327. {
  2328. unit.changeState(nextState.AsState());
  2329. }
  2330. }
  2331. private void end()
  2332. {
  2333. isEnd = true;
  2334. unit.changeState(new StateDead(unit, attacker));
  2335. }
  2336. /// <summary>
  2337. /// 自己落地摔一下
  2338. /// </summary>
  2339. private void doFlyDownAttack()
  2340. {
  2341. unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
  2342. }
  2343. /// <summary>
  2344. /// 飞行过程中打别人
  2345. /// </summary>
  2346. private void doBodyAttack()
  2347. {
  2348. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  2349. {
  2350. zone.getObjectsRoundLineRange<InstanceUnit>(
  2351. Collider.Object_HitBody_TouchRoundLine,
  2352. hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
  2353. unit.X, unit.Y,
  2354. unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
  2355. list, unit.AoiStatus);
  2356. if (list.Count > 0)
  2357. {
  2358. CUtils.RemoveAll(list, body_hited.Values);
  2359. zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
  2360. if (list.Count > 0)
  2361. {
  2362. for (int i = 0; i < list.Count; i++)
  2363. {
  2364. InstanceUnit o = list[i];
  2365. body_hited.Put(o.ID, o);
  2366. }
  2367. }
  2368. }
  2369. }
  2370. }
  2371. }
  2372. /// <summary>
  2373. /// 死亡状态
  2374. /// </summary>
  2375. public class StateDead : State, IStateNoneControllable
  2376. {
  2377. private TimeExpire<int> dead_time;
  2378. private TimeExpire<int> rebirth_time;
  2379. private InstanceUnit attacker;
  2380. private bool crushed;
  2381. private List<TimeExpire<int>> dead_delayTimes = new List<TimeExpire<int>>();
  2382. public StateDead(InstanceUnit unit, InstanceUnit attacker, bool crush = false)
  2383. : base(unit)
  2384. {
  2385. this.attacker = attacker;
  2386. this.crushed = crush;
  2387. }
  2388. public State AsState()
  2389. {
  2390. return this;
  2391. }
  2392. override public bool onBlock(State new_state)
  2393. {
  2394. if (new_state is StateDamage)
  2395. {
  2396. return unit.CanWhiplashDeadBody;
  2397. }
  2398. if (new_state is StateRebirth)
  2399. {
  2400. return true;
  2401. }
  2402. return !unit.IsDead();
  2403. }
  2404. override protected void onStart()
  2405. {
  2406. //某些状态帧下死亡调用漏掉了
  2407. if (unit.IsNeedProcessDead())
  2408. {
  2409. unit.Parent.cb_unitDeadCallBack(unit, this.attacker);
  2410. }
  2411. unit.SetActionStatus(UnitActionStatus.Dead);
  2412. this.rebirth_time = new TimeExpire<int>(unit.mInfo.RebirthTimeMS);
  2413. this.dead_time = new TimeExpire<int>(unit.mInfo.DeadTimeMS);
  2414. if (unit.Info.DeadLaunchSpell != null)
  2415. {
  2416. // 自爆法术
  2417. zone.unitLaunchSpell(XmdsSkillType.none, unit, unit.Info.DeadLaunchSpell, unit.X, unit.Y);
  2418. }
  2419. // Boss 死亡之后灵魂飘到击杀者身上
  2420. if (unit.mInfo.DeadDelayTimeMS > 0)
  2421. {
  2422. dead_delayTimes.Clear();
  2423. for (var i = 0; i < unit.Info.DeadDelaySpellNumber; i++)
  2424. {
  2425. this.dead_delayTimes.Add(new TimeExpire<int>(
  2426. unit.mInfo.DeadDelayTimeMS + i * unit.Info.DeadDelaySpellIntervalMS));
  2427. }
  2428. }
  2429. }
  2430. override protected void onUpdate()
  2431. {
  2432. if (dead_time.Update(zone.UpdateIntervalMS))
  2433. {
  2434. if (unit.mInfo.RebirthTimeMS <= 0)
  2435. {
  2436. zone.RemoveObject(unit);
  2437. }
  2438. //跨服场景中,单位死亡,需要移除单位,然后才能复活
  2439. if (unit.IsDead() && unit.IsPlayer && unit.GetSceneType() >= CommonAI.Data.SceneType.CROSS_SERVER_NEW)
  2440. {
  2441. // zone.RemoveObject(unit);
  2442. unit.DeadHide = true;
  2443. }
  2444. }
  2445. if (rebirth_time.Update(zone.UpdateIntervalMS))
  2446. {
  2447. if (unit.mInfo.RebirthTimeMS > 0)
  2448. {
  2449. unit.startRebirth();
  2450. }
  2451. }
  2452. for (var i = 0; i < this.dead_delayTimes.Count; i++)
  2453. {
  2454. if (dead_delayTimes[i] != null && dead_delayTimes[i].Update(zone.UpdateIntervalMS))
  2455. {
  2456. zone.unitLaunchSpell(XmdsSkillType.none, unit, unit.Info.DeadDelayLaunchSpell,
  2457. unit.X, unit.Y, attacker == null ? 0 : ((InstanceUnit)attacker).mSyncInfo.ObjectID);
  2458. dead_delayTimes[i] = null;
  2459. }
  2460. }
  2461. }
  2462. override protected void onStop()
  2463. {
  2464. //unit.SetActionStatus(UnitActionStatus.Idle);
  2465. }
  2466. }
  2467. /// <summary>
  2468. /// 复活
  2469. /// </summary>
  2470. public class StateRebirth : State, IStateNoneControllable
  2471. {
  2472. private bool is_done = false;
  2473. private int max_hp = 0;
  2474. private int max_mp = 0;
  2475. public StateRebirth(InstanceUnit unit, int max_hp, int max_mp)
  2476. : base(unit)
  2477. {
  2478. this.max_hp = max_hp;
  2479. this.max_mp = max_mp;
  2480. }
  2481. public State AsState()
  2482. {
  2483. return this;
  2484. }
  2485. override public bool onBlock(State new_state)
  2486. {
  2487. return is_done;
  2488. }
  2489. override protected void onStart()
  2490. {
  2491. unit.SetActionStatus(UnitActionStatus.Rebirth);
  2492. }
  2493. override protected void onUpdate()
  2494. {
  2495. this.is_done = true;
  2496. unit.doRebirth(max_hp, max_mp);
  2497. unit.doSomething();
  2498. }
  2499. override protected void onStop()
  2500. {
  2501. }
  2502. }
  2503. //---------------------------------------------------------------------------------------
  2504. /// <summary>
  2505. /// 眩晕状态
  2506. /// </summary>
  2507. public class StateStun : State, IStateNoneControllable
  2508. {
  2509. public StateStun(InstanceUnit unit)
  2510. : base(unit)
  2511. {
  2512. }
  2513. public State AsState()
  2514. {
  2515. return this;
  2516. }
  2517. override public bool onBlock(State new_state)
  2518. {
  2519. if (new_state is StateDamage)
  2520. {
  2521. return true;
  2522. }
  2523. if (unit.IsDead() && new_state is StateDead)
  2524. {
  2525. return true;
  2526. }
  2527. if (new_state is StateSkill)
  2528. {
  2529. StateSkill ss = new_state as StateSkill;
  2530. // 反击或者状态解除//
  2531. if (ss.SkillData.IsCounter)
  2532. {
  2533. return true;
  2534. }
  2535. }
  2536. return !unit.IsStun;
  2537. }
  2538. override protected void onStart()
  2539. {
  2540. unit.SetActionStatus(UnitActionStatus.Stun);
  2541. }
  2542. override protected void onUpdate()
  2543. {
  2544. if (!unit.IsStun)
  2545. {
  2546. unit.doSomething();
  2547. }
  2548. }
  2549. override protected void onStop()
  2550. {
  2551. //unit.SetActionStatus(UnitActionStatus.Idle);
  2552. }
  2553. }
  2554. /// <summary>
  2555. /// 不可移动状态
  2556. /// </summary>
  2557. public class StateChuanGongA : State, IStateNoneControllable
  2558. {
  2559. private bool markRemove;
  2560. public StateChuanGongA(InstanceUnit unit)
  2561. : base(unit)
  2562. {
  2563. }
  2564. public State AsState()
  2565. {
  2566. return this;
  2567. }
  2568. override public bool onBlock(State new_state)
  2569. {
  2570. if (this.markRemove)
  2571. {
  2572. return true;
  2573. }
  2574. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2575. }
  2576. public override void MarkRemove()
  2577. {
  2578. this.markRemove = true;
  2579. }
  2580. override protected void onStart()
  2581. {
  2582. this.markRemove = false;
  2583. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 60000;
  2584. unit.SetActionStatus(UnitActionStatus.ChuanGongA);
  2585. }
  2586. override protected void onUpdate()
  2587. {
  2588. if (this.markRemove)
  2589. {
  2590. unit.startIdle();
  2591. }
  2592. else if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2593. {
  2594. unit.doSomething();
  2595. }
  2596. }
  2597. override protected void onStop()
  2598. {
  2599. unit.SetActionStatus(UnitActionStatus.Idle);
  2600. }
  2601. }
  2602. /// <summary>
  2603. /// 不可移动状态
  2604. /// </summary>
  2605. public class StateChuanGongB : State, IStateNoneControllable
  2606. {
  2607. private bool markRemove;
  2608. public StateChuanGongB(InstanceUnit unit)
  2609. : base(unit)
  2610. {
  2611. }
  2612. public State AsState()
  2613. {
  2614. return this;
  2615. }
  2616. override public bool onBlock(State new_state)
  2617. {
  2618. if (this.markRemove)
  2619. {
  2620. return true;
  2621. }
  2622. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2623. }
  2624. public override void MarkRemove()
  2625. {
  2626. this.markRemove = true;
  2627. }
  2628. override protected void onStart()
  2629. {
  2630. this.markRemove = false;
  2631. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 60000;
  2632. unit.SetActionStatus(UnitActionStatus.ChuanGongB);
  2633. }
  2634. override protected void onUpdate()
  2635. {
  2636. if (this.markRemove)
  2637. {
  2638. unit.startIdle();
  2639. }
  2640. else if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2641. {
  2642. unit.doSomething();
  2643. }
  2644. }
  2645. override protected void onStop()
  2646. {
  2647. unit.SetActionStatus(UnitActionStatus.Idle);
  2648. }
  2649. }
  2650. /// <summary>
  2651. /// 不可移动状态 净妖,和传功一样锁定4秒,时间变量用传功的
  2652. /// </summary>
  2653. public class StateClearYaoQi : State, IStateNoneControllable
  2654. {
  2655. public StateClearYaoQi(InstanceUnit unit)
  2656. : base(unit)
  2657. {
  2658. }
  2659. public State AsState()
  2660. {
  2661. return this;
  2662. }
  2663. override public bool onBlock(State new_state)
  2664. {
  2665. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2666. }
  2667. override protected void onStart()
  2668. {
  2669. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 4000;
  2670. unit.SetActionStatus(UnitActionStatus.ClearYaoQi);
  2671. }
  2672. override protected void onUpdate()
  2673. {
  2674. if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2675. {
  2676. unit.doSomething();
  2677. }
  2678. }
  2679. override protected void onStop()
  2680. {
  2681. unit.SetActionStatus(UnitActionStatus.Idle);
  2682. }
  2683. }
  2684. /// <summary>
  2685. /// 打坐
  2686. /// </summary>
  2687. public class StateDaZuo : State
  2688. {
  2689. private float startX;
  2690. private float startY;
  2691. private long lockEndTime;
  2692. private bool markRemove;
  2693. public StateDaZuo(InstanceUnit unit, int time)
  2694. : base(unit)
  2695. {
  2696. int lockTime = Math.Max(1, time);
  2697. //锁定短暂时间,避免一打坐就被客户端取消了。
  2698. lockEndTime = 0;// CommonLang.CUtils.localTimeMS + lockTime * 1000;
  2699. }
  2700. public override void MarkRemove()
  2701. {
  2702. this.markRemove = true;
  2703. }
  2704. override public bool onBlock(State new_state)
  2705. {
  2706. if (this.markRemove)
  2707. {
  2708. return true;
  2709. }
  2710. if (CommonLang.CUtils.localTimeMS < lockEndTime)
  2711. {
  2712. return false;
  2713. }
  2714. if (new_state is StateIdle)
  2715. {
  2716. return false;
  2717. }
  2718. return true;
  2719. }
  2720. override protected void onStart()
  2721. {
  2722. unit.SetActionStatus(UnitActionStatus.DaZuo);
  2723. }
  2724. override protected void onUpdate()
  2725. {
  2726. if(this.markRemove)
  2727. {
  2728. unit.startIdle();
  2729. }
  2730. else if(lockEndTime > CommonLang.CUtils.localTimeMS)
  2731. {
  2732. startX = unit.X;
  2733. startY = unit.Y;
  2734. }
  2735. else if (unit.X != startX || unit.Y != startY)
  2736. {
  2737. unit.doSomething();
  2738. }
  2739. }
  2740. override protected void onStop()
  2741. {
  2742. unit.SetActionStatus(UnitActionStatus.Idle);
  2743. }
  2744. }
  2745. /// <summary>
  2746. /// 打坐恢复血量
  2747. /// </summary>
  2748. public class StateDaZuoRecoveryAttr : State
  2749. {
  2750. private long endTime;//回血结束时间
  2751. private bool markRemove;
  2752. private int sustainTime; //持续时间
  2753. private long nextRecoveryTime; //下一次恢复时间
  2754. public StateDaZuoRecoveryAttr(InstanceUnit unit, int time)
  2755. : base(unit)
  2756. {
  2757. endTime = CommonLang.CUtils.CurrentTimeMS + time*1000;
  2758. sustainTime = time-3; //减去3是因为等待坐下去后才开始回血
  2759. nextRecoveryTime = CommonLang.CUtils.CurrentTimeMS+2350; //2350是客户端坐下去整个动作的时间,等待动作播放完成,开始回血
  2760. }
  2761. public override void MarkRemove()
  2762. {
  2763. this.markRemove = true;
  2764. }
  2765. override public bool onBlock(State new_state)
  2766. {
  2767. return true;
  2768. }
  2769. override protected void onStart()
  2770. {
  2771. unit.SetActionStatus(UnitActionStatus.DaZuoRecoveryAttr);
  2772. }
  2773. override protected void onUpdate()
  2774. {
  2775. if(this.markRemove)
  2776. {
  2777. unit.startIdle();
  2778. }
  2779. else
  2780. {
  2781. if (unit.Virtual.GetBattleStatus() != BattleStatus.None)
  2782. {
  2783. unit.startIdle();
  2784. return;
  2785. }
  2786. if (CommonLang.CUtils.CurrentTimeMS > endTime)
  2787. {
  2788. unit.startIdle();
  2789. return;
  2790. }
  2791. if (nextRecoveryTime < CommonLang.CUtils.CurrentTimeMS)
  2792. {
  2793. int Recover = 0;
  2794. //恢复主角血量
  2795. // if (unit.CurrentHP < unit.MaxHP)
  2796. //{
  2797. Recover = (int)(unit.MaxHP / sustainTime);
  2798. /*if (unit.CurrentHP + Recover > unit.MaxHP)
  2799. {
  2800. Recover = unit.MaxHP - unit.CurrentHP;
  2801. }*/
  2802. unit.add_hp(Recover);
  2803. // }
  2804. //恢复宠物血量
  2805. InstanceUnit petUnit = unit.Virtual.GetPetUnit();
  2806. if (petUnit != null)
  2807. {
  2808. if (petUnit.CurrentHP < petUnit.MaxHP && petUnit.Virtual.GetBattleStatus() == BattleStatus.None)
  2809. {
  2810. Recover = (int)(petUnit.MaxHP / sustainTime);
  2811. if (petUnit.CurrentHP + Recover > petUnit.MaxHP)
  2812. {
  2813. Recover = petUnit.MaxHP - petUnit.CurrentHP;
  2814. }
  2815. petUnit.add_hp(Recover);
  2816. }
  2817. }
  2818. nextRecoveryTime = CommonLang.CUtils.CurrentTimeMS + 1 * 1000;
  2819. }
  2820. }
  2821. }
  2822. override protected void onStop()
  2823. {
  2824. unit.SetActionStatus(UnitActionStatus.Idle);
  2825. }
  2826. }
  2827. /// <summary>
  2828. /// 随从向玩家瞬移
  2829. /// </summary>
  2830. public class PetLockState : State
  2831. {
  2832. private long endTime;//瞬移后定住时间
  2833. private bool markRemove;
  2834. public PetLockState(InstanceUnit unit)
  2835. : base(unit)
  2836. {
  2837. endTime = CommonLang.CUtils.CurrentTimeMS + 1*1000; //持续时间1秒
  2838. }
  2839. public override void MarkRemove()
  2840. {
  2841. this.markRemove = true;
  2842. }
  2843. override public bool onBlock(State new_state)
  2844. {
  2845. return true;
  2846. }
  2847. override protected void onStart()
  2848. {
  2849. unit.SetActionStatus(UnitActionStatus.PetLock);
  2850. }
  2851. override protected void onUpdate()
  2852. {
  2853. if(this.markRemove)
  2854. {
  2855. unit.startIdle();
  2856. }
  2857. else
  2858. {
  2859. if (CommonLang.CUtils.CurrentTimeMS > endTime)
  2860. {
  2861. unit.startIdle();
  2862. return;
  2863. }
  2864. }
  2865. }
  2866. override protected void onStop()
  2867. {
  2868. unit.SetActionStatus(UnitActionStatus.Idle);
  2869. }
  2870. }
  2871. /// <summary>
  2872. /// 渡劫场景,不能移动
  2873. /// </summary>
  2874. public class StateDuJie : State, IStateNoneControllable
  2875. {
  2876. public StateDuJie(InstanceUnit unit)
  2877. : base(unit)
  2878. {
  2879. }
  2880. public State AsState()
  2881. {
  2882. return this;
  2883. }
  2884. override public bool onBlock(State new_state)
  2885. {
  2886. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2887. }
  2888. override protected void onStart()
  2889. {
  2890. // 管他多长时间,锁一个小时再说
  2891. unit.chuangongTime = CommonLang.CUtils.localTimeMS + 3600000;
  2892. unit.SetActionStatus(UnitActionStatus.CrossRobbery);
  2893. }
  2894. override protected void onUpdate()
  2895. {
  2896. //if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
  2897. //{
  2898. // unit.doSomething();
  2899. //}
  2900. }
  2901. override protected void onStop()
  2902. {
  2903. unit.SetActionStatus(UnitActionStatus.Idle);
  2904. }
  2905. }
  2906. /// <summary>
  2907. /// 渡劫场景,不能移动
  2908. /// </summary>
  2909. public class StateTransport : State, IStateNoneControllable
  2910. {
  2911. private long mLockEndTime = 0;
  2912. public StateTransport(InstanceUnit unit)
  2913. : base(unit)
  2914. {
  2915. }
  2916. public State AsState()
  2917. {
  2918. return this;
  2919. }
  2920. override public bool onBlock(State new_state)
  2921. {
  2922. if(new_state is StateSkill)
  2923. {
  2924. return true;
  2925. }
  2926. return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
  2927. }
  2928. override protected void onStart()
  2929. {
  2930. // 管他多长时间,锁定300ms
  2931. this.mLockEndTime = CommonLang.CUtils.localTimeMS + 250;
  2932. unit.SetActionStatus(UnitActionStatus.Transport);
  2933. //System.Console.WriteLine("开始传送状态:" + CommonLang.CUtils.localTimeMS);
  2934. }
  2935. override protected void onUpdate()
  2936. {
  2937. if (this.mLockEndTime <= CommonLang.CUtils.localTimeMS)
  2938. {
  2939. unit.doSomething();
  2940. }
  2941. }
  2942. override protected void onStop()
  2943. {
  2944. unit.SetActionStatus(UnitActionStatus.Idle);
  2945. //System.Console.WriteLine("end传送状态:" + CommonLang.CUtils.localTimeMS);
  2946. }
  2947. }
  2948. //---------------------------------------------------------------------------------------
  2949. /// <summary>
  2950. /// 状态限制移动
  2951. /// </summary>
  2952. abstract public class StateMoveNoneControllable : State, IStateNoneControllable
  2953. {
  2954. private TimeExpire<int> mExpire;
  2955. private MoveAI mMoveAI;
  2956. private MoveBlockResult mLastResult;
  2957. public MoveBlockResult LastMoveResult { get { return mLastResult; } }
  2958. public State AsState()
  2959. {
  2960. return this;
  2961. }
  2962. public StateMoveNoneControllable(InstanceUnit unit, int timeMS)
  2963. : base(unit)
  2964. {
  2965. this.mExpire = new TimeExpire<int>(timeMS);
  2966. this.mMoveAI = new MoveAI(unit, false);
  2967. }
  2968. /// <summary>
  2969. /// 搜索要去的地方
  2970. /// </summary>
  2971. /// <returns></returns>
  2972. protected abstract Vector2 FindTargetPos();
  2973. public override bool onBlock(State new_state)
  2974. {
  2975. if (new_state is StateDead)
  2976. {
  2977. return true;
  2978. }
  2979. if (new_state is StateDamage)
  2980. {
  2981. (new_state as StateDamage).SetNextNoneControllable(this);
  2982. return true;
  2983. }
  2984. if (new_state is StateSkill)
  2985. {
  2986. StateSkill ss = new_state as StateSkill;
  2987. // 反击或者状态解除//
  2988. if (ss.SkillData.IsCounter)
  2989. {
  2990. return true;
  2991. }
  2992. }
  2993. return mExpire.IsEnd;
  2994. }
  2995. protected override void onStart()
  2996. {
  2997. Vector2 target = this.FindTargetPos();
  2998. this.mMoveAI.FindPath(target.X, target.Y);
  2999. }
  3000. protected override void onUpdate()
  3001. {
  3002. if (mExpire.Update(zone.UpdateIntervalMS))
  3003. {
  3004. unit.doSomething();
  3005. }
  3006. else
  3007. {
  3008. mLastResult = mMoveAI.Update();
  3009. if ((mLastResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  3010. {
  3011. Vector2 target = this.FindTargetPos();
  3012. this.mMoveAI.FindPath(target.X, target.Y);
  3013. }
  3014. }
  3015. }
  3016. protected override void onStop()
  3017. {
  3018. }
  3019. }
  3020. /// <summary>
  3021. /// 拾取状态
  3022. /// </summary>
  3023. public class StateAutoPick : State
  3024. {
  3025. private InstanceItem mPickItem; //要自动拾取的物品
  3026. private MoveAI mMoveAi;
  3027. bool mIsEnd;
  3028. public StateAutoPick(InstanceUnit unit, InstanceItem item)
  3029. : base(unit)
  3030. {
  3031. this.mPickItem = item;
  3032. this.mMoveAi = new MoveAI(unit);
  3033. this.mMoveAi.FindPath(item.X, item.Y);
  3034. }
  3035. override public bool onBlock(State new_state)
  3036. {
  3037. if (unit.IsDead())
  3038. {
  3039. return true;
  3040. }
  3041. return true;
  3042. }
  3043. override protected void onStart()
  3044. {
  3045. //unit.SetActionStatus(UnitActionStatus.AutoPick);
  3046. }
  3047. override protected void onUpdate()
  3048. {
  3049. if (mIsEnd || this.mPickItem.IsDead() || !this.mPickItem.Enable || this.unit.Virtual.GetBattleStatus() != BattleStatus.None)
  3050. {
  3051. unit.doSomething();
  3052. }
  3053. else
  3054. {
  3055. MoveBlockResult result = mMoveAi.Update();
  3056. if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
  3057. {
  3058. mIsEnd = true;
  3059. unit.doSomething();
  3060. }
  3061. else
  3062. {
  3063. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  3064. if (CMath.includeRoundPoint(unit.X, unit.Y, r, mPickItem.X, mPickItem.Y))
  3065. {
  3066. mIsEnd = true;
  3067. unit.doSomething();
  3068. }
  3069. }
  3070. }
  3071. }
  3072. override protected void onStop()
  3073. {
  3074. unit.SetActionStatus(UnitActionStatus.Idle);
  3075. }
  3076. }
  3077. //---------------------------------------------------------------------------------------
  3078. /// <summary>
  3079. /// 逃跑状态
  3080. /// </summary>
  3081. public class StateEscape : StateMoveNoneControllable
  3082. {
  3083. private float mDistance;
  3084. public StateEscape(InstanceUnit unit, int timeMS, float distance = 0) : base(unit, timeMS)
  3085. {
  3086. if (distance > 0)
  3087. {
  3088. this.mDistance = distance;
  3089. }
  3090. else
  3091. {
  3092. this.mDistance = unit.Info.GuardRange;
  3093. }
  3094. }
  3095. protected override void onStart()
  3096. {
  3097. base.onStart();
  3098. unit.SetActionStatus(UnitActionStatus.Escape);
  3099. }
  3100. protected override Vector2 FindTargetPos()
  3101. {
  3102. var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, unit.X, unit.Y, mDistance);
  3103. return new Vector2(node.BX, node.BY);
  3104. }
  3105. }
  3106. //---------------------------------------------------------------------------------------
  3107. /// <summary>
  3108. /// 混乱状态
  3109. /// </summary>
  3110. public class StateChaos : StateMoveNoneControllable
  3111. {
  3112. public StateChaos(InstanceUnit unit, int timeMS) : base(unit, timeMS)
  3113. {
  3114. }
  3115. protected override void onStart()
  3116. {
  3117. base.onStart();
  3118. unit.SetActionStatus(UnitActionStatus.Chaos);
  3119. }
  3120. protected override void onStop()
  3121. {
  3122. base.onStop();
  3123. }
  3124. protected override Vector2 FindTargetPos()
  3125. {
  3126. var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, unit.X, unit.Y, unit.Info.GuardRange);
  3127. return new Vector2(node.BX, node.BY);
  3128. }
  3129. }
  3130. //---------------------------------------------------------------------------------------
  3131. /// <summary>
  3132. /// 破定
  3133. /// </summary>
  3134. public class StateBreakShield : State
  3135. {
  3136. private long mStateEndTime;
  3137. public StateBreakShield(InstanceUnit unit) : base(unit)
  3138. {
  3139. this.mStateEndTime = CommonLang.CUtils.localTimeMS + GlobalData.SHIELD_BREAK_HOLDTIME;
  3140. }
  3141. protected override void onStart()
  3142. {
  3143. unit.SetActionStatus(UnitActionStatus.BreakShield);
  3144. unit.AddBuff(GlobalData.SHIELD_BREAK_BUFF, 0, true);
  3145. }
  3146. protected override void onUpdate()
  3147. {
  3148. if(CommonLang.CUtils.localTimeMS > this.mStateEndTime)
  3149. {
  3150. unit.startIdle();
  3151. }
  3152. }
  3153. public override bool onBlock(State new_state)
  3154. {
  3155. if (CommonLang.CUtils.localTimeMS > this.mStateEndTime)
  3156. {
  3157. return true;
  3158. }
  3159. else if (new_state is StateDead)
  3160. {
  3161. return true;
  3162. }
  3163. return false;
  3164. }
  3165. protected override void onStop()
  3166. {
  3167. unit.SetActionStatus(UnitActionStatus.Idle);
  3168. //unit.removeBuff(GlobalData.SHIELD_BREAK_BUFF);
  3169. unit.Virtual.StartRecoverMP();
  3170. }
  3171. }
  3172. //---------------------------------------------------------------------------------------
  3173. #endregion
  3174. #region __AI行为__
  3175. //---------------------------------------------------------------------------------------
  3176. /// <summary>
  3177. /// 移动状态
  3178. /// </summary>
  3179. abstract public class StateFollow : State
  3180. {
  3181. public enum MoveState
  3182. {
  3183. Hold,
  3184. Move,
  3185. }
  3186. // 被追目标
  3187. protected IPositionObject target;
  3188. // 如果被单位碰撞,则向左或右挪动//
  3189. readonly private MoveAI moveAI;
  3190. // 检测切换走停的间隔时间 //
  3191. readonly private State<MoveState> state = new State<MoveState>(MoveState.Move);
  3192. readonly private bool begin_in_min_range;
  3193. // 检测切换走停的间隔时间 //
  3194. private TimeInterval<int> start_move_hold_time;
  3195. public abstract bool IsActive { get; }
  3196. public MoveAI MoveAI
  3197. {
  3198. get
  3199. {
  3200. return moveAI;
  3201. }
  3202. private set { }
  3203. }
  3204. public bool IsNoWay { get { if (moveAI != null) { return moveAI.IsNoWay; } return false; } }
  3205. public IPositionObject Target { get { return target; } }
  3206. public MoveState FollowState { get { return state.Value; } }
  3207. /// <summary>
  3208. /// Hold到Move之间的检测间隔
  3209. /// </summary>
  3210. public int StartMoveHoldTimeMS
  3211. {
  3212. get { return (start_move_hold_time != null) ? start_move_hold_time.IntervalTimeMS : 0; }
  3213. set
  3214. {
  3215. if (value > 0 && value != StartMoveHoldTimeMS)
  3216. {
  3217. start_move_hold_time = new TimeInterval<int>(value);
  3218. }
  3219. }
  3220. }
  3221. public StateFollow(InstanceUnit unit, InstanceZoneObject target, bool beginInMinRange = true, int holdTime = 0) : base(unit)
  3222. {
  3223. this.begin_in_min_range = beginInMinRange;
  3224. this.target = target;
  3225. this.moveAI = new MoveAI(unit, true, holdTime);
  3226. }
  3227. public override bool onBlock(State new_state)
  3228. {
  3229. return true;
  3230. }
  3231. protected override void onStart()
  3232. {
  3233. if (this.unit.Info.SumType == SummonType.AwaitMaster || cheekBeginInRange())
  3234. {
  3235. state.ChangeState(MoveState.Hold);
  3236. unit.SetActionStatus(UnitActionStatus.Idle);
  3237. }
  3238. else
  3239. {
  3240. state.ChangeState(MoveState.Move);
  3241. unit.SetActionStatus(UnitActionStatus.Move);
  3242. moveAI.FindPath(target);
  3243. }
  3244. }
  3245. protected virtual void OnMoveAiNoWay() { }
  3246. protected override void onUpdate()
  3247. {
  3248. if (!IsActive)
  3249. {
  3250. unit.doSomething();
  3251. }
  3252. else if ((unit.CurrentActionSubstate & (int)UnitActionSubStatus.CanNotMove) > 0)
  3253. {
  3254. //不能移动,啥事也不干
  3255. }
  3256. else
  3257. {
  3258. switch (FollowState)
  3259. {
  3260. case MoveState.Move:
  3261. // 进入最小追踪距离 //
  3262. if (CheckTargetInMinRange() == true)
  3263. {
  3264. changeToHold();
  3265. }
  3266. else
  3267. {
  3268. if (moveAI.Target == null)
  3269. {
  3270. moveAI.FindPath(target);
  3271. }
  3272. moveAI.Update();
  3273. if (moveAI.IsNoWay)
  3274. {
  3275. this.OnMoveAiNoWay();
  3276. if (CheckTargetInMaxRange() == true)
  3277. {
  3278. changeToHold();
  3279. }
  3280. else
  3281. {
  3282. unit.doSomething();
  3283. }
  3284. }
  3285. }
  3286. break;
  3287. case MoveState.Hold:
  3288. // 超过最大追踪范围 //
  3289. if ((start_move_hold_time == null || start_move_hold_time.Update(zone.UpdateIntervalMS)) && (CheckTargetInMaxRange() == false))
  3290. {
  3291. changeToMove();
  3292. }
  3293. else
  3294. {
  3295. if (unit.CurrentActionStatus != UnitActionStatus.Idle)
  3296. {
  3297. unit.SetActionStatus(UnitActionStatus.Idle);
  3298. }
  3299. }
  3300. onUpdateFollowed(target);
  3301. break;
  3302. }
  3303. }
  3304. }
  3305. protected override void onStop() { }
  3306. private bool cheekBeginInRange()
  3307. {
  3308. if (begin_in_min_range)
  3309. {
  3310. return CheckTargetInMinRange();
  3311. }
  3312. else
  3313. {
  3314. return CheckTargetInMaxRange();
  3315. }
  3316. }
  3317. private void changeToMove()
  3318. {
  3319. state.ChangeState(MoveState.Move);
  3320. unit.SetActionStatus(UnitActionStatus.Move);
  3321. moveAI.FindPath(target);
  3322. onInRangeChanged();
  3323. onChangedToMove(target);
  3324. }
  3325. private void changeToHold()
  3326. {
  3327. state.ChangeState(MoveState.Hold);
  3328. unit.SetActionStatus(UnitActionStatus.Idle);
  3329. moveAI.Pause();
  3330. onInRangeChanged();
  3331. onChangedToHold(target);
  3332. }
  3333. /// <summary>
  3334. /// 检查目标是否在跟随范围内。
  3335. /// 检测为True,停止移动。
  3336. /// </summary>
  3337. protected abstract bool CheckTargetInMinRange();
  3338. /// <summary>
  3339. /// 检查目标是否在跟随范围内。
  3340. /// 检测为False,开始移动。
  3341. /// </summary>
  3342. protected abstract bool CheckTargetInMaxRange();
  3343. /// <summary>
  3344. /// 行为改变
  3345. /// </summary>
  3346. protected virtual void onInRangeChanged()
  3347. {
  3348. }
  3349. /// <summary>
  3350. /// 目标已经被追踪到
  3351. /// </summary>
  3352. /// <param name="target"></param>
  3353. protected virtual void onUpdateFollowed(IPositionObject target)
  3354. {
  3355. }
  3356. /// <summary>
  3357. /// 开始移动
  3358. /// </summary>
  3359. /// <param name="target"></param>
  3360. protected virtual void onChangedToMove(IPositionObject target)
  3361. {
  3362. }
  3363. /// <summary>
  3364. /// changeToHold
  3365. /// </summary>
  3366. /// <param name="target"></param>
  3367. protected virtual void onChangedToHold(IPositionObject target)
  3368. {
  3369. }
  3370. }
  3371. /// <summary>
  3372. ///
  3373. /// </summary>
  3374. public class StateFollowObject : StateFollow
  3375. {
  3376. protected float distance;
  3377. //protected InstanceZoneObject target;
  3378. public override bool IsActive
  3379. {
  3380. get
  3381. {
  3382. var t = target as InstanceZoneObject;
  3383. if (t != null)
  3384. {
  3385. return t.Enable;
  3386. }
  3387. return false;
  3388. }
  3389. }
  3390. public float Distance
  3391. {
  3392. set
  3393. {
  3394. this.distance = value;
  3395. }
  3396. get
  3397. {
  3398. return this.distance;
  3399. }
  3400. }
  3401. public StateFollowObject(InstanceUnit unit, InstanceZoneObject target, int holdTime = 0)
  3402. : base(unit, target, true, holdTime)
  3403. {
  3404. //this.target = target;
  3405. this.distance = Target.RadiusSize + unit.BodyBlockSize * 2;
  3406. }
  3407. protected override bool CheckTargetInMaxRange()
  3408. {
  3409. return CMath.includeRoundPoint(
  3410. unit.X, unit.Y,
  3411. distance * 2,
  3412. Target.X, Target.Y);
  3413. }
  3414. protected override bool CheckTargetInMinRange()
  3415. {
  3416. return CMath.includeRoundPoint(
  3417. unit.X, unit.Y,
  3418. distance,
  3419. Target.X, Target.Y);
  3420. }
  3421. }
  3422. public class VStateFollowLeader : StateFollowObject
  3423. {
  3424. public VStateFollowLeader(InstanceUnit unit, InstanceZoneObject target, int holdTime = 0) : base(unit, target, holdTime)
  3425. {
  3426. }
  3427. }
  3428. /// <summary>
  3429. ///
  3430. /// </summary>
  3431. public class StateFollowAndGuard : StateFollow
  3432. {
  3433. private readonly float distance_min;
  3434. private readonly float distance_max;
  3435. private readonly InstanceUnit targetUnit;
  3436. public InstanceUnit TargetUnit { get { return targetUnit; } }
  3437. public override bool IsActive { get { return targetUnit.Enable; } }
  3438. public StateFollowAndGuard(InstanceUnit unit, InstanceUnit target, float minDistance, float maxDistance)
  3439. : base(unit, target)
  3440. {
  3441. this.targetUnit = target;
  3442. this.distance_min = Math.Max(minDistance, target.BodyBlockSize + unit.BodyBlockSize * 2);
  3443. this.distance_max = Math.Max(maxDistance, distance_min);
  3444. }
  3445. protected override bool CheckTargetInMinRange()
  3446. {
  3447. return CMath.includeRoundPoint(
  3448. unit.X, unit.Y,
  3449. distance_min,
  3450. Target.X, Target.Y);
  3451. }
  3452. protected override bool CheckTargetInMaxRange()
  3453. {
  3454. return CMath.includeRoundPoint(
  3455. unit.X, unit.Y,
  3456. distance_max,
  3457. Target.X, Target.Y);
  3458. }
  3459. }
  3460. /// <summary>
  3461. ///
  3462. /// </summary>
  3463. public class StateFollowAndAttack : StateFollow
  3464. {
  3465. /*readonly private*/
  3466. InstanceUnit targetUnit;
  3467. private SkillTemplate.CastTarget expectTarget;
  3468. private TimeInterval<int> checkAdjustLaunchSkillTime;
  3469. private SkillState launchSkill;
  3470. private bool can_auto_focus_near_target;
  3471. private bool can_random_skill = true;
  3472. private bool can_attack = true;
  3473. private float min_distance;
  3474. private float max_distance;
  3475. /// <summary>
  3476. /// 是否追踪
  3477. /// </summary>
  3478. /// <returns></returns>
  3479. override public bool IsActive
  3480. {
  3481. get { return can_attack && targetUnit.IsActive; }
  3482. }
  3483. public SkillState ExpectSkillState
  3484. {
  3485. get { return launchSkill; }
  3486. set
  3487. {
  3488. launchSkill = value;
  3489. expectTarget = value.Data.ExpectTarget;
  3490. }
  3491. }
  3492. public SkillTemplate ExpectSkill
  3493. {
  3494. get { return (launchSkill != null) ? launchSkill.Data : unit.DefaultSkill; }
  3495. }
  3496. public InstanceUnit TargetUnit
  3497. {
  3498. get { return targetUnit; }
  3499. set { targetUnit = value; }
  3500. }
  3501. public SkillTemplate.CastTarget ExpectTarget
  3502. {
  3503. get { return expectTarget; }
  3504. }
  3505. public bool IsLaunchRandomSkill
  3506. {
  3507. get { return can_random_skill; }
  3508. set { can_random_skill = value; }
  3509. }
  3510. public bool IsAutoFocusNearTarget
  3511. {
  3512. get { return can_auto_focus_near_target; }
  3513. set { can_auto_focus_near_target = value; }
  3514. }
  3515. public StateFollowAndAttack(
  3516. InstanceUnit unit,
  3517. InstanceUnit target,
  3518. SkillTemplate.CastTarget expectTarget = SkillTemplate.CastTarget.Enemy,
  3519. bool autoFocusNearTarget = false)
  3520. : base(unit, target, false)
  3521. {
  3522. this.targetUnit = target;
  3523. // Console.WriteLine(" - - - - StateFollowAndAttack - - - - " + this.TargetUnit.Name);
  3524. this.expectTarget = expectTarget;
  3525. this.can_auto_focus_near_target = autoFocusNearTarget;
  3526. this.StartMoveHoldTimeMS = zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS;
  3527. }
  3528. protected override void onStart()
  3529. {
  3530. this.checkAdjustLaunchSkillTime = new TimeInterval<int>(zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS);
  3531. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  3532. base.onStart();
  3533. }
  3534. protected override void onUpdateFollowed(IPositionObject target)
  3535. {
  3536. this.can_attack = zone.IsAttackable(unit, targetUnit, expectTarget, AttackReason.Tracing, this.ExpectSkill);
  3537. if (can_attack)
  3538. {
  3539. unit.faceTo(target.X, target.Y);
  3540. if (TryLaunchSkill() == null)
  3541. {
  3542. SkillTemplate expect_skill = ExpectSkill;
  3543. if (expect_skill != null && expect_skill.AttackKeepRange > 0)
  3544. {
  3545. if (checkAdjustLaunchSkillTime.IntervalTimeMS == 0 || checkAdjustLaunchSkillTime.Update(zone.UpdateIntervalMS))
  3546. {
  3547. if (CUtils.RandomPercent(zone.RandomN, zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_ADJUST_ESCAPE_PCT))
  3548. {
  3549. unit.startAdjustLaunchSkill(expect_skill, targetUnit, this.OnEndAdjustKeepRange);
  3550. }
  3551. }
  3552. }
  3553. }
  3554. }
  3555. }
  3556. protected override void onUpdate()
  3557. {
  3558. if (IsActive)
  3559. {
  3560. resetMaxMinRange();
  3561. }
  3562. base.onUpdate();
  3563. }
  3564. private void resetMaxMinRange()
  3565. {
  3566. var expect_skill = ExpectSkillState;
  3567. min_distance = unit.BodyBlockSize + targetUnit.BodyBlockSize;
  3568. max_distance = Math.Max(min_distance, unit.BodyBlockSize + targetUnit.BodyHitSize);
  3569. bool needCheck = true;
  3570. if (expect_skill == null)
  3571. {
  3572. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  3573. if (new_skill != null)
  3574. {
  3575. ExpectSkillState = new_skill;
  3576. expect_skill = new_skill;
  3577. needCheck = false;
  3578. }
  3579. }
  3580. if (expect_skill != null)
  3581. {
  3582. if (can_random_skill)
  3583. {
  3584. if (needCheck == true && !expect_skill.checkTargetRange(targetUnit))
  3585. {
  3586. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  3587. if (new_skill != null)
  3588. {
  3589. ExpectSkillState = new_skill;
  3590. expect_skill = new_skill;
  3591. }
  3592. }
  3593. }
  3594. float skill_distance = unit.GetSkillAttackRange(expect_skill.Data);
  3595. float half = (skill_distance - min_distance) / 2;
  3596. if (expect_skill.Data.AttackKeepRange > 0 && expect_skill.Data.AttackRange > expect_skill.Data.AttackKeepRange)
  3597. {
  3598. min_distance = Math.Max(min_distance, unit.GetSkillAttackRange(expect_skill.Data.AttackKeepRange) + targetUnit.BodyBlockSize);
  3599. max_distance = Math.Max(max_distance, min_distance);
  3600. }
  3601. else if (half > 0)
  3602. {
  3603. min_distance = min_distance + half;
  3604. max_distance = Math.Max(max_distance, min_distance);
  3605. }
  3606. else
  3607. {
  3608. min_distance = Math.Max(min_distance, skill_distance + targetUnit.BodyBlockSize);
  3609. max_distance = Math.Max(max_distance, min_distance);
  3610. }
  3611. max_distance = Math.Max(max_distance, skill_distance + targetUnit.BodyHitSize);
  3612. }
  3613. }
  3614. protected override bool CheckTargetInMaxRange()
  3615. {
  3616. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  3617. }
  3618. protected override bool CheckTargetInMinRange()
  3619. {
  3620. return CMath.includeRoundPoint(unit.X, unit.Y, min_distance, targetUnit.X, targetUnit.Y);
  3621. }
  3622. protected virtual bool OnEndAdjustKeepRange(StateMove m)
  3623. {
  3624. unit.faceTo(Target.X, Target.Y);
  3625. unit.changeState(this);
  3626. return true;
  3627. }
  3628. protected virtual SkillTemplate TryLaunchSkill()
  3629. {
  3630. if (launchSkill == null)
  3631. {
  3632. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  3633. }
  3634. StateSkill ret = null;
  3635. Vector2 targetPos = null;
  3636. if (launchSkill != null)
  3637. {
  3638. ret = unit.launchSkill(launchSkill, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target));
  3639. if (ret != null)
  3640. {
  3641. return ret.SkillData;
  3642. }
  3643. }
  3644. if (can_random_skill)
  3645. {
  3646. ret = unit.launchRandomSkill(expectTarget, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target));
  3647. if (ret != null)
  3648. {
  3649. return ret.SkillData;
  3650. }
  3651. }
  3652. return null;
  3653. }
  3654. }
  3655. /// <summary>
  3656. ///
  3657. /// </summary>
  3658. public class StateFollowAndPickObject : StateFollow
  3659. {
  3660. private readonly InstanceZoneObject targetObject;
  3661. private readonly int pickTimeMS;
  3662. private readonly StatePickObject.OnPickDone doneEvent;
  3663. private readonly string status;
  3664. public StateFollowAndPickObject(InstanceUnit unit, InstanceZoneObject target, int timeMS, StatePickObject.OnPickDone done, string status = null)
  3665. : base(unit, target, false)
  3666. {
  3667. this.targetObject = target;
  3668. this.pickTimeMS = timeMS;
  3669. this.doneEvent = done;
  3670. this.status = status;
  3671. }
  3672. public override bool IsActive
  3673. {
  3674. get { return targetObject.Enable; }
  3675. }
  3676. protected override bool CheckTargetInMinRange()
  3677. {
  3678. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, targetObject.X, targetObject.Y);
  3679. }
  3680. protected override bool CheckTargetInMaxRange()
  3681. {
  3682. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize + targetObject.BodyBlockSize, targetObject.X, targetObject.Y);
  3683. }
  3684. protected override void onUpdateFollowed(IPositionObject target)
  3685. {
  3686. unit.startPickProgressObject(targetObject, pickTimeMS, doneEvent, status);
  3687. }
  3688. }
  3689. /// <summary>
  3690. ///
  3691. /// </summary>
  3692. public class StateFollowAndPickItem : StateFollow
  3693. {
  3694. public readonly InstanceItem targetObject;
  3695. private long mLockEndTime;
  3696. private bool mDoPick;
  3697. public StateFollowAndPickItem(InstanceUnit unit, InstanceItem target, bool doPick)
  3698. : base(unit, target, false)
  3699. {
  3700. this.targetObject = target;
  3701. this.mDoPick = doPick;
  3702. }
  3703. public override bool IsActive
  3704. {
  3705. get { return (targetObject.Enable && targetObject.IsPosValid()) || mLockEndTime > CommonLang.CUtils.localTimeMS; }
  3706. }
  3707. protected override bool CheckTargetInMinRange()
  3708. {
  3709. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, targetObject.X, targetObject.Y);
  3710. }
  3711. protected override bool CheckTargetInMaxRange()
  3712. {
  3713. return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize + targetObject.BodyBlockSize, targetObject.X, targetObject.Y);
  3714. }
  3715. protected override void onUpdateFollowed(IPositionObject target)
  3716. {
  3717. if (this.mDoPick && this.targetObject.Enable)
  3718. {
  3719. targetObject.PickItem(unit);
  3720. }
  3721. }
  3722. public void CheckAndLockPickTime()
  3723. {
  3724. if(!this.targetObject.Enable && mLockEndTime == 0)
  3725. {
  3726. mLockEndTime = CommonLang.CUtils.localTimeMS + 350;
  3727. }
  3728. }
  3729. protected override void onUpdate()
  3730. {
  3731. this.CheckAndLockPickTime();
  3732. if (!this.IsActive && unit.IsPlayer)
  3733. {
  3734. if (unit.DoAutoPick(true))
  3735. {
  3736. return;
  3737. }
  3738. }
  3739. base.onUpdate();
  3740. }
  3741. protected override void OnMoveAiNoWay()
  3742. {
  3743. this.mLockEndTime = 0;
  3744. targetObject.setPosNotValid();
  3745. }
  3746. public override bool onBlock(State new_state)
  3747. {
  3748. if(/*!(new_state is StateFollowAndPickItem) && */mLockEndTime > CommonLang.CUtils.localTimeMS)
  3749. {
  3750. return false;
  3751. }
  3752. //把当前拾取弄完,再去跟随
  3753. if (new_state is VStateFollowLeader && unit.DoAutoPick(true))
  3754. {
  3755. return false;
  3756. }
  3757. return true;
  3758. }
  3759. }
  3760. /// <summary>
  3761. ///
  3762. /// </summary>
  3763. public abstract class StateAttackTo : State
  3764. {
  3765. public IPositionObject Target { get { return target; } }
  3766. public MoveBlockResult LastMovingResult { get; private set; }
  3767. public MoveAI UnitMoveAI { get { return moveAI; } }
  3768. // 如果被地图碰撞,则寻路
  3769. private IPositionObject target;
  3770. // 如果被单位碰撞,则向左或右挪动
  3771. private MoveAI moveAI;
  3772. private int holdTimeMS;
  3773. private long mPeaceEndTime;
  3774. public StateAttackTo(InstanceUnit unit, int move_ai_hold_time_ms = -1, int peaceTime = 0)
  3775. : base(unit)
  3776. {
  3777. this.holdTimeMS = move_ai_hold_time_ms;
  3778. this.mPeaceEndTime = CommonLang.CUtils.CurrentTimeMS + peaceTime;
  3779. }
  3780. override public bool onBlock(State new_state)
  3781. {
  3782. if(this.mPeaceEndTime > CommonLang.CUtils.CurrentTimeMS)
  3783. {
  3784. return false;
  3785. }
  3786. return true;
  3787. }
  3788. override protected void onStart()
  3789. {
  3790. if (target == null)
  3791. {
  3792. target = PopNextPos();
  3793. }
  3794. this.moveAI = new MoveAI(unit, true, holdTimeMS);
  3795. if (target != null)
  3796. {
  3797. this.moveAI.FindPath(target);
  3798. unit.SetActionStatus(UnitActionStatus.Move);
  3799. }
  3800. else
  3801. {
  3802. unit.SetActionStatus(UnitActionStatus.Idle);
  3803. }
  3804. }
  3805. override protected void onUpdate()
  3806. {
  3807. if (target == null)
  3808. {
  3809. unit.doSomething();
  3810. }
  3811. else
  3812. {
  3813. LastMovingResult = this.moveAI.Update();
  3814. if (TestPopNext())
  3815. {
  3816. this.target = PopNextPos();
  3817. if (target != null)
  3818. {
  3819. this.moveAI.FindPath(target);
  3820. }
  3821. }
  3822. }
  3823. }
  3824. override protected void onStop()
  3825. {
  3826. }
  3827. abstract protected bool HasNextPos();
  3828. abstract protected IPositionObject PopNextPos();
  3829. protected virtual bool TestPopNext()
  3830. {
  3831. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  3832. {
  3833. return true;
  3834. }
  3835. return false;
  3836. }
  3837. public virtual bool IsDone
  3838. {
  3839. get
  3840. {
  3841. if (IsStarted)
  3842. {
  3843. if (target == null)
  3844. {
  3845. return true;
  3846. }
  3847. if (HasNextPos())
  3848. {
  3849. return false;
  3850. }
  3851. return TestPopNext();
  3852. }
  3853. return false;
  3854. }
  3855. }
  3856. }
  3857. public class StateAttackToZoneWayPoint : StateAttackTo
  3858. {
  3859. // 如果被地图碰撞,则寻路
  3860. private ZoneWayPoint paths;
  3861. private ZoneWayPoint prev;
  3862. protected ZoneWayPoint Paths { get { return paths; } }
  3863. public StateAttackToZoneWayPoint(InstanceUnit unit, ZoneWayPoint wps, int peaceTime)
  3864. : base(unit, -1, peaceTime)
  3865. {
  3866. this.paths = wps;
  3867. }
  3868. protected override bool HasNextPos()
  3869. {
  3870. return paths != null;
  3871. }
  3872. protected override IPositionObject PopNextPos()
  3873. {
  3874. if (paths != null)
  3875. {
  3876. ZoneWayPoint ret = paths;
  3877. ZoneWayPoint prv = prev;
  3878. prev = paths;
  3879. paths = paths.GetRandomNext(prv);
  3880. return ret;
  3881. }
  3882. return null;
  3883. }
  3884. protected override bool TestPopNext()
  3885. {
  3886. if (LastMovingResult.obj != null)
  3887. {
  3888. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, Target.X, Target.Y))
  3889. {
  3890. return true;
  3891. }
  3892. }
  3893. else
  3894. {
  3895. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, Target.X, Target.Y))
  3896. {
  3897. return true;
  3898. }
  3899. }
  3900. return false;
  3901. }
  3902. }
  3903. public class StatePatrolWayPoint : StateAttackTo
  3904. {
  3905. // 如果被地图碰撞,则寻路
  3906. private ZoneWayPoint paths;
  3907. private ZoneWayPoint prev;
  3908. private TimeExpire<int> hold_time;
  3909. private int hold_max_time_ms, hold_min_time_ms;
  3910. protected ZoneWayPoint Paths { get { return paths; } }
  3911. public StatePatrolWayPoint(InstanceUnit unit, ZoneWayPoint wps, int hold_max_time_ms, int hold_min_time_ms, int peaceTime)
  3912. : base(unit, -1, peaceTime)
  3913. {
  3914. this.paths = wps;
  3915. this.hold_max_time_ms = Math.Max(hold_max_time_ms, hold_min_time_ms);
  3916. this.hold_min_time_ms = Math.Min(hold_max_time_ms, hold_min_time_ms);
  3917. }
  3918. protected override void onUpdate()
  3919. {
  3920. if (this.hold_time != null)
  3921. {
  3922. if (this.hold_time.Update(zone.UpdateIntervalMS))
  3923. {
  3924. this.hold_time = null;
  3925. }
  3926. }
  3927. else
  3928. {
  3929. base.onUpdate();
  3930. }
  3931. }
  3932. protected override bool HasNextPos()
  3933. {
  3934. return paths != null;
  3935. }
  3936. private void Hold()
  3937. {
  3938. if (hold_max_time_ms > 0)
  3939. {
  3940. unit.SetActionStatus(UnitActionStatus.Idle);
  3941. this.hold_time = new TimeExpire<int>(unit.RandomN.Next(hold_min_time_ms, hold_max_time_ms));
  3942. }
  3943. }
  3944. protected override IPositionObject PopNextPos()
  3945. {
  3946. if (paths != null)
  3947. {
  3948. ZoneWayPoint ret = paths;
  3949. ZoneWayPoint prv = prev;
  3950. prev = paths;
  3951. paths = paths.GetRandomNext(prv);
  3952. Hold();
  3953. return ret;
  3954. }
  3955. return null;
  3956. }
  3957. protected override bool TestPopNext()
  3958. {
  3959. if (LastMovingResult.obj != null)
  3960. {
  3961. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, Target.X, Target.Y))
  3962. {
  3963. return true;
  3964. }
  3965. }
  3966. else
  3967. {
  3968. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, Target.X, Target.Y))
  3969. {
  3970. return true;
  3971. }
  3972. }
  3973. return false;
  3974. }
  3975. }
  3976. /// <summary>
  3977. ///
  3978. /// </summary>
  3979. public class StateGuardInPosition : State
  3980. {
  3981. public Vector2 Target { get { return target; } }
  3982. public MoveBlockResult LastMovingResult { get; private set; }
  3983. // 如果被地图碰撞,则寻路
  3984. private Vector2 target;
  3985. // 如果被单位碰撞,则向左或右挪动
  3986. private MoveAI moveAI;
  3987. public StateGuardInPosition(InstanceUnit unit, Vector2 target)
  3988. : base(unit)
  3989. {
  3990. this.target = target;
  3991. this.moveAI = new MoveAI(unit);
  3992. }
  3993. override public bool onBlock(State new_state)
  3994. {
  3995. return true;
  3996. }
  3997. override protected void onStart()
  3998. {
  3999. this.moveAI.FindPath(target.X, target.Y);
  4000. if (moveAI.IsNoWay)
  4001. {
  4002. unit.SetActionStatus(UnitActionStatus.Idle);
  4003. }
  4004. else
  4005. {
  4006. unit.SetActionStatus(UnitActionStatus.Move);
  4007. }
  4008. }
  4009. override protected void onUpdate()
  4010. {
  4011. this.LastMovingResult = this.moveAI.Update();
  4012. if ((LastMovingResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  4013. {
  4014. if ((LastMovingResult.result & MoveResult.MOVE_RESULT_BLOCK_MAP) != 0)
  4015. {
  4016. moveAI.Pause();
  4017. unit.SetActionStatus(UnitActionStatus.Idle);
  4018. }
  4019. else if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  4020. {
  4021. moveAI.Pause();
  4022. unit.SetActionStatus(UnitActionStatus.Idle);
  4023. }
  4024. }
  4025. }
  4026. override protected void onStop()
  4027. {
  4028. }
  4029. }
  4030. /// <summary>
  4031. ///
  4032. /// </summary>
  4033. public class StateBackToPosition : State
  4034. {
  4035. public Vector2 Target { get { return target; } }
  4036. public MoveBlockResult LastMovingResult { get; private set; }
  4037. public bool IsDone { get { return isDone; } }
  4038. // 如果被地图碰撞,则寻路
  4039. private Vector2 target;
  4040. // 如果被单位碰撞,则向左或右挪动
  4041. private MoveAI moveAI;
  4042. private bool isDone = false;
  4043. public StateBackToPosition(InstanceUnit unit, Vector2 target)
  4044. : base(unit)
  4045. {
  4046. this.target = target;
  4047. this.moveAI = new MoveAI(unit);
  4048. }
  4049. override public bool onBlock(State new_state)
  4050. {
  4051. if (new_state is StateDamage)
  4052. {
  4053. return true;
  4054. }
  4055. if (new_state is StateDead)
  4056. {
  4057. return true;
  4058. }
  4059. return isDone;
  4060. }
  4061. override protected void onStart()
  4062. {
  4063. this.unit.onBack2PositionStart();
  4064. this.moveAI.FindPath(target.X, target.Y);
  4065. if (moveAI.IsNoWay)
  4066. {
  4067. unit.SetActionStatus(UnitActionStatus.Idle);
  4068. }
  4069. else
  4070. {
  4071. unit.SetActionStatus(UnitActionStatus.Move);
  4072. }
  4073. }
  4074. override protected void onUpdate()
  4075. {
  4076. this.LastMovingResult = this.moveAI.Update();
  4077. if ((LastMovingResult.result & MoveResult.RESULTS_MOVE_END) != 0)
  4078. {
  4079. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
  4080. {
  4081. isDone = true;
  4082. unit.doSomething();
  4083. }
  4084. else
  4085. {
  4086. moveAI.FindPath(target.X, target.Y);
  4087. }
  4088. }
  4089. }
  4090. override protected void onStop()
  4091. {
  4092. this.unit.onBack2PositionEnd();
  4093. }
  4094. }
  4095. protected virtual void onBack2PositionEnd()
  4096. {
  4097. }
  4098. protected virtual void onBack2PositionStart()
  4099. {
  4100. }
  4101. #endregion
  4102. //---------------------------------------------------------------------------------------
  4103. #region __强制剧情播放__
  4104. public abstract class ForceState : State
  4105. {
  4106. public ForceState(InstanceUnit unit)
  4107. : base(unit)
  4108. {
  4109. }
  4110. override public bool onBlock(State new_state)
  4111. {
  4112. if (new_state is StateDead) return true;
  4113. if (new_state is StateRebirth) return true;
  4114. if (new_state is StateStun) return true;
  4115. return unit.IsDead();
  4116. }
  4117. }
  4118. /// <summary>
  4119. /// 强制移动到目标点,一般用于剧情动画
  4120. /// </summary>
  4121. public class ForceStateMoveTo : ForceState
  4122. {
  4123. private readonly float targetX;
  4124. private readonly float targetY;
  4125. private bool isEnd = false;
  4126. private MoveAI moveAI;
  4127. public ForceStateMoveTo(InstanceUnit unit, float x, float y)
  4128. : base(unit)
  4129. {
  4130. this.targetX = x;
  4131. this.targetY = y;
  4132. }
  4133. override public bool onBlock(State new_state)
  4134. {
  4135. if (base.onBlock(new_state)) return true;
  4136. return isEnd;
  4137. }
  4138. override protected void onStart()
  4139. {
  4140. unit.SetActionStatus(UnitActionStatus.Move);
  4141. this.moveAI = new MoveAI(unit);
  4142. this.moveAI.FindPath(targetX, targetY);
  4143. }
  4144. override protected void onUpdate()
  4145. {
  4146. if (!isEnd)
  4147. {
  4148. unit.faceTo(targetX, targetY);
  4149. MoveBlockResult result = moveAI.Update();
  4150. if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
  4151. {
  4152. isEnd = true;
  4153. unit.doSomething();
  4154. }
  4155. else if ((result.result & MoveResult.RESULTS_MOVE_END) != 0)
  4156. {
  4157. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  4158. if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
  4159. {
  4160. isEnd = true;
  4161. unit.doSomething();
  4162. }
  4163. }
  4164. else
  4165. {
  4166. float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
  4167. if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
  4168. {
  4169. isEnd = true;
  4170. unit.doSomething();
  4171. }
  4172. }
  4173. }
  4174. }
  4175. override protected void onStop()
  4176. {
  4177. }
  4178. }
  4179. public class ForceStateLaunchSkill : ForceState
  4180. {
  4181. private readonly int SkillID;
  4182. private readonly bool IsRandom;
  4183. private readonly StateStopHandler SkillOver;
  4184. private StateSkill mSkillState;
  4185. public ForceStateLaunchSkill(InstanceUnit unit, int skillID, bool random, StateStopHandler over = null)
  4186. : base(unit)
  4187. {
  4188. this.SkillID = skillID;
  4189. this.IsRandom = random;
  4190. this.SkillOver = over;
  4191. }
  4192. public override bool onBlock(State new_state)
  4193. {
  4194. if (base.onBlock(new_state)) return true;
  4195. if (new_state is StateSkill)
  4196. {
  4197. return true;
  4198. }
  4199. return mSkillState != null;
  4200. }
  4201. protected override void onStart()
  4202. {
  4203. }
  4204. protected override void onUpdate()
  4205. {
  4206. if (IsRandom)
  4207. {
  4208. mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
  4209. }
  4210. else
  4211. {
  4212. mSkillState = unit.launchSkill(SkillID, new InstanceUnit.LaunchSkillParam());
  4213. }
  4214. if (mSkillState == null)
  4215. {
  4216. mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
  4217. }
  4218. if (mSkillState != null && SkillOver != null)
  4219. {
  4220. mSkillState.AddStopOnce(SkillOver);
  4221. }
  4222. }
  4223. protected override void onStop()
  4224. {
  4225. if (mSkillState == null && SkillOver != null)
  4226. {
  4227. SkillOver.Invoke(unit, this);
  4228. }
  4229. }
  4230. }
  4231. public class ForceStateIdleTime : ForceState
  4232. {
  4233. private readonly TimeExpire<int> mIdleTime;
  4234. public ForceStateIdleTime(InstanceUnit unit, float timeSEC)
  4235. : base(unit)
  4236. {
  4237. mIdleTime = new TimeExpire<int>((int)(timeSEC * 1000));
  4238. }
  4239. override public bool onBlock(State new_state)
  4240. {
  4241. if (base.onBlock(new_state)) return true;
  4242. return mIdleTime.IsEnd;
  4243. }
  4244. override protected void onStart()
  4245. {
  4246. unit.SetActionStatus(UnitActionStatus.Idle);
  4247. }
  4248. override protected void onUpdate()
  4249. {
  4250. if (mIdleTime.Update(zone.UpdateIntervalMS))
  4251. {
  4252. unit.doSomething();
  4253. }
  4254. }
  4255. override protected void onStop() { }
  4256. }
  4257. public class ForceStateActionTime : ForceStateIdleTime
  4258. {
  4259. private readonly string ActionName;
  4260. public ForceStateActionTime(InstanceUnit unit, float timeSEC, string actionName)
  4261. : base(unit, timeSEC)
  4262. {
  4263. this.ActionName = actionName;
  4264. }
  4265. override protected void onStart()
  4266. {
  4267. unit.queueEvent(new UnitDoActionEvent(unit.ID, ActionName));
  4268. }
  4269. }
  4270. #endregion
  4271. }
  4272. }