InstanceUnit.cs 71 KB

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