InstanceUnit.cs 70 KB

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