InstanceUnit.cs 66 KB

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