InstanceUnit.cs 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonAI.RTS;
  5. using CommonLang.Vector;
  6. using CommonAI.Zone;
  7. using CommonAI.Zone.Instance;
  8. using CommonAI.RTS.Manhattan;
  9. using CommonLang;
  10. using CommonLang.Log;
  11. using CommonAI.Zone.Formula;
  12. using CommonAI.ZoneClient;
  13. using CommonAI.Zone.Helper;
  14. using CommonLang.Property;
  15. using CommonAI.Zone.Attributes;
  16. using CommonAI.Zone.EventTrigger;
  17. using CommonAI.Zone.Instance.Helper;
  18. using CommonAI.Data;
  19. using static CommonAI.Zone.UnitInfo;
  20. using CommonAI.data;
  21. using CommonAI.ZoneServer.JSGModule;
  22. namespace CommonAI.Zone.Instance
  23. {
  24. abstract public partial class InstanceUnit : InstanceZoneObject
  25. {
  26. private readonly UnitInfo mInfo;
  27. private readonly DropItemGenerator mDropItems;
  28. /// <summary>
  29. /// 统计信息
  30. /// </summary>
  31. public IUnitStatistic Statistic { get; private set; }
  32. //最后一次受伤时间
  33. public long mLastDamageTime;
  34. //最后一次攻击事件
  35. public long mLastHitOtherTime;
  36. //上一次处理死亡回调时间
  37. public long mProcessDeadCallbackTime;
  38. private IFormula mFormula = TemplateManager.Factory.Formula;
  39. private IVirtualUnit mUnitVirtual;
  40. private readonly string mName;
  41. private int mForce = 0;
  42. private int mAlliesForce = 0; // 服务器对战情况下,有盟友服务器
  43. private int mLevel = 0;
  44. private int mMoney = 0;
  45. private readonly bool mIsAttackRangeIncludeBodySize;
  46. // 当前HP
  47. private readonly RangeValue __mCurrentHP;
  48. // 当前MP
  49. private readonly RangeValue __mCurrentMP;
  50. // 当前移动速度加成
  51. private float __mCurrentMoveSpeedSEC = 0;
  52. // 攻速
  53. private int __mCurrentAttackSpeed = 10000;
  54. // 当前技能速度加成
  55. private float __mSkillActionSpeedRate = 0;
  56. // 当前快速施法速度
  57. private float mFastCastRate = 0;
  58. /// <summary>
  59. /// 当前单位是否地图阻挡
  60. /// </summary>
  61. private bool mIntersectMap = false;
  62. /// <summary>
  63. /// 当前单位是否单位阻挡
  64. /// </summary>
  65. private bool mIntersectObj = false;
  66. // 当前技能cd减少
  67. private float __mCurrentSkillCdReduce;
  68. // 控制减速,韧性
  69. private float __mCurrentControledTimeReduce;
  70. // 控制增幅
  71. private float __mControledTimeAdd;
  72. /// 单位最后死亡时间
  73. public long mDeadTime = 0;
  74. //游戏服标记字段,怪物死亡的时候,回传给游戏服
  75. public int gameServerFlag = 0;
  76. //回血
  77. public int recoverHP = 0;
  78. //随从最后一次瞬移时间
  79. public long petTeleportTime = 0;
  80. //最后一次返回召回随从操作频繁时间
  81. public long lastTeleportTime = 0;
  82. //绑定攻击玩家
  83. private string mBindAttackPlayerId;
  84. //主人id
  85. private long mRemoveSelfTime;
  86. private InstanceUnit mBindMaster;
  87. //检测散开间隔
  88. protected long mCheckAttackSpread = 0;
  89. /// <summary>
  90. /// 自动恢复计数器
  91. /// </summary>
  92. private TimeInterval<int> mRecoveryTime = null;
  93. public InstanceUnit(InstanceZone zone, UnitInfo uinfo, string name, int force, int level, bool is_static_block = false, int alliesForce = 0)
  94. : base(zone, is_static_block)
  95. {
  96. this.mInfo = (UnitInfo)uinfo.Clone();
  97. this.mName = string.IsNullOrEmpty(name) ? "" : name;
  98. this.mIsAttackRangeIncludeBodySize = zone.Templates.CFG.OBJECT_ATTACK_RANGE_INCLUDE_BODYSIZE;
  99. this.mForce = force;
  100. this.mAlliesForce = alliesForce;
  101. this.mLevel = level;
  102. this.mIntersectMap = true;
  103. this.mIntersectObj = !Templates.CFG.OBJECT_NONE_TOUCH && (mInfo.BodySize > 0);
  104. this.mSyncInfo = new SyncUnitInfo(zone.IsHalfSync);
  105. this.mSyncInfo.Name = mName;
  106. this.mSyncInfo.TemplateID = mInfo.ID;
  107. this.__mCurrentHP = new RangeValue(mInfo.HealthPoint, 0, mInfo.HealthPoint);
  108. this.__mCurrentMP = new RangeValue(mInfo.ManaPoint, 0, mInfo.ManaPoint);
  109. this.__mCurrentMoveSpeedSEC = mInfo.MoveSpeedSEC;
  110. this.mDropItems = new DropItemGenerator(uinfo.DropItemsSet);
  111. this.mTriggerHelper = new UnitTriggerHelper(this);
  112. if (mInfo.IdleRecover && mInfo.RecoveryIntervalMS > 0)
  113. {
  114. this.mRecoveryTime = new TimeInterval<int>(mInfo.RecoveryIntervalMS);
  115. this.recoverHP = Info.HealthRecoveryPoint;
  116. }
  117. this.InitTimeLines();
  118. this.InitBagSlots();
  119. this.InitSkills(mInfo.BaseSkillID, mInfo.Skills.ToArray());
  120. this.Statistic = this.CreateUnitStatistic();
  121. this.mUnitVirtual = TemplateManager.Factory.CreateUnitVirtual(this);
  122. }
  123. public override bool IsNeedProcessDead()
  124. {
  125. return this.mProcessDeadTime == 0;
  126. }
  127. protected override void Disposing()
  128. {
  129. //if(this.IsMonster && (this.mProcessDeadTime == 0 || this.CurrentHP > 0 || this.mProcessDeadTime == 0))
  130. //{
  131. // log.Info("单位Disposing:" + this.Parent.UUID + ", " + this.Parent.GetSceneID() + ", " + this.ID + ", " + this.CurrentHP + "," +
  132. // this.IsMonster + ", " + mProcessDeadTime);
  133. //}
  134. if (mUnitVirtual != null)
  135. {
  136. mUnitVirtual.OnDispose(this);
  137. }
  138. base.Disposing();
  139. this.clearBindEvents();
  140. this.clearSkills();
  141. this.clearBuffs();
  142. this.clearItemSlots();
  143. this.clearTriggers();
  144. this.mNoneBlockTimeMS.Disposing();
  145. this.mStunTimeMS.Disposing();
  146. this.mInvisibleTimeMS.Disposing();
  147. this.mInvincibleTimeMS.Disposing();
  148. this.mSilentTimeMS.Disposing();
  149. this.mCannotMoveTimeMS.Disposing();
  150. this.mIgnoreControl.Disposing();
  151. foreach (MultiTimeLine timeLine in mMultiTimeLineGroup)
  152. {
  153. timeLine.Disposing();
  154. }
  155. this.mMultiTimeLineGroup.Clear();
  156. this.mMultiTimeLineGroup = null;
  157. this.mMultiTimeLineSync = null;
  158. }
  159. protected override void onAdded(bool pointLv)
  160. {
  161. if (this.mUnitVirtual != null)
  162. {
  163. this.mUnitVirtual.OnInit(this, pointLv);
  164. if (this.mRecoveryTime != null && this.Info.UType == UnitType.TYPE_MONSTER)
  165. {
  166. this.recoverHP = Info.HealthRecoveryPoint == 0 ? (int)(this.MaxHP * 0.005f) : Info.HealthRecoveryPoint;
  167. }
  168. }
  169. this.mSyncInfo.ObjectID = base.ID;
  170. this.mSyncInfo.IsTouchObj = this.mIntersectObj;
  171. this.mSyncInfo.IsTouchMap = this.mIntersectMap;
  172. this.mSyncInfo.IsStaticBlockable = this.IsStaticBlockable;
  173. this.mSyncInfo.fateType = (byte)this.Virtual.GetUnitFateType();
  174. if (mInfo.SpawnTimeMS > 0)
  175. {
  176. this.SetInvincibleTimeMS(mInfo.SpawnTimeMS);
  177. }
  178. if (this.mInfo.InventoryList != null)
  179. {
  180. foreach (InventoryItem item in mInfo.InventoryList)
  181. {
  182. ItemTemplate temp = Templates.getItem(item.ItemTemplateID);
  183. if (temp != null)
  184. {
  185. AddItemToEmptyInventory(temp, item.Count);
  186. }
  187. }
  188. }
  189. if (mInfo.Events != null)
  190. {
  191. foreach (int evt_id in mInfo.Events)
  192. {
  193. this.BindUnitEvent(evt_id);
  194. }
  195. }
  196. if (mOnAdded != null)
  197. {
  198. mOnAdded.Invoke(this);
  199. }
  200. }
  201. protected override void onRemoved()
  202. {
  203. if (mOnRemoved != null)
  204. {
  205. mOnRemoved.Invoke(this);
  206. }
  207. foreach (UnitEventTriggerCollection uc in mBindEvents.Values)
  208. {
  209. uc.Dispose();
  210. }
  211. mBindEvents.Clear();
  212. if (Info.RemovedEffect != null)
  213. {
  214. Parent.queueEvent(new AddEffectEvent(0/*this.ID*/, X, Y, Direction, Info.RemovedEffect));
  215. }
  216. }
  217. public void BindAttackPlayer(string playerId)
  218. {
  219. this.mBindAttackPlayerId = playerId;
  220. }
  221. public string GetAttackPlayer()
  222. {
  223. return this.mBindAttackPlayerId;
  224. }
  225. public void BindMasterId(int masterId)
  226. {
  227. InstanceUnit master = this.Parent.getUnitByID(masterId);
  228. if (master == null)
  229. {
  230. log.Error("BindMasterId找不到单位:" + this.Parent.GetSceneID() + ", " + masterId);
  231. return;
  232. }
  233. if (this.mBindMaster != null)
  234. {
  235. log.Error("BindMasterId重复:" + this.Info.ID + ", " + master.PlayerUUID);
  236. return;
  237. }
  238. this.mBindMaster = master;
  239. }
  240. //--------------------------------------------------------------------------
  241. /// <summary>
  242. /// 扩展功能绑定接口
  243. /// </summary>
  244. public IVirtualUnit Virtual { get { return mUnitVirtual; } }
  245. /// <summary>
  246. /// 扩展数据
  247. /// </summary>
  248. public virtual IUnitProperties Properties { get { return mInfo.Properties; } }
  249. //--------------------------------------------------------------------------
  250. //--------------------------------------------------------------------------
  251. public UnitInfo Info { get { return mInfo; } }
  252. public string Name { get { return mName; } }
  253. public string Alias { get { return mSyncInfo.Alias; } set { if (value != null) { mSyncInfo.Alias = value; } } }
  254. public int Force { get { return mForce; } protected set { mForce = value; } }
  255. public int AlliesForce { get { return mAlliesForce; } protected set { mAlliesForce = value; } }
  256. public float MoveSpeedSEC { get { return __mCurrentMoveSpeedSEC; } }
  257. public float SkillCdReduce { get { return __mCurrentSkillCdReduce; } }
  258. public float ControledTimeReduce { get { return __mCurrentControledTimeReduce; } }
  259. public float ControledTimeAdd { get { return __mControledTimeAdd; } }
  260. public float FastCastRate { get { return mFastCastRate; } }
  261. public override float BodyBlockSize { get { return mInfo.BodySize; } }
  262. public override float BodyHitSize { get { return mInfo.BodyHitSize; } }
  263. public override float BodyHeight { get { return mInfo.BodyHeight; } }
  264. public override float Weight { get { return mInfo.Weight; } }
  265. public override bool IntersectMap { get { return mIntersectMap; } }
  266. public override bool IntersectObj { get { return mIntersectObj && IsVisible; } }
  267. public override bool Moveable { get { return mInfo.IsMoveable; } }
  268. public override bool ClientVisible { get { return true; } }
  269. public override bool SyncPos { get { return true; } }
  270. public override bool IsDead() { return __mCurrentHP.Value <= 0; }
  271. public int DeadCount { get { return Statistic.DeadCount; } }
  272. public long LastDeadTimeMS { get { return mDeadTime; } }
  273. public bool CanWhiplashDeadBody { get { return Templates.CFG.UNIT_CAN_WHIPLASH_BODY && (LastDeadTimeMS + mInfo.DeadTimeMS > Parent.PassTimeMS); } }
  274. /// <summary>
  275. /// 是否为玩家
  276. /// </summary>
  277. virtual public bool IsPlayer { get { return false; } }
  278. virtual public bool IsMonster { get { return false; } }
  279. virtual public bool IsPet { get { return false; } }
  280. //玩家或者宠物合集
  281. virtual public bool IsPlayerUnit { get { return false; } }
  282. virtual public void OnUnitDead() { }
  283. /// <summary>
  284. /// 是否中立
  285. /// </summary>
  286. virtual public bool IsNature { get { return false; } }
  287. /// <summary>
  288. /// 此单位是否能被攻击并且活着
  289. /// </summary>
  290. virtual public bool IsActive { get { return (__mCurrentHP.Value > 0) && (base.Enable)/*&& !IsInvincible && IsVisible*/; } }
  291. /// <summary>
  292. /// 此单位是否可以被打到,包括鞭尸
  293. /// </summary>
  294. virtual public bool IsAttackable { get { return (base.Enable) /*&& !IsInvincible && IsVisible*/; } }
  295. /// <summary>
  296. /// 此单位是否无技能
  297. /// </summary>
  298. public bool IsNoneSkill {
  299. get
  300. {
  301. foreach (SkillState st in mSkillStatus.Skills)
  302. {
  303. if (st.LaunchSkill.AutoLaunch) return false;
  304. }
  305. return true;
  306. }
  307. }
  308. /// <summary>
  309. /// 技能可产生位移,或者多段由服务器决定
  310. /// </summary>
  311. virtual public bool IsSkillControllableByServer { get { return true; } }
  312. /// <summary>
  313. /// 单位是否可控
  314. /// </summary>
  315. virtual public bool IsControllable { get { return IsActive && !IsStun && CurrentActionStatus != UnitActionStatus.Damage; } }
  316. /// <summary>
  317. /// 用于显示的,单位横向数据
  318. /// </summary>
  319. public ZoneClient.IUnitVisibleData VisibleInfo { get { return mSyncInfo.VisibleInfo; } }
  320. //-----------------------------------------------------------------------------------------------------//
  321. protected virtual IUnitStatistic CreateUnitStatistic()
  322. {
  323. return new UnitStatistic(this);
  324. }
  325. //-----------------------------------------------------------------------------------------------------//
  326. #region __StateMachine__
  327. private bool is_init = false;
  328. private State current_state = null;
  329. private State next_state;
  330. private Queue<State> next_state_queue = new Queue<State>();
  331. /// <summary>
  332. /// 当前状态机
  333. /// </summary>
  334. public State CurrentState { get { return current_state; } }
  335. //状态机额外数据
  336. public long chuangongTime = 0;
  337. /// <summary>
  338. /// 下一个状态
  339. /// </summary>
  340. protected State NextState { get { return next_state; } }
  341. /// <summary>
  342. /// 死亡状态可能的状态机,一般配合<see cref="IsDead"/>使用
  343. /// </summary>
  344. public bool IsStateDead
  345. {
  346. get
  347. {
  348. if ((current_state is StateDead) ||
  349. (current_state is StateDamage) ||
  350. (current_state is StateDeadFuck) ||
  351. (current_state is StateDeadFuckFuck) ||
  352. (current_state is StateRebirth) ||
  353. (next_state is StateDead) ||
  354. (next_state is StateDamage) ||
  355. (next_state is StateDeadFuck) ||
  356. (next_state is StateDeadFuckFuck) ||
  357. (next_state is StateRebirth))
  358. {
  359. return true;
  360. }
  361. return false;
  362. }
  363. }
  364. public void queueState(State s)
  365. {
  366. next_state_queue.Enqueue(s);
  367. }
  368. public bool TryEnqueueIdleState()
  369. {
  370. if (next_state_queue.Count > 0)
  371. {
  372. return false;
  373. }
  374. next_state_queue.Enqueue(new StateIdle(this));
  375. return true;
  376. }
  377. public void queueCurrentState(State s)
  378. {
  379. if (current_state != null)
  380. {
  381. if (!changeState(s))
  382. {
  383. State.StateStopHandler onstop = new State.StateStopHandler((obj, st) =>
  384. {
  385. changeState(s);
  386. });
  387. current_state.AddStopOnce(onstop);
  388. }
  389. }
  390. else
  391. {
  392. changeState(s);
  393. }
  394. }
  395. public bool changeState(State s, bool force = false)
  396. {
  397. if (s == current_state)
  398. {
  399. return true;
  400. }
  401. if (s == next_state)
  402. {
  403. return true;
  404. }
  405. if (s.unit != this)
  406. {
  407. throw new Exception("State is not Owner unit : " + s);
  408. }
  409. if (current_state == null || current_state.onBlock(s) || force)
  410. {
  411. if (next_state == null || next_state.onBlock(s) || force)
  412. {
  413. if (next_state != null)
  414. {
  415. next_state.stop();
  416. }
  417. onNewStateBeginChange(current_state, ref s);
  418. next_state = s;
  419. return true;
  420. }
  421. }
  422. return false;
  423. }
  424. private void updateState()
  425. {
  426. //绑定主人类怪物,脱战或者主人移除自杀
  427. if (mRemoveSelfTime == 0)
  428. {
  429. if (this.mBindMaster != null && (!this.mBindMaster.IsActive || this.mBindMaster.Virtual.GetBattleStatus() <= BattleStatus.ReadyBattle))
  430. {
  431. //添加移除逻辑
  432. mRemoveSelfTime = CommonLang.CUtils.localTimeMS + 3000;
  433. }
  434. }
  435. else if (mRemoveSelfTime < CommonLang.CUtils.localTimeMS)
  436. {
  437. this.kill(null, false);
  438. }
  439. if (IsDead() && (!IsStateDead))
  440. {
  441. changeState(new StateDead(this, this, false), true);
  442. queueEvent(new UnitDeadEvent(ID, this.ID, false, mInfo.RebirthTimeMS));
  443. if(this.mProcessDeadCallbackTime < CommonLang.CUtils.localTimeMS)
  444. {
  445. log.Warn("手动调用死亡逻辑2:" + this.PlayerUUID + ", " + this.mZone.GetSceneID() + ", templateID:" + this.Info.ID + ", state: " + this.current_state);
  446. Parent.cb_unitDeadCallBack(this, this);
  447. }
  448. }
  449. else if (next_state == null && next_state_queue.Count > 0)
  450. {
  451. // 尝试从队列中取一个状态机,有机会就执行 //
  452. while (next_state_queue.Count > 0)
  453. {
  454. State queued_state = next_state_queue.Peek();
  455. if (changeState(queued_state))
  456. {
  457. next_state_queue.Dequeue();
  458. }
  459. else { break; }
  460. }
  461. }
  462. if (next_state != null && next_state != current_state)
  463. {
  464. State old_state = current_state;
  465. if (old_state != null)
  466. {
  467. old_state.stop();
  468. }
  469. this.current_state = next_state;
  470. this.next_state = null;
  471. this.current_state.start();
  472. this.onStateChanged(old_state, current_state);
  473. if (mOnStateChanged != null)
  474. {
  475. mOnStateChanged.Invoke(this, old_state, current_state);
  476. }
  477. }
  478. if (current_state != null)
  479. {
  480. current_state.update();
  481. }
  482. UpdateTimeLines(Parent.UpdateIntervalMS);
  483. }
  484. #endregion
  485. protected virtual void onUpdateRecover()
  486. {
  487. //自动恢复
  488. if (this.recoverHP > 0 && mRecoveryTime != null && this.CurrentHP < this.MaxHP && CurrentActionStatus != UnitActionStatus.Damage
  489. && this.Virtual.GetBattleStatus() == BattleStatus.None && !IsDead())
  490. {
  491. if (mRecoveryTime.Update(Parent.UpdateIntervalMS))
  492. {
  493. AddHP(this.recoverHP, null);
  494. //AddMP(Info.ManaRecoveryPoint);
  495. }
  496. }
  497. }
  498. override protected void onUpdate(bool slowRefresh)
  499. {
  500. if (!is_init)
  501. {
  502. is_init = true;
  503. onInit();
  504. }
  505. if (!IsPaused)
  506. {
  507. updatePhysical();
  508. updateState();
  509. onUpdateRecover();
  510. updateSkills();
  511. updateBuffs();
  512. updateTriggers(slowRefresh);
  513. updateItems();
  514. if (mOnUpdate != null)
  515. {
  516. mOnUpdate.Invoke(this);
  517. }
  518. }
  519. updateSyncFields();
  520. updateSyncSkillActives();
  521. }
  522. internal void doAction(ObjectAction act)
  523. {
  524. this.onAction(act);
  525. if (mOnHandleObjectAction != null)
  526. {
  527. mOnHandleObjectAction.Invoke(this, act);
  528. }
  529. }
  530. private void doRebirth(int max_hp, int max_mp)
  531. {
  532. next_state_queue.Clear();
  533. if (this.Moveable)
  534. {
  535. this.mIntersectObj = !Templates.CFG.OBJECT_NONE_TOUCH && (mInfo.BodySize > 0);
  536. }
  537. else
  538. {
  539. this.mIntersectObj = (mInfo.BodySize > 0);
  540. }
  541. if (max_hp == 0) max_hp = this.MaxHP;
  542. if (max_mp == 0) max_mp = this.MaxMP;
  543. if (max_hp != 0) this.__mCurrentHP.SetValue(max_hp);
  544. if (max_mp != 0) this.__mCurrentMP.SetValue(max_mp);
  545. syncFields(UnitFieldChangedEvent.MASK_HP | UnitFieldChangedEvent.MASK_MP);
  546. Parent.cb_unitRebirthCallBack(this);
  547. queueEvent(new UnitRebirthEvent(ID));
  548. }
  549. public void updateContinueKills()
  550. {
  551. if (this.Statistic.continueKills < short.MaxValue)
  552. {
  553. ++this.Statistic.continueKills;
  554. }
  555. UnitContinueKillValue temp = new UnitContinueKillValue(this.ID, this.Statistic.continueKills);
  556. queueEvent(temp);
  557. }
  558. public void resetContinueKills()
  559. {
  560. this.Statistic.continueKills = 0;
  561. }
  562. private void doActivated()
  563. {
  564. next_state_queue.Clear();
  565. if (this.Moveable)
  566. {
  567. this.mIntersectObj = !Templates.CFG.OBJECT_NONE_TOUCH && (mInfo.BodySize > 0);
  568. }
  569. else
  570. {
  571. this.mIntersectObj = (mInfo.BodySize > 0);
  572. }
  573. this.addTriggers(mInfo.Triggers);
  574. Parent.cb_unitActivatedCallBack(this);
  575. }
  576. internal void doDead(InstanceUnit killer)
  577. {
  578. if (killer != null)
  579. {
  580. this.Statistic.LogDead(killer.Statistic);
  581. }
  582. this.mIntersectObj = false;
  583. this.clearBuffs();
  584. //吴永辉:5v5人物死亡后会重置技能CD 无论在哪个地图死亡都不能重置 你可能要指给黄鱼 bugid=2748
  585. //this.ClearAllSkillCD();
  586. this.resetAI();
  587. }
  588. //------------------------------------------------------------------------------------------------------//
  589. internal void callback_onActivated(InstanceZone zone)
  590. {
  591. if (this.mOnActivated != null)
  592. this.mOnActivated.Invoke(this);
  593. }
  594. internal void callback_onAttack(InstanceZone zone, InstanceUnit target, int reduceHP, AttackSource source)
  595. {
  596. if (this.mOnAttack != null)
  597. this.mOnAttack(this, target, reduceHP, source);
  598. }
  599. internal void callback_onDamage(InstanceZone zone, InstanceUnit attacker, int reduceHP, AttackSource source)
  600. {
  601. if (this.mOnDamage != null)
  602. this.mOnDamage.Invoke(this, attacker, reduceHP, source);
  603. }
  604. public long mProcessDeadTime = 0;
  605. internal void callback_onDead(InstanceZone zone, InstanceUnit attacker)
  606. {
  607. this.mProcessDeadTime = CommonLang.TimeUtil.GetTimestampMS();
  608. this.OnUnitDead();
  609. if (this.mOnDead != null)
  610. this.mOnDead.Invoke(this, attacker);
  611. }
  612. internal void callback_onRebirth(InstanceZone zone)
  613. {
  614. if (this.mOnRebirth != null)
  615. this.mOnRebirth.Invoke(this);
  616. }
  617. internal void callback_onGotInstanceItem(InstanceZone zone, InstanceItem item)
  618. {
  619. if (this.mOnGotInstanceItem != null)
  620. this.mOnGotInstanceItem.Invoke(this, item);
  621. }
  622. internal void callback_onGotInventoryItem(InstanceZone zone, ItemTemplate item)
  623. {
  624. if (this.mOnGotInventoryItem != null)
  625. this.mOnGotInventoryItem.Invoke(this, item);
  626. }
  627. internal void callback_onLostInventoryItem(InstanceZone zone, ItemTemplate item)
  628. {
  629. if (this.mOnLostInventoryItem != null)
  630. this.mOnLostInventoryItem.Invoke(this, item);
  631. }
  632. internal void callback_onUseItem(InstanceZone zone, ItemTemplate item, InstanceUnit item_creater)
  633. {
  634. if (this.mOnUseItem != null)
  635. this.mOnUseItem.Invoke(this, item, item_creater);
  636. }
  637. internal void callback_onGotBuff(InstanceZone zone, InstanceUnit.BuffState buff)
  638. {
  639. if (this.mOnGotBuff != null)
  640. this.mOnGotBuff.Invoke(this, buff);
  641. }
  642. internal void callback_onLostBuff(InstanceZone zone, InstanceUnit.BuffState buff)
  643. {
  644. if (this.mOnLostBuff != null)
  645. this.mOnLostBuff.Invoke(this, buff);
  646. }
  647. //-----------------------------------------------------------------------------------------------------//
  648. private void onInit()
  649. {
  650. syncFields(UnitFieldChangedEvent.MASK_ALL);
  651. if (mInfo.SpawnTimeMS > 0)
  652. {
  653. changeState(new StateSpawn(this, mInfo.SpawnTimeMS));
  654. }
  655. else
  656. {
  657. changeState(new StateIdle(this));
  658. doActivated();
  659. }
  660. }
  661. #region _Overrideable_
  662. /// <summary>
  663. /// 收到协议
  664. /// </summary>
  665. /// <param name="act"></param>
  666. virtual protected void onAction(ObjectAction act) { }
  667. /// <summary>
  668. /// 新的状态将要切换时回调
  669. /// </summary>
  670. virtual protected void onNewStateBeginChange(State old_state, ref State new_state) { }
  671. /// <summary>
  672. /// 状态已切换时回调
  673. /// </summary>
  674. /// <param name="old_state"></param>
  675. /// <param name="state"></param>
  676. virtual protected void onStateChanged(State old_state, State state) { }
  677. // 被攻击时回调
  678. virtual protected void onDamaged(InstanceUnit attacker, AttackSource source, int reduceHP) { }
  679. virtual protected void onDead(InstanceUnit killer) { }
  680. #endregion
  681. /// <summary>
  682. /// 单位被攻击核心函数,里面处理受击状态,死亡状态
  683. /// </summary>
  684. /// <param name="attacker"></param>
  685. /// <param name="source"></param>
  686. virtual protected internal void doHitAttack(InstanceUnit attacker, AttackSource source)
  687. {
  688. if (IsDead())
  689. {
  690. //死亡后鞭尸//
  691. if (Moveable && CanWhiplashDeadBody)
  692. {
  693. source.Begin(this);
  694. changeState(new StateDamage(this, source, attacker));
  695. }
  696. doHitAttackEndEffect(attacker, source);
  697. return;
  698. }
  699. else
  700. {
  701. if (current_state is StateDamage)
  702. {
  703. StateDamage state_damage = current_state as StateDamage;
  704. if (state_damage.IsDamageProtect)
  705. {
  706. return;
  707. }
  708. }
  709. // TODO HP //
  710. source.Begin(this);
  711. int reduceHP = mFormula.OnHit(attacker, source, this);
  712. // 统计 //
  713. reduceHP = this.AddHP(-reduceHP, attacker, !source.OutSendEvent, source);
  714. //this.Statistic.LogDamage(attacker.Statistic, reduceHP);
  715. AttackProp attack = source.Attack;
  716. // Post Event //
  717. if (source.OutSendEvent)
  718. {
  719. UnitHitEvent evt = new UnitHitEvent(ID);
  720. evt.senderId = attacker.ID;
  721. evt.senderMasterId = attacker.Virtual.GetMasterID();
  722. evt.hitMasterId = this.Virtual.GetMasterID();
  723. evt.hp = reduceHP;
  724. evt.isDead = IsDead();
  725. evt.isCritical = source.Attack.MaskMustCritical;
  726. evt.effect = source.OutHitEffect;
  727. evt.SourceAttack = source.Attack;
  728. evt.client_state = source.OutClientState;
  729. //if (source != null && attacker != null && source.FromSkillType == XmdsSkillType.cardSkill && this.IsMonster && attacker.IsMonster)
  730. //{
  731. evt.InViewForceSend = true;
  732. //}
  733. queueEvent(evt);
  734. }
  735. if (reduceHP > 0)
  736. {
  737. source.HasHitted = true;
  738. onDamaged(attacker, source, reduceHP);
  739. Parent.cb_unitDamageCallBack(this, attacker, reduceHP, source);
  740. if (IsDead())
  741. {
  742. //doDeadProcess(attacker, source);
  743. return;
  744. }
  745. }
  746. // 非免控,才会有受击
  747. if (!this.IsIgnoreControl && source.OutIsDamage && Moveable)
  748. {
  749. changeState(new StateDamage(this, source, attacker));
  750. }
  751. doHitAttackEndEffect(attacker, source);
  752. return;
  753. }
  754. }
  755. /** 执行死亡处理逻辑 */
  756. private void doDeadProcess(InstanceUnit attacker, AttackSource source)
  757. {
  758. try
  759. {
  760. onDead(attacker);
  761. Parent.cb_unitDeadCallBack(this, attacker);
  762. if (source == null)
  763. {
  764. queueEvent(new UnitDeadEvent(ID, attacker == null ? 0 : attacker.ID, false, mInfo.RebirthTimeMS));
  765. changeState(new StateDead(this, attacker, true));
  766. }
  767. else
  768. {
  769. queueEvent(new UnitDeadEvent(ID, attacker.ID, source.OutIsCrush, mInfo.RebirthTimeMS));
  770. // 被击碎,秒杀 //
  771. if (source.OutIsCrush)
  772. {
  773. Parent.queueEvent(new AddEffectEvent(this.ID, X, Y, Direction, source.Attack.CrushEffect));
  774. changeState(new StateDead(this, attacker, true));
  775. }
  776. else
  777. {
  778. if (!Moveable)
  779. {
  780. changeState(new StateDead(this, attacker));
  781. }
  782. else if (source.OutHasKnockDown || source.OutHasFly)
  783. {
  784. if (!changeState(new StateDamage(this, source, attacker)))
  785. {
  786. changeState(new StateDead(this, attacker));
  787. }
  788. }
  789. else
  790. {
  791. changeState(new StateDead(this, attacker));
  792. }
  793. }
  794. doHitAttackEndEffect(attacker, source);
  795. }
  796. }
  797. catch (Exception e)
  798. {
  799. log.Warn("doDeadProcess catch:" + this.Info.ID + ", AttackId: " + (attacker == null ? -1 : attacker.Info.ID), e);
  800. }
  801. }
  802. /// <summary>
  803. /// 死亡后自爆或者触发法术
  804. /// </summary>
  805. /// <param name="attacker"></param>
  806. /// <param name="source"></param>
  807. private void doHitAttackEndEffect(InstanceUnit attacker, AttackSource source)
  808. {
  809. if (source.Attack.Buff != null)
  810. {
  811. AddBuff(source.Attack.Buff, attacker);
  812. }
  813. if (source.Attack.Spell != null)
  814. {
  815. Parent.attackLaunchSpell(source.FromSkillType, attacker, this, source);
  816. }
  817. }
  818. // 单位获取道具
  819. protected virtual bool tryGotItem(InstanceItem item, out bool removeItem)
  820. {
  821. if (!Parent.IsVisibleAOI(this, item))
  822. {
  823. removeItem = false;
  824. return false;
  825. }
  826. if (item.Info.DropMoneyMin > 0 && item.Info.DropMoneyMax > 0)
  827. {
  828. int min = Math.Min(item.Info.DropMoneyMin, item.Info.DropMoneyMax);
  829. int max = Math.Max(item.Info.DropMoneyMin, item.Info.DropMoneyMax);
  830. int money = RandomN.Next(min, max + 1);
  831. this.CurrentMoney += money;
  832. removeItem = item.Info.RemoveOnFinishPick;
  833. return true;
  834. }
  835. if (item.Info.GotOnUse)
  836. {
  837. // 获取后立即使用 //
  838. if (UseItem(item.Info, item.ItemCreater))
  839. {
  840. removeItem = item.Info.RemoveOnFinishPick;
  841. return true;
  842. }
  843. }
  844. else
  845. {
  846. // 获取后进背包 //
  847. if (AddItemToEmptyInventory(item.Info) > 0)
  848. {
  849. removeItem = item.Info.RemoveOnFinishPick;
  850. return true;
  851. }
  852. }
  853. removeItem = false;
  854. return false;
  855. }
  856. internal bool doGotInstanceItem(InstanceItem item, out bool removeItem, out int pickTimes)
  857. {
  858. pickTimes = 0;
  859. var ret = tryGotItem(item, out removeItem);
  860. if (ret)
  861. {
  862. pickTimes = Parent.cb_unitGotInstanceItemCallBack(this, item);
  863. if (item.Info.GotEffect != null)
  864. {
  865. queueEvent(new UnitEffectEvent(ID, item.Info.GotEffect));
  866. }
  867. }
  868. return ret;
  869. }
  870. internal void doGotInventoryItem(InventorySlot slot, ItemTemplate item, int index, int count)
  871. {
  872. mFormula.OnGotInventoryItem(this, item);
  873. queueEvent(new UnitSyncInventoryItemEvent(ID, item.ID, index, slot.Count));
  874. Parent.cb_unitGotInventoryItemCallBack(this, item, count);
  875. // 添加装备Buff //
  876. if (item.EquipBuffs != null)
  877. {
  878. foreach (LaunchBuff buff in item.EquipBuffs)
  879. {
  880. AddBuff(buff, this, true);
  881. }
  882. }
  883. }
  884. internal void doLostInventoryItem(InventorySlot slot, ItemTemplate item, int index, int count)
  885. {
  886. mFormula.OnLostInventoryItem(this, item);
  887. queueEvent(new UnitSyncInventoryItemEvent(ID, item.ID, index, slot.Count));
  888. Parent.cb_unitLostInventoryItemCallBack(this, item, count);
  889. // 移除装备Buff //
  890. if (item.EquipBuffs != null)
  891. {
  892. foreach (LaunchBuff buff in item.EquipBuffs)
  893. {
  894. removeBuff(buff.BuffID);
  895. }
  896. }
  897. }
  898. // 单位获取道具
  899. internal void doGotBuff(InstanceUnit.BuffState buff)
  900. {
  901. Parent.cb_unitGotBuffCallBack(this, buff);
  902. }
  903. internal void doLostBuff(InstanceUnit.BuffState buff)
  904. {
  905. Parent.cb_unitLostBuffCallBack(this, buff);
  906. }
  907. public bool UseItem(int itemTemplateID, InstanceUnit item_creater = null)
  908. {
  909. ItemTemplate item = Templates.getItem(itemTemplateID);
  910. if (item != null)
  911. {
  912. return UseItem(item, item_creater);
  913. }
  914. return false;
  915. }
  916. public bool UseItem(ItemTemplate item, InstanceUnit item_creater = null)
  917. {
  918. if (item_creater == null)
  919. {
  920. item_creater = this;
  921. }
  922. if (tryUseItem(item, item_creater))
  923. {
  924. beginUseItem(item);
  925. // 如果关键帧绑定特效
  926. if (item.UseEffect != null)
  927. {
  928. queueEvent(new UnitEffectEvent(ID, item.UseEffect));
  929. }
  930. // 如果关键帧绑定释放法术
  931. if (item.UseSpell != null)
  932. {
  933. Parent.unitLaunchSpell(XmdsSkillType.none, item_creater, item.UseSpell, X, Y);
  934. }
  935. if (item.UseSummon != null)
  936. {
  937. Parent.unitSummonUnit(item_creater, item.UseSummon);
  938. }
  939. // 如果关键帧绑定自己释放BUFF
  940. if (item.UseBuffs != null)
  941. {
  942. foreach (LaunchBuff buff in item.UseBuffs)
  943. {
  944. this.AddBuff(buff, item_creater);
  945. }
  946. }
  947. mFormula.OnUseItem(this, item, item_creater);
  948. queueEvent(new UnitUseItemEvent(ID, item.ID));
  949. Parent.cb_unitUseItemCallBack(this, item, item_creater);
  950. Statistic.LogUseItem(item);
  951. return true;
  952. }
  953. return false;
  954. }
  955. //-----------------------------------------------------------------------------------------------------//
  956. /// <summary>
  957. /// 有伤害源的扣血
  958. /// </summary>
  959. /// <param name="hp"></param>
  960. /// <param name="attacker"></param>
  961. /// <param name="sendHit"></param>
  962. /// <param name="hitMessage"></param>
  963. /// force: 忽略无敌效果,强制扣血
  964. public int ReduceHP(int hp, InstanceUnit attacker, AttackSource source, bool sendHit = true, UnitHitEvent hitMessage = null,
  965. bool IngoreHealEffect = false, DamageSource dmgSrc = DamageSource.Def, bool force = false)
  966. {
  967. //1.目标无敌,不扣血
  968. if (IsDead() || hp == 0 || (hp > 0 && this.IsInvincible && !force))
  969. {
  970. return 0;
  971. }
  972. //2. 针对有发送者的伤害来源
  973. if (attacker != null)
  974. {
  975. if (hp < 0 && !IngoreHealEffect)
  976. {
  977. int healedEffect = this.Virtual.GetHealedEffect();
  978. int healEffect = (attacker.Virtual.GetUnitPro() == XmdsUnitPro.Priest) ? attacker.Virtual.GetHealEffect() : 0;
  979. if (healedEffect != 0 || healEffect != 0)
  980. {
  981. hp = CUtils.CastInt(hp * (1 + healEffect * 0.0001) * (1 + healedEffect * 0.0001));
  982. }
  983. }
  984. if (hp < 0)
  985. {
  986. //传递加血事件
  987. hp = attacker.Virtual.DispatchAddOtherHPEvent(hp, this, source);
  988. //2 道灵对宠物加血,需要有个系数
  989. if (attacker.IsPlayer && this.IsPet && attacker.Virtual.GetUnitPro() == XmdsUnitPro.Priest)
  990. {
  991. hp = (int)(hp * XmdsConstConfig.PET_HEALD_RATIO);
  992. }
  993. }
  994. else
  995. {
  996. //传递单位-被其他人扣血
  997. hp = this.Virtual.DispatchAddOtherHPEvent(hp, attacker, source);
  998. if (this.IsPlayer && attacker.IsMonster)
  999. {
  1000. //2.3 如果玩家受伤,伤害来源存在且为怪物,宠物可以分担伤害
  1001. InstanceUnit petUnit = this.Virtual.GetPetUnit();
  1002. if (petUnit != null)
  1003. {
  1004. int petShareDmg = Math.Max(1, (int)(hp * XmdsConstConfig.PET_SHARE_MASTERDMG_RATIO));
  1005. hp = Math.Max(1, hp - petShareDmg);
  1006. petUnit.PetShareDamage(petShareDmg, this);
  1007. }
  1008. }
  1009. }
  1010. this.Statistic.LogDamage(attacker.Statistic, hp);
  1011. }
  1012. this.DoAddHP(-hp);
  1013. if (attacker != null && sendHit)
  1014. {
  1015. if (hitMessage == null)
  1016. {
  1017. hitMessage = new UnitHitEvent();
  1018. }
  1019. hitMessage.object_id = this.ID;
  1020. hitMessage.senderId = attacker == null ? 0 : attacker.ID;
  1021. hitMessage.senderMasterId = attacker == null ? 0 : attacker.Virtual.GetMasterID();
  1022. hitMessage.hitMasterId = this.Virtual.GetMasterID();
  1023. hitMessage.hp = hp;
  1024. hitMessage.isSpecialHit = dmgSrc != DamageSource.Def;
  1025. hitMessage.dmgSrc = dmgSrc;
  1026. hitMessage.isDead = IsDead();
  1027. queueEvent(hitMessage);
  1028. }
  1029. //if (IsDead())
  1030. //{
  1031. // changeState(new StateDead(this, attacker, false));
  1032. // Parent.cb_unitDeadCallBack(this, attacker);
  1033. // queueEvent(new UnitDeadEvent(ID, attacker == null ? 0 : attacker.ID, false, mInfo.RebirthTimeMS));
  1034. //}
  1035. if (hp > 0 && this.IsDead())
  1036. {
  1037. this.doDeadProcess(attacker, source);
  1038. }
  1039. return hp;
  1040. }
  1041. public void AddMP(int mp, InstanceUnit sender, bool sendMsg = true, bool force = false, AttackSource source = null)
  1042. {
  1043. if (mp == 0 || this.MaxMP <= 0)
  1044. {
  1045. return;
  1046. }
  1047. //传递扣定力事件
  1048. if (mp < 0 && sender != null)
  1049. {
  1050. int finalHP = mp;
  1051. sender.Virtual.DispatchAddMPEvent(mp, this, out finalHP, source);
  1052. mp = finalHP;
  1053. }
  1054. this.DoAddMP(mp, force);
  1055. }
  1056. public void removeFromParent()
  1057. {
  1058. Parent.RemoveObjectByID(ID);
  1059. }
  1060. public void kill(InstanceUnit killer = null, bool sendHit = true, UnitHitEvent hitMessage = null)
  1061. {
  1062. ReduceHP(this.CurrentHP, killer, null, sendHit, hitMessage, true);
  1063. }
  1064. virtual protected void onResetAI()
  1065. {
  1066. this.doSomething();
  1067. }
  1068. public void resetAI()
  1069. {
  1070. next_state_queue.Clear();
  1071. onResetAI();
  1072. }
  1073. public override void faceTo(float d)
  1074. {
  1075. if (Info.IsTurnable) { base.faceTo(d); }
  1076. }
  1077. public override void faceTo(float x, float y)
  1078. {
  1079. if (Info.IsTurnable) { base.faceTo(x, y); }
  1080. }
  1081. /// <summary>
  1082. /// 瞬移
  1083. /// </summary>
  1084. /// <param name="x"></param>
  1085. /// <param name="y"></param>
  1086. public void transport(float x, float y, bool setDirction = false, float direction = 0)
  1087. {
  1088. this.SetActionStatus(UnitActionStatus.Transport);
  1089. this.changeState(new StateTransport(this), true);
  1090. setPos(x, y);
  1091. if (setDirction)
  1092. {
  1093. this.Direction = direction;
  1094. }
  1095. SendForceSync();
  1096. if (mOnRegionTransport != null)
  1097. {
  1098. mOnRegionTransport.Invoke();
  1099. }
  1100. }
  1101. //-----------------------------------------------------------------------------------------------------//
  1102. public virtual void doSomething()
  1103. {
  1104. startIdle();
  1105. }
  1106. /// <summary>
  1107. /// 单位待机
  1108. /// </summary>
  1109. public bool startIdle(bool force = false)
  1110. {
  1111. if (current_state is StateIdle)
  1112. {
  1113. }
  1114. else
  1115. {
  1116. return changeState(new StateIdle(this), force);
  1117. }
  1118. return false;
  1119. }
  1120. /// <summary>
  1121. /// 直接复活
  1122. /// </summary>
  1123. public bool startRebirth(int max_hp = 0, int max_mp = 0)
  1124. {
  1125. if (IsDead())
  1126. {
  1127. //this.DeadHide = false;
  1128. this.mProcessDeadCallbackTime = 0;
  1129. return changeState(new StateRebirth(this, max_hp, max_mp));
  1130. }
  1131. return false;
  1132. }
  1133. /// <summary>
  1134. /// 单位移动
  1135. /// </summary>
  1136. /// <param name="x"></param>
  1137. /// <param name="y"></param>
  1138. public bool startMoveTo(float x, float y)
  1139. {
  1140. return changeState(new StateMove(this, x, y));
  1141. }
  1142. /// <summary>
  1143. /// 调整射击位置
  1144. /// </summary>
  1145. /// <param name="expect_skill"></param>
  1146. /// <param name="target"></param>
  1147. /// <param name="onEndAction"></param>
  1148. public bool startAdjustLaunchSkill(SkillTemplate expect_skill, InstanceUnit target, Predicate<StateMove> onEndAction)
  1149. {
  1150. if (expect_skill.AttackKeepRange > 0 && expect_skill.AttackRange > expect_skill.AttackKeepRange)
  1151. {
  1152. float keep_range = GetSkillAttackRange(expect_skill.AttackKeepRange) + target.BodyBlockSize;
  1153. float distance = MathVector.getDistance(this.X, this.Y, target.X, target.Y);
  1154. if (distance < keep_range)
  1155. {
  1156. var half = (expect_skill.AttackRange - expect_skill.AttackKeepRange) / 2;
  1157. var md = (keep_range - distance) + (float)(RandomN.NextDouble() * half);
  1158. var target_pos = new Vector2(this.X, this.Y);
  1159. var rd = (CMath.PI_DIV_2 / 4);
  1160. var target_direction = this.Direction + CMath.PI_F - (rd / 2 + (float)(RandomN.NextDouble() * rd));
  1161. MathVector.movePolar(target_pos, target_direction, md);
  1162. if (!Parent.TryTouchMap(this, target_pos.X, target_pos.Y))
  1163. {
  1164. var back_pos = new Vector2(this.X, this.Y);
  1165. MathVector.movePolar(back_pos, target_direction, this.BodyBlockSize);
  1166. if (!Parent.TryTouchMap(this, back_pos.X, back_pos.Y))
  1167. {
  1168. var move = new StateMove(this, target_pos.X, target_pos.Y);
  1169. move.MinStepCheckCount = 0;
  1170. move.StopOnTouchMap = true;
  1171. move.EndMoveAction = onEndAction;
  1172. return this.changeState(move);
  1173. }
  1174. }
  1175. }
  1176. }
  1177. return false;
  1178. }
  1179. /// <summary>
  1180. /// 单位移动
  1181. /// </summary>
  1182. /// <param name="obj"></param>
  1183. public bool startFollowTo(InstanceZoneObject obj)
  1184. {
  1185. return changeState(new StateFollowObject(this, obj));
  1186. }
  1187. /// <summary>
  1188. /// 单位逃跑
  1189. /// </summary>
  1190. /// <param name="timeMS"></param>
  1191. /// <param name="distance"></param>
  1192. public bool startEscape(int timeMS, float distance = 0)
  1193. {
  1194. return changeState(new StateEscape(this, timeMS, distance));
  1195. }
  1196. /// <summary>
  1197. /// 和自身交互(搓炉石)
  1198. /// </summary>
  1199. /// <param name="timeMS"></param>
  1200. /// <param name="done"></param>
  1201. /// <param name="status"></param>
  1202. public StatePickObject startPickProgressSelf(int timeMS, StatePickObject.OnPickDone done, string status = null)
  1203. {
  1204. StatePickObject picking = new StatePickObject(this, this, timeMS, status, done);
  1205. changeState(picking);
  1206. return picking;
  1207. }
  1208. /// <summary>
  1209. /// 和目标交互
  1210. /// </summary>
  1211. /// <param name="item"></param>
  1212. /// <param name="timeMS"></param>
  1213. /// <param name="done"></param>
  1214. /// <param name="status"></param>
  1215. /// <returns></returns>
  1216. public StatePickObject startPickProgressObject(InstanceZoneObject item, int timeMS, StatePickObject.OnPickDone done, string status = null)
  1217. {
  1218. StatePickObject picking = new StatePickObject(this, item, timeMS, status, done);
  1219. changeState(picking);
  1220. return picking;
  1221. }
  1222. //-----------------------------------------------------------------------------------------------------------------
  1223. #region _SKILL_
  1224. //-----------------------------------------------------------------------------------------------------------------
  1225. public float GetSkillAttackRange(SkillTemplate skill, InstanceUnit targetUnit)
  1226. {
  1227. return mIsAttackRangeIncludeBodySize ? (BodyBlockSize + skill.AttackRange + targetUnit.BodyBlockSize) : skill.AttackRange;
  1228. //return mIsAttackRangeIncludeBodySize ? (BodyBlockSize + skill.AttackRange) : skill.AttackRange;
  1229. }
  1230. public float GetSkillAttackRange(SkillTemplate skill)
  1231. {
  1232. return mIsAttackRangeIncludeBodySize ? (BodyBlockSize + skill.AttackRange) : skill.AttackRange;
  1233. }
  1234. public float GetSkillAttackRange(float range)
  1235. {
  1236. return (mIsAttackRangeIncludeBodySize ? BodyBlockSize + range : range);
  1237. }
  1238. /// <summary>
  1239. /// 判断当前目标在攻击范围内
  1240. /// </summary>
  1241. /// <param name="skill"></param>
  1242. /// <param name="unit"></param>
  1243. /// <returns></returns>
  1244. public bool IsTargetInSkillRange(SkillTemplate skill, InstanceUnit unit)
  1245. {
  1246. float rg = GetSkillAttackRange(skill);
  1247. float dr = skill.AttackAngle / 2;
  1248. if (Collider.Object_HitBody_TouchFan(unit, X, Y, rg, Direction - dr, Direction + dr))
  1249. {
  1250. return true;
  1251. }
  1252. return false;
  1253. }
  1254. [Obsolete]
  1255. public List<InstanceUnit> getSkillAttackableTargets(SkillTemplate skill, AttackReason reason)
  1256. {
  1257. float rg = GetSkillAttackRange(skill);
  1258. List<InstanceUnit> list = Parent.getObjectsRoundRange<InstanceUnit>(Collider.Object_HitBody_TouchRound, X, Y, rg, this.AoiStatus);
  1259. Parent.getAttackableUnits(this, list, skill.ExpectTarget, reason, skill);
  1260. return list;
  1261. }
  1262. public void getSkillAttackableTargets(SkillTemplate skill, List<InstanceUnit> list, AttackReason reason)
  1263. {
  1264. float rg = GetSkillAttackRange(skill);
  1265. Parent.getObjectsRoundRange<InstanceUnit>(Collider.Object_HitBody_TouchRound, X, Y, rg, list, this.AoiStatus);
  1266. Parent.getAttackableUnits(this, list, skill.ExpectTarget, reason, skill);
  1267. }
  1268. /// <summary>
  1269. /// 获得可用的技能
  1270. /// </summary>
  1271. /// <param name="expect"></param>
  1272. /// <returns></returns>
  1273. public SkillState getAvailableSkill(SkillTemplate.CastTarget expect)
  1274. {
  1275. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1276. {
  1277. SkillTemplate st = mAllSkills[si];
  1278. if (st.ExpectTarget == expect)
  1279. {
  1280. SkillState sst = mSkillStatus.Get(st.GetID());
  1281. if (sst.IsActive && sst.IsDone)
  1282. {
  1283. return sst;
  1284. }
  1285. }
  1286. }
  1287. return null;
  1288. }
  1289. /** 获取当前可自动释放的技能 */
  1290. public SkillState getAvailableAutoLaunchSkill(SkillTemplate.CastTarget expect)
  1291. {
  1292. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1293. {
  1294. SkillTemplate st = mAllSkills[si];
  1295. if (st.ExpectTarget == expect)
  1296. {
  1297. SkillState sst = mSkillStatus.Get(st.GetID());
  1298. if (sst.IsActive && sst.IsDone && sst.LaunchSkill.AutoLaunch && sst.CanAutoLaunch())
  1299. {
  1300. return sst;
  1301. }
  1302. }
  1303. }
  1304. return null;
  1305. }
  1306. /// <summary>
  1307. /// 获取当前最适合攻击的目标
  1308. /// </summary>
  1309. /// <param name="skill"></param>
  1310. /// <param name="reason"></param>
  1311. /// <param name="directionChange"></param>
  1312. /// <returns></returns>
  1313. public InstanceUnit getSkillAttackableFirstTarget(SkillTemplate skill, AttackReason reason, ref bool directionChange)
  1314. {
  1315. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1316. {
  1317. getSkillAttackableTargets(skill, list, reason);
  1318. if (list.Count > 0)
  1319. {
  1320. float rg = GetSkillAttackRange(skill);
  1321. // 检测攻击范围内的单位 //
  1322. float dr = skill.AttackAngle / 2;
  1323. for (int i = 0; i < list.Count; i++)
  1324. {
  1325. InstanceUnit u = list[i];
  1326. if (Collider.Object_HitBody_TouchFan(u, X, Y, rg, Direction - dr, Direction + dr))
  1327. {
  1328. directionChange = false;
  1329. return u;
  1330. }
  1331. }
  1332. // 优先当前朝向的目标 //
  1333. dr = CMath.PI_DIV_2;
  1334. directionChange = true;
  1335. for (int i = 0; i < list.Count; i++)
  1336. {
  1337. InstanceUnit u = list[i];
  1338. if (Collider.Object_HitBody_TouchFan(u, X, Y, rg, Direction - dr, Direction + dr))
  1339. {
  1340. return u;
  1341. }
  1342. }
  1343. // 最后选取最近的目标 //
  1344. InstanceUnit min = null;
  1345. float min_len = float.MaxValue;
  1346. for (int i = 0; i < list.Count; i++)
  1347. {
  1348. InstanceUnit u = list[i];
  1349. float len = MathVector.getDistanceSquare(u.X, u.Y, X, Y);
  1350. if (min_len > len)
  1351. {
  1352. min_len = len;
  1353. min = u;
  1354. }
  1355. }
  1356. return min;
  1357. }
  1358. }
  1359. return null;
  1360. }
  1361. public struct LaunchSkillParam
  1362. {
  1363. public uint TargetUnitID;
  1364. public Vector2 SpellTargetPos;
  1365. public bool AutoFocusNearTarget;
  1366. public bool LaunchSetDirection; // 释放时是否重新设置方向
  1367. public float direction;
  1368. public bool IsAutoLaunch; // 是否自动战斗时释放
  1369. public bool IsInGuard; // 是否是自动战斗
  1370. public LaunchSkillParam(uint targetUnitID = 0, Vector2 target_pos = null, bool autoFocusNearTarget = false,
  1371. bool LaunchSetDirection = false, float direction = 0, bool IsAutoLaunch = false, bool IsInGuard = false)
  1372. {
  1373. this.TargetUnitID = targetUnitID;
  1374. this.SpellTargetPos = target_pos;
  1375. this.AutoFocusNearTarget = autoFocusNearTarget;
  1376. this.LaunchSetDirection = LaunchSetDirection;
  1377. this.direction = direction;
  1378. this.IsAutoLaunch = IsAutoLaunch;
  1379. this.IsInGuard = IsInGuard;
  1380. }
  1381. }
  1382. public StateSkill launchSkill(SkillState ss, LaunchSkillParam param)
  1383. {
  1384. if (ss == null) return null;
  1385. StateSkill current = CurrentState as StateSkill;
  1386. if (current != null && current.SkillData.IsManuallyCancelable)
  1387. {
  1388. //如果当前技能为手动取消//
  1389. if (current.Skill.ID == ss.ID)
  1390. {
  1391. //判断停止当前技能//
  1392. if (current.IsCancelableBySkill)
  1393. {
  1394. current.block();
  1395. }
  1396. }
  1397. //手动取消技能禁止其他技能打断//
  1398. return null;
  1399. }
  1400. if (ss.TryLaunch() && mFormula.TryLaunchSkill(this, ss, ref param))
  1401. {
  1402. if (param.IsAutoLaunch && !ss.CanAutoLaunch())
  1403. {
  1404. return null;
  1405. }
  1406. //沉默不能释放其他技能//
  1407. if (IsSilent && ss != mDefaultSkill)
  1408. {
  1409. return null;
  1410. }
  1411. // 自动战斗下,释放技能要在范围内
  1412. InstanceUnit target = null;
  1413. if (ss.Data.AttackMustBeInRange)
  1414. {
  1415. target = Parent.getUnit(param.TargetUnitID);
  1416. if (!ss.checkTargetRange(target))
  1417. {
  1418. return null;
  1419. }
  1420. }
  1421. //是否在合理的攻击范围内
  1422. if (param.IsInGuard && !ss.checkTargetInAttackRange(target, param))
  1423. {
  1424. return null;
  1425. }
  1426. if (ss.GetSkillType() == XmdsSkillType.petGiveAcitve)
  1427. {
  1428. mFormula.TriggerPetSkill(this, ss, ref param);
  1429. return null;
  1430. }
  1431. StateSkill state = new StateSkill(this, ss, param, ss.ActionSpeed, (st) =>
  1432. {
  1433. overLaunchSkill(ss, st);
  1434. if (mOnLaunchSkill != null)
  1435. {
  1436. mOnLaunchSkill.Invoke(this, ss);
  1437. }
  1438. });
  1439. if (state.tryLaunch())
  1440. {
  1441. this.Virtual.SetCombatState(BattleStatus.ReadyBattle);
  1442. Parent.cb_unitLaunchSkill(this, ss);
  1443. if (changeState(state))
  1444. {
  1445. //释放成功
  1446. if (param.LaunchSetDirection)
  1447. {
  1448. //Console.WriteLine("launchSkill - 设置方向: " + param.direction + ", " + this.ID);
  1449. //this.faceTo(param.direction);
  1450. //this.SendForceSync();
  1451. }
  1452. return state;
  1453. }
  1454. }
  1455. }
  1456. return null;
  1457. }
  1458. protected virtual void overLaunchSkill(SkillState ss, StateSkill state)
  1459. {
  1460. this.AddHP(-ss.Data.CostHP, null);
  1461. this.AddMP(-ss.Data.CostMP, null);
  1462. }
  1463. /// <summary>
  1464. /// 单位释放技能
  1465. /// </summary>
  1466. public StateSkill launchSkill(int skillID, LaunchSkillParam param)
  1467. {
  1468. SkillState skill = getSkillState(skillID);
  1469. return launchSkill(skill, param);
  1470. }
  1471. /// <summary>
  1472. /// 释放随机技能,一般用于AI
  1473. /// </summary>
  1474. /// <param name="expectTarget"></param>
  1475. /// <param name="param"></param>
  1476. /// <returns></returns>
  1477. public virtual StateSkill launchRandomSkill(SkillTemplate.CastTarget expectTarget, LaunchSkillParam param)
  1478. {
  1479. StateSkill current = CurrentState as StateSkill;
  1480. if (current != null && !current.IsCancelableBySkill)
  1481. {
  1482. return null;
  1483. }
  1484. int rand = RandomN.Next(0, mAllSkills.Count);
  1485. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1486. {
  1487. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  1488. if (st.ExpectTarget == expectTarget)
  1489. {
  1490. SkillState sst = mSkillStatus.Get(st.GetID());
  1491. if (sst.LaunchSkill.AutoLaunch && sst.TryLaunch())
  1492. {
  1493. StateSkill state = launchSkill(sst, param);
  1494. if (state != null)
  1495. {
  1496. return state;
  1497. }
  1498. }
  1499. }
  1500. }
  1501. return null;
  1502. }
  1503. public virtual StateSkill launchRandomSkillForAll(LaunchSkillParam param)
  1504. {
  1505. StateSkill current = CurrentState as StateSkill;
  1506. if (current != null && !current.IsCancelableBySkill)
  1507. {
  1508. return null;
  1509. }
  1510. int rand = RandomN.Next(0, mAllSkills.Count);
  1511. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1512. {
  1513. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  1514. SkillState sst = mSkillStatus.Get(st.GetID());
  1515. if (sst.LaunchSkill.AutoLaunch && sst.TryLaunch())
  1516. {
  1517. StateSkill state = launchSkill(sst, param);
  1518. if (state != null)
  1519. {
  1520. return state;
  1521. }
  1522. }
  1523. }
  1524. return null;
  1525. }
  1526. /// <summary>
  1527. /// 尝试取消当前技能,打出连击
  1528. /// </summary>
  1529. /// <param name="target"></param>
  1530. /// <param name="autoFocusNearTarget"></param>
  1531. /// <returns></returns>
  1532. public virtual bool tryLaunchRandomSkillAndCancelCurrentSkill(InstanceUnit target, bool autoFocusNearTarget = false)
  1533. {
  1534. StateSkill current = CurrentState as StateSkill;
  1535. if (current != null && !current.IsChanting && current.IsCancelableBySkill)
  1536. {
  1537. LaunchSkillParam param = new LaunchSkillParam(target.ID, null, autoFocusNearTarget);
  1538. //优先多段攻击//
  1539. if (current.SkillData.IsSingleAction)
  1540. {
  1541. if (IsTargetInSkillRange(current.SkillData, target))
  1542. {
  1543. StateSkill st = launchSkill(current.SkillData.ID, param);
  1544. if (st != null)
  1545. {
  1546. return true;
  1547. }
  1548. }
  1549. }
  1550. //随机其他技能//
  1551. int rand = RandomN.Next(0, mAllSkills.Count);
  1552. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1553. {
  1554. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  1555. if (Parent.IsAttackable(this, target, st.ExpectTarget, AttackReason.Attack, st))
  1556. {
  1557. SkillState sst = mSkillStatus.Get(st.GetID());
  1558. if (sst.LaunchSkill.AutoLaunch && IsTargetInSkillRange(st, target))
  1559. {
  1560. StateSkill state = launchSkill(sst, param);
  1561. if (state != null)
  1562. {
  1563. return true;
  1564. }
  1565. }
  1566. }
  1567. }
  1568. }
  1569. return false;
  1570. }
  1571. /// <summary>
  1572. /// 尝试在范围内找到目标释放技能,并进入StateFollowAndAttack,自己走过去打。
  1573. /// </summary>
  1574. /// <param name="range"></param>
  1575. /// <param name="skill_auto_launchable"></param>
  1576. /// <returns></returns>
  1577. public virtual bool tryFollowAndLaunchRandomSkillToTargetInRange(float range, bool skill_auto_launchable = false)
  1578. {
  1579. if (!IsNoneSkill)
  1580. {
  1581. SkillState[] skills = mSkillStatus.SkillsArray;
  1582. CUtils.RandomArray(RandomN, skills);
  1583. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1584. {
  1585. //随机找个目标施法//
  1586. Parent.getObjectsRoundRange<InstanceUnit>(Collider.Object_Pos_IncludeInRound, X, Y, Info.GuardRange, list, this.AoiStatus);
  1587. DoAndRemoveCollection.UpdateAndRemove<InstanceUnit>(list, (InstanceUnit u) =>
  1588. {
  1589. return !u.IsActive;
  1590. });
  1591. if (list.Count == 0) { return false; }
  1592. CUtils.RandomList(Parent.RandomN, list);
  1593. foreach (SkillState skill in skills)
  1594. {
  1595. if ((!skill_auto_launchable || skill.LaunchSkill.AutoLaunch) && skill.TryLaunch())
  1596. {
  1597. for (int i = 0; i < list.Count; i++)
  1598. {
  1599. InstanceUnit u = list[i];
  1600. if (Parent.IsAttackable(this, u, skill.Data.ExpectTarget, AttackReason.Attack, skill.Data))
  1601. {
  1602. //检测是否有可释放技能//
  1603. changeState(new StateFollowAndAttack(this, u, skill.Data.ExpectTarget));
  1604. return true;
  1605. }
  1606. }
  1607. }
  1608. }
  1609. }
  1610. }
  1611. return false;
  1612. }
  1613. #endregion
  1614. //-----------------------------------------------------------------------------------------------------------------
  1615. internal void deadDropItems(int force)
  1616. {
  1617. foreach (KeyValuePair<ItemTemplate, DropItem> e in mDropItems.Drop(Parent, Parent.RandomN))
  1618. {
  1619. float x;
  1620. float y;
  1621. CMath.RandomPosInRound(RandomN, X, Y, e.Value.DropPosRange, out x, out y);
  1622. Parent.AddItem(e.Key, e.Key.Name, x, y, CMath.RandomAngle(RandomN), force, e.Key.Name, this);
  1623. }
  1624. }
  1625. protected bool tryPickObject(InstanceUnit unit)
  1626. {
  1627. if (!Parent.IsVisibleAOI(this, unit))
  1628. {
  1629. return false;
  1630. }
  1631. bool ret = true;
  1632. if (mOnTryPickUnit != null)
  1633. {
  1634. foreach (TryPickUnitHandler trypick in mOnTryPickUnit.GetInvocationList())
  1635. {
  1636. if (!trypick.Invoke(this, unit))
  1637. {
  1638. ret = false;
  1639. }
  1640. }
  1641. }
  1642. return ret;
  1643. }
  1644. public void PickUnit(InstanceUnit pickable)
  1645. {
  1646. if (tryPickObject(pickable))
  1647. {
  1648. Parent.cb_unitPickUnitCallBack(this, pickable);
  1649. if (mOnPickUnit != null)
  1650. {
  1651. mOnPickUnit.Invoke(this, pickable);
  1652. }
  1653. }
  1654. }
  1655. public CommonAI.Data.SceneType GetSceneType()
  1656. {
  1657. return mZone.SceneType;
  1658. }
  1659. //-----------------------------------------------------------------------------------------------------//
  1660. #region Environment
  1661. //-----------------------------------------------------------------------------------------------------//
  1662. private HashMap<string, EnvironmentVar> EnvironmentVarMap = new HashMap<string, EnvironmentVar>();
  1663. public void SetEnvironmentVar(string key, object value, bool syncToClient = false)
  1664. {
  1665. if (!string.IsNullOrEmpty(key))
  1666. {
  1667. EnvironmentVar var = EnvironmentVarMap.Get(key);
  1668. if (var != null)
  1669. {
  1670. if (EnvironmentVar.ALWAYS_SYNC_ENVIRONMENT_VAR || (var.SyncToClient && var.Value != value))
  1671. {
  1672. queueEvent(new PlayerSyncEnvironmentVarEvent(ID, key, value));
  1673. }
  1674. var.Value = value;
  1675. }
  1676. else
  1677. {
  1678. var = new EnvironmentVar(key, syncToClient, value);
  1679. EnvironmentVarMap.Add(key, var);
  1680. if (EnvironmentVar.ALWAYS_SYNC_ENVIRONMENT_VAR || var.SyncToClient)
  1681. {
  1682. queueEvent(new PlayerSyncEnvironmentVarEvent(ID, key, value));
  1683. }
  1684. }
  1685. }
  1686. }
  1687. public T GetEnvironmentVarAs<T>(string key)
  1688. {
  1689. if (!string.IsNullOrEmpty(key))
  1690. {
  1691. EnvironmentVar var = EnvironmentVarMap.Get(key);
  1692. if (var != null)
  1693. {
  1694. try
  1695. {
  1696. return (T)var.Value;
  1697. }
  1698. catch (Exception err)
  1699. {
  1700. log.Warn("GetEnvironmentVarAs : " + key + ", catch: " + err);
  1701. }
  1702. }
  1703. }
  1704. return default(T);
  1705. }
  1706. public int ListEnvironmentVars(List<EnvironmentVar> list)
  1707. {
  1708. list.AddRange(EnvironmentVarMap.Values);
  1709. return EnvironmentVarMap.Count;
  1710. }
  1711. public List<EnvironmentVar> ListEnvironmentVars()
  1712. {
  1713. return new List<EnvironmentVar>(EnvironmentVarMap.Values);
  1714. }
  1715. public ClientStruct.ZoneEnvironmentVar[] GetCurrentUnitVars()
  1716. {
  1717. ClientStruct.ZoneEnvironmentVar[] ret = new ClientStruct.ZoneEnvironmentVar[EnvironmentVarMap.Count];
  1718. int i = 0;
  1719. try
  1720. {
  1721. foreach (EnvironmentVar var in EnvironmentVarMap.Values)
  1722. {
  1723. ret[i].Key = var.Key;
  1724. ret[i].Value = var.Value;
  1725. ret[i].SyncToClient = var.SyncToClient;
  1726. i++;
  1727. }
  1728. }
  1729. catch (Exception e)
  1730. {
  1731. log.Warn("GetCurrentUnitVars catch: " + ret.Length + ", " + EnvironmentVarMap.Count + ", " + i + ", e: " + e);
  1732. }
  1733. return ret;
  1734. }
  1735. #endregion
  1736. //-----------------------------------------------------------------------------------------------------//
  1737. //-----------------------------------------------------------------------------------------------------//
  1738. #region SceneEvents
  1739. //-----------------------------------------------------------------------------------------------------//
  1740. private HashMap<int, UnitEventTriggerCollection> mBindEvents = new HashMap<int, UnitEventTriggerCollection>();
  1741. public void BindUnitEvent(int unit_event_id)
  1742. {
  1743. if (!mBindEvents.ContainsKey(unit_event_id))
  1744. {
  1745. UnitEventTemplate uet = Templates.getUnitEvent(unit_event_id);
  1746. if (uet != null)
  1747. {
  1748. uet = uet.Clone() as UnitEventTemplate;
  1749. UnitEventTriggerCollection bind_event = new UnitEventTriggerCollection(this);
  1750. bind_event.Bind(uet.Events);
  1751. mBindEvents.Add(unit_event_id, bind_event);
  1752. }
  1753. }
  1754. }
  1755. protected virtual void clearBindEvents()
  1756. {
  1757. foreach (var evt in mBindEvents.Values)
  1758. {
  1759. evt.Dispose();
  1760. }
  1761. mBindEvents.Clear();
  1762. }
  1763. /** 计算并获得伤害 */
  1764. public virtual void PetShareDamage(int baseDmgValue, InstanceUnit sender)
  1765. {
  1766. }
  1767. #endregion
  1768. //-----------------------------------------------------------------------------------------------------//
  1769. //-----------------------------------------------------------------------------------------------------//
  1770. }
  1771. }