InstanceUnit.cs 67 KB

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