123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using CommonLang;
- using CommonAI.Zone.Helper;
- using CommonAI.Zone.Formula;
- using CommonAI.Zone.UnitTriggers;
- using CommonAI.Zone.Attributes;
- using CommonLang.Property;
- using CommonAI.RTS;
- using CommonLang.Vector;
- using CommonAI.ZoneClient;
- using CommonAI.data;
- using static CommonAI.Zone.LaunchSpell;
- using CommonAI.Data;
- namespace CommonAI.Zone.Instance
- {
- public delegate int ReadBuffDelegate(InstanceUnit.BuffState buffState);
- /// <summary>
- /// 所有常态状态(Buff,技能,被动系)
- /// </summary>
- partial class InstanceUnit
- {
- //--------------------------------------------------------------------------------------------------------------
- #region _时效性效果_
- //--------------------------------------------------------------------------------------------------------------
- /// <summary>
- /// 是否可被发现
- /// </summary>
- virtual public bool IsVisible { get { return !mInvisibleTimeMS.Enable;/* && !DeadHide; */} }
- /// <summary>
- /// 是否无敌
- /// </summary>
- virtual public bool IsInvincible { get { return mInvincibleTimeMS.Enable; } }
- /// <summary>
- /// 此单位是否霸体
- /// </summary>
- virtual public bool IsNoneBlock { get { return mNoneBlockTimeMS.Enable; } }
- /// <summary>
- /// 是否为眩晕
- /// </summary>
- virtual public bool IsStun { get { return mStunTimeMS.Enable; } }
- /// <summary>
- /// 是否沉默
- /// </summary>
- virtual public bool IsSilent { get { return mSilentTimeMS.Enable; } }
- /// <summary>
- /// 是否免控
- /// </summary>
- virtual public bool IsIgnoreControl { get { return mIgnoreControl.Enable; } }
- /** 是否不允许移动 */
- public bool MarkCannotMove = false;
- virtual public bool IsCannotMove { get { return mCannotMoveTimeMS.Enable; } }
- //死亡隐藏
- private bool mDeadHide = false;
- public bool DeadHide{ get { return mDeadHide; } set { mDeadHide = value; } }
- public bool IsInPVP() { return this.Virtual.IsInPVP(); }
- public bool IsInPVE() { return this.Virtual.IsInPVE(); }
- virtual public string PlayerUUID
- {
- get { return ""; }
- }
- //--------------------------------------------------------------------------------------------------------------
- private List<MultiTimeLine> mMultiTimeLineGroup = new List<MultiTimeLine>();
- private UnitSyncMultiTimeLine mMultiTimeLineSync;
- /// <summary>
- /// 注册自定义MultiTimeLine
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public MultiTimeLine RegistMultiTimeLine(out int index)
- {
- index = mMultiTimeLineGroup.Count;
- var timeline = new MultiTimeLine(MultiTimeLineRefresh);
- mMultiTimeLineGroup.Add(timeline);
- return timeline;
- }
- ///// <summary>
- ///// 获取指定ID TimeLine
- ///// </summary>
- ///// <param name="index"></param>
- ///// <returns></returns>
- //public MultiTimeLine GetTimeLine(int index)
- //{
- // return mMultiTimeLineGroup[index];
- //}
- public bool IsMoveAble()
- {
- return this.Info.IsMoveable && this.Info.MoveSpeedSEC > 0;
- }
- /// <summary>
- /// 指定TimeLine是否还有任务
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public bool IsTimeLineEnable(int index)
- {
- return mMultiTimeLineGroup[index].Enable;
- }
- /// <summary>
- /// 获取指定 TimeLine 的 ID
- /// </summary>
- /// <param name="timeline"></param>
- /// <returns></returns>
- public int GetTimeLineIndex(MultiTimeLine timeline)
- {
- return mMultiTimeLineGroup.IndexOf(timeline);
- }
- //是否跟随队长
- public virtual bool IsFollowMaster() { return false; }
- /// <summary>
- /// 添加TimeLine任务
- /// </summary>
- /// <param name="index"></param>
- /// <param name="timeMS"></param>
- /// <returns></returns>
- public TimeExpire<int> AddTimeLineTask(int index, int timeMS)
- {
- return mMultiTimeLineGroup[index].Add(timeMS);
- }
- /// <summary>
- /// 强制移除TimeLine任务
- /// </summary>
- /// <param name="index"></param>
- /// <param name="task"></param>
- /// <returns></returns>
- public bool RemoveTimeLineTask(int index, TimeExpire<int> task)
- {
- return mMultiTimeLineGroup[index].Remove(task);
- }
- //--------------------------------------------------------------------------------------------------------------
- /// <summary>
- /// 被击中后不会被中断(霸体),是一个叠加值
- /// </summary>
- private MultiTimeLine mNoneBlockTimeMS;
- /// <summary>
- /// 眩晕,移动限制状态,是一个叠加值
- /// </summary>
- private MultiTimeLine mStunTimeMS;
- /// <summary>
- /// 隐形,是一个叠加值
- /// </summary>
- private MultiTimeLine mInvisibleTimeMS;
- /// <summary>
- /// 无敌时间,是个叠加值
- /// </summary>
- private MultiTimeLine mInvincibleTimeMS;
- /// <summary>
- /// 沉默时间,是个叠加值
- /// </summary>
- private MultiTimeLine mSilentTimeMS;
- /// <summary>
- /// 沉默时间,是个叠加值
- /// </summary>
- private MultiTimeLine mCannotMoveTimeMS;
- /// <summary>
- /// 免控时间
- /// </summary>
- private MultiTimeLine mIgnoreControl;
- //是否刷新标记
- private bool mNeedUpdate = false;
- public bool IsCanLaunchSkill(int skillID)
- {
- if (this.IsStun || (this.IsSilent == true && skillID != this.Virtual.GetBaseSkillID()))
- {
- return false;
- }
- return true;
- }
- private void InitTimeLines()
- {
- int index;
- mNoneBlockTimeMS = RegistMultiTimeLine(out index);
- mStunTimeMS = RegistMultiTimeLine(out index);
- mInvisibleTimeMS = RegistMultiTimeLine(out index);
- mInvincibleTimeMS = RegistMultiTimeLine(out index);
- mSilentTimeMS = RegistMultiTimeLine(out index);
- mCannotMoveTimeMS = RegistMultiTimeLine(out index);
- mIgnoreControl = RegistMultiTimeLine(out index);
- mMultiTimeLineSync = new UnitSyncMultiTimeLine(this.ID);
- }
- private void UpdateTimeLines(int intervalMS)
- {
- if(mMultiTimeLineGroup == null || !mNeedUpdate)
- {
- return;
- }
- bool hasUpdate = false;
- for (int i = mMultiTimeLineGroup.Count - 1; i >= 0; --i)
- {
- if (mMultiTimeLineGroup[i].Update(intervalMS))
- {
- hasUpdate = true;
- }
- }
- // 全部timer都没有,不刷新
- if (!hasUpdate)
- {
- mNeedUpdate = false;
- }
- //不能移动单独刷新,此状态需要通知客户端
- if(this.MarkCannotMove && !this.IsCannotMove)
- {
- this.MarkCannotMove = false;
- this.RemoveActionSubState(UnitActionSubStatus.CanNotMove);
- }
- if (mMultiTimeLineSync.Update(mMultiTimeLineGroup))
- {
- queueEvent(mMultiTimeLineSync);
- }
- }
- public void MultiTimeLineRefresh(bool isAdd)
- {
- this.mNeedUpdate = true;
- }
- /// <summary>
- /// 设置霸体时间,如果当前已霸体,则取最大值
- /// </summary>
- /// <param name="timeMS"></param>
- public TimeExpire<int> SetNoneBlockTimeMS(int timeMS)
- {
- return mNoneBlockTimeMS.Add(timeMS);
- }
- /// <summary>
- /// 设置眩晕时间,如果当前已眩晕,则取最大值
- /// </summary>
- /// <param name="timeMS"></param>
- public TimeExpire<int> SetStunTimeMS(int timeMS)
- {
- TimeExpire<int> ret = mStunTimeMS.Add(timeMS);
- if (!IsStateDead)
- {
- changeState(new StateStun(this));
- }
- return ret;
- }
- /// <summary>
- /// 设置隐身时间,如果当前已隐身,则取最大值
- /// </summary>
- /// <param name="timeMS"></param>
- public TimeExpire<int> SetInvisibleTimeMS(int timeMS)
- {
- return mInvisibleTimeMS.Add(timeMS);
- }
- /// <summary>
- /// 设置无敌时间,如果当前已无敌,则取最大值
- /// </summary>
- /// <param name="timeMS"></param>
- public TimeExpire<int> SetInvincibleTimeMS(int timeMS)
- {
- return mInvincibleTimeMS.Add(timeMS);
- }
- /// <summary>
- /// 设置沉默时间
- /// </summary>
- /// <param name="timeMS"></param>
- public TimeExpire<int> SetSilentTimeMS(int timeMS)
- {
- return mSilentTimeMS.Add(timeMS);
- }
- /** 设置无法移动时间 */
- public TimeExpire<int> SetCannotMoveTimeMS(int timeMS)
- {
- this.MarkCannotMove = true;
- this.AddActionSubState(UnitActionSubStatus.CanNotMove);
- return mCannotMoveTimeMS.Add(timeMS);
- }
- /// <summary>
- /// 设置免控时间
- /// </summary>
- /// <param name="timeMS"></param>
- public TimeExpire<int> SetIgnoreControlTimeMS(int timeMS)
- {
- return mIgnoreControl.Add(timeMS);
- }
- public bool RemoveNoneBlock(TimeExpire<int> task)
- {
- return mNoneBlockTimeMS.Remove(task);
- }
- public bool RemoveStun(TimeExpire<int> task)
- {
- return mStunTimeMS.Remove(task);
- }
- public bool RemoveInvisible(TimeExpire<int> task)
- {
- return mInvisibleTimeMS.Remove(task);
- }
- public bool RemoveInvincible(TimeExpire<int> task)
- {
- return mInvincibleTimeMS.Remove(task);
- }
- public bool RemoveSilent(TimeExpire<int> task)
- {
- return mSilentTimeMS.Remove(task);
- }
- public bool RemoveCannotMove(TimeExpire<int> task)
- {
- return mCannotMoveTimeMS.Remove(task);
- }
- public bool RemoveIgnoreControl(TimeExpire<int> task)
- {
- return mIgnoreControl.Remove(task);
- }
- public void ClearNoneBlock()
- {
- mNoneBlockTimeMS.Clear();
- }
- public void ClearStun()
- {
- mStunTimeMS.Clear();
- }
- public void ClearInvisible()
- {
- mInvisibleTimeMS.Clear();
- }
- public void ClearInvincible()
- {
- mInvincibleTimeMS.Clear();
- }
- public void ClearSilent()
- {
- mSilentTimeMS.Clear();
- }
- public void ClearCannotMove()
- {
- mCannotMoveTimeMS.Clear();
- }
- public void ClearIgnoreControl()
- {
- mIgnoreControl.Clear();
- }
- #endregion
- //-----------------------------------------------------------------------------------------------------//
- //-----------------------------------------------------------------------------------------------
- #region _技能状态类_
- public PlayerSkillChangedEvent GetSkillEvent()
- {
- PlayerSkillChangedEvent evt = new PlayerSkillChangedEvent(ID);
- evt.unitFastCastRate = this.mFastCastRate;
- if (mDefaultSkill != null)
- {
- evt.baseSkill = mDefaultSkill.Data;
- }
- foreach (SkillState sk in mSkillStatus.Skills)
- {
- if (sk.GetSkillType() != XmdsSkillType.cardSkill && (evt.baseSkill == null || sk.Data.TemplateID != evt.baseSkill.ID))
- {
- sk.Data.CurUseTimes = (short)sk.OveryLayer;
- evt.skills.Add(sk.Data);
- }
- }
- return evt;
- }
- public ClientStruct.UnitSkillStatus[] GetCurrentSkillStatus()
- {
- ClientStruct.UnitSkillStatus[] ret = new ClientStruct.UnitSkillStatus[mSkillStatus.Count];
- int i = 0;
- foreach (SkillState sk in mSkillStatus.Skills)
- {
- ret[i].SkillTemplateID = (sk.Data.TemplateID);
- ret[i].PassTime = (sk.PassTime);
- ret[i].useTimes = sk.OveryLayer;
- i++;
- }
- return ret;
- }
- /// <summary>
- /// 初始化技能可用性
- /// </summary>
- public PlayerSkillActiveChangedEvent GetSyncSkillActives()
- {
- PlayerSkillActiveChangedEvent mSyncSkillActives = new PlayerSkillActiveChangedEvent(this.ID, mSkillStatus.Count);
- foreach (SkillState st in mSkillStatus.Skills)
- {
- PlayerSkillActiveChangedEvent.State sat = new PlayerSkillActiveChangedEvent.State();
- sat.SkillTemplateID = st.ID;
- sat.ST = st.ActiveState;
- mSyncSkillActives.Skills.Add(sat);
- }
- return mSyncSkillActives;
- }
- private SkillMap mSkillStatus = new SkillMap();
- private SkillState mDefaultSkill;
- private List<SkillTemplate> mAllSkills = new List<SkillTemplate>();
- private bool mSyncSkillActivesChanged = false;
- private void updateSyncSkillActives()
- {
- if (mSyncSkillActivesChanged)
- {
- mSyncSkillActivesChanged = false;
- queueEvent(GetSyncSkillActives());
- }
- }
- protected bool DoTryAddSkill(ref SkillTemplate sk)
- {
- bool ret = true;
- if (mOnTryAddSkill != null)
- {
- foreach (TryAddSkill tryadd in mOnTryAddSkill.GetInvocationList())
- {
- if (!tryadd.Invoke(this, ref sk))
- {
- ret = false;
- }
- }
- }
- return ret;
- }
- /// <summary>
- /// 重置当前单位技能,当前单位技能重置为单位模板指定技能
- /// </summary>
- public void ResetSkills()
- {
- if (mFormula.TryResetSkill(this))
- {
- InitSkills(mInfo.BaseSkillID, mInfo.Skills.ToArray());
- }
- }
- /// <summary>
- /// 设置当前单位技能
- /// </summary>
- /// <param name="baseSkill"></param>
- /// <param name="skills"></param>
- public virtual void InitSkills(LaunchSkill baseSkill, LaunchSkill[] skills)
- {
- List<SkillState> exist = new List<SkillState>(mSkillStatus.Skills);
- mSyncSkillActivesChanged = true;
- mDefaultSkill = null;
- mSkillStatus.Clear();
- mAllSkills.Clear();
- foreach (SkillState st in exist)
- {
- if (mOnSkillRemoved != null) { mOnSkillRemoved.Invoke(this, st); }
- }
- if (baseSkill != null)
- {
- SkillTemplate st = Templates.getSkill(baseSkill.SkillID);
- if (DoTryAddSkill(ref st))
- {
- if (st != null && !mSkillStatus.ContainsKey(baseSkill.SkillID))
- {
- mDefaultSkill = new SkillState(st, baseSkill, this);
- mAllSkills.Add(st);
- mSkillStatus.Add(mDefaultSkill);
- if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, mDefaultSkill); }
- }
- }
- }
- foreach (LaunchSkill lsk in skills)
- {
- SkillTemplate stt = Templates.getSkill(lsk.SkillID);
- if (DoTryAddSkill(ref stt))
- {
- if (stt != null && !mSkillStatus.ContainsKey(lsk.SkillID))
- {
- SkillState sk = new SkillState(stt, lsk, this);
- mAllSkills.Add(stt);
- mSkillStatus.Add(sk);
- if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, sk); }
- }
- }
- }
- if (IsInZone)
- {
- Parent.queueObjectEvent(this, GetSkillEvent());
- }
- if (mOnSkillChanged != null)
- {
- mOnSkillChanged.Invoke(this, mDefaultSkill, mSkillStatus.SkillsArray);
- }
- }
- /// <summary>
- /// 设置当前单位技能
- /// </summary>
- /// <param name="baseSkill"></param>
- /// <param name="skills"></param>
- public void InitSkills(SkillTemplate baseSkill, SkillTemplate[] skills)
- {
- List<SkillState> exist = new List<SkillState>(mSkillStatus.Skills);
- mSyncSkillActivesChanged = true;
- mDefaultSkill = null;
- mSkillStatus.Clear();
- mAllSkills.Clear();
- foreach (SkillState st in exist)
- {
- if (mOnSkillRemoved != null) { mOnSkillRemoved.Invoke(this, st); }
- }
- if (baseSkill != null)
- {
- if (DoTryAddSkill(ref baseSkill))
- {
- if (baseSkill != null && !mSkillStatus.ContainsKey(baseSkill.ID))
- {
- mDefaultSkill = new SkillState(baseSkill, new LaunchSkill(baseSkill.ID), this);
- mAllSkills.Add(baseSkill);
- mSkillStatus.Add(mDefaultSkill);
- if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, mDefaultSkill); }
- }
- }
- }
- foreach (SkillTemplate ssk in skills)
- {
- SkillTemplate stt = ssk;
- if (DoTryAddSkill(ref stt))
- {
- if (stt != null && !mSkillStatus.ContainsKey(stt.ID))
- {
- LaunchSkill lsk = null;
- foreach(LaunchSkill _lsk in mInfo.Skills)
- {
- if (_lsk.SkillID == stt.ID)
- {
- lsk = _lsk;
- break;
- }
- }
- if (lsk == null)
- lsk = new LaunchSkill(stt.ID);
- SkillState state = new SkillState(stt, lsk, this);
- mAllSkills.Add(stt);
- mSkillStatus.Add(state);
- if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, state); }
- }
- }
- }
- if (IsInZone)
- {
- Parent.queueObjectEvent(this, GetSkillEvent());
- }
- if (mOnSkillChanged != null)
- {
- mOnSkillChanged.Invoke(this, mDefaultSkill, mSkillStatus.SkillsArray);
- }
- }
- public SkillState AddSkill(SkillTemplate st, bool autoLaunch, bool is_default = false)
- {
- if (DoTryAddSkill(ref st))
- {
- if (st != null && !mSkillStatus.ContainsKey(st.ID))
- {
- mSyncSkillActivesChanged = true;
- SkillState state = new SkillState(st, new LaunchSkill(st.ID, autoLaunch), this);
- mAllSkills.Add(st);
- mSkillStatus.Add(state);
- if (is_default)
- {
- mDefaultSkill = state;
- }
- if (IsInZone)
- {
- st.CurUseTimes = (short)state.OveryLayer;
- Parent.queueObjectEvent(this, new PlayerSkillAddedEvent(ID, st, is_default));
- }
- if (mOnSkillAdded != null) { mOnSkillAdded.Invoke(this, state); }
- if (mOnSkillChanged != null) { mOnSkillChanged.Invoke(this, mDefaultSkill, mSkillStatus.SkillsArray); }
- return state;
- }
- }
- return null;
- }
- public SkillState RemoveSkill(int skillTemplateID)
- {
- SkillState state = mSkillStatus.RemoveByKey(skillTemplateID);
- if (state != null)
- {
- mSyncSkillActivesChanged = true;
- mAllSkills.RemoveAll((t) => { return (t.TemplateID == skillTemplateID); });
- if (state == mDefaultSkill)
- {
- mDefaultSkill = null;
- }
- if (IsInZone) { Parent.queueObjectEvent(this, new PlayerSkillRemovedEvent(ID, skillTemplateID)); }
- if (mOnSkillRemoved != null) { mOnSkillRemoved.Invoke(this, state); }
- if (mOnSkillChanged != null) { mOnSkillChanged.Invoke(this, mDefaultSkill, mSkillStatus.SkillsArray); }
- }
- return state;
- }
- public SkillTemplate DefaultSkill
- {
- get
- {
- if (mDefaultSkill != null)
- {
- return mDefaultSkill.Data;
- }
- return null;
- }
- }
- public SkillState DefaultSkillStatus()
- {
- return mDefaultSkill;
- }
- public IEnumerable<SkillState> SkillStatus
- {
- get
- {
- return mSkillStatus.Skills;
- }
- }
- public SkillTemplate getSkill(int skillID)
- {
- SkillState st = mSkillStatus.Get(skillID);
- if (st != null)
- {
- return st.Data;
- }
- return null;
- }
- public SkillState getSkillState(int skillID)
- {
- SkillState st = mSkillStatus.Get(skillID);
- if (st != null)
- {
- return st;
- }
- return null;
- }
- public void onSkillHitTarget()
- {
- if (mOnSkillHitTarger != null) mOnSkillHitTarger.Invoke();
- }
- /// <summary>
- /// 获得当前随机可释放的技能
- /// </summary>
- /// <param name="expectTarget"></param>
- /// <returns></returns>
- public virtual SkillState getRandomLaunchableExpectSkill(SkillTemplate.CastTarget expectTarget)
- {
- int rand = RandomN.Next(0, mAllSkills.Count);
- for (int si = mAllSkills.Count - 1; si >= 0; --si)
- {
- SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
- if (st.ExpectTarget == expectTarget)
- {
- SkillState sst = mSkillStatus.Get(st.GetID());
- if (sst.LaunchSkill.AutoLaunch && sst.TryLaunch() && sst.CanAutoLaunch())
- {
- return sst;
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 获得当前随机可释放的技能
- /// </summary>
- /// <param name="target"></param>
- /// <param name="expectTarget"></param>
- /// <param name="reason"></param>
- /// <param name="checkRange"></param>
- /// <returns></returns>
- public virtual SkillState getRandomLaunchableExpectSkill(InstanceUnit target, SkillTemplate.CastTarget expectTarget, AttackReason reason = AttackReason.Tracing, bool checkRange = false)
- {
- int rand = RandomN.Next(0, mAllSkills.Count);
- for (int si = mAllSkills.Count - 1; si >= 0; --si)
- {
- SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
- if (st.ExpectTarget == expectTarget)
- {
- SkillState sst = mSkillStatus.Get(st.GetID());
- if (sst.LaunchSkill.AutoLaunch && sst.TryLaunch() && Parent.IsAttackable(this, target, expectTarget, reason, st))
- {
- if (!checkRange || sst.checkTargetRange(target))
- {
- return sst;
- }
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 获得当前随机可释放的技能
- /// </summary>
- /// <returns></returns>
- public virtual SkillState getRandomLaunchableSkill()
- {
- int rand = RandomN.Next(0, mAllSkills.Count);
- for (int si = mAllSkills.Count - 1; si >= 0; --si)
- {
- SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
- SkillState sst = mSkillStatus.Get(st.GetID());
- if (sst.TryLaunch())
- {
- return sst;
- }
- }
- return null;
- }
- public void getKeepSkills(List<int> keeps, List<LaunchSkill> ret)
- {
- foreach (SkillState st in mSkillStatus.Skills)
- {
- if (keeps.Contains(st.ID))
- {
- ret.Add(st.LaunchSkill);
- }
- }
- }
- private void updateSkills()
- {
- //int intervalMS = Parent.UpdateIntervalMS;
- //for (int i = 0; i < mSkillStatus.Count; i++)
- //{
- // SkillState st = mSkillStatus.GetAt(i);
- // st.Update(intervalMS);
- //}
- mSkillStatus.update(Parent.UpdateIntervalMS);
- }
- protected virtual void clearSkills()
- {
- this.mSkillStatus.Clear();
- }
- // Skills
- class SkillMap
- {
- private HashMap<int, SkillState> Map = new HashMap<int, SkillState>();
- private List<SkillState> For = new List<SkillState>();
- public int Count { get { return For.Count; } }
- public IEnumerable<SkillState> Skills { get { return For; } }
- public SkillState[] SkillsArray { get { return For.ToArray(); } }
- public SkillState GetAt(int i)
- {
- return For[i];
- }
- public SkillState Get(int id)
- {
- return Map.Get(id);
- }
- public void Add(SkillState state)
- {
- if (!Map.ContainsKey(state.ID))
- {
- Map.Put(state.ID, state);
- For.Add(state);
- }
- }
- public SkillState RemoveByKey(int id)
- {
- SkillState ret = Map.RemoveByKey(id);
- if (ret != null)
- {
- For.Remove(ret);
- }
- return ret;
- }
- public bool ContainsKey(int id)
- {
- return Map.ContainsKey(id);
- }
- public void Clear()
- {
- Map.Clear();
- For.Clear();
- }
- public void update(int intervalMS)
- {
- foreach (SkillState st in this.For)
- {
- st.Update(intervalMS);
- }
- }
- }
- public delegate void ISkillCDStatusChange(SkillState skill, bool iscd);
- public class SkillState
- {
- // 技能CD状态更改监听
- readonly private List<ISkillCDStatusChange> mSkillCDStatusChange = new List<ISkillCDStatusChange>();
- readonly public SkillTemplate Data;
- readonly public LaunchSkill LaunchSkill;
- readonly public InstanceUnit Owner;
- private SkillActiveState current_state = SkillActiveState.Active;
- private int total_cd_time;
- private int decrease_total_timems;
- private int trigger_decrease_times; //机制触发,减少CD
- // 叠层
- private int overLayer = -1;
- /// <summary>
- /// 从技能开始时的逝去时间
- /// </summary>
- private int pass_time;
- /// <summary>
- /// 最后CD完成时间
- /// </summary>
- private int stop_time;
- /// <summary>
- /// 如果是多段攻击,记录段数
- /// </summary>
- private int action_step = -1;
- private int lockActionStep = -1; //锁定执行序列号
- // 是否是多段式技能
- private bool mCanStoreTimes = false;
- /// <summary>
- /// 多段攻击自动下段
- /// </summary>
- private bool auto_increase_action_step = true;
- /// <summary>
- /// 当前是否在冷却
- /// </summary>
- private bool is_cd = false;
- /// <summary>
- /// 当前是否在多段攻击允许时间内
- /// </summary>
- private bool is_in_mutil_time = true;
- /// <summary>
- /// 当前释放技能时的状态机
- /// </summary>
- private StateSkill state;
- private float action_speed;
- public int ID { get { return Data.ID; } }
- /// <summary>
- /// 当前技能是否在转CD
- /// </summary>
- public bool IsCD { get { return is_cd; } }
- public SkillActiveState ActiveState { get { return current_state; } }
- public bool IsActive { get { return current_state == SkillActiveState.Active || current_state == SkillActiveState.ActiveAndHide; } }
- public bool IsPauseOnDeactive { get { return current_state == SkillActiveState.DeactiveAndPause; } }
- public XmdsSkillType GetSkillType() { return this.Data.Properties.GetSkillType(); }
- //是否是多段式技能
- private byte mMaxMutilStep = 0;
- //代码逻辑控制cd
- private bool mCodeControl_HasNext;
-
- /// <summary>
- /// 当前技能是否放完
- /// </summary>
- public bool IsDone
- {
- get
- {
- if (Data.IsCoolDownWithAction)
- {
- if (state == null)
- return true;
- if (state.IsDone)
- return true;
- if (state.IsCancelableBySkill)
- return true;
- return false;
- }
- else if (this.IsMutilAction() && this.IsHasNext())//多段式攻击
- {
- if (is_in_mutil_time)
- {
- return true;
- }
- }
- return !is_cd;
- }
- }
- public bool CanAutoLaunch()
- {
- return this.stop_time == 0 || this.GetSkillType() == XmdsSkillType.normalAtk || this.stop_time + 2000 < this.pass_time || this.IsInMutilTime;
- }
- public bool IsCodeConttrolCD()
- {
- return this.mCodeControl_HasNext;
- }
- /// <summary>
- /// CD 需要的总时间
- /// </summary>
- public int TotalCDTime
- {
- get { return total_cd_time - this.trigger_decrease_times; } set { total_cd_time = value; }
- }
- /// <summary>
- /// CD 需要的总时间
- /// </summary>
- public bool IsInMutilTime
- {
- get { return is_in_mutil_time; } set { is_in_mutil_time = value; }
- }
- public void ResetTotalTime()
- {
- this.total_cd_time = Data.ToUnitSkillTotalTime(Owner.mFastCastRate) - decrease_total_timems;
- this.total_cd_time = Math.Max(0, this.total_cd_time);
- }
- public int PassTime
- {
- get { return pass_time; }
- }
- public int OveryLayer
- {
- get { return this.overLayer; }
- }
- public void ResetPassTime()
- {
- this.pass_time = 0;
- }
- public void SetCDStates(bool cdStatus)
- {
- this.is_cd = cdStatus;
- for (int i = 0; i < mSkillCDStatusChange.Count; i++)
- {
- mSkillCDStatusChange[i].Invoke(this, this.is_cd);
- }
- }
- public void ResgistSkillCDChangeListen(ISkillCDStatusChange listen)
- {
- if(listen != null)
- {
- this.mSkillCDStatusChange.Add(listen);
- }
- }
- public bool IsMutilAction()
- {
- return Data.IsSingleAction && (Data.ActionQueue.Count > 1);
- }
- public bool IsHasNext()
- {
- bool isHasNext = this.ActionIndex < this.mMaxMutilStep;
- if(!isHasNext && this.Data.ActionQueue.Count > this.ActionIndex && this.Data.ActionQueue[this.ActionIndex].IsCancelBySkillNext)
- {
- isHasNext = true;
- }
- return isHasNext;
- }
- public byte GetMaxMutilStep()
- {
- return this.mMaxMutilStep;
- }
- public int GetMaxSteps()
- {
- return this.mMaxMutilStep > 0 ? Math.Min(this.mMaxMutilStep, this.Data.ActionQueue.Count-1) : (this.Data.ActionQueue.Count-1);
- }
- public UnitActionData GetNextAction()
- {
- if(this.ActionIndex + 1 >= this.Data.ActionQueue.Count)
- {
- return null;
- }
- return this.Data.ActionQueue[this.ActionIndex + 1];
- }
- public UnitActionData GetCurAction()
- {
- if (this.ActionIndex < 0 || this.ActionIndex >= this.Data.ActionQueue.Count)
- {
- return null;
- }
- return this.Data.ActionQueue[this.ActionIndex];
- }
- public float ActionSpeed { get { return action_speed; } }
- public int ActionIndex { get { return action_step; }}
- public int LockActionStep { get { return lockActionStep; } set { lockActionStep = value; } }
- public object Tag { get; set; }
- public bool CanStoreTimes { get { return this.mCanStoreTimes; }}
- internal SkillState(SkillTemplate data, LaunchSkill skill, InstanceUnit owner)
- {
- this.Owner = owner;
- this.Data = data;
- this.mCanStoreTimes = (this.Data.Properties.GetSkillUseTimes() > 1 && this.Data.Properties.GetSkillAddTimeInterval() > 0);
- //默认有最大层数
- if (this.mCanStoreTimes)
- {
- this.overLayer = this.Data.Properties.GetSkillUseTimes();
- }
- this.LaunchSkill = skill;
- this.total_cd_time = Data.ToUnitSkillTotalTime(Owner.mFastCastRate) - decrease_total_timems;
- this.pass_time = int.MaxValue / 2;
- this.action_speed = data.ActionSpeedRate + Owner.__mSkillActionSpeedRate;
- // 多段式技能,但是只能释放一段
- if(Data.IsSingleAction && (Data.ActionQueue.Count > 1))
- {
- for (int i = 0; i < Data.ActionQueue.Count; i++)
- {
- this.mMaxMutilStep = (byte)i;
- if (Data.ActionQueue[i].SigleActionType == ActionEnum.forbidNext || Data.ActionQueue[i].SigleActionType == ActionEnum.forbidNext_lock
- || Data.ActionQueue[i].SigleActionType == ActionEnum.forbidNext_showTime)
- {
- break;
- }
- }
- }
- }
- //设置自动释放
- public void setAutoLaunch(bool AutoLaunch)
- {
- this.LaunchSkill.AutoLaunch = AutoLaunch;
- }
- public void setMaxMutilStep(int vlaue)
- {
- this.mMaxMutilStep = (byte)vlaue;
- }
- public void SetActionIndex(int index)
- {
- this.action_step = index;
- }
- // 设置机制冷却缩减时间
- public void setTriggerDecreaseTime(int time, bool notify = false)
- {
- this.trigger_decrease_times = time;
- // time==0是开始释放时的重置
- if(notify)
- {
- PlayerCDEvent evt = new PlayerCDEvent(this.Owner.ID);
- evt.is_decrease_time = true;
- evt.is_all = false;
- evt.decrease_timeMS = time;
- evt.skill_template_id = this.LaunchSkill.SkillID;
- this.Owner.Parent.queueObjectEvent(this.Owner, evt);
- }
- }
- /// <summary>
- /// 检测目标距离
- /// </summary>
- /// <returns></returns>
- public bool checkTargetRange(InstanceUnit targetUnit)
- {
- if (Data.AttackMustBeInRange && Data.AttackAngle == 0)
- {
- if (targetUnit != null)
- {
- float rg = Owner.GetSkillAttackRange(this.Data, targetUnit);
- if (Collider.Object_HitBody_TouchRound(targetUnit, Owner.X, Owner.Y, rg))
- {
- return true;
- }
- }
- return false;
- }
- return true;
- }
- /// <summary>
- /// 检测目标距离
- /// </summary>
- /// <returns></returns>
- public bool checkTargetInAttackRange(InstanceUnit targetUnit, LaunchSkillParam param)
- {
- //只处理怪物的自动战斗,玩家控制不了
- if (this.Owner.IsMonster && this.Data.AttackKeepRange > 0)
- {
- targetUnit = targetUnit == null ? this.Owner.Parent.getUnit(param.TargetUnitID) : targetUnit;
- if (targetUnit != null && CMath.getDistance(targetUnit.X, targetUnit.Y, this.Owner.X, this.Owner.Y) < Data.AttackKeepRange)
- {
- return false;
- }
- }
- if (this.Data.AttackRange > 0)
- {
- targetUnit = targetUnit == null ? this.Owner.Parent.getUnit(param.TargetUnitID) : targetUnit;
- if (targetUnit != null && CMath.getDistance(targetUnit.X, targetUnit.Y, this.Owner.X, this.Owner.Y) > (Owner.GetSkillAttackRange(this.Data) + targetUnit.BodyBlockSize))
- {
- return false;
- }
- }
- return true;
- }
- public void SetDecreaseTotalTimeMS(int timeMS)
- {
- this.decrease_total_timems = timeMS;
- }
- public void AddDecreaseTotalTimeMS(int timeMS)
- {
- this.decrease_total_timems += timeMS;
- }
- public int GetDecreaseTotalTimeMS()
- {
- return this.decrease_total_timems;
- }
- /// <summary>
- /// 指定技能动作
- /// </summary>
- /// <param name="step"></param>
- public void Reset(bool init = false)
- {
- this.action_step = -1;
- if (!init)
- {
- this.IsInMutilTime = false;
- }
- }
- /// <summary>
- /// 设置是否自动控制多段攻击(如果要手动指定播放技能,则设置为False)
- /// </summary>
- /// <param name="at"></param>
- public void SetAutoIncreaseActionIndex(bool at)
- {
- this.auto_increase_action_step = at;
- }
- internal void Launch(StateSkill s, LaunchSkillParam param)
- {
- this.NextAction();
- this.total_cd_time = this.GetDefTotalCDTime();
- this.pass_time = 0;
- this.state = s;
- this.SetCDStates(true);
- }
- private int GetDefTotalCDTime()
- {
- int cd_time = 0;
- cd_time = Data.ToUnitSkillTotalTime(Owner.mFastCastRate) - decrease_total_timems;
- if(this.GetSkillType() == XmdsSkillType.normalAtk)
- {
- cd_time = (int)(cd_time / (Owner.GetAttackSpeed() * 0.0001f));
- }
- else
- {
- cd_time = (int)((1.0f - Math.Min(XmdsConstConfig.SKILL_CD_REDUCE_MAX, Owner.SkillCdReduce) * 0.0001f) * cd_time);
- }
-
- cd_time = Math.Max(0, cd_time);
- return cd_time;
- }
- public void updateOverLayer(int value)
- {
- this.overLayer = value;
- //通知叠层
- var queData = new PlayerSkillUseTimeChangedEvent(this.Owner.ID, this.Data.ID, (byte)this.overLayer);
- queData.hasNext = (byte)((this.OveryLayer >= this.Data.Properties.GetSkillUseTimes()) ? 0 : 1);
- this.Owner.queueEvent(queData);
- }
- public void StartCD(int total_time = 0)
- {
- if (total_time > 0)
- {
- this.total_cd_time = Math.Max(0, total_time - decrease_total_timems);
- }
- else
- {
- //this.total_cd_time = Data.ToUnitSkillTotalTime(Owner.mFastCastRate) - decrease_total_timems;
- //this.total_cd_time = Math.Max(0, this.total_cd_time);
- this.total_cd_time = this.GetDefTotalCDTime();
- }
- this.pass_time = 0;
- this.SetCDStates(true);
- Owner.queueEvent(new PlayerSkillTimeChangedEvent(Owner.ID, ID, this.pass_time, this.TotalCDTime));
- }
- public void LogicToCD()
- {
- this.mCodeControl_HasNext = true;
- this.StartCD();
- }
- public void SetCodeControlNext()
- {
- this.mCodeControl_HasNext = false;
- }
- public void ForceIntoCD()
- {
- this.is_in_mutil_time = false;
- this.action_step = -1;
- this.StartCD();
- }
- internal void Stop(StateSkill s)
- {
- this.state = null;
- // CD 依靠动作结束
- if (Data.IsCoolDownWithAction)
- {
- this.SetCDStates(false);
- this.stop_time = this.pass_time;
- }
- }
- internal void Update(int intervalMS)
- {
- if (!IsActive && IsPauseOnDeactive)
- {
- return;
- }
- this.pass_time += intervalMS;
- if(this.CanStoreTimes)
- {
- if(this.overLayer < Data.Properties.GetSkillUseTimes() && this.pass_time >= Data.Properties.GetSkillAddTimeInterval())
- {
- this.pass_time = 0;
- this.updateOverLayer(this.overLayer + 1);
- }
- }
- else if (is_cd && !Data.IsCoolDownWithAction)
- {
- // 技能 CD
- if (this.pass_time >= TotalCDTime)
- {
- if(this.IsMutilAction() && (this.is_in_mutil_time || (this.action_step >= Data.ActionQueue.Count)))
- {
- if(this.action_step < Data.ActionQueue.Count && this.action_step > 0 &&
- (Data.ActionQueue[this.action_step].SigleActionType == ActionEnum.chargeAtk || Data.ActionQueue[this.action_step].SigleActionType == ActionEnum.IsAutoNext))
- {
- pass_time = 0;
- return;
- }
- else
- {
- this.Reset();
- this.StartCD();
- }
- }
- else
- {
- this.is_in_mutil_time = true;
- this.SetCDStates(false);
- this.stop_time = this.pass_time;
- //Console.WriteLine("out cd : " + TimeUtil.GetTimestampMS());
- }
- }
- }
- }
- public bool TryLaunch()
- {
- if (!IsActive)
- {
- return false;
- }
- if (!this.Owner.IsCanLaunchSkill(Data.TemplateID))
- {
- return false;
- }
- //if (Owner.__mCurrentHP.Value >= Data.CostHP)
- {
- return IsDone;
- }
- //return false;
- }
- public int NextAction()
- {
- if (Data.IsSingleAction && auto_increase_action_step)
- {
- int action_time = this.TotalCDTime;
-
- // 是放技能时,处于多段攻击连击冷却时间范围
- //if (pass_time - stop_time < Data.SingleActionCoolDownMS || pass_time < (action_time + Data.SingleActionCoolDownMS))
- if (this.action_step < 0)
- {
- this.action_step = 0;
- }
- else
- {
- if (Data.IsCoolDownWithAction)
- {
- action_time = Data.ActionQueue[action_step % Data.ActionQueue.Count].TotalTimeMS;
- }
- if (this.IsMutilAction())
- {
- if (this.IsInMutilTime || (this.Data.IsCoolDownWithAction && this.IsDone) || (!this.IsDone && this.action_step < this.mMaxMutilStep))
- {
- this.action_step += 1;
- this.action_step = (byte)(action_step % Data.ActionQueue.Count);
- //if (this.action_step >= Data.ActionQueue.Count || !IsCanLaunchNextStepByPlayer(Data.ActionQueue[this.action_step]))
- //{
- // this.action_step = 0;
- // this.is_in_mutil_time = false;
- //}
- }
- }
- }
- }
- return action_step;
- }
- public bool IsCanLaunchNextStepByPlayer(UnitActionData actionData)
- {
- // = this.Data.ActionQueue[this.ActionIndex];
- if (actionData.SigleActionType == ActionEnum.forbidNext ||
- actionData.SigleActionType == ActionEnum.forbidNext_lock)
- {
- return false;
- }
- return true;
- }
- internal void ClearCD()
- {
- this.pass_time = TotalCDTime;
- }
- internal void DecreaseSkillCD(int timeMS)
- {
- if (!Data.IsCoolDownWithAction)
- {
- this.pass_time += timeMS;
- }
- }
- internal void DecreaseSkillCD_Pct(float percent)
- {
- if (!Data.IsCoolDownWithAction)
- {
- this.pass_time += (int)(TotalCDTime * percent);
- }
- }
- internal void SetPassTime(int passtime)
- {
- if (!Data.IsCoolDownWithAction)
- {
- if (passtime != this.pass_time)
- {
- this.pass_time = passtime;
- Owner.queueEvent(new PlayerSkillTimeChangedEvent(Owner.ID, ID, this.pass_time, this.total_cd_time));
- if (pass_time < TotalCDTime)
- {
- this.SetCDStates(true);
- }
- }
- }
- }
- internal bool TrySetActive(SkillActiveState state)
- {
- if (state != current_state)
- {
- this.current_state = state;
- this.Owner.mSyncSkillActivesChanged = true;
- return true;
- }
- return false;
- }
- }
- /// <summary>
- /// 设置技能可用性
- /// </summary>
- /// <param name="skillTemplateID"></param>
- /// <param name="active"></param>
- /// <param name="pause_on_deactive"></param>
- public void SetSkillActive(int skillTemplateID, bool active, bool pause_on_deactive = false)
- {
- SkillState st = getSkillState(skillTemplateID);
- if (st != null)
- {
- if (active)
- st.TrySetActive(SkillActiveState.Active);
- else if (pause_on_deactive)
- st.TrySetActive(SkillActiveState.DeactiveAndPause);
- else
- st.TrySetActive(SkillActiveState.Deactive);
- }
- //标记冻结
- this.Virtual.SetSkillActive(skillTemplateID, active, pause_on_deactive);
- }
- public void SetSkillActive(int skillTemplateID, SkillActiveState state)
- {
- SkillState st = getSkillState(skillTemplateID);
- if (st != null)
- {
- st.TrySetActive(state);
- }
- }
- /// <summary>
- /// 清除技能CD
- /// </summary>
- /// <param name="skillTemplateID"></param>
- public void ClearSkillCD(int skillTemplateID)
- {
- SkillState ss = getSkillState(skillTemplateID);
- if (ss != null)
- {
- ss.ClearCD();
- PlayerCDEvent evt = new PlayerCDEvent(ID);
- evt.is_clear = true;
- evt.is_all = false;
- evt.skill_template_id = skillTemplateID;
- Parent.queueObjectEvent(this, evt);
- }
- }
- /// <summary>
- /// 清除所有技能CD
- /// </summary>
- public void ClearAllSkillCD()
- {
- foreach (SkillState ss in mSkillStatus.Skills)
- {
- ss.ClearCD();
- }
- PlayerCDEvent evt = new PlayerCDEvent(ID);
- evt.is_clear = true;
- evt.is_all = true;
- Parent.queueObjectEvent(this, evt);
- }
- /// <summary>
- /// 减少当前CD固定时间
- /// </summary>
- /// <param name="skillTemplateID"></param>
- /// <param name="updateTimeMS"></param>
- public void DecreaseSkillCD(int skillTemplateID, int updateTimeMS)
- {
- SkillState ss = getSkillState(skillTemplateID);
- if (ss != null)
- {
- ss.DecreaseSkillCD(updateTimeMS);
- PlayerCDEvent evt = new PlayerCDEvent(ID);
- evt.is_decrease_time = true;
- evt.is_all = false;
- evt.decrease_timeMS = updateTimeMS;
- evt.skill_template_id = skillTemplateID;
- Parent.queueObjectEvent(this, evt);
- }
- }
- /// <summary>
- /// 减少当前CD固定时间
- /// </summary>
- /// <param name="updateTimeMS"></param>
- public void DecreaseAllSkillCD(int updateTimeMS)
- {
- foreach (SkillState ss in mSkillStatus.Skills)
- {
- ss.DecreaseSkillCD(updateTimeMS);
- }
- PlayerCDEvent evt = new PlayerCDEvent(ID);
- evt.is_decrease_time = true;
- evt.is_all = true;
- evt.decrease_timeMS = updateTimeMS;
- Parent.queueObjectEvent(this, evt);
- }
- public void DecreaseSkillCD_Pct(int skillTemplateID, float percent)
- {
- SkillState ss = getSkillState(skillTemplateID);
- if (ss != null)
- {
- ss.DecreaseSkillCD_Pct(percent);
- PlayerCDEvent evt = new PlayerCDEvent(ID);
- evt.is_decrease_pct = true;
- evt.is_all = false;
- evt.decrease_pct = percent;
- evt.skill_template_id = skillTemplateID;
- Parent.queueObjectEvent(this, evt);
- }
- }
- public void DecreaseAllSkillCD_Pct(float percent)
- {
- foreach (SkillState ss in mSkillStatus.Skills)
- {
- ss.DecreaseSkillCD_Pct(percent);
- }
- PlayerCDEvent evt = new PlayerCDEvent(ID);
- evt.is_decrease_pct = true;
- evt.is_all = true;
- evt.decrease_pct = percent;
- Parent.queueObjectEvent(this, evt);
- }
- public void SetSkillPassTime(int skillID, int passTimeMS)
- {
- SkillState ss = this.getSkillState(skillID);
- if (ss != null)
- {
- ss.SetPassTime(passTimeMS);
- }
- }
- public void StartSkillCD(int skillID, int totalTimeMS = 0)
- {
- SkillState ss = this.getSkillState(skillID);
- if (ss != null)
- {
- ss.StartCD(totalTimeMS);
- }
- }
- #endregion
- //-----------------------------------------------------------------------------------------------
- #region _BUFF状态类_
- public ClientStruct.UnitBuffStatus[] GetCurrentBuffStatus()
- {
- List<ClientStruct.UnitBuffStatus> ret = new List<ClientStruct.UnitBuffStatus>(mBuffs.Count);
- mBuffs.ForEachRead((buff) =>
- {
- if (buff.Data.ClientVisible)
- {
- ClientStruct.UnitBuffStatus bf = new ClientStruct.UnitBuffStatus();
- bf.BuffTemplateID = (buff.Data.TemplateID);
- bf.SenderID = buff.SenderID;
- bf.IsEquip = buff.IsEquip;
- bf.TotalTime = buff.Data.LifeTimeMS;
- bf.PassTime = (buff.PassTimeMS);
- bf.OverlayLevel = buff.OverlayLevel;
- bf.ext = buff.BuffExtData;
- ret.Add(bf);
- }
- return false;
- });
- return ret.ToArray();
- }
- public bool IshasControlBuff(Predicate<BuffTemplate> cb)
- {
- bool hasControlBuff = false;
- mBuffs.ForEachReadTrueReturn((buff) =>
- {
- if (cb(buff.Data))
- {
- hasControlBuff = true;
- return true;
- }
- return false;
- });
- return hasControlBuff;
- }
- public void GetAllBuffStatus(List<BuffState> ret)
- {
- mBuffs.ForEachRead((bf) =>
- {
- ret.Add(bf);
- return false;
- });
- }
- private class BuffMap
- {
- class BuffList : IEnumerable<InstanceUnit.BuffState>
- {
- readonly public BuffTemplate bufft;
- private HashMap<uint, InstanceUnit.BuffState> list;
- private InstanceUnit.BuffState last;
- public BuffList(BuffTemplate bufft)
- {
- this.bufft = bufft;
- this.list = new HashMap<uint, BuffState>(1);
- }
- public void Add(InstanceUnit.BuffState bs)
- {
- if (bs.Data.IsDuplicating)
- {
- list.Add(bs.SenderID, bs);
- }
- else
- {
- list.Add(0, bs);
- }
- last = bs;
- }
- public InstanceUnit.BuffState Remove(uint unit_id)
- {
- if (bufft.IsDuplicating)
- {
- InstanceUnit.BuffState bs = list.RemoveByKey(unit_id);
- if (bs != null && bs == last)
- {
- if (list.Count > 0)
- {
- foreach (InstanceUnit.BuffState bf in list.Values)
- {
- last = bf;
- break;
- }
- }
- else
- {
- last = null;
- }
- }
- return bs;
- }
- else
- {
- last = null;
- return list.RemoveByKey(0);
- }
- }
- public void Clear(List<InstanceUnit.BuffState> cleared)
- {
- last = null;
- cleared.AddRange(list.Values);
- list.Clear();
- }
- public InstanceUnit.BuffState Get(uint unit_id)
- {
- return list.Get(unit_id);
- }
- public int Count { get { return list.Count; } }
- public InstanceUnit.BuffState Last { get { return last; } }
- IEnumerator<BuffState> IEnumerable<BuffState>.GetEnumerator()
- {
- return list.Values.GetEnumerator();
- }
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return list.Values.GetEnumerator();
- }
- }
- private HashMap<int, BuffList> mObjects = new HashMap<int, BuffList>();
- private List<BuffState> mObjectsList = new List<BuffState>();
- private bool dirty = true;
- public int Count { get { return mObjects.Count; } }
- //-----------------------------------------------------------------------
- // read
- //-----------------------------------------------------------------------
- public InstanceUnit.BuffState GetObject(int id)
- {
- BuffList list;
- if (mObjects.TryGetValue(id, out list))
- {
- return list.Last;
- }
- return null;
- }
- public void GetObjects(int id, List<InstanceUnit.BuffState> ret)
- {
- BuffList list;
- if (mObjects.TryGetValue(id, out list))
- {
- ret.AddRange(list);
- }
- }
- public InstanceUnit.BuffState GetObject(int buff_id, uint unit_id)
- {
- BuffList list;
- if (mObjects.TryGetValue(buff_id, out list))
- {
- return list.Get(unit_id);
- }
- return null;
- }
- //-----------------------------------------------------------------------
- // write
- //-----------------------------------------------------------------------
- public bool RemoveObject(InstanceUnit.BuffState bs)
- {
- BuffList list;
- if (mObjects.TryGetValue(bs.ID, out list))
- {
- dirty = true;
- return list.Remove(bs.SenderID) != null;
- }
- return false;
- }
- public InstanceUnit.BuffState RemoveObject(int buffID, uint unit_id)
- {
- BuffList list;
- if (mObjects.TryGetValue(buffID, out list))
- {
- dirty = true;
- return list.Remove(unit_id);
- }
- return null;
- }
- public void RemoveObjects(int buffID, List<InstanceUnit.BuffState> removed)
- {
- BuffList list;
- if (mObjects.TryGetValue(buffID, out list))
- {
- dirty = true;
- list.Clear(removed);
- }
- }
- public void AddObject(InstanceUnit.BuffState obj)
- {
- BuffList list = mObjects.Get(obj.ID);
- if (list == null)
- {
- list = new BuffList(obj.Data);
- mObjects.Add(obj.ID, list);
- }
- list.Add(obj);
- dirty = true;
- }
- private IList<BuffState> Refresh()
- {
- if (dirty)
- {
- dirty = false;
- mObjectsList.Clear();
- foreach (BuffList list in mObjects.Values)
- {
- foreach (InstanceUnit.BuffState buff in list)
- {
- mObjectsList.Add(buff);
- }
- }
- }
- return mObjectsList;
- }
- private void ToList(List<BuffState> ret)
- {
- foreach (BuffList list in mObjects.Values)
- {
- foreach (InstanceUnit.BuffState buff in list)
- {
- ret.Add(buff);
- }
- }
- }
- //-----------------------------------------------------------------------
- //
- //-----------------------------------------------------------------------
- /// <summary>
- ///
- /// </summary>
- /// <param name="action">返回true终止迭代!</param>
- public void ForEachRead(Predicate<InstanceUnit.BuffState> action)
- {
- foreach (BuffList list in mObjects.Values)
- {
- foreach (InstanceUnit.BuffState buff in list)
- {
- if (action(buff))
- {
- return;
- }
- }
- }
- }
- public bool ForEachReadTrueReturn(Predicate<InstanceUnit.BuffState> action)
- {
- foreach (BuffList list in mObjects.Values)
- {
- foreach (InstanceUnit.BuffState buff in list)
- {
- if (action(buff))
- {
- return true;
- }
- }
- }
- return false;
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="action">返回true终止迭代!</param>
- public void ForEachWrite(Predicate<InstanceUnit.BuffState> action)
- {
- using (var list = ListObjectPool<BuffState>.AllocAutoRelease())
- {
- ToList(list);
- for (int i = 0; i < list.Count; i++)
- {
- if (action(list[i]))
- {
- return;
- }
- }
- }
- }
- public void Update(InstanceZone parent)
- {
- IList<BuffState> list = Refresh();
- for (int i = 0; i < list.Count; i++)
- {
- BuffState bs = list[i];
- bs.OnUpdate(parent);
- if (bs.IsEnd() && !bs.MarkRemove)
- {
- this.RemoveObject(bs);
- bs.OnEnd(BuffState.EndResult_ByTimeUp);
- }
- }
- }
- }
- public class BuffState
- {
- public const string EndResult_ByTimeUp = "time_up";
- public const string EndResult_ByReplaced = "replaced";
- public const string EndResult_ByClientRemoved = "client_removed";
- public const string EndResult_ByCatgoryExclusive = "catgory_exclusive";
- public const string EndResult_ByCode = "code";
- public const string ENDResult_HuDunBreak = "HuDunBreak";
- readonly private BuffTemplate mData;
- readonly private InstanceUnit mOwner;
- readonly private InstanceUnit sender;
- private TimeExpire<int> st_stun;
- private TimeExpire<int> st_invisible;
- private TimeExpire<int> st_invincible;
- private TimeExpire<int> st_silent;
- private TimeExpire<int> st_canmove;
- private TimeExpire<int> st_ignoreControl;
- private int mBuffExtData; //buff扩展字段
- private TimeInterval<BuffTemplate.KeyFrame> interval;
- private PopupKeyFrames<BuffTemplate.KeyFrame> keyframes;
- private int total_time;
- private int passtime;
- private byte overlay_level;
- public BuffTemplate Data { get { return mData; } }
- public int ID { get { return mData.ID; } }
- public byte OverlayLevel { get { return overlay_level; }}
- public int PassTimeMS { get { return passtime; } }
- public int LifeTimeMS { get { return total_time; } }
- public bool IsEquip { get; private set; }
- public uint SenderID { get { return sender.ID; } }
- public int BuffExtData { get { return mBuffExtData; } set { mBuffExtData = value; } }
- public InstanceZone Zone { get { return mOwner.Parent; } }
- public InstanceUnit Owner { get { return mOwner; } }
- public InstanceUnit Sender { get { return sender; } }
- public object Tag { get; set; }
- //释放时位置信息
- private Vector2 mHitPos = new Vector2();
- public bool MarkRemove { get; set; }
- //set和get放在一起虽然爽,但是可读性真一般
- public void SetLayerLevel(byte value)
- {
- this.overlay_level = value;
- }
- internal BuffState(BuffTemplate data, InstanceUnit unit, InstanceUnit sender, byte overlay_level, bool forever, bool isControlBuf)
- {
- this.mData = data;
- this.mOwner = unit;
- this.sender = sender;
- this.IsEquip = forever;
- this.overlay_level = overlay_level;
- this.passtime = 0;
- this.MarkRemove = false;
- this.interval = new TimeInterval<BuffTemplate.KeyFrame>(data.HitIntervalMS);
- this.interval.FirstTimeEnable = data.FirstTimeEnable;
- this.interval.Tag = data.HitKeyFrame;
- this.keyframes = new PopupKeyFrames<BuffTemplate.KeyFrame>();
- this.keyframes.AddRange(data.KeyFrames);
- this.total_time = data.LifeTimeMS;
- //控制增幅
- if (sender.__mControledTimeAdd != 0 && (mData.IsSilent || mData.MakeStun || isControlBuf))
- {
- this.total_time = (int)(Math.Max(0.0f, 1.0f + unit.__mControledTimeAdd * 0.0001f) * this.total_time);
- }
- //韧性,控制减
- if (unit.ControledTimeReduce != 0 && (mData.IsSilent || mData.MakeStun || isControlBuf))
- {
- this.total_time = (int)(Math.Max(0.0f, 1.0f - unit.ControledTimeReduce * 0.0001f) * this.total_time);
- }
- }
- internal void OnStart()
- {
- mHitPos.SetX(mOwner.X);
- mHitPos.SetY(mOwner.Y);
- mOwner.mFormula.OnBuffBegin(mOwner, this, sender);
- if (mData.MakeStun)
- {
- this.st_stun = mOwner.SetStunTimeMS(total_time);
- }
- if (mData.IsInvisible)
- {
- this.st_invisible = mOwner.SetInvisibleTimeMS(total_time);
- }
- if (mData.IsInvincible)
- {
- this.st_invincible = mOwner.SetInvincibleTimeMS(total_time);
- }
- if (mData.IsSilent)
- {
- this.st_silent = mOwner.SetSilentTimeMS(total_time);
- }
- if(!mData.IsCanMove)
- {
- this.st_canmove = mOwner.SetCannotMoveTimeMS(total_time);
- }
- // 免疫控制
- if(mData.IgnoreControl)
- {
- this.st_ignoreControl = mOwner.SetIgnoreControlTimeMS(total_time);
- }
- if (mData.UnitTrigger != null)
- {
- mOwner.addTrigger(mData.UnitTrigger.TriggerTemplateID);
- }
- if (mData.UnitChangeSkills)
- {
- if (mData.UnitKeepSkillsID != null && mData.UnitKeepSkillsID.Count > 0)
- {
- using (var keeps = ListObjectPool<LaunchSkill>.AllocAutoRelease())
- {
- mOwner.getKeepSkills(mData.UnitKeepSkillsID, keeps);
- keeps.AddRange(mData.UnitSkills);
- mOwner.InitSkills(mData.UnitBaseSkillID, keeps.ToArray());
- }
- }
- else
- {
- mOwner.InitSkills(mData.UnitBaseSkillID, mData.UnitSkills.ToArray());
- }
- }
- mOwner.doGotBuff(this);
- if (mData.ClientVisible)
- {
- //Console.WriteLine("获得剑影 - " + this.BuffExtData);
- mOwner.queueEvent(new UnitLaunchBuffEvent(mOwner.ID, this.ID, this.SenderID,
- this.LifeTimeMS, this.IsEquip, overlay_level, this.BuffExtData));
- }
- }
- internal void OnEnd(string result, bool replace = false)
- {
- this.passtime = total_time;
- this.MarkRemove = true;
- mOwner.doLostBuff(this);
- if (this.st_stun != null)
- {
- mOwner.mStunTimeMS.Remove(st_stun);
- }
- if (this.st_invisible != null)
- {
- mOwner.mInvisibleTimeMS.Remove(st_invisible);
- }
- if (this.st_invincible != null)
- {
- mOwner.mInvisibleTimeMS.Remove(st_invincible);
- }
- if (this.st_silent != null)
- {
- mOwner.mSilentTimeMS.Remove(st_silent);
- }
- if(this.st_canmove != null)
- {
- mOwner.mCannotMoveTimeMS.Remove(st_canmove);
- }
- if(this.st_ignoreControl != null)
- {
- mOwner.mIgnoreControl.Remove(st_ignoreControl);
- }
- if (mData.UnitTrigger != null)
- {
- mOwner.removeTrigger(mData.UnitTrigger.TriggerTemplateID);
- }
- if (mData.UnitChangeSkills)
- {
- mOwner.ResetSkills();
- }
- if (mData.EndKeyFrame != null)
- {
- doKeyFrame(mData.EndKeyFrame);
- }
- mOwner.mFormula.OnBuffEnd(mOwner, this, result, replace);
- if (this.Data.ClientVisible)
- {
- if (!replace)
- {
- mOwner.queueEvent(new UnitStopBuffEvent(mOwner.ID, this.ID, SenderID));
- }
- }
- }
- internal void OnUpdate(InstanceZone zone)
- {
- using (var kfs = ListObjectPool<BuffTemplate.KeyFrame>.AllocAutoRelease())
- {
- if (keyframes.PopKeyFrames(passtime, kfs) > 0)
- {
- foreach (BuffTemplate.KeyFrame kf in kfs)
- {
- doKeyFrame(kf);
- }
- }
- }
- if (interval.Update(zone.UpdateIntervalMS))
- {
- doKeyFrame(mData.HitKeyFrame);
- mOwner.mFormula.OnBuffUpdate(mOwner, this, interval.TotalTickCount);
- }
- passtime += zone.UpdateIntervalMS;
- }
- private void doKeyFrame(BuffTemplate.KeyFrame kf)
- {
- if (kf != null)
- {
- if (kf.Attack != null)
- {
- mOwner.doHitAttack(sender, new AttackSource(this, kf.Attack));
- }
- if (kf.Spell != null)
- {
- if(kf.Spell.SenderUnit == LaunchSpllSenderUnit.AttackerUnit)
- {
- this.sender.Parent.unitLaunchSpell(XmdsSkillType.none, sender, kf.Spell,
- this.sender.X, this.sender.Y, mOwner.ID, null);
- }
- else
- {
- mOwner.Parent.unitLaunchSpell(XmdsSkillType.none, sender, kf.Spell,
- mOwner.X, mOwner.Y, mOwner.ID, null);
- }
- }
- if (kf.Item != null)
- {
- mOwner.UseItem(kf.Item.ItemTemplateID);
- }
- if(kf.addItem != null)
- {
- this.BuffAddItem(kf.addItem);
- }
- }
- }
- /** buff生成道具 */
- private void BuffAddItem(BuffAddItem addItem)
- {
- var itemInfo = Zone.Templates.getItem(addItem.ItemTemplateID);
- if(itemInfo == null)
- {
- log.Warn("BuffAddItem找不到指定道具: " + addItem.ItemTemplateID);
- return;
- }
- if(addItem.posType == CommonAI.Zone.BuffAddItem.PosType.HitBuffPos)
- {
- this.mOwner.Parent.AddItem(itemInfo, null, this.mHitPos.X, this.mHitPos.Y, 0, this.mOwner.Force, null, this.Owner);
- }
- else if(addItem.posType == CommonAI.Zone.BuffAddItem.PosType.BindUnitPos)
- {
- this.mOwner.Parent.AddItem(itemInfo, null, this.Owner.X, this.Owner.Y, 0, this.mOwner.Force, null, this.Owner);
- }
- else if(addItem.posType == CommonAI.Zone.BuffAddItem.PosType.PointPos)
- {
- this.mOwner.Parent.AddItem(itemInfo, null, addItem.pointX, addItem.pointY, 0, this.mOwner.Force, null, this.Owner);
- }
- }
- public bool IsEnd()
- {
- if (Data.IsRemoveOnSenderRemoved)
- {
- if (!sender.Enable || sender.IsDead())
- {
- return true;
- }
- }
- if(Data.IsRemoveOnDead && this.mOwner.IsDead())
- {
- return true;
- }
- if (IsEquip) return false;
- return (passtime >= total_time);
- }
- public void AddLifeTimeMS(int timeMS)
- {
- this.total_time += timeMS;
- }
- //public void RefrushToMaxOverlayer()
- //{
- // this.overlay_level = this.mData.MaxOverlay;
- //}
- }
- private BuffMap mBuffs = new BuffMap();
- /// <summary>
- /// 是否存在技能变身BUFF
- /// </summary>
- public bool IsBuffChangeSkills
- {
- get
- {
- return GetChangeSkillBuff() != null;
- }
- }
- /// <summary>
- /// 获得技能变身BUFF
- /// </summary>
- public BuffState GetChangeSkillBuff()
- {
- BuffState ret = null;
- mBuffs.ForEachRead((bs) =>
- {
- if (bs.Data.UnitChangeSkills && !bs.IsEnd())
- {
- ret = bs;
- return true;
- }
- return false;
- });
- return ret;
- }
- public BuffState GetBuffByID(int buffTemplateID)
- {
- return mBuffs.GetObject(buffTemplateID);
- }
- public BuffState GetBuffByIDAndSender(int buffTemplateID, uint senderUnitID)
- {
- return mBuffs.GetObject(buffTemplateID, senderUnitID);
- }
- public BuffState AddBuff(int buffID, int pointMaxOverLayer = 0, bool forever = false, bool bMaxOverlayer = false, bool bMaxReset = false)
- {
- BuffTemplate buff = Templates.getBuff(buffID);
- if (buff != null)
- {
- return this.AddBuff(buff, this, pointMaxOverLayer, forever, bMaxOverlayer, 0, false, bMaxReset);
- }
- return null;
- }
- public BuffState AddBuff(int buffID, InstanceUnit sender, int pointMaxOverLayer = 0, bool forever = false,
- bool bMaxOverlayer = false, bool bMaxReset = false, int buffExt = 0, int addLyers = 0)
- {
- BuffTemplate buff = Templates.getBuff(buffID);
- if (buff != null)
- {
- return this.AddBuff(buff, sender, pointMaxOverLayer, forever, bMaxOverlayer, buffExt, false, bMaxReset, addLyers);
- }
- return null;
- }
- /** 编辑器直接编写增加buff接口 */
- public BuffState AddBuff(LaunchBuff buff, InstanceUnit sender, bool forever = false)
- {
- //如果绑定了事件id,就表示去触发事件
- if (buff.BuffID == 0 && buff.BindEventID > 0)
- {
- BattleFunction.GetInstance().TriggrBattleFunction(buff.BindEventID, this.Virtual, sender == null ? null : sender.Virtual);
- return null;
- }
- return this.AddBuff(buff.BuffID, sender, 0, forever);
- }
- /// <summary>
- /// 增加Buff
- /// </summary>
- /// <param name="forever">如果为True,则表示Buff不受时间CD控制</param>
- public virtual BuffState AddBuff(BuffTemplate buff, InstanceUnit sender, int pointMaxOverLayer = 0, bool forever = false,
- bool bMaxOverlayer = false, int buffExt = 0, bool isControlBuf = false, bool maxReset = false, int addLayers = 0)
- {
- object tag = null;
- if (mFormula.TryAddBuff(this, sender, ref buff, out tag))
- {
- if (!forever && buff.LifeTimeMS <= 0)
- {
- return null;
- }
- //凌驾于一切之上的免控
- if(isControlBuf && this.IsIgnoreControl)
- {
- return null;
- }
- if (!removeBuffByCatgory(buff.ID, buff.ExclusiveCatgory, buff.ExclusivePriority))
- {
- return null;
- }
- int finalMaxOverLay = (pointMaxOverLayer <= 0) ? buff.MaxOverlay : pointMaxOverLayer;
- int overlay_level = bMaxOverlayer ? (finalMaxOverLay - 1) : 0;
- BuffState bs = mBuffs.RemoveObject(buff.ID, sender.ID);
- if (bs != null)
- {
- if (buff.IsOverlay)
- {
- int finalAddLayer = Math.Max(1, addLayers);
- if(bs.OverlayLevel + finalAddLayer >= finalMaxOverLay && maxReset)
- {
- overlay_level = 0;
- }
- else
- {
- overlay_level = bMaxOverlayer ? (finalMaxOverLay - 1) : (Math.Min(bs.OverlayLevel + finalAddLayer, finalMaxOverLay - 1));
- }
- }
- bs.OnEnd(BuffState.EndResult_ByReplaced, true);
- }
- else if(addLayers > 0 && buff.MaxOverlay > 0)
- {
- overlay_level = Math.Max(0, Math.Min(buff.MaxOverlay - 1, addLayers-1));
- }
- BuffState newbs = new BuffState(buff, this, sender, (byte)overlay_level, forever, isControlBuf);
- newbs.Tag = tag;
- newbs.BuffExtData = (bs == null) ? buffExt : bs.BuffExtData;
- mBuffs.AddObject(newbs);
- newbs.OnStart();
- //System.Console.WriteLine("AddBuf - " + newbs.Data.ID + ", " + newbs.OverlayLevel);
- mFormula.SendAddBuffEvent(this, sender, buff);
-
- return newbs;
- }
- return null;
- }
- /// <summary>
- /// 移除互斥技能,根据优先级
- /// </summary>
- /// <param name="buffID"></param>
- /// <param name="catgory"></param>
- /// <param name="priority"></param>
- private bool removeBuffByCatgory(int buffID, int catgory, int priority)
- {
- if (catgory != 0)
- {
- // 检测当时有更高优先级 //
- {
- BuffState greater_buff = null;
- mBuffs.ForEachRead((bs) =>
- {
- if ((bs.ID != buffID) && (bs.Data.ExclusiveCatgory == catgory) && (bs.Data.ExclusivePriority > priority))
- {
- greater_buff = bs;
- return true;
- }
- return false;
- });
- if (greater_buff != null)
- {
- return false;
- }
- }
- // 移除低优先级 //
- mBuffs.ForEachWrite((bs) =>
- {
- if ((bs.ID != buffID) && (bs.Data.ExclusiveCatgory == catgory))
- {
- mBuffs.RemoveObject(bs);
- bs.OnEnd(BuffState.EndResult_ByCatgoryExclusive);
- }
- return false;
- });
- }
- return true;
- }
- public void removeBuff(int buffID, string result = BuffState.EndResult_ByCode)
- {
- using (var bss = ListObjectPool<BuffState>.AllocAutoRelease())
- {
- mBuffs.RemoveObjects(buffID, bss);
- if (bss.Count > 0)
- {
- foreach (BuffState bs in bss)
- {
- bs.OnEnd(result);
- }
- }
- }
- }
- public void removeBuffBySender(int buffID, uint senderUnitID, string result = BuffState.EndResult_ByCode)
- {
- BuffState bs = mBuffs.RemoveObject(buffID, senderUnitID);
- if (bs != null)
- {
- bs.OnEnd(result);
- }
- }
- public void clearBuffs(string result = BuffState.EndResult_ByCode)
- {
- mBuffs.ForEachWrite((bs) =>
- {
- if (!bs.Data.IsPassive && !bs.IsEquip)
- {
- mBuffs.RemoveObject(bs);
- bs.OnEnd(result);
- }
- return false;
- });
- }
- private void updateBuffs()
- {
- mBuffs.Update(Parent);
- }
- #endregion
- //---------------------------------------------------------------------------------------------------------------
- #region _被动系触发类_
- private readonly List<TriggerState> mTriggerStatus = new List<TriggerState>();
- private readonly UnitTriggerHelper mTriggerHelper;
- /// <summary>
- /// 保存被动状态
- /// </summary>
- public class TriggerState
- {
- readonly internal UnitTriggerTemplate mData;
- readonly internal InstanceUnit mOwner;
- public bool IsDamageTrigger { get; private set; }
- public bool IsAttackTrigger { get; private set; }
- public UnitTriggerTemplate Data { get { return mData; } }
- public object Tag { get; set; }
- private long launch_time;
- internal TriggerState(UnitTriggerTemplate data, InstanceUnit unit)
- {
- this.mData = data;
- this.mOwner = unit;
- this.IsDamageTrigger = false;
- this.IsAttackTrigger = false;
- this.launch_time = -mData.CoolDownTimeMS;
- foreach (BaseTriggerEvent evt in data.Triggers)
- {
- UnitTriggerEventTypeAttribute ut = PropertyUtil.GetAttribute<UnitTriggerEventTypeAttribute>(evt.GetType());
- foreach (Type dtype in ut.DelegateTypes)
- {
- if (dtype == typeof(DamageHandler))
- {
- this.IsDamageTrigger = true;
- }
- else if (dtype == typeof(AttackHandler))
- {
- this.IsAttackTrigger = true;
- }
- }
- }
- }
- internal void Start(InstanceUnit target)
- {
- if (IsActive)
- {
- this.launch_time = mOwner.Parent.PassTimeMS;
- uint targetID = 0;
- Vector2 targetPos = new Vector2(mOwner.X, mOwner.Y);
- if (target != null)
- {
- targetPos.SetX(target.X);
- targetPos.SetY(target.Y);
- targetID = target.ID;
- }
- // 如果关键帧绑定特效
- if (mData.DoEffect != null)
- {
- mOwner.queueEvent(new UnitEffectEvent(mOwner.ID, mData.DoEffect));
- }
- // 如果关键帧绑定释放法术
- if (mData.DoSpell != null)
- {
- mOwner.Parent.unitLaunchSpell(XmdsSkillType.none, mOwner, mData.DoSpell, mOwner.X, mOwner.Y, 0, targetPos);
- }
- // 如果关键帧绑定自己释放BUFF
- if (mData.DoBuff != null)
- {
- mOwner.AddBuff(mData.DoBuff, mOwner);
- }
- // 如果关键帧绑定单位位移
- if (mData.DoSkill != null)
- {
- mOwner.launchSkill(mData.DoSkill.SkillID, new InstanceUnit.LaunchSkillParam(targetID, targetPos));
- }
- //召唤
- if (mData.DoSummon != null)
- {
- mOwner.Parent.unitSummonUnit(mOwner, mData.DoSummon);
- }
- mOwner.mFormula.OnTriggerStart(mOwner, this, target);
- }
- }
- public bool Test(AttackSource source)
- {
- if (IsActive)
- {
- foreach (BaseTriggerEvent evt in mData.Triggers)
- {
- if (!mOwner.mFormula.TryLaunchTrigger(mOwner, mData, evt, source) || !evt.Test(mOwner, source))
- {
- return false;
- }
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// 是否处于冷却状态
- /// </summary>
- public bool IsActive
- {
- get { return (launch_time + mData.CoolDownTimeMS) < mOwner.Parent.PassTimeMS; }
- }
- }
- private void updateTriggers(bool slowRefresh)
- {
- this.mFormula.OnUpdateTriggerSkill(this, Parent.UpdateIntervalMS, slowRefresh);
- }
- public void addTriggers(ICollection<LaunchTrigger> triggers)
- {
- foreach (LaunchTrigger launch in triggers)
- {
- addTrigger(launch.TriggerTemplateID);
- }
- }
- public bool containsTrigger(int tgID)
- {
- for (int i = mTriggerStatus.Count - 1; i >= 0; --i)
- {
- if (mTriggerStatus[i].mData.ID == tgID)
- {
- return true;
- }
- }
- return false;
- }
- public TriggerState findTrigger(int tgID)
- {
- for (int i = mTriggerStatus.Count - 1; i >= 0; --i)
- {
- if (mTriggerStatus[i].mData.ID == tgID)
- {
- return mTriggerStatus[i];
- }
- }
- return null;
- }
- public bool addTrigger(UnitTriggerTemplate trigger)
- {
- if (!containsTrigger(trigger.ID))
- {
- TriggerState ts = new TriggerState(trigger, this);
- mTriggerStatus.Add(ts);
- }
- return false;
- }
- public bool addTrigger(int triggerID)
- {
- if (!containsTrigger(triggerID))
- {
- UnitTriggerTemplate tg = Templates.getUnitTrigger(triggerID);
- if (tg != null)
- {
- TriggerState ts = new TriggerState(tg, this);
- mTriggerStatus.Add(ts);
- }
- }
- return false;
- }
- public void removeTrigger(int tgID)
- {
- TriggerState bs = findTrigger(tgID);
- if (bs != null)
- {
- mTriggerStatus.Remove(bs);
- }
- }
- public bool startTrigger(int tgID, InstanceUnit target)
- {
- TriggerState bs = findTrigger(tgID);
- if (bs != null && bs.IsActive)
- {
- bs.Start(target);
- return true;
- }
- return false;
- }
- public void clearTriggers()
- {
- mTriggerStatus.Clear();
- }
- /// <summary>
- /// 如果被攻击,尝试启动单位触发器
- /// </summary>
- /// <param name="attacker"></param>
- /// <param name="source"></param>
- public void TryStartDamageTriggers(InstanceUnit attacker, AttackSource source)
- {
- foreach (TriggerState ts in this.mTriggerStatus)
- {
- if (ts.IsDamageTrigger && ts.Test(source))
- {
- ts.Start(attacker);
- }
- }
- }
- /// <summary>
- /// 如果攻击别人,尝试启动触发器
- /// </summary>
- /// <param name="target"></param>
- /// <param name="source"></param>
- public void TryStartAttackTriggers(InstanceUnit target, AttackSource source)
- {
- foreach (TriggerState ts in this.mTriggerStatus)
- {
- if (ts.IsAttackTrigger && ts.Test(source))
- {
- ts.Start(target);
- }
- }
- }
- public class UnitTriggerHelper
- {
- private readonly InstanceUnit unit;
- public UnitTriggerHelper(InstanceUnit unit)
- {
- this.unit = unit;
- unit.OnDamage += this.onUnitDamage;
- unit.OnAttack += this.onUnitAttack;
- }
- public void onUnitDamage(InstanceUnit obj, InstanceUnit attacker, int hp, AttackSource source)
- {
- unit.TryStartDamageTriggers(attacker, source);
- }
- public void onUnitAttack(InstanceUnit obj, InstanceUnit target, int hp, AttackSource source)
- {
- unit.TryStartAttackTriggers(target, source);
- }
- }
- #endregion
- //---------------------------------------------------------------------------------------------------------------
- #region _背包和道具_
- public ClientStruct.UnitItemStatus[] GetCurrentItemStatus()
- {
- ClientStruct.UnitItemStatus[] ret = new ClientStruct.UnitItemStatus[mInventorySlots.Count];
- int i = 0;
- foreach (InventorySlot item in mInventorySlots)
- {
- ret[i].ItemTemplateID = item.ItemTemplateID;
- ret[i].Count = item.Count;
- i++;
- }
- return ret;
- }
- public class InventorySlot
- {
- readonly public int Index;
- readonly public InstanceUnit Owner;
- public InventorySlot(int index, InstanceUnit owner)
- {
- this.Index = index;
- this.Owner = owner;
- }
- public bool IsEmpty
- {
- get
- {
- return (Item == null);
- }
- }
- public int ItemTemplateID
- {
- get
- {
- if (Item != null)
- return Item.TemplateID;
- return 0;
- }
- }
- public ItemTemplate Item { get; private set; }
- public int Count { get; private set; }
- internal int AddItem(ItemTemplate item, int count)
- {
- if (item == null) return 0;
- if (count <= 0) return 0;
- if (this.IsEmpty)
- {
- // 背包为空 //
- this.Item = item;
- this.Count = Math.Min(this.Item.MaxStackCount, count);
- this.Owner.doGotInventoryItem(this, item, Index, count);
- return this.Count;
- }
- else if (item.TemplateID == this.ItemTemplateID)
- {
- int oldcount = this.Count;
- // 道具类型一致, 堆叠 //
- this.Count = Math.Min(this.Item.MaxStackCount, this.Count + count);
- if (oldcount != this.Count)
- {
- this.Owner.doGotInventoryItem(this, item, Index, count);
- return this.Count - oldcount;
- }
- return 0;
- }
- return 0;
- }
- internal int TryUse(int count)
- {
- if (IsEmpty) return 0;
- if (!Item.IsInventoryUseable) return 0;
- if (Owner.tryUseItem(Item, Owner))
- {
- return Math.Min(count, this.Count);
- }
- return 0;
- }
- internal int Use(int count)
- {
- if (IsEmpty) return 0;
- if (!Item.IsInventoryUseable) return 0;
- if (Owner.tryUseItem(Item, Owner))
- {
- int used = 0;
- count = Math.Min(count, this.Count);
- for (int i = 0; i < count; i++)
- {
- Owner.UseItem(Item);
- this.Count--;
- used++;
- if (Count == 0)
- {
- break;
- }
- }
- this.Owner.doLostInventoryItem(this, Item, Index, count);
- if (Count == 0)
- {
- this.Item = null;
- }
- return used;
- }
- return 0;
- }
- internal int Drop(int count)
- {
- if (IsEmpty) return 0;
- count = Math.Min(count, this.Count);
- this.Count -= count;
- if (this.Count <= 0)
- {
- this.Count = 0;
- }
- this.Owner.doLostInventoryItem(this, Item, Index, count);
- if (this.Count == 0)
- {
- this.Item = null;
- }
- return count;
- }
- internal int Clean()
- {
- return Drop(this.Count);
- }
- }
- private List<InventorySlot> mInventorySlots = new List<InventorySlot>();
- private HashMap<int, TimeExpire<ItemTemplate>> mCoolDownItems = new HashMap<int, TimeExpire<ItemTemplate>>();
- private List<TimeExpire<ItemTemplate>> mRemoved = new List<TimeExpire<ItemTemplate>>();
- private void InitBagSlots()
- {
- for (int i = 0; i < mInfo.InventorySize; i++)
- {
- mInventorySlots.Add(new InventorySlot(i, this));
- }
- }
- private bool tryUseItem(ItemTemplate item, InstanceUnit item_creater)
- {
- if (mFormula.TryUseItem(this, item, item_creater))
- {
- if (item.UseCoolDownTimeMS > 0)
- {
- if (IsItemCoolDown(item.ID))
- {
- return false;
- }
- return true;
- }
- return true;
- }
- return false;
- }
- private void beginUseItem(ItemTemplate item)
- {
- if (item.UseCoolDownTimeMS > 0)
- {
- mCoolDownItems.Put(item.ID, new TimeExpire<ItemTemplate>(item, item.UseCoolDownTimeMS));
- }
- }
- private void updateItems()
- {
- int intervalMS = Parent.UpdateIntervalMS;
-
- /*
- using (var removed = ListObjectPool<TimeExpire<ItemTemplate>>.AllocAutoRelease())
- {
- foreach (TimeExpire<ItemTemplate> expire in mCoolDownItems.Values)
- {
- if (expire.Update(intervalMS))
- {
- removed.Add(expire);
- }
- }
- if (removed.Count > 0)
- {
- foreach (TimeExpire<ItemTemplate> expire in removed)
- {
- if (expire.IsEnd)
- {
- mCoolDownItems.RemoveByKey(expire.Tag.ID);
- }
- }
- }
- }*/
- if(mCoolDownItems.Count <= 0)
- {
- return;
- }
- //换一个写法
- mRemoved.Clear();
- foreach (TimeExpire<ItemTemplate> expire in mCoolDownItems.Values)
- {
- if (expire.Update(intervalMS))
- {
- mRemoved.Add(expire);
- }
- }
- if (mRemoved.Count > 0)
- {
- foreach (TimeExpire<ItemTemplate> expire in mRemoved)
- {
- if (expire.IsEnd)
- {
- mCoolDownItems.RemoveByKey(expire.Tag.ID);
- }
- }
- mRemoved.Clear();
- }
- }
- protected virtual void clearItemSlots()
- {
- foreach (var slot in mInventorySlots)
- {
- slot.Clean();
- }
- mInventorySlots.Clear();
- mCoolDownItems.Clear();
- }
- /// <summary>
- /// 检测此道具是否在CD
- /// </summary>
- /// <param name="itemTemplateID"></param>
- /// <returns></returns>
- public bool IsItemCoolDown(int itemTemplateID)
- {
- if (mCoolDownItems.ContainsKey(itemTemplateID))
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 添加道具到白空的背包
- /// </summary>
- /// <param name="item"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- public int AddItemToEmptyInventory(ItemTemplate item, int count = 1)
- {
- if (item == null) return 0;
- if (count <= 0) return 0;
- if (!item.IsDuplicateInventory || item.HoldingLimit > 0)
- {
- int holding = GetItemCountInInventory(item.ID);
- if (!item.IsDuplicateInventory)
- {
- if (holding > 0)
- {
- return 0;
- }
- count = 1;
- }
- if (item.HoldingLimit > 0)
- {
- if (holding >= item.HoldingLimit)
- {
- return 0;
- }
- if (holding + count > item.HoldingLimit)
- {
- count = item.HoldingLimit - holding;
- }
- if (count <= 0) return 0;
- }
- }
- int added = 0;
- //优先堆叠//
- for (int i = 0; i < mInventorySlots.Count; i++)
- {
- InventorySlot slot = mInventorySlots[i];
- if (slot.ItemTemplateID == item.ID)
- {
- added += slot.AddItem(item, count - added);
- if (added >= count)
- {
- break;
- }
- }
- }
- //使用空闲格子//
- for (int i = 0; i < mInventorySlots.Count; i++)
- {
- InventorySlot slot = mInventorySlots[i];
- added += slot.AddItem(item, count - added);
- if (added >= count)
- {
- break;
- }
- }
- return added;
- }
- /// <summary>
- /// 添加道具到指定背包
- /// </summary>
- /// <param name="item"></param>
- /// <param name="index"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- public int AddItemToInventory(ItemTemplate item, int index, int count = 1)
- {
- if (item == null) return 0;
- if (count <= 0) return 0;
- if (!item.IsDuplicateInventory || item.HoldingLimit > 0)
- {
- int holding = GetItemCountInInventory(item.ID);
- if (!item.IsDuplicateInventory)
- {
- if (holding > 0)
- {
- return 0;
- }
- count = 1;
- }
- if (item.HoldingLimit > 0)
- {
- if (holding >= item.HoldingLimit)
- {
- return 0;
- }
- if (holding + count > item.HoldingLimit)
- {
- count = item.HoldingLimit - holding;
- }
- if (count <= 0) return 0;
- }
- }
- if (index >= 0 && index < mInventorySlots.Count)
- {
- InventorySlot slot = mInventorySlots[index];
- return slot.AddItem(item, count);
- }
- return 0;
- }
- /// <summary>
- /// 添加道具到指定背包
- /// </summary>
- /// <param name="itemTemplateID"></param>
- /// <param name="index"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- public int AddItemToInventory(int itemTemplateID, int index, int count = 1)
- {
- if (count <= 0) return 0;
- return AddItemToInventory(Templates.getItem(itemTemplateID), index, count);
- }
- private int UseItemInternal(InventorySlot slot, int count)
- {
- if (slot.Item != null)
- {
- if (slot.Item.UseInProgressTimeMS > 0)
- {
- count = slot.TryUse(count);
- if (count > 0)
- {
- this.startPickProgressSelf(slot.Item.UseInProgressTimeMS, (s, p) =>
- {
- slot.Use(count);
- });
- }
- return count;
- }
- else
- {
- return slot.Use(count);
- }
- }
- return 0;
- }
- /// <summary>
- /// 使用背包的道具
- /// </summary>
- /// <param name="index"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- public int UseInventoryItem(int index, int count = 1)
- {
- if (count <= 0) return 0;
- if (index >= 0 && index < mInventorySlots.Count)
- {
- InventorySlot slot = mInventorySlots[index];
- return UseItemInternal(slot, count);
- }
- return 0;
- }
- public int UseInventoryItemByType(int itemTemplateID, int count = 1)
- {
- if (count <= 0) return 0;
- for (int i = 0; i < mInventorySlots.Count; i++)
- {
- InventorySlot slot = mInventorySlots[i];
- if (slot.ItemTemplateID == itemTemplateID)
- {
- return UseItemInternal(slot, count);
- }
- }
- return 0;
- }
- public int DropInventoryItemByIndex(int index, int count = 1)
- {
- if (count <= 0) return 0;
- if (index >= 0 && index < mInventorySlots.Count)
- {
- InventorySlot slot = mInventorySlots[index];
- return slot.Drop(count);
- }
- return 0;
- }
- public int DropInventoryItemByType(int itemTemplateID, int count = 1)
- {
- if (count <= 0) return 0;
- int droped = 0;
- for (int i = 0; i < mInventorySlots.Count; i++)
- {
- InventorySlot slot = mInventorySlots[i];
- if (slot.ItemTemplateID == itemTemplateID)
- {
- droped += slot.Drop(count);
- if (droped >= count)
- {
- break;
- }
- }
- }
- return droped;
- }
- public int ClearInventoryItemByIndex(int index)
- {
- if (index >= 0 && index < mInventorySlots.Count)
- {
- InventorySlot slot = mInventorySlots[index];
- return slot.Clean();
- }
- return 0;
- }
- public int ClearInventoryItemByType(int itemTemplateID)
- {
- int droped = 0;
- for (int i = 0; i < mInventorySlots.Count; i++)
- {
- InventorySlot slot = mInventorySlots[i];
- if (slot.ItemTemplateID == itemTemplateID)
- {
- droped += slot.Clean();
- }
- }
- return droped;
- }
- /// <summary>
- /// 背包内是否有此道具
- /// </summary>
- /// <param name="itemTemplateID"></param>
- /// <returns></returns>
- public bool ContainsItemInInventory(int itemTemplateID)
- {
- for (int i = 0; i < mInventorySlots.Count; i++)
- {
- InventorySlot slot = mInventorySlots[i];
- if (slot.ItemTemplateID == itemTemplateID)
- {
- return true;
- }
- }
- return false;
- }
- public int GetItemCountInInventory(int itemTemplateID)
- {
- int ret = 0;
- for (int i = 0; i < mInventorySlots.Count; i++)
- {
- InventorySlot slot = mInventorySlots[i];
- if (slot.ItemTemplateID == itemTemplateID)
- {
- ret += slot.Count;
- }
- }
- return ret;
- }
- #endregion
- //---------------------------------------------------------------------------------------------------------------
- #region _物理运动_
- public bool IsFallingDown
- {
- get
- {
- if (mCurrentFallingDown != null)
- return !mCurrentFallingDown.IsEnd;
- return false;
- }
- }
- public bool IsDamageFallingDown
- {
- get
- {
- StateDamage damage = (this.CurrentState as StateDamage);
- if (damage != null)
- {
- return damage.IsFallingDown;
- }
- return false;
- }
- }
- public bool IsPhysicalMove
- {
- get
- {
- if (mCurrentStartMove != null)
- return !mCurrentStartMove.IsEnd;
- return false;
- }
- }
- private FallingDown mCurrentFallingDown;
- private HitMoveSpeed mCurrentStartMove;
- private void updatePhysical()
- {
- if (mCurrentFallingDown != null && mCurrentFallingDown.Update(Parent.UpdateIntervalMS))
- {
- mCurrentFallingDown = null;
- }
- if (mCurrentStartMove != null && mCurrentStartMove.Update(Parent.UpdateIntervalMS))
- {
- mCurrentStartMove = null;
- }
- }
- /// <summary>
- /// 开始自由落体
- /// </summary>
- /// <param name="launcher"></param>
- /// <param name="speedZ"></param>
- /// <param name="gravity"></param>
- /// <param name="zlimit"></param>
- /// <returns></returns>
- public virtual FallingDown StartFly(object launcher, float speedZ, float gravity, float zlimit)
- {
- mCurrentFallingDown = new FallingDown(this, speedZ, gravity, zlimit);
- return mCurrentFallingDown;
- }
- /// <summary>
- /// 开始复杂移动
- /// </summary>
- /// <param name="launcher"></param>
- /// <param name="direction"></param>
- /// <param name="rotateSpeedSEC"></param>
- /// <param name="expectlTimeMS"></param>
- /// <param name="moveSpeedSEC"></param>
- /// <param name="moveSpeedAdd"></param>
- /// <param name="moveSpeedAcc"></param>
- /// <param name="isNoneTouch"></param>
- public virtual HitMoveSpeed StartHitMove(
- object launcher,
- float direction,
- float rotateSpeedSEC,
- int expectlTimeMS,
- float moveSpeedSEC,
- float moveSpeedAdd,
- float moveSpeedAcc,
- bool isNoneTouch)
- {
- if (mCurrentStartMove != null)
- {
- mCurrentStartMove.Stop();
- }
- HitMoveSpeed move = new HitMoveSpeed(
- this,
- direction,
- rotateSpeedSEC,
- expectlTimeMS,
- moveSpeedSEC,
- moveSpeedAdd,
- moveSpeedAcc,
- isNoneTouch);
- mCurrentStartMove = move;
- return move;
- }
- /// <summary>
- /// 落体运动
- /// </summary>
- public class FallingDown
- {
- private readonly InstanceUnit unit;
- private readonly float gravity;
- private readonly float z_limit;
- private float z_speed;
- public FallingDown(InstanceUnit unit, float zspeed, float zgravity, float zlimit)
- {
- this.unit = unit;
- this.z_limit = zlimit;
- this.z_speed = zspeed;
- this.gravity = zgravity == 0 ? unit.Templates.CFG.GLOBAL_GRAVITY : zgravity;
- this.ExpectTimeMS = MoveHelper.CalculateFlyTimeMS(unit.Z, zspeed, zlimit, gravity, 10);
- this.IsEnd = false;
- }
- public bool Update(int intervalMS)
- {
- //向上受到ZLimit限制//
- unit.Z += MoveHelper.GetDistance(intervalMS, z_speed);
- unit.Z = Math.Max(0, unit.Z);
- this.IsEnd = unit.Z <= 0;
- //骤停//
- if (z_limit != 0 && z_speed > 0 && unit.Z > z_limit)
- {
- z_speed = 0;
- }
- this.z_speed -= MoveHelper.GetDistance(intervalMS, gravity);
- return IsEnd;
- }
- public bool IsEnd { get; private set; }
- public int ExpectTimeMS { get; private set; }
- public float ZSpeedSEC { get { return z_speed; } }
- public float ZLimit { get { return z_limit; } }
- }
- public class HitMoveSpeed
- {
- private readonly InstanceUnit mOwner;
- private readonly float mStartDirection;
- private readonly float mMoveSpeedAdd;
- private readonly float mMoveSpeedAcc;
- private readonly float mRotateSpeedSEC;
- private float moveSpeedSEC;
- private TimeExpire<int> hitMoveTime;
- private FallingDown hasFly;
- private IPositionObject moveTarget;
- private bool moveTargetBody;
- private bool isNoneTouch = false;
- public HitMoveSpeed(
- InstanceUnit owner,
- float direction,
- float rotateSpeedSEC,
- int expectlTimeMS,
- float moveSpeedSEC,
- float moveSpeedAdd,
- float moveSpeedAccPct,
- bool isNoneTouch)
- {
- this.mOwner = owner;
- this.mStartDirection = direction;
- this.moveSpeedSEC = moveSpeedSEC;
- this.mMoveSpeedAdd = moveSpeedAdd;
- this.mMoveSpeedAcc = moveSpeedAccPct / 100f;
- this.mRotateSpeedSEC = rotateSpeedSEC;
- this.isNoneTouch = isNoneTouch;
- this.TotalTimeMS = expectlTimeMS;
- this.hitMoveTime = new TimeExpire<int>(TotalTimeMS);
- this.IsEnd = false;
- this.PrevX = owner.X;
- this.PrevY = owner.Y;
- }
- public void SetFly(object launcher, float moveZSpeed, float gravity, float zlimit)
- {
- if (moveZSpeed != 0)
- {
- this.hasFly = mOwner.StartFly(launcher, moveZSpeed, gravity, zlimit);
- this.TotalTimeMS = hasFly.ExpectTimeMS;
- this.hitMoveTime = new TimeExpire<int>(TotalTimeMS);
- }
- }
- public void SetMoveTarget(IPositionObject target, bool targetBodyBlock)
- {
- moveTarget = target;
- moveTargetBody = targetBodyBlock;
- }
- public void Stop()
- {
- this.IsEnd = true;
- }
- public bool Update(int intervalMS)
- {
- int intervalMS_move = intervalMS;
- if (hitMoveTime.PassTimeMS + intervalMS >= hitMoveTime.TotalTimeMS)
- {
- intervalMS_move = hitMoveTime.TotalTimeMS - hitMoveTime.PassTimeMS;
- }
- if (mRotateSpeedSEC != 0)
- {
- mOwner.turn(MoveHelper.GetDistance(intervalMS_move, mRotateSpeedSEC));
- }
- this.PrevX = mOwner.X;
- this.PrevY = mOwner.Y;
- // 移动 //
- if (moveTarget != null)
- {
- move(intervalMS_move, moveTarget);
- }
- else
- {
- move(intervalMS_move, mStartDirection);
- }
- // 递增 //
- {
- //每秒递减速度绝对值//
- MoveHelper.UpdateSpeed(intervalMS_move, ref moveSpeedSEC, mMoveSpeedAdd, mMoveSpeedAcc);
- }
- if (hitMoveTime.PassTimeMS + intervalMS >= hitMoveTime.TotalTimeMS || hitMoveTime.Update(intervalMS))
- {
- IsEnd = true;
- }
- return IsEnd;
- }
- private void move(int intervalMS, IPositionObject target)
- {
- if (moveTargetBody)
- {
- float distance = MoveHelper.GetDistance(intervalMS, moveSpeedSEC);
- if (CMath.includeRoundPoint(mOwner.X, mOwner.Y, target.RadiusSize + mOwner.RadiusSize + distance, target.X, target.Y))
- {
- return;
- }
- }
- if (hasFly != null)
- {
- mOwner.jumpToTarget(target.X, target.Y, moveSpeedSEC, intervalMS, IsNoneTouch);
- }
- else
- {
- if (IsNoneTouch)
- {
- mOwner.jumpToTarget(target.X, target.Y, moveSpeedSEC, intervalMS, IsNoneTouch);
- }
- else if (mMoveSpeedAdd != 0 || mMoveSpeedAcc != 0)
- {
- mOwner.moveImpactTo(target.X, target.Y, moveSpeedSEC, intervalMS);
- }
- else
- {
- mOwner.moveBlockTo(target.X, target.Y, moveSpeedSEC, intervalMS);
- }
- }
- }
- private void move(int intervalMS, float direction)
- {
- if (hasFly != null)
- {
- mOwner.jumpTo(direction, moveSpeedSEC, intervalMS, IsNoneTouch);
- }
- else
- {
- if (IsNoneTouch)
- {
- mOwner.jumpTo(direction, moveSpeedSEC, intervalMS, IsNoneTouch);
- }
- else if (mMoveSpeedAdd != 0 || mMoveSpeedAcc != 0)
- {
- mOwner.moveImpact(direction, moveSpeedSEC, intervalMS);
- }
- else
- {
- mOwner.moveBlockTo(direction, moveSpeedSEC, intervalMS);
- }
- }
- }
- public float PrevX { get; private set; }
- public float PrevY { get; private set; }
- public bool IsFly { get { return hasFly != null; } }
- public int TotalTimeMS { get; private set; }
- public bool IsEnd { get; private set; }
- public bool IsNoneTouch
- {
- get { return isNoneTouch; }
- set { this.isNoneTouch = value; }
- }
- public float ZSpeedSEC
- {
- get
- {
- if (hasFly != null)
- return hasFly.ZSpeedSEC;
- return 0;
- }
- }
- }
- #endregion
- //---------------------------------------------------------------------------------------------------------------
- }
- }
|