1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using CommonAI.RTS;
- using CommonLang.Vector;
- using CommonAI.Zone;
- using CommonAI.Zone.Instance;
- using CommonAI.RTS.Manhattan;
- using CommonLang;
- using CommonLang.Log;
- using CommonAI.Zone.Formula;
- using CommonAI.ZoneClient;
- using CommonAI.Zone.Helper;
- using CommonAI.Zone.Attributes;
- using CommonLang.Property;
- using static CommonAI.Zone.AttackProp;
- using CommonAI.Data;
- namespace CommonAI.Zone.Instance
- {
- partial class InstanceUnit
- {
- //-----------------------------------------------------------------------------------------------------//
- #region SEND_EVENT
- private UnitForceSyncPosEvent mForceSync = new UnitForceSyncPosEvent();
- private UnitActionStatus mCurrentActionMainState = UnitActionStatus.Idle;
- //子状态状态
- private int mCurrentActionSubState = 0;
- //子状态扩展信息
- private short mSubStateExt = 0;
- private UnitActionStatus mLastActionStatus = 0;
- private int mLastActionSubstate = 0;
- //蓄力状态
- private byte chargeAtkStatus = 0;
- /// <summary>
- /// 当前动做主状态
- /// </summary>
- public UnitActionStatus CurrentActionStatus
- {
- get { return mCurrentActionMainState; }
- }
- /// <summary>
- /// 当前动做子状态, 0-正常,1-隐身
- /// </summary>
- public int CurrentActionSubstate
- {
- get { return mCurrentActionSubState; }
- }
- //蓄力状态
- public byte ChargeAtkStatus { get; set; }
- public bool IsActionStatusChanged
- {
- get; private set;
- }
- public void SetActionStatus(UnitActionStatus st)
- {
- if (st == UnitActionStatus.Move && !this.Moveable)
- {
- // nothing //
- }
- else
- {
- this.mCurrentActionMainState = st;
- }
- }
- public void AddActionSubState(UnitActionSubStatus substate, short ext = 0)
- {
- this.mCurrentActionSubState = this.mCurrentActionSubState | (int)substate;
- this.mSubStateExt = ext;
-
- // 特殊状态额外信息通知
- if (this.IsPlayer && substate == UnitActionSubStatus.Stealth)
- {
- UnitStealthInfo stealInfo = new UnitStealthInfo(this.ID, 1, (short)ext);
- this.queueEvent(stealInfo);
- }
- //Console.WriteLine("add - sub - " + this.mCurrentActionSubState);
- }
- public void RemoveActionSubState(UnitActionSubStatus substate)
- {
- //有这个状态,才能进行移除
- if (this.IsHasSubState(substate))
- {
- this.mCurrentActionSubState = this.mCurrentActionSubState ^ (int)substate;
- // 特殊状态额外信息通知
- if (this.IsPlayer && substate == UnitActionSubStatus.Stealth)
- {
- UnitStealthInfo stealInfo = new UnitStealthInfo(this.ID, 0, 0);
- this.queueEvent(stealInfo);
- }
- }
- //Console.WriteLine("remove - sub - " + this.mCurrentActionSubState);
- }
- //是否拥有该子状态
- public bool IsHasSubState(UnitActionSubStatus state)
- {
- return ((this.mCurrentActionSubState & (int)state) > 0);
- }
- public void queueEvent(ObjectEvent evt, bool force = false)
- {
- Parent.queueObjectEvent(this, evt, force);
- }
- public void queueEvent(Event evt)
- {
- Parent.queueEventInternal(evt);
- }
- public virtual bool DoAutoPick(bool ignoreInterval = false)
- {
- return false;
- }
- //通知拾取消息
- public virtual void NotifyRemoveDropItemB2C(InstanceItem item)
- {
- }
- public override void SendForceSync()
- {
- mForceSync.object_id = this.ID;
- mForceSync.X = this.X;
- mForceSync.Y = this.Y;
- mForceSync.Direction = this.Direction;
- mForceSync.UnitMainState = (byte)mCurrentActionMainState;
- this.mLastActionStatus = mCurrentActionMainState;
- mForceSync.UnitSubState = (byte)mCurrentActionSubState;
- Parent.queueObjectEvent(this, mForceSync);
- //Console.WriteLine("--------SendForceSync x:" + mForceSync.X + ", y : " + mForceSync.Y);
- }
- internal override bool updatePos(InstanceZone zone)
- {
- this.IsActionStatusChanged = (mLastActionStatus != mCurrentActionMainState) || (mLastActionSubstate != mCurrentActionSubState);
- this.mLastActionStatus = mCurrentActionMainState;
- this.mLastActionSubstate = mCurrentActionSubState;
- if (base.updatePos(zone) || IsActionStatusChanged)
- {
- return true;
- }
- return false;
- }
- protected internal virtual bool GenSyncState(ref SyncPosEvent.UnitState ret)
- {
- if (IsActionStatusChanged)
- {
- ret.SetObject(this);
- return true;
- }
- return false;
- }
- public SyncPosEvent.UnitState GetSyncState()
- {
- SyncPosEvent.UnitState ret = new SyncPosEvent.UnitState();
- ret.SetObject(this);
- return ret;
- }
- //-------------------------------------------------------------------------------------
- #endregion
- //-----------------------------------------------------------------------------------------------------//
- public abstract class State : IDisposable
- {
- private bool m_started = false;
- public InstanceUnit unit { get; private set; }
- public InstanceZone zone { get { return unit.Parent; } }
- public bool IsStarted { get { return m_started; } }
- public State(InstanceUnit unit)
- {
- this.unit = unit;
- }
- internal void start()
- {
- if (!m_started)
- {
- m_started = true;
- onStart();
- if (this.mOnStart != null)
- {
- this.mOnStart(unit, this);
- }
- }
- }
- internal void update()
- {
- onUpdate();
- }
- internal void stop()
- {
- if (m_started)
- {
- m_started = false;
- onStop();
- if (this.mStopOnce != null)
- {
- var invoke = mStopOnce;
- this.mStopOnce = null;
- invoke.Invoke(unit, this);
- }
- if (this.mOnStop != null)
- {
- this.mOnStop.Invoke(unit, this);
- }
- }
- }
- public virtual void MarkRemove() { }
- /// 当前状态是否可以被新状态打断
- public abstract bool onBlock(State new_state);
- protected abstract void onStart();
- protected abstract void onUpdate();
- protected abstract void onStop();
- public virtual void Dispose()
- {
- mOnStart = null;
- mOnStop = null;
- mStopOnce = null;
- }
- public delegate void StateStartHandler(InstanceUnit obj, State state);
- public delegate void StateStopHandler(InstanceUnit obj, State state);
- private StateStartHandler mOnStart;
- private StateStopHandler mOnStop;
- private StateStopHandler mStopOnce;
- [EventTriggerDescAttribute("状态机每次开始时触发")]
- public event StateStartHandler OnStart { add { mOnStart += value; } remove { mOnStart -= value; } }
- [EventTriggerDescAttribute("状态机每次结束时触发")]
- public event StateStopHandler OnStop { add { mOnStop += value; } remove { mOnStop -= value; } }
- [DescAttribute("状态机结束时触发,停止后自动清理所有监听")]
- public void AddStopOnce(StateStopHandler stop)
- {
- mStopOnce += stop;
- }
- }
- //-----------------------------------------------------------------------------------------------------//
- public class StateSpawn : State
- {
- private TimeExpire<int> timer;
- public StateSpawn(InstanceUnit unit, int timeMS)
- : base(unit)
- {
- timer = new TimeExpire<int>(timeMS);
- }
- override public bool onBlock(State new_state)
- {
- if(new_state is StateDuJie)
- {
- return true;
- }
- return timer.IsEnd;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Spawn);
- }
- override protected void onUpdate()
- {
- if (timer.Update(zone.UpdateIntervalMS))
- {
- unit.doSomething();
- }
- }
- override protected void onStop()
- {
- unit.doActivated();
- }
- }
- //-----------------------------------------------------------------------------------------------------//
- /// <summary>
- /// 待机状态
- /// </summary>
- public class StateIdle : State
- {
- public StateIdle(InstanceUnit unit)
- : base(unit)
- {
- }
- override public bool onBlock(State new_state)
- {
- if (new_state is StateIdle)
- {
- return false;
- }
- return true;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- override protected void onUpdate()
- {
- unit.doSomething();
- //unit.SetActionStatus(UnitActionStatus.Idle);
- }
- override protected void onStop()
- {
- }
- }
- /// <summary>
- /// 浪
- /// </summary>
- public class StateIdleMove : State
- {
- private readonly TimeExpire<int> mExpire;
- private readonly MoveAI mMoveAI;
- private readonly Vector2 mOrginPos;
- private readonly float mRange;
- private MoveBlockResult mLastResult;
- public MoveBlockResult LastMoveResult { get { return mLastResult; } }
- public StateIdleMove(InstanceUnit unit, Vector2 orginPos, int timeMS, float range)
- : base(unit)
- {
- this.mExpire = new TimeExpire<int>(timeMS);
- this.mMoveAI = new MoveAI(unit, false);
- this.mRange = Math.Abs(range);
- this.mOrginPos = orginPos;
- }
- public override bool onBlock(State new_state)
- {
- return true;
- }
- protected override void onStart()
- {
- Vector2 target = this.FindTargetPos();
- this.mMoveAI.FindPath(target.X, target.Y);
- unit.SetActionStatus(UnitActionStatus.Move);
- }
- protected override void onStop()
- {
- }
- protected override void onUpdate()
- {
- if (mExpire.Update(zone.UpdateIntervalMS))
- {
- unit.doSomething();
- }
- else
- {
- mLastResult = mMoveAI.Update();
- if ((mLastResult.result & MoveResult.RESULTS_MOVE_END) != 0)
- {
- Vector2 target = this.FindTargetPos();
- this.mMoveAI.FindPath(target.X, target.Y);
- }
- }
- }
- /// <summary>
- /// 搜索要去的地方
- /// </summary>
- /// <returns></returns>
- protected virtual Vector2 FindTargetPos()
- {
- var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, mOrginPos.X, mOrginPos.Y, mRange);
- if (node != null)
- {
- return new Vector2(node.PosX, node.PosY);
- }
- return new Vector2(mOrginPos.X, mOrginPos.Y);
- }
- }
- /// <summary>
- /// 移动状态
- /// </summary>
- public class StateMove : State
- {
- private Vector2 target;
- private int minStepTime;
- private bool moveEnd = false;
- private MoveBlockResult mLastMoveResult;
- public Vector2 Target { get { return target; } }
- public bool IsMoveEnd { get { return moveEnd; } }
- public MoveBlockResult LastMoveResult { get { return mLastMoveResult; } }
- public int MinStepCheckCount { get; set; }
- public bool StopOnTouchMap { get; set; }
- public Predicate<StateMove> EndMoveAction { get; set; }
- public StateMove(InstanceUnit unit, float x, float y)
- : base(unit)
- {
- this.StopOnTouchMap = false;
- this.MinStepCheckCount = 10;
- this.target = new Vector2(x, y);
- }
- override public bool onBlock(State new_state)
- {
- return true;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Move);
- }
- override protected void onUpdate()
- {
- unit.SetActionStatus(UnitActionStatus.Move);
- unit.faceTo(target.X, target.Y);
- mLastMoveResult = unit.moveBlockTo(target.X, target.Y, unit.MoveSpeedSEC, zone.UpdateIntervalMS);
- if ((mLastMoveResult.result & MoveResult.MOVE_RESULT_BLOCK_MAP) != 0 && StopOnTouchMap)
- {
- moveEnd = true;
- if (EndMoveAction == null || !EndMoveAction(this))
- {
- unit.doSomething();
- }
- }
- else if ((mLastMoveResult.result & MoveResult.RESULTS_MOVE_END) != 0)
- {
- moveEnd = true;
- if (EndMoveAction == null || !EndMoveAction(this))
- {
- unit.doSomething();
- }
- }
- else if ((mLastMoveResult.result & MoveResult.MOVE_RESULT_MIN_STEP) != 0)
- {
- moveEnd = true;
- minStepTime++;
- if (minStepTime > MinStepCheckCount)
- {
- if (EndMoveAction == null || !EndMoveAction(this))
- {
- unit.doSomething();
- }
- }
- }
- else
- {
- minStepTime = 0;
- }
- }
- override protected void onStop()
- {
- }
- }
- /// <summary>
- /// 释放技能状态
- /// </summary>
- public class StateSkill : State
- {
- readonly private InstanceUnit.SkillState skill;
- readonly private SkillLaunched launched;
- readonly private AttackRangeHelper attack_range;
- private Queue<UnitActionData> action_queue;
- private LaunchSkillParam param;
- private InstanceUnit targetUnit;
- private bool is_done = false;
- private UnitActionData current_action = null;
- private int curActionTotalTimes = 0;
- private PopupKeyFrames<UnitActionData.KeyFrame> current_frames = new PopupKeyFrames<UnitActionData.KeyFrame>();
- private BitSet8 current_action_status = new BitSet8();
- private int current_pass_time = 0;
- private Vector2 move_to;
- private HitMoveSpeed start_move;
- private Vector2 body_hited_last_pos = new Vector2();
- private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
- private TimeExpire<int> chantExpire;
- private AstarManhattan.MWayPoint move_to_target_path;
- private float jump_to_target_speed_sec;
- private TVector2 jump_to_target_pos;
- // 技能加速,攻速
- private float mSkillSpeed = 0.0f;
- public InstanceUnit.SkillState Skill { get { return skill; } }
- public SkillTemplate SkillData { get { return skill.Data; } }
- public InstanceUnit TargetUnit { get { return targetUnit; } }
- public Queue<UnitActionData> ActionQqueue { get { return action_queue; } }
- public bool IsControlMoveable { get { return current_action_status.Get(0); } private set { current_action_status.Set(0, value); } }
- public bool IsControlFaceable { get { return current_action_status.Get(1); } private set { current_action_status.Set(1, value); } }
- public bool IsCancelableBySkill { get { return current_action_status.Get(2); } private set { current_action_status.Set(2, value); } }
- public bool IsCancelableByMove { get { return current_action_status.Get(3); } private set { current_action_status.Set(3, value); } }
- public bool IsNoneBlock { get { return current_action_status.Get(4); } private set { current_action_status.Set(4, value); } }
- public bool IsNoneTouch { get { return current_action_status.Get(5); } private set { current_action_status.Set(5, value); } }
- public bool IsFaceToTarget { get { return current_action_status.Get(6); } private set { current_action_status.Set(6, value); } }
- public Vector2 GetMoveToPos() { return this.move_to; }
- // public bool IsCancelableBySkill { get; private set; }
- // public bool IsCancelableByMove { get; private set; }
- // public bool IsNoneBlock { get; private set; }
- // public bool IsNoneTouch { get; private set; }
- // public bool IsFaceToTarget { get; private set; }
- // public bool IsControlMoveable { get; private set; }
- // public bool IsControlFaceable { get; private set; }
- /// <summary>
- /// 技能是否完结
- /// </summary>
- public bool IsDone { get { return is_done; } }
- /// <summary>
- /// 是否在吟唱中
- /// </summary>
- public bool IsChanting { get { return chantExpire != null; } }
- /// <summary>
- /// 动作加速因子
- /// </summary>
- public float ActionSpeed { get; private set; }
- /// <summary>
- /// 技能释放结束后的朝向
- /// </summary>
- public Vector2 StopFaceTo { set; get; }
- /// <summary>
- /// 动作总时间
- /// </summary>
- public int ActionTotalTimeMS { get; private set; }
- public int CurrentActionIndex
- {
- get
- {
- if (current_action != null)
- {
- return SkillData.ActionQueue.IndexOf(current_action);
- }
- return 0;
- }
- }
- public StateSkill(InstanceUnit unit, InstanceUnit.SkillState skill, LaunchSkillParam param, float actionSpeed, SkillLaunched skillLaunched)
- : base(unit)
- {
- this.skill = skill;
- this.param = param;
- this.ActionSpeed = actionSpeed;
- this.ActionTotalTimeMS = skill.Data.ActionQueueTimeMS;
- this.launched = skillLaunched;
- this.targetUnit = zone.getUnit(param.TargetUnitID);
- this.attack_range = new AttackRangeHelper(unit);
- //if (SkillData.ChantTimeMS > 0)
- //{
- // this.chantExpire = new TimeExpire<int>(SkillData.ChantTimeMS);
- // this.IsCancelableByMove = true;
- //}
- //this.beginLaunch();
- }
- /// <summary>
- /// 释放前判断
- /// </summary>
- /// <returns></returns>
- public bool tryLaunch()
- {
- if (SkillData.ActionQueue.Count > 0)
- {
- if (!checkTargetRange())
- {
- return false;
- }
- if (!checkMoveToTarget(SkillData.ActionQueue[0]))
- {
- return false;
- }
- return true;
- }
- return false;
- }
- public bool CanBlockByNewSkill(SkillTemplate sk)
- {
- // 反击
- if (sk.IsCounter)
- {
- return true;
- }
- // 高优先级技能打断老技能, 0普通,-1标识位移技能
- if (sk.ActionPriority > 0 && sk.ActionPriority > this.SkillData.ActionPriority)
- {
- return true;
- }
- // 当前动作是否可被打断
- if (this.IsCancelableBySkill)
- {
- return true;
- }
- return false;
- }
- override public bool onBlock(State new_state)
- {
- // 吟唱中 //
- if (IsChanting)
- {
- return true;
- }
- // 技能完结 //
- if (is_done)
- {
- return true;
- }
- // 被死亡或控制类技能中断 //
- if (new_state is StateDead || new_state is StateStun)
- {
- skill.Reset();
- return true;
- }
- // 如果死亡则受击 //
- else if (new_state is StateDamage)
- {
- if (IsNoneBlock)
- {
- return false;
- }
- skill.ForceIntoCD();
- return true;
- }
- else if (new_state is StateSkill)
- {
- StateSkill ns = new_state as StateSkill;
- if (CanBlockByNewSkill(ns.SkillData))
- {
- return true;
- }
- else if (SkillData.ID == ns.SkillData.ID && SkillData.ActionQueue.Count > CurrentActionIndex)
- {
- if (SkillData.ActionQueue[this.CurrentActionIndex].IsCancelBySkillNext)
- {
- return true;
- }
- }
- }
- else
- {
- // 当前动作是否可被打断 //
- if (this.IsCancelableByMove)
- {
- return true;
- }
- }
- return false;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Skill);
- if (IsChanting)
- {
- unit.queueEvent(new UnitChantSkillEvent(unit.ID, SkillData));
- }
- else
- {
- beginLaunch();
- }
- }
- override protected void onStop()
- {
- move_to_target_path = null;
- if (start_move != null)
- {
- start_move.Stop();
- start_move = null;
- }
- unit.Z = 0;
- is_done = true;
- if (!IsChanting)
- {
- if (skill != null)
- {
- skill.Stop(this);
- }
- if (unit.IsSkillControllableByServer)
- {
- if (StopFaceTo != null)
- {
- //unit.Direction = MathVector.getDegree(StopFaceTo);
- unit.faceTo(StopFaceTo.X, StopFaceTo.Y);
- }
- }
- }
- PlayerSkillStopEvent evt = new PlayerSkillStopEvent(unit.ID, skill.ID);
- unit.queueEvent(evt);
- unit.Virtual.DispatchSkillBlockEvent(this, null);
- }
- override protected void onUpdate()
- {
- if (chantExpire != null)
- {
- if (!checkTargetRange())
- {
- is_done = true;
- unit.doSomething();
- return;
- }
- if (chantExpire.Update(zone.UpdateIntervalMS))
- {
- beginLaunch();
- chantExpire = null;
- }
- }
- if (IsChanting) return;
- if (is_done) return;
- this.current_pass_time += (int)(zone.UpdateIntervalMS * ActionSpeed);
- if (current_action == null)
- {
- nextAction();
- }
- if (current_action == null)
- {
- is_done = true;
- unit.doSomething();
- }
- else
- {
- body_hited_last_pos.SetX(unit.X);
- body_hited_last_pos.SetY(unit.Y);
- if (IsFaceToTarget)
- {
- float tx = 0;
- float ty = 0;
- if (getTargetPos(out tx, out ty))
- {
- unit.faceTo(tx, ty);
- unit.SendForceSync();
- }
- }
- // 关键帧 //
- List<UnitActionData.KeyFrame> kfs = new List<UnitActionData.KeyFrame>();
- // 技能加速
- int timePassVirtual = current_pass_time;
- if (this.Skill.GetSkillType() == XmdsSkillType.normalAtk && this.mSkillSpeed != 0)
- {
- timePassVirtual = (int)(timePassVirtual * this.mSkillSpeed);
- }
- if (current_frames.PopKeyFrames(timePassVirtual, kfs) > 0)
- {
- for (int i = 0; i < kfs.Count; i++)
- {
- doKeyFrame(kfs[i]);
- }
- }
- if (current_action != null)
- {
- var action = current_action;
- // 技能位移 //
- // 移动到目标时,不切换动作 //
- if (action.IsMoveToTarget)
- {
- // 冲到目标,立即下段 //
- doMoveToTarget(action);
- }
- else if (action.IsJumpToTarget)
- {
- doJumpToTarget(action);
- }
- else
- {
- if (unit.IsSkillControllableByServer)
- {
- if (start_move != null)
- {
- doMove();
- }
- if (move_to != null)
- {
- if (IsControlMoveable && !unit.IsCannotMove)
- {
- unit.moveImpactTo(move_to.X, move_to.Y, unit.MoveSpeedSEC, zone.UpdateIntervalMS);
- }
- else
- {
- move_to = null;
- }
- }
- }
- // 身体攻击 //
- if (action.BodyHit != null)
- {
- doBodyHit(action);
- }
- // 防止技能位移导致单位重合 //
- if (action.BodyBlockOnAttackRange)
- {
- doBodyBlock(action);
- }
- if (IsFaceToTarget)
- {
- float tx = 0;
- float ty = 0;
- if (getTargetPos(out tx, out ty))
- {
- unit.faceTo(tx, ty);
- unit.SendForceSync();
- }
- }
- }
- }
- //ADD 6/21 By Zou 加入自动战斗取消后摇
- if (current_action != null)
- {
- var isFinish = current_pass_time >= this.curActionTotalTimes;
- if (!isFinish && (IsCancelableBySkill) && unit is InstancePlayer)
- {
- isFinish = (unit as InstancePlayer).IsGuard;
- }
- if (isFinish && !nextAction())
- {
- is_done = true;
- unit.doSomething();
- }
- }
- }
- }
- private void beginLaunch()
- {
- //是否使用多段中字段时间作为cd
- bool isUseActionQueuetime = false;
- int launchIndex = 0;
- skill.setTriggerDecreaseTime(0);
- skill.Launch(this, param);
- {
- launchIndex = skill.ActionIndex;
- int maxCount = skill.GetMaxSteps();// skill.Data.ActionQueue.Count;
- if (skill.Data.IsSingleAction)
- {
- if (launchIndex > 0 && launchIndex < skill.Data.ActionQueue.Count
- //&& skill.Data.ActionQueue[actionIndex].SigleActionType == ActionEnum.forbidNext_lock)
- && !skill.IsCanLaunchNextStepByPlayer(skill.Data.ActionQueue[launchIndex]))
- {
- skill.Reset();
- }
- this.action_queue = new Queue<UnitActionData>(1);
- if (0 <= skill.LockActionStep && skill.LockActionStep <= maxCount)
- {
- this.action_queue = new Queue<UnitActionData>(1);
- launchIndex = skill.LockActionStep;
- skill.Reset();
- this.action_queue.Enqueue(skill.Data.ActionQueue[skill.LockActionStep]);
- }
- else
- {
- skill.LockActionStep = -1;
- for (int i = launchIndex; i <= maxCount; i++)
- {
- UnitActionData data = skill.Data.ActionQueue[i];
- this.action_queue.Enqueue(data);
- if (data.SigleActionType == ActionEnum.chargeAtk
- || data.SigleActionType == ActionEnum.showLeftTime)
- {
- isUseActionQueuetime = true;
- }
- else if (data.SigleActionType == ActionEnum.forbidNext_showTime)
- {
- isUseActionQueuetime = true;
- break;
- }
- else if (data.SigleActionType == ActionEnum.IsAutoNext)
- {
- continue;
- }
- else
- {
- break;
- }
- }
- }
- }
- else
- {
- //if (skill.OveryLayer > 0 && maxCount > 1)
- if(skill.CanStoreTimes)
- {
- int maxIndex = Math.Min(skill.OveryLayer, maxCount);
- this.action_queue = new Queue<UnitActionData>(maxIndex);
- for (int i = 0; i < maxIndex; i++)
- {
- UnitActionData temp = skill.Data.ActionQueue[i];
- this.action_queue.Enqueue(temp);
- if (temp.SigleActionType == ActionEnum.forbidNext
- || temp.SigleActionType == ActionEnum.forbidNext_lock
- || temp.SigleActionType == ActionEnum.forbidNext_showTime)
- {
- break;
- }
- }
- //技能释放
- if (skill.CanStoreTimes)
- {
- skill.updateOverLayer(0);
- }
- }
- else
- {
- this.action_queue = new Queue<UnitActionData>(skill.Data.ActionQueue);
- }
- }
- launched.Invoke(this);
- if (mSkillLaunched != null)
- {
- mSkillLaunched.Invoke(this);
- mSkillLaunched = null;
- }
- }
- if (unit.IsSkillControllableByServer && param.AutoFocusNearTarget && (targetUnit == null || !targetUnit.IsActive))
- {
- //自动锁定目标//
- bool directionChange = false;
- this.targetUnit = unit.getSkillAttackableFirstTarget(this.SkillData, AttackReason.Look, ref directionChange);
- if (targetUnit != null && targetUnit != unit && directionChange)
- {
- unit.faceTo(targetUnit.X, targetUnit.Y);
- }
- }
- else
- {
- if (param.SpellTargetPos != null)
- {
- unit.faceTo(param.SpellTargetPos.X, param.SpellTargetPos.Y);
- unit.SendForceSync();
- }
- }
- {
- if (skill.IsMutilAction())
- {
- //最后一段取消连招标记
- if (launchIndex + 1 == skill.Data.ActionQueue.Count &&
- skill.Data.ActionQueue[launchIndex].SigleActionType != ActionEnum.showLeftTime)
- {
- skill.Reset();
- skill.ResetTotalTime();
- }
- else if(!skill.IsCodeConttrolCD())
- {
- if (isUseActionQueuetime)
- {
- skill.TotalCDTime = skill.Data.ActionQueue[launchIndex].TotalTimeMS;
- }
- else if (launchIndex + 1 != skill.Data.ActionQueue.Count &&
- skill.IsCanLaunchNextStepByPlayer(skill.Data.ActionQueue[launchIndex]))//非最后一段才用这个时间
- {
- skill.TotalCDTime = skill.Data.SingleActionCoolDownMS;
- }
- }
- }
- //Console.WriteLine("skill.TotalCDTime - " + launchIndex);
- PushLaunchSkillEvent(launchIndex);
- }
- nextAction();
- beginMoveToTarget();
- }
- private void PushLaunchSkillEvent(int launchIndex)
- {
- float speed = ActionSpeed;
- if (this.Skill.GetSkillType() == XmdsSkillType.normalAtk)
- {
- speed = speed * this.unit.GetAttackSpeed() / 10000;
- }
- UnitLaunchSkillEvent evt = new UnitLaunchSkillEvent(
- unit.ID,
- skill.Data,
- (byte)launchIndex,
- speed,
- skill.TotalCDTime,
- param.AutoFocusNearTarget,
- targetUnit != unit ? param.SpellTargetPos : null,
- targetUnit != null ? targetUnit.ID : 0,
- skill.LockActionStep);
- unit.queueEvent(evt);
- }
- private bool beginMoveToTarget()
- {
- if (current_action != null && current_action.IsMoveToTarget)
- {
- if (targetUnit != null && zone.TouchObject2(unit, targetUnit))
- {
- if (nextAction())
- {
- unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
- }
- return true;
- }
- }
- return false;
- }
- private bool nextAction()
- {
- move_to_target_path = null;
- if (start_move != null)
- {
- start_move.Stop();
- start_move = null;
- }
- if (action_queue != null && action_queue.Count > 0)
- {
- bool isNeedNotifyClient = (this.current_action != null && (this.current_action.SigleActionType == ActionEnum.IsAutoNext
- || this.current_action.SigleActionType == ActionEnum.chargeAtk));
- //bool perChargeAtk = (this.current_action != null && this.current_action.SigleActionType == ActionEnum.chargeAtk);
- this.current_action = action_queue.Dequeue();
- if (this.Skill.GetSkillType() == XmdsSkillType.normalAtk && this.unit.GetAttackSpeed() != 0)
- {
- this.mSkillSpeed = this.unit.GetAttackSpeed() * 0.0001f;
- this.curActionTotalTimes = (int)(this.current_action.TotalTimeMS / this.mSkillSpeed);
- }
- else
- {
- this.mSkillSpeed = 0;
- this.curActionTotalTimes = this.current_action.TotalTimeMS;
- }
- //如果为蓄力技能,通知客户端状态
- if (this.current_action.SigleActionType == ActionEnum.chargeAtk)
- {
- this.skill.Owner.AddActionSubState(UnitActionSubStatus.ChargeAtkIdle);
- }
- else
- {
- this.skill.Owner.RemoveActionSubState(UnitActionSubStatus.ChargeAtkIdle);
- this.skill.Owner.RemoveActionSubState(UnitActionSubStatus.ChargeAtkMove);
- }
- this.current_frames.AddRange(current_action.KeyFrames);
- this.current_pass_time = 0;
- this.IsCancelableByMove = current_action.IsCancelable;
- this.IsCancelableBySkill = current_action.IsCancelableBySkill;
- this.IsNoneBlock = current_action.IsNoneBlock;
- this.IsNoneTouch = current_action.IsNoneTouch;
- this.IsFaceToTarget = current_action.IsFaceToTarget;
- this.IsControlFaceable = current_action.IsControlFaceable;
- this.IsControlMoveable = current_action.IsControlMoveable;
- if (isNeedNotifyClient)
- {
- skill.ResetPassTime();
- skill.NextAction();
- //程序自动连接每一段动作,时间默认为totalMS
- UnitActionData actionData = skill.Data.ActionQueue[skill.ActionIndex];
- if(!skill.IsCodeConttrolCD())
- {
- if (skill.Data.IsSingleAction && actionData.SigleActionType == ActionEnum.None)
- {
- skill.TotalCDTime = skill.Data.SingleActionCoolDownMS;
- }
- else
- {
- skill.TotalCDTime = actionData.TotalTimeMS;
- }
- }
- this.PushLaunchSkillEvent(skill.ActionIndex);
- }
- if (IsNoneBlock)
- {
- unit.SetNoneBlockTimeMS(this.curActionTotalTimes);
- }
- if (current_action.IsMoveToTarget)
- {
- //先寻路//
- checkMoveToTarget(current_action);
- }
- else if (current_action.IsJumpToTarget)
- {
- //计算跳跃距离//
- checkJumpToTarget(current_action);
- }
- else
- {
- if (unit.IsSkillControllableByServer)
- {
- if (param.AutoFocusNearTarget && (targetUnit == null || !targetUnit.IsActive))
- {
- //自动锁定目标//
- bool directionChange = false;
- targetUnit = unit.getSkillAttackableFirstTarget(this.SkillData, AttackReason.Look, ref directionChange);
- if (targetUnit != null && targetUnit != unit && directionChange)
- {
- unit.faceTo(targetUnit.X, targetUnit.Y);
- }
- }
- }
- }
- return true;
- }
- //如果为蓄力技能,通知客户端状态
- if (this.current_action != null && this.current_action.SigleActionType == ActionEnum.chargeAtk)
- {
- this.skill.Owner.RemoveActionSubState(UnitActionSubStatus.ChargeAtkIdle);
- }
- current_action = null;
- return false;
- }
- public void doKeyFrame(UnitActionData.KeyFrame kf)
- {
- // 关键帧改变状态
- if (kf.ChangeStatus != null)
- {
- this.IsNoneBlock = kf.ChangeStatus.IsNoneBlock;
- this.IsNoneTouch = kf.ChangeStatus.IsNoneTouch;
- this.IsFaceToTarget = kf.ChangeStatus.IsFaceToTarget;
- this.IsCancelableByMove = kf.ChangeStatus.IsCancelable;
- this.IsCancelableBySkill = kf.ChangeStatus.IsCancelableBySkill;
- this.IsControlFaceable = kf.ChangeStatus.IsControlFaceable;
- this.IsControlMoveable = kf.ChangeStatus.IsControlMoveable;
- }
- // 如果关键帧绑定特效
- if (kf.Effect != null)
- {
- unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Effect));
- }
- // 如果关键帧绑定近战攻击
- if (kf.Attack != null)
- {
- doHitAttack(kf.Attack);
- }
- // 如果关键帧绑定释放法术
- if (kf.Spell != null)
- {
- float tx = unit.X;
- float ty = unit.Y;
- if (param.SpellTargetPos != null)
- {
- float td = MathVector.getDistance(unit.X, unit.Y, param.SpellTargetPos.X, param.SpellTargetPos.Y);
- // TargetPos超出技能范围 //
- if (td > SkillData.AttackRange)
- {
- // 把TargetPos拉回 //
- MathVector.movePolar(param.SpellTargetPos,
- MathVector.getDegree(unit.X, unit.Y, param.SpellTargetPos.X, param.SpellTargetPos.Y),
- SkillData.AttackRange - td);
- }
- // 设置法术出生点 (非自身坐标发射,比如Cannon) //
- if (!SkillData.IsLaunchBody)
- {
- tx = param.SpellTargetPos.X;
- ty = param.SpellTargetPos.Y;
- }
- }
- //else if(this.targetUnit != null)
- //{
- // tx = this.targetUnit.X;
- // ty = this.targetUnit.Y;
- //}
- zone.unitLaunchSpell(this.skill == null ?XmdsSkillType.none : this.skill.GetSkillType(), unit, kf.Spell,
- tx, ty, param.TargetUnitID, param.SpellTargetPos, this.CurrentActionIndex);
- }
- // 如果关键帧绑定自己释放BUFF
- if (kf.SelfBuff != null)
- {
- unit.AddBuff(kf.SelfBuff, unit);
- }
- // 如果关键帧绑定单位位移
- if (kf.Move != null && (targetUnit == null || kf.hitTargetMoveEnable))
- {
- StartMove action_move = kf.Move;
- //if (unit.IsSkillControllableByServer)
- {
- this.start_move = unit.StartHitMove(this,
- unit.Direction + action_move.Direction,
- action_move.RotateSpeedSEC,
- action_move.KeepTimeMS,
- action_move.SpeedSEC,
- action_move.SpeedAdd,
- action_move.SpeedAcc,
- action_move.IsNoneTouch);
- this.start_move.SetFly(this,
- action_move.ZSpeedSEC,
- action_move.ZLimit,
- 0);
- }
- if (action_move.HasFly && !zone.IsSyncZ)
- {
- unit.queueEvent(new UnitJumpEvent(unit.ID, action_move.ZSpeedSEC, action_move.OverrideGravity, action_move.ZLimit));
- }
- }
- if (kf.Blink != null)
- {
- if (kf.Blink.BeginEffect != null)
- {
- unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Blink.BeginEffect));
- }
- unit.moveBlink(kf.Blink, param.SpellTargetPos, targetUnit);
- unit.SendForceSync();
- if (kf.Blink.TargetEffect != null)
- {
- unit.queueEvent(new UnitEffectEvent(unit.ID, kf.Blink.TargetEffect));
- }
- }
- }
- /// <summary>
- /// 近战攻击
- /// </summary>
- /// <param name="attack"></param>
- private void doHitAttack(AttackProp attack)
- {
- float rg = unit.GetSkillAttackRange(skill.Data);
- if (current_action.OverrideAttackShape != null)
- {
- attack_range.Shape = (AttackShape)current_action.OverrideAttackShape.AShape;
- attack_range.Direction = unit.Direction;
- attack_range.ExpectTarget = SkillData.ExpectTarget;
- attack_range.BodySize = current_action.OverrideAttackShape.AttackRange;
- attack_range.Distance = current_action.OverrideAttackShape.AttackRange;
- attack_range.FanAngle = current_action.OverrideAttackShape.AttackAngle;
- attack_range.StripWide = current_action.OverrideAttackShape.StripWide;
- float dx = unit.X;
- float dy = unit.Y;
- if (current_action.OverrideAttackShape.OffsetRadius != 0)
- {
- MathVector.movePolar(ref dx, ref dy, unit.Direction, current_action.OverrideAttackShape.OffsetRadius);
- }
- using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
- {
- attack_range.GetShapeAttackable(list, AttackReason.Attack, SkillData, dx, dy, dx, dy, 0, unit.AoiStatus);
- if (list.Count > 0)
- {
- zone.unitAttackDirect(unit, new AttackSource(skill, attack), list);
- }
- }
- }
- else if (skill.Data.AttackAngle == 0)
- {
- if (targetUnit != null)
- {
- if (checkTargetRange())
- {
- zone.unitAttackSingle(
- unit,
- new AttackSource(skill, attack),
- targetUnit,
- SkillData.ExpectTarget);
- }
- }
- }
- else
- {
- int hitcount = zone.unitAttackFan(
- unit,
- new AttackSource(skill, attack),
- unit.Direction,
- rg,
- skill.Data.AttackAngle,
- SkillData.ExpectTarget);
- }
- }
- /// <summary>
- /// 身体攻击
- /// </summary>
- private void doBodyHit(UnitActionData current_action)
- {
- using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
- {
- float line_r = (current_action.BodyHitSize > 0) ? current_action.BodyHitSize : unit.BodyHitSize;
- zone.getObjectsRoundLineRange<InstanceUnit>(
- Collider.Object_HitBody_TouchRoundLine,
- body_hited_last_pos.X,
- body_hited_last_pos.Y,
- unit.X,
- unit.Y,
- line_r,
- list,
- unit.AoiStatus);
- if (list.Count > 0)
- {
- CUtils.RemoveAll(list, body_hited.Values);
- zone.unitAttack(unit, new AttackSource(skill, current_action.BodyHit), list, SkillData.ExpectTarget);
- if (list.Count > 0)
- {
- for (int i = 0; i < list.Count; i++)
- {
- InstanceUnit o = list[i];
- body_hited.Put(o.ID, o);
- }
- if (current_action.BodyHitNextAction)
- {
- if (nextAction())
- {
- unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
- }
- unit.SendForceSync();
- }
- }
- }
- }
- }
- private void doMove()
- {
- start_move.IsNoneTouch = IsNoneTouch;
- if (start_move.IsEnd)
- {
- start_move = null;
- }
- }
- /// <summary>
- /// 防止技能位移导致单位重合
- /// </summary>
- private void doBodyBlock(UnitActionData current_action)
- {
- if (start_move != null || move_to != null)
- {
- if (unit.ElasticOtherObjects())
- {
- unit.SendForceSync();
- }
- }
- }
- /// <summary>
- /// 移动到目标面前
- /// </summary>
- private bool doMoveToTarget(UnitActionData current_action)
- {
- if (this.move_to_target_path == null)
- {
- if (nextAction())
- {
- unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
- }
- return true;
- }
- else if (targetUnit != null && zone.TouchObject2(unit, targetUnit))
- {
- this.move_to_target_path = null;
- unit.ElasticOtherObjects();
- if (nextAction())
- {
- unit.queueEvent(new UnitSkillActionChangeEvent(unit.ID, (byte)CurrentActionIndex));
- }
- return true;
- }
- else
- {
- float tx, ty;
- tx = move_to_target_path.PosX;
- ty = move_to_target_path.PosY;
- unit.faceTo(tx, ty);// = MathVector.getDegree(tx - unit.X, ty - unit.Y);
- if (unit.jumpToTarget(tx, ty, current_action.MoveToTargetSpeedSEC, zone.UpdateIntervalMS))
- {
- move_to_target_path = move_to_target_path.Next;
- }
- unit.SendForceSync();
- return false;
- }
- }
- private bool doJumpToTarget(UnitActionData action)
- {
- unit.Z = MoveHelper.CalulateParabolicHeight(action.JumpToTargetHeightZ, action.TotalTimeMS, this.current_pass_time);
- unit.jumpToTarget(jump_to_target_pos.X, jump_to_target_pos.Y, jump_to_target_speed_sec, zone.UpdateIntervalMS);
- unit.SendForceSync();
- return false;
- }
- public bool setMoveTo(float x, float y)
- {
- //if(Math.Abs(this.unit.X - x) < 0.5 && Math.Abs(this.unit.Y - y) < 0.5)
- //{
- // return true;
- //}
- if (unit.IsSkillControllableByServer)
- {
- if (IsControlMoveable && !unit.IsCannotMove)
- {
- if (move_to == null)
- {
- move_to = new Vector2(x, y);
- }
- else
- {
- move_to.SetX(x);
- move_to.SetY(y);
- }
- }
- if (IsControlFaceable)
- {
- unit.faceTo(x, y);
- }
- this.StopFaceTo = new Vector2(x - unit.X, y - unit.Y);
- }
- return false;
- }
- public void setMoveTo(Vector2 pos)
- {
- if (unit.IsSkillControllableByServer)
- {
- if (pos == null)
- {
- this.move_to = null;
- return;
- }
- if (IsControlMoveable && !unit.IsCannotMove)
- {
- move_to = pos;
- }
- if (IsControlFaceable)
- {
- unit.faceTo(pos.X, pos.Y);
- }
- this.StopFaceTo = new Vector2(pos.X - unit.X, pos.Y - unit.Y);
- }
- }
- public void block(State newState = null)
- {
- bool old_done = is_done;
- is_done = true;
- if (newState != null)
- {
- unit.changeState(newState);
- }
- else if (!old_done)
- {
- unit.doSomething();
- }
- }
- /// <summary>
- /// 检测目标距离
- /// </summary>
- /// <returns></returns>
- private bool checkTargetRange()
- {
- return skill.checkTargetRange(targetUnit);
- }
- /// <summary>
- /// 检测冲锋距离
- /// </summary>
- /// <returns></returns>
- private bool checkMoveToTarget(UnitActionData action)
- {
- if (action.IsMoveToTarget)
- {
- float tx; float ty;
- if (getTargetPos(out tx, out ty))
- {
- this.move_to_target_path = zone.findPath(unit.X, unit.Y, tx, ty);
- if (this.move_to_target_path != null)
- {
- float max = MoveHelper.GetDistance(action.TotalTimeMS, action.MoveToTargetSpeedSEC);
- float total = this.move_to_target_path.GetTotalDistance();
- if (total > max)
- {
- move_to_target_path = null;
- return false;
- }
- return true;
- }
- }
- return false;
- }
- return true;
- }
- /// <summary>
- /// 检测跳跃速度
- /// </summary>
- /// <param name="action"></param>
- /// <returns></returns>
- private bool checkJumpToTarget(UnitActionData action)
- {
- if (action.IsJumpToTarget)
- {
- float tx; float ty;
- if (getTargetPos(out tx, out ty))
- {
- float distance = CMath.getDistance(unit.X, unit.Y, tx, ty);
- this.jump_to_target_pos = new TVector2(tx, ty);
- this.jump_to_target_speed_sec = distance / (action.TotalTimeMS * 0.001f);
- }
- }
- return true;
- }
- private bool getTargetPos(out float x, out float y)
- {
- x = unit.X;
- y = unit.Y;
- if (param.SpellTargetPos != null)
- {
- x = param.SpellTargetPos.X;
- y = param.SpellTargetPos.Y;
- return true;
- }
- else if (targetUnit != null && targetUnit != unit)
- {
- x = targetUnit.X;
- y = targetUnit.Y;
- return true;
- }
- else return false;
- }
- public void doUpdatePosByClient(UnitUpdatePosAction act)
- {
- if (!unit.IsSkillControllableByServer)
- {
- if(this.skill.GetSkillType() == XmdsSkillType.petGiveAcitve)
- {
- unit.setPos(act.x, act.y, unit.IntersectMap);
- unit.faceTo(act.d);
- }
- else
- {
- //暂时只管方向,不管pos
- if (this.IsControlMoveable && !unit.IsCannotMove)
- {
- unit.setPos(act.x, act.y, unit.IntersectMap);
- }
- if (this.IsControlFaceable)
- {
- unit.faceTo(act.d);
- }
- else
- {
- unit.SendForceSync();
- }
- }
- }
- }
- public delegate void SkillLaunched(StateSkill state);
- private SkillLaunched mSkillLaunched;
- public event SkillLaunched OnSkillLaunched { add { mSkillLaunched += value; } remove { mSkillLaunched -= value; } }
- }
- internal void SetActionSubState(UnitActionStatus unitSubState)
- {
- throw new NotImplementedException();
- }
- //--------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------
- public class StatePickObject : State
- {
- readonly private TimeExpire<int> mTimer;
- readonly private int mTotalTimeMS;
- readonly private InstanceZoneObject mPickable;
- private bool mIsDone = false;
- private string mStopReason;
- private string mStatus;
- private OnPickDone mOnDone;
- private OnCheckPickable mCheckTargetActive;
- public string StopReason { get { return mStopReason; } }
- /// <summary>
- /// 设置目标检测状态方法
- /// </summary>
- /// <param name="check">check返回True,终止状态机。</param>
- public void SetCheckTargetAcvite(OnCheckPickable check)
- {
- this.mCheckTargetActive = check;
- }
- public StatePickObject(InstanceUnit unit, InstanceZoneObject pickable, int timeMS, string status, OnPickDone done)
- : base(unit)
- {
- this.mStatus = status;
- this.mTimer = new TimeExpire<int>(timeMS);
- this.mPickable = pickable;
- this.mTotalTimeMS = timeMS;
- this.mOnDone = done;
- }
- public override bool onBlock(State new_state)
- {
- if (new_state is StateIdle)
- return mIsDone;
- //block by anything//
- return true;
- }
- protected override void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Pick);
- unit.queueEvent(new UnitStartPickObjectEvent(unit.ID, mTotalTimeMS, mPickable.ID, mStatus));
- }
- protected override void onStop()
- {
- unit.queueEvent(new UnitStopPickObjectEvent(unit.ID, mStopReason));
- this.mOnDone = null;
- mCheckTargetActive = null;
- }
- protected override void onUpdate()
- {
- if (!mPickable.Enable)
- {
- mIsDone = true;
- unit.doSomething();
- return;
- }
- if (mCheckTargetActive != null && mCheckTargetActive(unit, mPickable, ref mStopReason))
- {
- mIsDone = true;
- unit.doSomething();
- return;
- }
- if (mTimer.Update(zone.UpdateIntervalMS))
- {
- mIsDone = true;
- unit.doSomething();
- if (mOnDone != null)
- {
- mOnDone.Invoke(unit, mPickable);
- }
- }
- }
- public delegate void OnPickDone(InstanceUnit unit, InstanceZoneObject pickable);
- public delegate bool OnCheckPickable(InstanceUnit unit, InstanceZoneObject pickable, ref string reason);
- }
- //---------------------------------------------------------------------------------------
- #region __状态限制__
- //---------------------------------------------------------------------------------------
- public interface IStateNoneControllable
- {
- State AsState();
- }
- /// <summary>
- /// 被击中受击过程
- /// </summary>
- public class StateDamage : State, IStateNoneControllable
- {
- private readonly AttackSource source;
- private readonly InstanceUnit attacker;
- private float startDirection;
- private float rotateSpeedSEC;
- private bool isEnd = false;
- private HitMoveSpeed hitMoveSpeed;
- private TimeExpire<object> damageExpire;
- private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
- private IStateNoneControllable nextState;
- public void SetNextNoneControllable(IStateNoneControllable state)
- {
- nextState = state;
- }
- public State AsState()
- {
- return this;
- }
- public bool IsFallingDown
- {
- get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
- }
- public bool IsKnockDown
- {
- get { return source.OutHasKnockDown; }
- }
- public bool IsDamageProtect
- {
- get { return source.Attack.IsDamageProtect; }
- }
- public int DamageTimeMS
- {
- get { return damageExpire.TotalTimeMS; }
- }
- public bool IsHitMove
- {
- get { return hitMoveSpeed != null; }
- }
- public StateDamage(InstanceUnit unit, AttackSource source, InstanceUnit attacker)
- : base(unit)
- {
- this.source = source;
- this.attacker = attacker;
- this.body_hited.Put(unit.ID, unit);
- AttackProp.HitMoveType mtype = source.Attack.HitMoveMType;
- if (source.FromSpell != null)
- {
- this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
- }
- else if (source.FromSkill != null)
- {
- this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
- }
- else
- {
- this.startDirection = unit.Direction + CMath.PI_F;
- }
- if (source.OutHitMove != null)
- {
- this.startDirection += source.OutHitMove.Direction;
- this.rotateSpeedSEC = source.OutHitMove.RotateSpeedSEC * ((unit.RandomN.Next() % 2) == 0 ? -1 : 1);
- }
- // 计算受击时间 //
- int damageTime = unit.Info.DamageTimeMS;
- if (source.OutKnockDownTimeMS > 0)
- {
- damageTime = source.OutKnockDownTimeMS;
- }
- else if (unit.mInfo.DamageTimeMS > 0)
- {
- damageTime = unit.mInfo.DamageTimeMS;
- }
- else
- {
- damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
- }
- this.damageExpire = new TimeExpire<object>(damageTime);
- }
- override public bool onBlock(State new_state)
- {
- if (isEnd)
- {
- return true;
- }
- if (new_state is StateDead)
- {
- return true;
- }
- if (new_state is StateDamage)
- {
- return onBlockNewDamage(new_state as StateDamage);
- }
- if (new_state is IStateNoneControllable)
- {
- this.SetNextNoneControllable(new_state as IStateNoneControllable);
- }
- if (unit.IsDead())
- {
- return false;
- }
- if (new_state is StateSkill)
- {
- StateSkill ss = new_state as StateSkill;
- // 反击或者状态解除//
- if (ss.SkillData.IsCounter)
- {
- return true;
- }
- }
- return isEnd;
- }
- private bool onBlockNewDamage(StateDamage new_damage)
- {
- //击飞只能被击飞中断//
- if (this.IsFallingDown)
- {
- if (!new_damage.source.OutHasFly)
- {
- return false;
- }
- }
- //击倒只能被击飞或击倒中断//
- if (this.IsKnockDown)
- {
- if (!new_damage.source.OutHasFly)
- {
- return false;
- }
- if (!new_damage.source.OutHasKnockDown)
- {
- return false;
- }
- }
- //如果正在位移,需要更高优先级//
- if (hitMoveSpeed != null)
- {
- if (new_damage.source.OutHasFly || new_damage.source.OutHasKnockDown || new_damage.source.OutHitMove != null)
- {
- //带位移的受击,相等优先级也可以打断当前位移//
- if (new_damage.source.OutWeight >= this.source.OutWeight)
- {
- return true;
- }
- }
- else
- {
- //普通受击,更高优先级才能打断当前位移//
- if (new_damage.source.OutWeight > this.source.OutWeight)
- {
- return true;
- }
- }
- return false;
- }
- return true;
- }
- override protected void onStart()
- {
- float zgravity = 0;
- float zlimit = 0;
- bool hasHitMove = false;
- if (source.OutHitMove != null)
- {
- int keepTimeMs = source.OutHitMove.KeepTimeMS;
- float speedSEC = source.OutHitMove.SpeedSEC;
- if (source.Attack.HitMoveMType == AttackProp.HitMoveType.BySenderRange)
- {
- float distance = CMath.getDistance(attacker.X, attacker.Y, unit.X, unit.Y);
- if (distance > source.FromSpell.BodySize)
- {
- keepTimeMs = 0;
- speedSEC = 0;
- }
- else
- {
- float moveDistance = source.FromSpell.BodySize - distance;
- keepTimeMs = (int)(moveDistance * 1000 / source.OutHitMove.SpeedSEC);
- }
- }
-
- hasHitMove = true;
- this.hitMoveSpeed = unit.StartHitMove(this,
- startDirection,
- source.OutHitMove.RotateSpeedSEC,
- keepTimeMs,
- speedSEC,
- source.OutHitMove.SpeedAdd,
- source.OutHitMove.SpeedAcc,
- source.OutHitMove.IsNoneTouch);
- if (source.OutHasFly)
- {
- this.hitMoveSpeed.SetFly(this,
- source.OutHitMove.ZSpeedSEC,
- source.OutHitMove.OverrideGravity,
- source.OutHitMove.ZLimit);
- zgravity = source.OutHitMove.OverrideGravity;
- zlimit = source.OutHitMove.ZLimit;
- }
- }
- else if (source.OutHasFly)
- {
- hasHitMove = true;
- this.hitMoveSpeed = unit.StartHitMove(this,
- startDirection,
- 0,
- 0, // 如果击飞,则计算按落地时间 //
- zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_SEC,
- zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_ADD,
- zone.Templates.CFG.OBJECT_DAMAGE_FLY_SPEED_ACC,
- false);
- this.hitMoveSpeed.SetFly(this,
- zone.Templates.CFG.OBJECT_DAMAGE_FLY_ZSPEED_SEC, 0, 0);
- }
- int moveTime = 0;
- float zspeed = 0;
- if (hitMoveSpeed != null)
- {
- moveTime = hitMoveSpeed.TotalTimeMS;
- zspeed = hitMoveSpeed.ZSpeedSEC;
- if (source.Attack.HitMoveMType == AttackProp.HitMoveType.ToSenderCenter)
- {
- if (source.FromSpellUnit != null)
- {
- this.hitMoveSpeed.SetMoveTarget(source.FromSpellUnit, false);
- }
- else if (source.FromSkill != null)
- {
- this.hitMoveSpeed.SetMoveTarget(this.attacker, false);
- }
- }
- else if (source.Attack.HitMoveMType == AttackProp.HitMoveType.ToSenderBodySize)
- {
- if (source.FromSpellUnit != null)
- {
- this.hitMoveSpeed.SetMoveTarget(source.FromSpellUnit, true);
- }
- else if (source.FromSkill != null)
- {
- this.hitMoveSpeed.SetMoveTarget(this.attacker, true);
- }
- }
- }
- unit.queueEvent(new UnitDamageEvent(
- unit.ID,
- DamageTimeMS,
- moveTime,
- zspeed,
- zgravity,
- zlimit,
- source.OutHasKnockDown,
- source.Attack));
- if (hasHitMove)
- {
- unit.SetActionStatus(UnitActionStatus.HitMove);
- }
- else
- {
- unit.SetActionStatus(UnitActionStatus.Damage);
- }
- }
- override protected void onUpdate()
- {
- if (hitMoveSpeed != null)
- {
- if (hitMoveSpeed.IsEnd)
- {
- hitMoveSpeed = null;
- if (source.Attack.FlyFallenDownAttack != null)
- {
- doFlyDownAttack();
- }
- }
- else if (source.Attack.HitMoveBodyAttack != null)
- {
- doBodyAttack();
- }
- }
- else if (damageExpire != null)
- {
- if (damageExpire.Update(zone.UpdateIntervalMS))
- {
- damageExpire = null;
- }
- }
- else
- {
- end();
- }
- }
- override protected void onStop()
- {
- if (unit.IsStun)
- {
- unit.changeState(new StateStun(unit));
- }
- else if (nextState != null)
- {
- unit.changeState(nextState.AsState());
- }
- }
- private void end()
- {
- if (unit.IsDead())
- {
- isEnd = true;
- unit.changeState(new StateDead(unit, attacker));
- }
- else if (unit.IsStun)
- {
- unit.SetActionStatus(UnitActionStatus.Stun);
- }
- else
- {
- isEnd = true;
- unit.doSomething();
- }
- }
- /// <summary>
- /// 自己落地摔一下
- /// </summary>
- private void doFlyDownAttack()
- {
- unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
- }
- /// <summary>
- /// 飞行过程中打别人
- /// </summary>
- private void doBodyAttack()
- {
- using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
- {
- zone.getObjectsRoundLineRange<InstanceUnit>(
- Collider.Object_HitBody_TouchRoundLine,
- hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
- unit.X, unit.Y,
- unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
- list, unit.AoiStatus);
- if (list.Count > 0)
- {
- CUtils.RemoveAll(list, body_hited.Values);
- zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
- if (list.Count > 0)
- {
- for (int i = 0; i < list.Count; i++)
- {
- InstanceUnit o = list[i];
- body_hited.Put(o.ID, o);
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 死了,而且是被艹翻的,暴击打死然后击退
- /// </summary>
- public class StateDeadFuckFuck : State, IStateNoneControllable
- {
- private readonly AttackSource source;
- private readonly InstanceUnit attacker;
- private float startDirection;
- private bool isEnd = false;
- private HitMoveSpeed hitMoveSpeed;
- private TimeExpire<object> damageExpire;
- private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
- private IStateNoneControllable nextState;
- public void SetNextNoneControllable(IStateNoneControllable state)
- {
- nextState = state;
- }
- public State AsState()
- {
- return this;
- }
- public bool IsFallingDown
- {
- get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
- }
- public bool IsKnockDown
- {
- get { return source.OutHasKnockDown; }
- }
- public bool IsDamageProtect
- {
- get { return source.Attack.IsDamageProtect; }
- }
- public int DamageTimeMS
- {
- get { return damageExpire.TotalTimeMS; }
- }
- public bool IsHitMove
- {
- get { return hitMoveSpeed != null; }
- }
- public StateDeadFuckFuck(InstanceUnit unit, AttackSource source, InstanceUnit attacker, HitMoveType movetype)
- : base(unit)
- {
- this.source = source;
- this.attacker = attacker;
- this.body_hited.Put(unit.ID, unit);
- HitMoveType mtype = movetype;
- if (source.FromSpell != null)
- {
- this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
- }
- else if (source.FromSkill != null)
- {
- this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
- }
- else
- {
- this.startDirection = unit.Direction + CMath.PI_F;
- }
- // 计算受击时间 //
- int damageTime = unit.Info.DamageTimeMS;
- if (source.OutKnockDownTimeMS > 0)
- {
- damageTime = source.OutKnockDownTimeMS;
- }
- else if (unit.mInfo.DamageTimeMS > 0)
- {
- damageTime = unit.mInfo.DamageTimeMS;
- }
- else
- {
- damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
- }
- this.damageExpire = new TimeExpire<object>(damageTime);
- }
- override public bool onBlock(State new_state)
- {
- if (isEnd)
- {
- return true;
- }
- if (new_state is StateDead)
- {
- return true;
- }
- if (new_state is IStateNoneControllable)
- {
- this.SetNextNoneControllable(new_state as IStateNoneControllable);
- }
- if (unit.IsDead())
- {
- return false;
- }
- if (new_state is StateDamage)
- {
- return unit.CanWhiplashDeadBody;
- }
- if (new_state is StateRebirth)
- {
- return true;
- }
- return isEnd;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Dead);
- float zgravity = 0;
- float zlimit = 0;
- if (true)
- {
- int keepTimeMs = 0;
- int moveDistance = new Random().Next(2, 12);
- float speedSEC = 15;
- keepTimeMs = (int)(moveDistance * 1000 / speedSEC);
- //speedSEC = new Random().Next(0, 11);
- this.hitMoveSpeed = unit.StartHitMove(this,
- startDirection,
- 0,
- keepTimeMs,
- speedSEC,
- 0,
- 0,
- false);
- }
- int moveTime = 0;
- float zspeed = 0;
- if (hitMoveSpeed != null)
- {
- moveTime = hitMoveSpeed.TotalTimeMS;
- zspeed = hitMoveSpeed.ZSpeedSEC;
- }
- unit.queueEvent(new UnitDamageEvent(
- unit.ID,
- DamageTimeMS,
- moveTime,
- zspeed,
- zgravity,
- zlimit,
- source.OutHasKnockDown,
- source.Attack));
- //unit.SetActionStatus(UnitActionStatus.Dead);
- }
- override protected void onUpdate()
- {
- if (hitMoveSpeed != null)
- {
- if (hitMoveSpeed.IsEnd)
- {
- hitMoveSpeed = null;
- if (source.Attack.FlyFallenDownAttack != null)
- {
- doFlyDownAttack();
- }
- }
- else if (source.Attack.HitMoveBodyAttack != null)
- {
- doBodyAttack();
- }
- }
- else if (damageExpire != null)
- {
- if (damageExpire.Update(zone.UpdateIntervalMS))
- {
- damageExpire = null;
- }
- }
- else
- {
- end();
- }
- }
- override protected void onStop()
- {
- if (unit.IsStun)
- {
- unit.changeState(new StateStun(unit));
- }
- else if (nextState != null)
- {
- unit.changeState(nextState.AsState());
- }
- }
- private void end()
- {
- isEnd = true;
- unit.changeState(new StateDead(unit, attacker));
- }
- /// <summary>
- /// 自己落地摔一下
- /// </summary>
- private void doFlyDownAttack()
- {
- unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
- }
- /// <summary>
- /// 飞行过程中打别人
- /// </summary>
- private void doBodyAttack()
- {
- using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
- {
- zone.getObjectsRoundLineRange<InstanceUnit>(
- Collider.Object_HitBody_TouchRoundLine,
- hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
- unit.X, unit.Y,
- unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
- list, unit.AoiStatus);
- if (list.Count > 0)
- {
- CUtils.RemoveAll(list, body_hited.Values);
- zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
- if (list.Count > 0)
- {
- for (int i = 0; i < list.Count; i++)
- {
- InstanceUnit o = list[i];
- body_hited.Put(o.ID, o);
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 死了,而且是被艹翻的,暴击打死然后击退
- /// </summary>
- public class StateDeadFuck : State, IStateNoneControllable
- {
- private readonly AttackSource source;
- private readonly InstanceUnit attacker;
- private float startDirection;
- private bool isEnd = false;
- private HitMoveSpeed hitMoveSpeed;
- private TimeExpire<object> damageExpire;
- private HashMap<uint, InstanceUnit> body_hited = new HashMap<uint, InstanceUnit>();
- private IStateNoneControllable nextState;
- public void SetNextNoneControllable(IStateNoneControllable state)
- {
- nextState = state;
- }
- public State AsState()
- {
- return this;
- }
- public bool IsFallingDown
- {
- get { return (hitMoveSpeed != null && hitMoveSpeed.IsFly && unit.Z > 0); }
- }
- public bool IsKnockDown
- {
- get { return source.OutHasKnockDown; }
- }
- public bool IsDamageProtect
- {
- get { return source.Attack.IsDamageProtect; }
- }
- public int DamageTimeMS
- {
- get { return damageExpire.TotalTimeMS; }
- }
- public bool IsHitMove
- {
- get { return hitMoveSpeed != null; }
- }
- public StateDeadFuck(InstanceUnit unit, AttackSource source, InstanceUnit attacker)
- : base(unit)
- {
- this.source = source;
- this.attacker = attacker;
- this.body_hited.Put(unit.ID, unit);
- AttackProp.HitMoveType mtype = AttackProp.HitMoveType.BySenderPosition;
- if (source.FromSpell != null)
- {
- this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, source.FromSpellUnit, mtype);
- }
- else if (source.FromSkill != null)
- {
- this.startDirection = MoveHelper.CalculateHitMoveDirection(unit, attacker, mtype);
- }
- else
- {
- this.startDirection = unit.Direction + CMath.PI_F;
- }
- // 计算受击时间 //
- int damageTime = unit.Info.DamageTimeMS;
- if (source.OutKnockDownTimeMS > 0)
- {
- damageTime = source.OutKnockDownTimeMS;
- }
- else if (unit.mInfo.DamageTimeMS > 0)
- {
- damageTime = unit.mInfo.DamageTimeMS;
- }
- else
- {
- damageTime = zone.Templates.CFG.OBJECT_DAMAGE_TIME_MS;
- }
- this.damageExpire = new TimeExpire<object>(damageTime);
- }
- override public bool onBlock(State new_state)
- {
- if (isEnd)
- {
- return true;
- }
- if (new_state is StateDead)
- {
- return true;
- }
- if (new_state is IStateNoneControllable)
- {
- this.SetNextNoneControllable(new_state as IStateNoneControllable);
- }
- if (unit.IsDead())
- {
- return false;
- }
- if (new_state is StateDamage)
- {
- return unit.CanWhiplashDeadBody;
- }
- if (new_state is StateRebirth)
- {
- return true;
- }
- return isEnd;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Dead);
- float zgravity = 0;
- float zlimit = 0;
- if (true)
- {
- int keepTimeMs = 0;
- float speedSEC = 3;//这个数字要随机1-10
- speedSEC = new Random().Next(0, 11);
- this.hitMoveSpeed = unit.StartHitMove(this,
- startDirection,
- 0,
- keepTimeMs,
- speedSEC,
- 0,
- 0,
- false);
- if (true)
- {
- this.hitMoveSpeed.SetFly(this,
- 15,//source.OutHitMove.ZSpeedSEC
- 0,
- 0);
- zgravity = 0;
- zlimit = 0;
- }
- }
- int moveTime = 0;
- float zspeed = 0;
- if (hitMoveSpeed != null)
- {
- moveTime = hitMoveSpeed.TotalTimeMS;
- zspeed = hitMoveSpeed.ZSpeedSEC;
- }
- unit.queueEvent(new UnitDamageEvent(
- unit.ID,
- DamageTimeMS,
- moveTime,
- zspeed,
- zgravity,
- zlimit,
- source.OutHasKnockDown,
- source.Attack));
- unit.SetActionStatus(UnitActionStatus.Dead);
- }
- override protected void onUpdate()
- {
- if (hitMoveSpeed != null)
- {
- if (hitMoveSpeed.IsEnd)
- {
- hitMoveSpeed = null;
- if (source.Attack.FlyFallenDownAttack != null)
- {
- doFlyDownAttack();
- }
- }
- else if (source.Attack.HitMoveBodyAttack != null)
- {
- doBodyAttack();
- }
- }
- else if (damageExpire != null)
- {
- if (damageExpire.Update(zone.UpdateIntervalMS))
- {
- damageExpire = null;
- }
- }
- else
- {
- end();
- }
- }
- override protected void onStop()
- {
- if (unit.IsStun)
- {
- unit.changeState(new StateStun(unit));
- }
- else if (nextState != null)
- {
- unit.changeState(nextState.AsState());
- }
- }
- private void end()
- {
- isEnd = true;
- unit.changeState(new StateDead(unit, attacker));
- }
- /// <summary>
- /// 自己落地摔一下
- /// </summary>
- private void doFlyDownAttack()
- {
- unit.doHitAttack(attacker, source.GenAttackSource(source.Attack.FlyFallenDownAttack));
- }
- /// <summary>
- /// 飞行过程中打别人
- /// </summary>
- private void doBodyAttack()
- {
- using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
- {
- zone.getObjectsRoundLineRange<InstanceUnit>(
- Collider.Object_HitBody_TouchRoundLine,
- hitMoveSpeed.PrevX, hitMoveSpeed.PrevY,
- unit.X, unit.Y,
- unit.BodyBlockSize + source.Attack.HitMoveBodyAttackSize,
- list, unit.AoiStatus);
- if (list.Count > 0)
- {
- CUtils.RemoveAll(list, body_hited.Values);
- zone.unitAttack(attacker, source.GenAttackSource(source.Attack.HitMoveBodyAttack), list, source.FromExpectTarget);
- if (list.Count > 0)
- {
- for (int i = 0; i < list.Count; i++)
- {
- InstanceUnit o = list[i];
- body_hited.Put(o.ID, o);
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 死亡状态
- /// </summary>
- public class StateDead : State, IStateNoneControllable
- {
- private TimeExpire<int> dead_time;
- private TimeExpire<int> rebirth_time;
- private InstanceUnit attacker;
- private bool crushed;
- private List<TimeExpire<int>> dead_delayTimes = new List<TimeExpire<int>>();
- public StateDead(InstanceUnit unit, InstanceUnit attacker, bool crush = false)
- : base(unit)
- {
- this.attacker = attacker;
- this.crushed = crush;
- }
- public State AsState()
- {
- return this;
- }
- override public bool onBlock(State new_state)
- {
- if (new_state is StateDamage)
- {
- return unit.CanWhiplashDeadBody;
- }
- if (new_state is StateRebirth)
- {
- return true;
- }
- return !unit.IsDead();
- }
- override protected void onStart()
- {
- //某些状态帧下死亡调用漏掉了
- if (unit.IsNeedProcessDead())
- {
- unit.Parent.cb_unitDeadCallBack(unit, this.attacker);
- }
- unit.SetActionStatus(UnitActionStatus.Dead);
- this.rebirth_time = new TimeExpire<int>(unit.mInfo.RebirthTimeMS);
- this.dead_time = new TimeExpire<int>(unit.mInfo.DeadTimeMS);
- if (unit.Info.DeadLaunchSpell != null)
- {
- // 自爆法术
- zone.unitLaunchSpell(XmdsSkillType.none, unit, unit.Info.DeadLaunchSpell, unit.X, unit.Y);
- }
- // Boss 死亡之后灵魂飘到击杀者身上
- if (unit.mInfo.DeadDelayTimeMS > 0)
- {
- dead_delayTimes.Clear();
- for (var i = 0; i < unit.Info.DeadDelaySpellNumber; i++)
- {
- this.dead_delayTimes.Add(new TimeExpire<int>(
- unit.mInfo.DeadDelayTimeMS + i * unit.Info.DeadDelaySpellIntervalMS));
- }
- }
- }
- override protected void onUpdate()
- {
- if (dead_time.Update(zone.UpdateIntervalMS))
- {
- if (unit.mInfo.RebirthTimeMS <= 0)
- {
- zone.RemoveObject(unit);
- }
- //跨服场景中,单位死亡,需要移除单位,然后才能复活
- if (unit.IsDead() && unit.IsPlayer && unit.GetSceneType() >= CommonAI.Data.SceneType.CROSS_SERVER_NEW)
- {
- // zone.RemoveObject(unit);
- unit.DeadHide = true;
- }
- }
- if (rebirth_time.Update(zone.UpdateIntervalMS))
- {
- if (unit.mInfo.RebirthTimeMS > 0)
- {
- unit.startRebirth();
- }
- }
- for (var i = 0; i < this.dead_delayTimes.Count; i++)
- {
- if (dead_delayTimes[i] != null && dead_delayTimes[i].Update(zone.UpdateIntervalMS))
- {
- zone.unitLaunchSpell(XmdsSkillType.none, unit, unit.Info.DeadDelayLaunchSpell,
- unit.X, unit.Y, attacker == null ? 0 : ((InstanceUnit)attacker).mSyncInfo.ObjectID);
- dead_delayTimes[i] = null;
- }
- }
- }
- override protected void onStop()
- {
- //unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- /// <summary>
- /// 复活
- /// </summary>
- public class StateRebirth : State, IStateNoneControllable
- {
- private bool is_done = false;
- private int max_hp = 0;
- private int max_mp = 0;
- public StateRebirth(InstanceUnit unit, int max_hp, int max_mp)
- : base(unit)
- {
- this.max_hp = max_hp;
- this.max_mp = max_mp;
- }
- public State AsState()
- {
- return this;
- }
- override public bool onBlock(State new_state)
- {
- return is_done;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Rebirth);
- }
- override protected void onUpdate()
- {
- this.is_done = true;
- unit.doRebirth(max_hp, max_mp);
- unit.doSomething();
- }
- override protected void onStop()
- {
- }
- }
- //---------------------------------------------------------------------------------------
- /// <summary>
- /// 眩晕状态
- /// </summary>
- public class StateStun : State, IStateNoneControllable
- {
- public StateStun(InstanceUnit unit)
- : base(unit)
- {
- }
- public State AsState()
- {
- return this;
- }
- override public bool onBlock(State new_state)
- {
- if (new_state is StateDamage)
- {
- return true;
- }
- if (unit.IsDead() && new_state is StateDead)
- {
- return true;
- }
- if (new_state is StateSkill)
- {
- StateSkill ss = new_state as StateSkill;
- // 反击或者状态解除//
- if (ss.SkillData.IsCounter)
- {
- return true;
- }
- }
- return !unit.IsStun;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Stun);
- }
- override protected void onUpdate()
- {
- if (!unit.IsStun)
- {
- unit.doSomething();
- }
- }
- override protected void onStop()
- {
- //unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- /// <summary>
- /// 不可移动状态
- /// </summary>
- public class StateChuanGongA : State, IStateNoneControllable
- {
- private bool markRemove;
- public StateChuanGongA(InstanceUnit unit)
- : base(unit)
- {
-
- }
- public State AsState()
- {
- return this;
- }
- override public bool onBlock(State new_state)
- {
- if (this.markRemove)
- {
- return true;
- }
- return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
- }
- public override void MarkRemove()
- {
- this.markRemove = true;
- }
- override protected void onStart()
- {
- this.markRemove = false;
- unit.chuangongTime = CommonLang.CUtils.localTimeMS + 60000;
- unit.SetActionStatus(UnitActionStatus.ChuanGongA);
- }
- override protected void onUpdate()
- {
- if (this.markRemove)
- {
- unit.startIdle();
- }
- else if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
- {
- unit.doSomething();
- }
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- /// <summary>
- /// 不可移动状态
- /// </summary>
- public class StateChuanGongB : State, IStateNoneControllable
- {
- private bool markRemove;
- public StateChuanGongB(InstanceUnit unit)
- : base(unit)
- {
- }
- public State AsState()
- {
- return this;
- }
- override public bool onBlock(State new_state)
- {
- if (this.markRemove)
- {
- return true;
- }
- return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
- }
- public override void MarkRemove()
- {
- this.markRemove = true;
- }
- override protected void onStart()
- {
- this.markRemove = false;
- unit.chuangongTime = CommonLang.CUtils.localTimeMS + 60000;
- unit.SetActionStatus(UnitActionStatus.ChuanGongB);
- }
- override protected void onUpdate()
- {
- if (this.markRemove)
- {
- unit.startIdle();
- }
- else if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
- {
- unit.doSomething();
- }
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- /// <summary>
- /// 不可移动状态 净妖,和传功一样锁定4秒,时间变量用传功的
- /// </summary>
- public class StateClearYaoQi : State, IStateNoneControllable
- {
- public StateClearYaoQi(InstanceUnit unit)
- : base(unit)
- {
- }
- public State AsState()
- {
- return this;
- }
- override public bool onBlock(State new_state)
- {
- return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
- }
- override protected void onStart()
- {
- unit.chuangongTime = CommonLang.CUtils.localTimeMS + 4000;
- unit.SetActionStatus(UnitActionStatus.ClearYaoQi);
- }
- override protected void onUpdate()
- {
- if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
- {
- unit.doSomething();
- }
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- /// <summary>
- /// 打坐
- /// </summary>
- public class StateDaZuo : State
- {
- private float startX;
- private float startY;
- private long lockEndTime;
- private bool markRemove;
- public StateDaZuo(InstanceUnit unit, int time)
- : base(unit)
- {
- int lockTime = Math.Max(1, time);
- //锁定短暂时间,避免一打坐就被客户端取消了。
- lockEndTime = 0;// CommonLang.CUtils.localTimeMS + lockTime * 1000;
- }
- public override void MarkRemove()
- {
- this.markRemove = true;
- }
- override public bool onBlock(State new_state)
- {
- if (this.markRemove)
- {
- return true;
- }
- if (CommonLang.CUtils.localTimeMS < lockEndTime)
- {
- return false;
- }
- if (new_state is StateIdle)
- {
- return false;
- }
- return true;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.DaZuo);
- }
- override protected void onUpdate()
- {
- if(this.markRemove)
- {
- unit.startIdle();
- }
- else if(lockEndTime > CommonLang.CUtils.localTimeMS)
- {
- startX = unit.X;
- startY = unit.Y;
- }
- else if (unit.X != startX || unit.Y != startY)
- {
- unit.doSomething();
- }
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
-
- /// <summary>
- /// 打坐恢复血量
- /// </summary>
- public class StateDaZuoRecoveryAttr : State
- {
- private long endTime;//回血结束时间
- private bool markRemove;
- private int sustainTime; //持续时间
- private long nextRecoveryTime; //下一次恢复时间
- public StateDaZuoRecoveryAttr(InstanceUnit unit, int time)
- : base(unit)
- {
- endTime = CommonLang.CUtils.CurrentTimeMS + time*1000;
- sustainTime = time-3; //减去3是因为等待坐下去后才开始回血
- nextRecoveryTime = CommonLang.CUtils.CurrentTimeMS+2350; //2350是客户端坐下去整个动作的时间,等待动作播放完成,开始回血
- }
- public override void MarkRemove()
- {
- this.markRemove = true;
- }
- override public bool onBlock(State new_state)
- {
- return true;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.DaZuoRecoveryAttr);
- }
- override protected void onUpdate()
- {
- if(this.markRemove)
- {
- unit.startIdle();
- }
- else
- {
- if (unit.Virtual.GetBattleStatus() != BattleStatus.None)
- {
- unit.startIdle();
- return;
- }
-
- if (CommonLang.CUtils.CurrentTimeMS > endTime)
- {
- unit.startIdle();
- return;
- }
-
- if (nextRecoveryTime < CommonLang.CUtils.CurrentTimeMS)
- {
- int Recover = 0;
- //恢复主角血量
- // if (unit.CurrentHP < unit.MaxHP)
- //{
- Recover = (int)(unit.MaxHP / sustainTime);
- /*if (unit.CurrentHP + Recover > unit.MaxHP)
- {
- Recover = unit.MaxHP - unit.CurrentHP;
- }*/
- unit.add_hp(Recover);
- // }
- //恢复宠物血量
- InstanceUnit petUnit = unit.Virtual.GetPetUnit();
- if (petUnit != null)
- {
- if (petUnit.CurrentHP < petUnit.MaxHP && petUnit.Virtual.GetBattleStatus() == BattleStatus.None)
- {
- Recover = (int)(petUnit.MaxHP / sustainTime);
- if (petUnit.CurrentHP + Recover > petUnit.MaxHP)
- {
- Recover = petUnit.MaxHP - petUnit.CurrentHP;
- }
- petUnit.add_hp(Recover);
- }
- }
- nextRecoveryTime = CommonLang.CUtils.CurrentTimeMS + 1 * 1000;
- }
- }
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
-
- /// <summary>
- /// 随从向玩家瞬移
- /// </summary>
- public class PetLockState : State
- {
- private long endTime;//瞬移后定住时间
- private bool markRemove;
- public PetLockState(InstanceUnit unit)
- : base(unit)
- {
- endTime = CommonLang.CUtils.CurrentTimeMS + 1*1000; //持续时间1秒
- }
- public override void MarkRemove()
- {
- this.markRemove = true;
- }
- override public bool onBlock(State new_state)
- {
- return true;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.PetLock);
- }
- override protected void onUpdate()
- {
- if(this.markRemove)
- {
- unit.startIdle();
- }
- else
- {
- if (CommonLang.CUtils.CurrentTimeMS > endTime)
- {
- unit.startIdle();
- return;
- }
- }
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- /// <summary>
- /// 渡劫场景,不能移动
- /// </summary>
- public class StateDuJie : State, IStateNoneControllable
- {
- public StateDuJie(InstanceUnit unit)
- : base(unit)
- {
- }
- public State AsState()
- {
- return this;
- }
- override public bool onBlock(State new_state)
- {
- return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
- }
- override protected void onStart()
- {
- // 管他多长时间,锁一个小时再说
- unit.chuangongTime = CommonLang.CUtils.localTimeMS + 3600000;
- unit.SetActionStatus(UnitActionStatus.CrossRobbery);
- }
- override protected void onUpdate()
- {
- //if (unit.chuangongTime < CommonLang.CUtils.localTimeMS)
- //{
- // unit.doSomething();
- //}
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- /// <summary>
- /// 渡劫场景,不能移动
- /// </summary>
- public class StateTransport : State, IStateNoneControllable
- {
- private long mLockEndTime = 0;
- public StateTransport(InstanceUnit unit)
- : base(unit)
- {
- }
- public State AsState()
- {
- return this;
- }
- override public bool onBlock(State new_state)
- {
- if(new_state is StateSkill)
- {
- return true;
- }
- return unit.chuangongTime < CommonLang.CUtils.localTimeMS;
- }
- override protected void onStart()
- {
- // 管他多长时间,锁定300ms
- this.mLockEndTime = CommonLang.CUtils.localTimeMS + 250;
- unit.SetActionStatus(UnitActionStatus.Transport);
- //System.Console.WriteLine("开始传送状态:" + CommonLang.CUtils.localTimeMS);
- }
- override protected void onUpdate()
- {
- if (this.mLockEndTime <= CommonLang.CUtils.localTimeMS)
- {
- unit.doSomething();
- }
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- //System.Console.WriteLine("end传送状态:" + CommonLang.CUtils.localTimeMS);
- }
- }
- //---------------------------------------------------------------------------------------
- /// <summary>
- /// 状态限制移动
- /// </summary>
- abstract public class StateMoveNoneControllable : State, IStateNoneControllable
- {
- private TimeExpire<int> mExpire;
- private MoveAI mMoveAI;
- private MoveBlockResult mLastResult;
- public MoveBlockResult LastMoveResult { get { return mLastResult; } }
- public State AsState()
- {
- return this;
- }
- public StateMoveNoneControllable(InstanceUnit unit, int timeMS)
- : base(unit)
- {
- this.mExpire = new TimeExpire<int>(timeMS);
- this.mMoveAI = new MoveAI(unit, false);
- }
- /// <summary>
- /// 搜索要去的地方
- /// </summary>
- /// <returns></returns>
- protected abstract Vector2 FindTargetPos();
- public override bool onBlock(State new_state)
- {
- if (new_state is StateDead)
- {
- return true;
- }
- if (new_state is StateDamage)
- {
- (new_state as StateDamage).SetNextNoneControllable(this);
- return true;
- }
- if (new_state is StateSkill)
- {
- StateSkill ss = new_state as StateSkill;
- // 反击或者状态解除//
- if (ss.SkillData.IsCounter)
- {
- return true;
- }
- }
- return mExpire.IsEnd;
- }
- protected override void onStart()
- {
- Vector2 target = this.FindTargetPos();
- this.mMoveAI.FindPath(target.X, target.Y);
- }
- protected override void onUpdate()
- {
- if (mExpire.Update(zone.UpdateIntervalMS))
- {
- unit.doSomething();
- }
- else
- {
- mLastResult = mMoveAI.Update();
- if ((mLastResult.result & MoveResult.RESULTS_MOVE_END) != 0)
- {
- Vector2 target = this.FindTargetPos();
- this.mMoveAI.FindPath(target.X, target.Y);
- }
- }
- }
- protected override void onStop()
- {
- }
- }
- /// <summary>
- /// 拾取状态
- /// </summary>
- public class StateAutoPick : State
- {
- private InstanceItem mPickItem; //要自动拾取的物品
- private MoveAI mMoveAi;
- bool mIsEnd;
- public StateAutoPick(InstanceUnit unit, InstanceItem item)
- : base(unit)
- {
- this.mPickItem = item;
- this.mMoveAi = new MoveAI(unit);
- this.mMoveAi.FindPath(item.X, item.Y);
- }
- override public bool onBlock(State new_state)
- {
- if (unit.IsDead())
- {
- return true;
- }
- return true;
- }
- override protected void onStart()
- {
- //unit.SetActionStatus(UnitActionStatus.AutoPick);
- }
- override protected void onUpdate()
- {
- if (mIsEnd || this.mPickItem.IsDead() || !this.mPickItem.Enable || this.unit.Virtual.GetBattleStatus() != BattleStatus.None)
- {
- unit.doSomething();
- }
- else
- {
- MoveBlockResult result = mMoveAi.Update();
- if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
- {
- mIsEnd = true;
- unit.doSomething();
- }
- else
- {
- float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
- if (CMath.includeRoundPoint(unit.X, unit.Y, r, mPickItem.X, mPickItem.Y))
- {
- mIsEnd = true;
- unit.doSomething();
- }
- }
- }
- }
- override protected void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- //---------------------------------------------------------------------------------------
- /// <summary>
- /// 逃跑状态
- /// </summary>
- public class StateEscape : StateMoveNoneControllable
- {
- private float mDistance;
- public StateEscape(InstanceUnit unit, int timeMS, float distance = 0) : base(unit, timeMS)
- {
- if (distance > 0)
- {
- this.mDistance = distance;
- }
- else
- {
- this.mDistance = unit.Info.GuardRange;
- }
- }
- protected override void onStart()
- {
- base.onStart();
- unit.SetActionStatus(UnitActionStatus.Escape);
- }
- protected override Vector2 FindTargetPos()
- {
- var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, unit.X, unit.Y, mDistance);
- return new Vector2(node.BX, node.BY);
- }
- }
- //---------------------------------------------------------------------------------------
- /// <summary>
- /// 混乱状态
- /// </summary>
- public class StateChaos : StateMoveNoneControllable
- {
- public StateChaos(InstanceUnit unit, int timeMS) : base(unit, timeMS)
- {
- }
- protected override void onStart()
- {
- base.onStart();
- unit.SetActionStatus(UnitActionStatus.Chaos);
- }
- protected override void onStop()
- {
- base.onStop();
- }
- protected override Vector2 FindTargetPos()
- {
- var node = zone.PathFinder.FindNearRandomMoveableNode(zone.RandomN, unit.X, unit.Y, unit.Info.GuardRange);
- return new Vector2(node.BX, node.BY);
- }
- }
- //---------------------------------------------------------------------------------------
- /// <summary>
- /// 破定
- /// </summary>
- public class StateBreakShield : State
- {
- private long mStateEndTime;
- public StateBreakShield(InstanceUnit unit) : base(unit)
- {
- this.mStateEndTime = CommonLang.CUtils.localTimeMS + GlobalData.SHIELD_BREAK_HOLDTIME;
- }
- protected override void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.BreakShield);
- unit.AddBuff(GlobalData.SHIELD_BREAK_BUFF, 0, true);
- }
- protected override void onUpdate()
- {
- if(CommonLang.CUtils.localTimeMS > this.mStateEndTime)
- {
- unit.startIdle();
- }
- }
- public override bool onBlock(State new_state)
- {
- if (CommonLang.CUtils.localTimeMS > this.mStateEndTime)
- {
- return true;
- }
- else if (new_state is StateDead)
- {
- return true;
- }
- return false;
- }
- protected override void onStop()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- //unit.removeBuff(GlobalData.SHIELD_BREAK_BUFF);
- unit.Virtual.StartRecoverMP();
- }
- }
- //---------------------------------------------------------------------------------------
- #endregion
- #region __AI行为__
- //---------------------------------------------------------------------------------------
- /// <summary>
- /// 移动状态
- /// </summary>
- abstract public class StateFollow : State
- {
- public enum MoveState
- {
- Hold,
- Move,
- }
- // 被追目标
- protected IPositionObject target;
- // 如果被单位碰撞,则向左或右挪动//
- readonly private MoveAI moveAI;
- // 检测切换走停的间隔时间 //
- readonly private State<MoveState> state = new State<MoveState>(MoveState.Move);
- readonly private bool begin_in_min_range;
- // 检测切换走停的间隔时间 //
- private TimeInterval<int> start_move_hold_time;
- public abstract bool IsActive { get; }
- public MoveAI MoveAI
- {
- get
- {
- return moveAI;
- }
- private set { }
- }
- public bool IsNoWay { get { if (moveAI != null) { return moveAI.IsNoWay; } return false; } }
- public IPositionObject Target { get { return target; } }
- public MoveState FollowState { get { return state.Value; } }
- /// <summary>
- /// Hold到Move之间的检测间隔
- /// </summary>
- public int StartMoveHoldTimeMS
- {
- get { return (start_move_hold_time != null) ? start_move_hold_time.IntervalTimeMS : 0; }
- set
- {
- if (value > 0 && value != StartMoveHoldTimeMS)
- {
- start_move_hold_time = new TimeInterval<int>(value);
- }
- }
- }
- public StateFollow(InstanceUnit unit, InstanceZoneObject target, bool beginInMinRange = true, int holdTime = 0) : base(unit)
- {
- this.begin_in_min_range = beginInMinRange;
- this.target = target;
- this.moveAI = new MoveAI(unit, true, holdTime);
- }
- public override bool onBlock(State new_state)
- {
- return true;
- }
- protected override void onStart()
- {
- if (this.unit.Info.SumType == SummonType.AwaitMaster || cheekBeginInRange())
- {
- state.ChangeState(MoveState.Hold);
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- else
- {
- state.ChangeState(MoveState.Move);
- unit.SetActionStatus(UnitActionStatus.Move);
- moveAI.FindPath(target);
- }
- }
- protected virtual void OnMoveAiNoWay() { }
- protected override void onUpdate()
- {
- if (!IsActive)
- {
- unit.doSomething();
- }
- else if ((unit.CurrentActionSubstate & (int)UnitActionSubStatus.CanNotMove) > 0)
- {
- //不能移动,啥事也不干
- }
- else
- {
- switch (FollowState)
- {
- case MoveState.Move:
- // 进入最小追踪距离 //
- if (CheckTargetInMinRange() == true)
- {
- changeToHold();
- }
- else
- {
- if (moveAI.Target == null)
- {
- moveAI.FindPath(target);
- }
- moveAI.Update();
- if (moveAI.IsNoWay)
- {
- this.OnMoveAiNoWay();
- if (CheckTargetInMaxRange() == true)
- {
- changeToHold();
- }
- else
- {
- unit.doSomething();
- }
- }
- }
-
- break;
- case MoveState.Hold:
- // 超过最大追踪范围 //
- if ((start_move_hold_time == null || start_move_hold_time.Update(zone.UpdateIntervalMS)) && (CheckTargetInMaxRange() == false))
- {
- changeToMove();
- }
- else
- {
- if (unit.CurrentActionStatus != UnitActionStatus.Idle)
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- onUpdateFollowed(target);
- break;
- }
- }
- }
- protected override void onStop() { }
- private bool cheekBeginInRange()
- {
- if (begin_in_min_range)
- {
- return CheckTargetInMinRange();
- }
- else
- {
- return CheckTargetInMaxRange();
- }
- }
- private void changeToMove()
- {
- state.ChangeState(MoveState.Move);
- unit.SetActionStatus(UnitActionStatus.Move);
- moveAI.FindPath(target);
- onInRangeChanged();
- onChangedToMove(target);
- }
- private void changeToHold()
- {
- state.ChangeState(MoveState.Hold);
- unit.SetActionStatus(UnitActionStatus.Idle);
- moveAI.Pause();
- onInRangeChanged();
- onChangedToHold(target);
- }
- /// <summary>
- /// 检查目标是否在跟随范围内。
- /// 检测为True,停止移动。
- /// </summary>
- protected abstract bool CheckTargetInMinRange();
- /// <summary>
- /// 检查目标是否在跟随范围内。
- /// 检测为False,开始移动。
- /// </summary>
- protected abstract bool CheckTargetInMaxRange();
- /// <summary>
- /// 行为改变
- /// </summary>
- protected virtual void onInRangeChanged()
- {
- }
- /// <summary>
- /// 目标已经被追踪到
- /// </summary>
- /// <param name="target"></param>
- protected virtual void onUpdateFollowed(IPositionObject target)
- {
- }
- /// <summary>
- /// 开始移动
- /// </summary>
- /// <param name="target"></param>
- protected virtual void onChangedToMove(IPositionObject target)
- {
- }
- /// <summary>
- /// changeToHold
- /// </summary>
- /// <param name="target"></param>
- protected virtual void onChangedToHold(IPositionObject target)
- {
- }
- }
- /// <summary>
- ///
- /// </summary>
- public class StateFollowObject : StateFollow
- {
- protected float distance;
- //protected InstanceZoneObject target;
- public override bool IsActive
- {
- get
- {
- var t = target as InstanceZoneObject;
- if (t != null)
- {
- return t.Enable;
- }
- return false;
- }
- }
- public float Distance
- {
- set
- {
- this.distance = value;
- }
- get
- {
- return this.distance;
- }
- }
- public StateFollowObject(InstanceUnit unit, InstanceZoneObject target, int holdTime = 0)
- : base(unit, target, true, holdTime)
- {
- //this.target = target;
- this.distance = Target.RadiusSize + unit.BodyBlockSize * 2;
- }
- protected override bool CheckTargetInMaxRange()
- {
- return CMath.includeRoundPoint(
- unit.X, unit.Y,
- distance * 2,
- Target.X, Target.Y);
- }
- protected override bool CheckTargetInMinRange()
- {
- return CMath.includeRoundPoint(
- unit.X, unit.Y,
- distance,
- Target.X, Target.Y);
- }
- }
- public class VStateFollowLeader : StateFollowObject
- {
- public VStateFollowLeader(InstanceUnit unit, InstanceZoneObject target, int holdTime = 0) : base(unit, target, holdTime)
- {
- }
- }
- /// <summary>
- ///
- /// </summary>
- public class StateFollowAndGuard : StateFollow
- {
- private readonly float distance_min;
- private readonly float distance_max;
- private readonly InstanceUnit targetUnit;
- public InstanceUnit TargetUnit { get { return targetUnit; } }
- public override bool IsActive { get { return targetUnit.Enable; } }
- public StateFollowAndGuard(InstanceUnit unit, InstanceUnit target, float minDistance, float maxDistance)
- : base(unit, target)
- {
- this.targetUnit = target;
- this.distance_min = Math.Max(minDistance, target.BodyBlockSize + unit.BodyBlockSize * 2);
- this.distance_max = Math.Max(maxDistance, distance_min);
- }
- protected override bool CheckTargetInMinRange()
- {
- return CMath.includeRoundPoint(
- unit.X, unit.Y,
- distance_min,
- Target.X, Target.Y);
- }
- protected override bool CheckTargetInMaxRange()
- {
- return CMath.includeRoundPoint(
- unit.X, unit.Y,
- distance_max,
- Target.X, Target.Y);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public class StateFollowAndAttack : StateFollow
- {
- /*readonly private*/
- InstanceUnit targetUnit;
- private SkillTemplate.CastTarget expectTarget;
- private TimeInterval<int> checkAdjustLaunchSkillTime;
- private SkillState launchSkill;
- private bool can_auto_focus_near_target;
- private bool can_random_skill = true;
- private bool can_attack = true;
- private float min_distance;
- private float max_distance;
- /// <summary>
- /// 是否追踪
- /// </summary>
- /// <returns></returns>
- override public bool IsActive
- {
- get { return can_attack && targetUnit.IsActive; }
- }
- public SkillState ExpectSkillState
- {
- get { return launchSkill; }
- set
- {
- launchSkill = value;
- expectTarget = value.Data.ExpectTarget;
- }
- }
- public SkillTemplate ExpectSkill
- {
- get { return (launchSkill != null) ? launchSkill.Data : unit.DefaultSkill; }
- }
- public InstanceUnit TargetUnit
- {
- get { return targetUnit; }
- set { targetUnit = value; }
- }
- public SkillTemplate.CastTarget ExpectTarget
- {
- get { return expectTarget; }
- }
- public bool IsLaunchRandomSkill
- {
- get { return can_random_skill; }
- set { can_random_skill = value; }
- }
- public bool IsAutoFocusNearTarget
- {
- get { return can_auto_focus_near_target; }
- set { can_auto_focus_near_target = value; }
- }
- public StateFollowAndAttack(
- InstanceUnit unit,
- InstanceUnit target,
- SkillTemplate.CastTarget expectTarget = SkillTemplate.CastTarget.Enemy,
- bool autoFocusNearTarget = false)
- : base(unit, target, false)
- {
- this.targetUnit = target;
- // Console.WriteLine(" - - - - StateFollowAndAttack - - - - " + this.TargetUnit.Name);
- this.expectTarget = expectTarget;
- this.can_auto_focus_near_target = autoFocusNearTarget;
- this.StartMoveHoldTimeMS = zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS;
- }
- protected override void onStart()
- {
- this.checkAdjustLaunchSkillTime = new TimeInterval<int>(zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS);
- this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
- base.onStart();
- }
- protected override void onUpdateFollowed(IPositionObject target)
- {
- this.can_attack = zone.IsAttackable(unit, targetUnit, expectTarget, AttackReason.Tracing, this.ExpectSkill);
- if (can_attack)
- {
- unit.faceTo(target.X, target.Y);
- if (TryLaunchSkill() == null)
- {
- SkillTemplate expect_skill = ExpectSkill;
- if (expect_skill != null && expect_skill.AttackKeepRange > 0)
- {
- if (checkAdjustLaunchSkillTime.IntervalTimeMS == 0 || checkAdjustLaunchSkillTime.Update(zone.UpdateIntervalMS))
- {
- if (CUtils.RandomPercent(zone.RandomN, zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_ADJUST_ESCAPE_PCT))
- {
- unit.startAdjustLaunchSkill(expect_skill, targetUnit, this.OnEndAdjustKeepRange);
- }
- }
- }
- }
- }
- }
- protected override void onUpdate()
- {
- if (IsActive)
- {
- resetMaxMinRange();
- }
- base.onUpdate();
- }
- private void resetMaxMinRange()
- {
- var expect_skill = ExpectSkillState;
- min_distance = unit.BodyBlockSize + targetUnit.BodyBlockSize;
- max_distance = Math.Max(min_distance, unit.BodyBlockSize + targetUnit.BodyHitSize);
- bool needCheck = true;
- if (expect_skill == null)
- {
- var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
- if (new_skill != null)
- {
- ExpectSkillState = new_skill;
- expect_skill = new_skill;
- needCheck = false;
- }
- }
- if (expect_skill != null)
- {
- if (can_random_skill)
- {
- if (needCheck == true && !expect_skill.checkTargetRange(targetUnit))
- {
- var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
- if (new_skill != null)
- {
- ExpectSkillState = new_skill;
- expect_skill = new_skill;
- }
- }
- }
- float skill_distance = unit.GetSkillAttackRange(expect_skill.Data);
- float half = (skill_distance - min_distance) / 2;
- if (expect_skill.Data.AttackKeepRange > 0 && expect_skill.Data.AttackRange > expect_skill.Data.AttackKeepRange)
- {
- min_distance = Math.Max(min_distance, unit.GetSkillAttackRange(expect_skill.Data.AttackKeepRange) + targetUnit.BodyBlockSize);
- max_distance = Math.Max(max_distance, min_distance);
- }
- else if (half > 0)
- {
- min_distance = min_distance + half;
- max_distance = Math.Max(max_distance, min_distance);
- }
- else
- {
- min_distance = Math.Max(min_distance, skill_distance + targetUnit.BodyBlockSize);
- max_distance = Math.Max(max_distance, min_distance);
- }
- max_distance = Math.Max(max_distance, skill_distance + targetUnit.BodyHitSize);
- }
- }
- protected override bool CheckTargetInMaxRange()
- {
- return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
- }
- protected override bool CheckTargetInMinRange()
- {
- return CMath.includeRoundPoint(unit.X, unit.Y, min_distance, targetUnit.X, targetUnit.Y);
- }
- protected virtual bool OnEndAdjustKeepRange(StateMove m)
- {
- unit.faceTo(Target.X, Target.Y);
- unit.changeState(this);
- return true;
- }
- protected virtual SkillTemplate TryLaunchSkill()
- {
- if (launchSkill == null)
- {
- this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
- }
- StateSkill ret = null;
- Vector2 targetPos = null;
- if (launchSkill != null)
- {
- ret = unit.launchSkill(launchSkill, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target));
- if (ret != null)
- {
- return ret.SkillData;
- }
- }
- if (can_random_skill)
- {
- ret = unit.launchRandomSkill(expectTarget, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target));
- if (ret != null)
- {
- return ret.SkillData;
- }
- }
- return null;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public class StateFollowAndPickObject : StateFollow
- {
- private readonly InstanceZoneObject targetObject;
- private readonly int pickTimeMS;
- private readonly StatePickObject.OnPickDone doneEvent;
- private readonly string status;
- public StateFollowAndPickObject(InstanceUnit unit, InstanceZoneObject target, int timeMS, StatePickObject.OnPickDone done, string status = null)
- : base(unit, target, false)
- {
- this.targetObject = target;
- this.pickTimeMS = timeMS;
- this.doneEvent = done;
- this.status = status;
- }
- public override bool IsActive
- {
- get { return targetObject.Enable; }
- }
- protected override bool CheckTargetInMinRange()
- {
- return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, targetObject.X, targetObject.Y);
- }
- protected override bool CheckTargetInMaxRange()
- {
- return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize + targetObject.BodyBlockSize, targetObject.X, targetObject.Y);
- }
- protected override void onUpdateFollowed(IPositionObject target)
- {
- unit.startPickProgressObject(targetObject, pickTimeMS, doneEvent, status);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public class StateFollowAndPickItem : StateFollow
- {
- public readonly InstanceItem targetObject;
- private long mLockEndTime;
- private bool mDoPick;
- public StateFollowAndPickItem(InstanceUnit unit, InstanceItem target, bool doPick)
- : base(unit, target, false)
- {
- this.targetObject = target;
- this.mDoPick = doPick;
- }
- public override bool IsActive
- {
- get { return (targetObject.Enable && targetObject.IsPosValid()) || mLockEndTime > CommonLang.CUtils.localTimeMS; }
- }
- protected override bool CheckTargetInMinRange()
- {
- return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, targetObject.X, targetObject.Y);
- }
- protected override bool CheckTargetInMaxRange()
- {
- return CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize + targetObject.BodyBlockSize, targetObject.X, targetObject.Y);
- }
- protected override void onUpdateFollowed(IPositionObject target)
- {
- if (this.mDoPick && this.targetObject.Enable)
- {
- targetObject.PickItem(unit);
- }
- }
- public void CheckAndLockPickTime()
- {
- if(!this.targetObject.Enable && mLockEndTime == 0)
- {
- mLockEndTime = CommonLang.CUtils.localTimeMS + 350;
- }
- }
- protected override void onUpdate()
- {
- this.CheckAndLockPickTime();
- if (!this.IsActive && unit.IsPlayer)
- {
- if (unit.DoAutoPick(true))
- {
- return;
- }
- }
-
- base.onUpdate();
- }
- protected override void OnMoveAiNoWay()
- {
- this.mLockEndTime = 0;
- targetObject.setPosNotValid();
- }
- public override bool onBlock(State new_state)
- {
- if(/*!(new_state is StateFollowAndPickItem) && */mLockEndTime > CommonLang.CUtils.localTimeMS)
- {
- return false;
- }
- //把当前拾取弄完,再去跟随
- if (new_state is VStateFollowLeader && unit.DoAutoPick(true))
- {
- return false;
- }
- return true;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public abstract class StateAttackTo : State
- {
- public IPositionObject Target { get { return target; } }
- public MoveBlockResult LastMovingResult { get; private set; }
- public MoveAI UnitMoveAI { get { return moveAI; } }
- // 如果被地图碰撞,则寻路
- private IPositionObject target;
- // 如果被单位碰撞,则向左或右挪动
- private MoveAI moveAI;
- private int holdTimeMS;
- private long mPeaceEndTime;
- public StateAttackTo(InstanceUnit unit, int move_ai_hold_time_ms = -1, int peaceTime = 0)
- : base(unit)
- {
- this.holdTimeMS = move_ai_hold_time_ms;
- this.mPeaceEndTime = CommonLang.CUtils.CurrentTimeMS + peaceTime;
- }
- override public bool onBlock(State new_state)
- {
- if(this.mPeaceEndTime > CommonLang.CUtils.CurrentTimeMS)
- {
- return false;
- }
- return true;
- }
- override protected void onStart()
- {
- if (target == null)
- {
- target = PopNextPos();
- }
- this.moveAI = new MoveAI(unit, true, holdTimeMS);
- if (target != null)
- {
- this.moveAI.FindPath(target);
- unit.SetActionStatus(UnitActionStatus.Move);
- }
- else
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- override protected void onUpdate()
- {
- if (target == null)
- {
- unit.doSomething();
- }
- else
- {
- LastMovingResult = this.moveAI.Update();
- if (TestPopNext())
- {
- this.target = PopNextPos();
- if (target != null)
- {
- this.moveAI.FindPath(target);
- }
- }
- }
- }
- override protected void onStop()
- {
- }
- abstract protected bool HasNextPos();
- abstract protected IPositionObject PopNextPos();
- protected virtual bool TestPopNext()
- {
- if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
- {
- return true;
- }
- return false;
- }
- public virtual bool IsDone
- {
- get
- {
- if (IsStarted)
- {
- if (target == null)
- {
- return true;
- }
- if (HasNextPos())
- {
- return false;
- }
- return TestPopNext();
- }
- return false;
- }
- }
- }
- public class StateAttackToZoneWayPoint : StateAttackTo
- {
- // 如果被地图碰撞,则寻路
- private ZoneWayPoint paths;
- private ZoneWayPoint prev;
- protected ZoneWayPoint Paths { get { return paths; } }
- public StateAttackToZoneWayPoint(InstanceUnit unit, ZoneWayPoint wps, int peaceTime)
- : base(unit, -1, peaceTime)
- {
- this.paths = wps;
- }
- protected override bool HasNextPos()
- {
- return paths != null;
- }
- protected override IPositionObject PopNextPos()
- {
- if (paths != null)
- {
- ZoneWayPoint ret = paths;
- ZoneWayPoint prv = prev;
- prev = paths;
- paths = paths.GetRandomNext(prv);
- return ret;
- }
- return null;
- }
- protected override bool TestPopNext()
- {
- if (LastMovingResult.obj != null)
- {
- if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, Target.X, Target.Y))
- {
- return true;
- }
- }
- else
- {
- if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, Target.X, Target.Y))
- {
- return true;
- }
- }
- return false;
- }
- }
- public class StatePatrolWayPoint : StateAttackTo
- {
- // 如果被地图碰撞,则寻路
- private ZoneWayPoint paths;
- private ZoneWayPoint prev;
- private TimeExpire<int> hold_time;
- private int hold_max_time_ms, hold_min_time_ms;
- protected ZoneWayPoint Paths { get { return paths; } }
- public StatePatrolWayPoint(InstanceUnit unit, ZoneWayPoint wps, int hold_max_time_ms, int hold_min_time_ms, int peaceTime)
- : base(unit, -1, peaceTime)
- {
- this.paths = wps;
- this.hold_max_time_ms = Math.Max(hold_max_time_ms, hold_min_time_ms);
- this.hold_min_time_ms = Math.Min(hold_max_time_ms, hold_min_time_ms);
- }
- protected override void onUpdate()
- {
- if (this.hold_time != null)
- {
- if (this.hold_time.Update(zone.UpdateIntervalMS))
- {
- this.hold_time = null;
- }
- }
- else
- {
- base.onUpdate();
- }
- }
- protected override bool HasNextPos()
- {
- return paths != null;
- }
- private void Hold()
- {
- if (hold_max_time_ms > 0)
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- this.hold_time = new TimeExpire<int>(unit.RandomN.Next(hold_min_time_ms, hold_max_time_ms));
- }
- }
- protected override IPositionObject PopNextPos()
- {
- if (paths != null)
- {
- ZoneWayPoint ret = paths;
- ZoneWayPoint prv = prev;
- prev = paths;
- paths = paths.GetRandomNext(prv);
- Hold();
- return ret;
- }
- return null;
- }
- protected override bool TestPopNext()
- {
- if (LastMovingResult.obj != null)
- {
- if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, Target.X, Target.Y))
- {
- return true;
- }
- }
- else
- {
- if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, Target.X, Target.Y))
- {
- return true;
- }
- }
- return false;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public class StateGuardInPosition : State
- {
- public Vector2 Target { get { return target; } }
- public MoveBlockResult LastMovingResult { get; private set; }
- // 如果被地图碰撞,则寻路
- private Vector2 target;
- // 如果被单位碰撞,则向左或右挪动
- private MoveAI moveAI;
- public StateGuardInPosition(InstanceUnit unit, Vector2 target)
- : base(unit)
- {
- this.target = target;
- this.moveAI = new MoveAI(unit);
- }
- override public bool onBlock(State new_state)
- {
- return true;
- }
- override protected void onStart()
- {
- this.moveAI.FindPath(target.X, target.Y);
- if (moveAI.IsNoWay)
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- else
- {
- unit.SetActionStatus(UnitActionStatus.Move);
- }
- }
- override protected void onUpdate()
- {
- this.LastMovingResult = this.moveAI.Update();
- if ((LastMovingResult.result & MoveResult.RESULTS_MOVE_END) != 0)
- {
- if ((LastMovingResult.result & MoveResult.MOVE_RESULT_BLOCK_MAP) != 0)
- {
- moveAI.Pause();
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- else if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
- {
- moveAI.Pause();
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- }
- }
- override protected void onStop()
- {
- }
- }
- /// <summary>
- ///
- /// </summary>
- public class StateBackToPosition : State
- {
- public Vector2 Target { get { return target; } }
- public MoveBlockResult LastMovingResult { get; private set; }
- public bool IsDone { get { return isDone; } }
- // 如果被地图碰撞,则寻路
- private Vector2 target;
- // 如果被单位碰撞,则向左或右挪动
- private MoveAI moveAI;
- private bool isDone = false;
- public StateBackToPosition(InstanceUnit unit, Vector2 target)
- : base(unit)
- {
- this.target = target;
- this.moveAI = new MoveAI(unit);
- }
- override public bool onBlock(State new_state)
- {
- if (new_state is StateDamage)
- {
- return true;
- }
- if (new_state is StateDead)
- {
- return true;
- }
- return isDone;
- }
- override protected void onStart()
- {
- this.unit.onBack2PositionStart();
- this.moveAI.FindPath(target.X, target.Y);
- if (moveAI.IsNoWay)
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- else
- {
- unit.SetActionStatus(UnitActionStatus.Move);
- }
- }
- override protected void onUpdate()
- {
- this.LastMovingResult = this.moveAI.Update();
- if ((LastMovingResult.result & MoveResult.RESULTS_MOVE_END) != 0)
- {
- if (CMath.includeRoundPoint(unit.X, unit.Y, unit.Info.GuardRange, target.X, target.Y))
- {
- isDone = true;
- unit.doSomething();
- }
- else
- {
- moveAI.FindPath(target.X, target.Y);
- }
- }
- }
- override protected void onStop()
- {
- this.unit.onBack2PositionEnd();
- }
- }
- protected virtual void onBack2PositionEnd()
- {
- }
- protected virtual void onBack2PositionStart()
- {
- }
- #endregion
- //---------------------------------------------------------------------------------------
- #region __强制剧情播放__
- public abstract class ForceState : State
- {
- public ForceState(InstanceUnit unit)
- : base(unit)
- {
- }
- override public bool onBlock(State new_state)
- {
- if (new_state is StateDead) return true;
- if (new_state is StateRebirth) return true;
- if (new_state is StateStun) return true;
- return unit.IsDead();
- }
- }
- /// <summary>
- /// 强制移动到目标点,一般用于剧情动画
- /// </summary>
- public class ForceStateMoveTo : ForceState
- {
- private readonly float targetX;
- private readonly float targetY;
- private bool isEnd = false;
- private MoveAI moveAI;
- public ForceStateMoveTo(InstanceUnit unit, float x, float y)
- : base(unit)
- {
- this.targetX = x;
- this.targetY = y;
- }
- override public bool onBlock(State new_state)
- {
- if (base.onBlock(new_state)) return true;
- return isEnd;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Move);
- this.moveAI = new MoveAI(unit);
- this.moveAI.FindPath(targetX, targetY);
- }
- override protected void onUpdate()
- {
- if (!isEnd)
- {
- unit.faceTo(targetX, targetY);
- MoveBlockResult result = moveAI.Update();
- if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
- {
- isEnd = true;
- unit.doSomething();
- }
- else if ((result.result & MoveResult.RESULTS_MOVE_END) != 0)
- {
- float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
- if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
- {
- isEnd = true;
- unit.doSomething();
- }
- }
- else
- {
- float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
- if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
- {
- isEnd = true;
- unit.doSomething();
- }
- }
- }
- }
- override protected void onStop()
- {
- }
- }
- public class ForceStateLaunchSkill : ForceState
- {
- private readonly int SkillID;
- private readonly bool IsRandom;
- private readonly StateStopHandler SkillOver;
- private StateSkill mSkillState;
- public ForceStateLaunchSkill(InstanceUnit unit, int skillID, bool random, StateStopHandler over = null)
- : base(unit)
- {
- this.SkillID = skillID;
- this.IsRandom = random;
- this.SkillOver = over;
- }
- public override bool onBlock(State new_state)
- {
- if (base.onBlock(new_state)) return true;
- if (new_state is StateSkill)
- {
- return true;
- }
- return mSkillState != null;
- }
- protected override void onStart()
- {
- }
- protected override void onUpdate()
- {
- if (IsRandom)
- {
- mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
- }
- else
- {
- mSkillState = unit.launchSkill(SkillID, new InstanceUnit.LaunchSkillParam());
- }
- if (mSkillState == null)
- {
- mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
- }
- if (mSkillState != null && SkillOver != null)
- {
- mSkillState.AddStopOnce(SkillOver);
- }
- }
- protected override void onStop()
- {
- if (mSkillState == null && SkillOver != null)
- {
- SkillOver.Invoke(unit, this);
- }
- }
- }
- public class ForceStateIdleTime : ForceState
- {
- private readonly TimeExpire<int> mIdleTime;
- public ForceStateIdleTime(InstanceUnit unit, float timeSEC)
- : base(unit)
- {
- mIdleTime = new TimeExpire<int>((int)(timeSEC * 1000));
- }
- override public bool onBlock(State new_state)
- {
- if (base.onBlock(new_state)) return true;
- return mIdleTime.IsEnd;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- override protected void onUpdate()
- {
- if (mIdleTime.Update(zone.UpdateIntervalMS))
- {
- unit.doSomething();
- }
- }
- override protected void onStop() { }
- }
- public class ForceStateActionTime : ForceStateIdleTime
- {
- private readonly string ActionName;
- public ForceStateActionTime(InstanceUnit unit, float timeSEC, string actionName)
- : base(unit, timeSEC)
- {
- this.ActionName = actionName;
- }
- override protected void onStart()
- {
- unit.queueEvent(new UnitDoActionEvent(unit.ID, ActionName));
- }
- }
- #endregion
- }
- }
|