InstanceUnit.cs 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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("额外调用死亡逻辑:" + 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. mDeadTime = Parent.PassTimeMS;
  749. onDead(attacker);
  750. Parent.cb_unitDeadCallBack(this, attacker);
  751. if (source == null)
  752. {
  753. queueEvent(new UnitDeadEvent(ID, attacker == null ? 0 : attacker.ID, false, mInfo.RebirthTimeMS));
  754. changeState(new StateDead(this, attacker, true));
  755. }
  756. else
  757. {
  758. queueEvent(new UnitDeadEvent(ID, attacker.ID, source.OutIsCrush, mInfo.RebirthTimeMS));
  759. // 被击碎,秒杀 //
  760. if (source.OutIsCrush)
  761. {
  762. Parent.queueEvent(new AddEffectEvent(this.ID, X, Y, Direction, source.Attack.CrushEffect));
  763. changeState(new StateDead(this, attacker, true));
  764. }
  765. else
  766. {
  767. if (!Moveable)
  768. {
  769. changeState(new StateDead(this, attacker));
  770. }
  771. else if (source.OutHasKnockDown || source.OutHasFly)
  772. {
  773. if (!changeState(new StateDamage(this, source, attacker)))
  774. {
  775. changeState(new StateDead(this, attacker));
  776. }
  777. }
  778. else
  779. {
  780. changeState(new StateDead(this, attacker));
  781. }
  782. }
  783. doHitAttackEndEffect(attacker, source);
  784. }
  785. }
  786. catch (Exception e)
  787. {
  788. log.Warn("doDeadProcess catch:" + this.Info.ID + ", AttackId: " + (attacker == null ? -1 : attacker.Info.ID), e);
  789. }
  790. }
  791. /// <summary>
  792. /// 死亡后自爆或者触发法术
  793. /// </summary>
  794. /// <param name="attacker"></param>
  795. /// <param name="source"></param>
  796. private void doHitAttackEndEffect(InstanceUnit attacker, AttackSource source)
  797. {
  798. if (source.Attack.Buff != null)
  799. {
  800. AddBuff(source.Attack.Buff, attacker);
  801. }
  802. if (source.Attack.Spell != null)
  803. {
  804. Parent.attackLaunchSpell(source.FromSkillType, attacker, this, source);
  805. }
  806. }
  807. // 单位获取道具
  808. protected virtual bool tryGotItem(InstanceItem item, out bool removeItem)
  809. {
  810. if (!Parent.IsVisibleAOI(this, item))
  811. {
  812. removeItem = false;
  813. return false;
  814. }
  815. if (item.Info.DropMoneyMin > 0 && item.Info.DropMoneyMax > 0)
  816. {
  817. int min = Math.Min(item.Info.DropMoneyMin, item.Info.DropMoneyMax);
  818. int max = Math.Max(item.Info.DropMoneyMin, item.Info.DropMoneyMax);
  819. int money = RandomN.Next(min, max + 1);
  820. this.CurrentMoney += money;
  821. removeItem = item.Info.RemoveOnFinishPick;
  822. return true;
  823. }
  824. if (item.Info.GotOnUse)
  825. {
  826. // 获取后立即使用 //
  827. if (UseItem(item.Info, item.ItemCreater))
  828. {
  829. removeItem = item.Info.RemoveOnFinishPick;
  830. return true;
  831. }
  832. }
  833. else
  834. {
  835. // 获取后进背包 //
  836. if (AddItemToEmptyInventory(item.Info) > 0)
  837. {
  838. removeItem = item.Info.RemoveOnFinishPick;
  839. return true;
  840. }
  841. }
  842. removeItem = false;
  843. return false;
  844. }
  845. internal bool doGotInstanceItem(InstanceItem item, out bool removeItem, out int pickTimes)
  846. {
  847. pickTimes = 0;
  848. var ret = tryGotItem(item, out removeItem);
  849. if (ret)
  850. {
  851. pickTimes = Parent.cb_unitGotInstanceItemCallBack(this, item);
  852. if (item.Info.GotEffect != null)
  853. {
  854. queueEvent(new UnitEffectEvent(ID, item.Info.GotEffect));
  855. }
  856. }
  857. return ret;
  858. }
  859. internal void doGotInventoryItem(InventorySlot slot, ItemTemplate item, int index, int count)
  860. {
  861. mFormula.OnGotInventoryItem(this, item);
  862. queueEvent(new UnitSyncInventoryItemEvent(ID, item.ID, index, slot.Count));
  863. Parent.cb_unitGotInventoryItemCallBack(this, item, count);
  864. // 添加装备Buff //
  865. if (item.EquipBuffs != null)
  866. {
  867. foreach (LaunchBuff buff in item.EquipBuffs)
  868. {
  869. AddBuff(buff, this, true);
  870. }
  871. }
  872. }
  873. internal void doLostInventoryItem(InventorySlot slot, ItemTemplate item, int index, int count)
  874. {
  875. mFormula.OnLostInventoryItem(this, item);
  876. queueEvent(new UnitSyncInventoryItemEvent(ID, item.ID, index, slot.Count));
  877. Parent.cb_unitLostInventoryItemCallBack(this, item, count);
  878. // 移除装备Buff //
  879. if (item.EquipBuffs != null)
  880. {
  881. foreach (LaunchBuff buff in item.EquipBuffs)
  882. {
  883. removeBuff(buff.BuffID);
  884. }
  885. }
  886. }
  887. // 单位获取道具
  888. internal void doGotBuff(InstanceUnit.BuffState buff)
  889. {
  890. Parent.cb_unitGotBuffCallBack(this, buff);
  891. }
  892. internal void doLostBuff(InstanceUnit.BuffState buff)
  893. {
  894. Parent.cb_unitLostBuffCallBack(this, buff);
  895. }
  896. public bool UseItem(int itemTemplateID, InstanceUnit item_creater = null)
  897. {
  898. ItemTemplate item = Templates.getItem(itemTemplateID);
  899. if (item != null)
  900. {
  901. return UseItem(item, item_creater);
  902. }
  903. return false;
  904. }
  905. public bool UseItem(ItemTemplate item, InstanceUnit item_creater = null)
  906. {
  907. if (item_creater == null)
  908. {
  909. item_creater = this;
  910. }
  911. if (tryUseItem(item, item_creater))
  912. {
  913. beginUseItem(item);
  914. // 如果关键帧绑定特效
  915. if (item.UseEffect != null)
  916. {
  917. queueEvent(new UnitEffectEvent(ID, item.UseEffect));
  918. }
  919. // 如果关键帧绑定释放法术
  920. if (item.UseSpell != null)
  921. {
  922. Parent.unitLaunchSpell(XmdsSkillType.none, item_creater, item.UseSpell, X, Y);
  923. }
  924. if (item.UseSummon != null)
  925. {
  926. Parent.unitSummonUnit(item_creater, item.UseSummon);
  927. }
  928. // 如果关键帧绑定自己释放BUFF
  929. if (item.UseBuffs != null)
  930. {
  931. foreach (LaunchBuff buff in item.UseBuffs)
  932. {
  933. this.AddBuff(buff, item_creater);
  934. }
  935. }
  936. mFormula.OnUseItem(this, item, item_creater);
  937. queueEvent(new UnitUseItemEvent(ID, item.ID));
  938. Parent.cb_unitUseItemCallBack(this, item, item_creater);
  939. Statistic.LogUseItem(item);
  940. return true;
  941. }
  942. return false;
  943. }
  944. //-----------------------------------------------------------------------------------------------------//
  945. /// <summary>
  946. /// 有伤害源的扣血
  947. /// </summary>
  948. /// <param name="hp"></param>
  949. /// <param name="attacker"></param>
  950. /// <param name="sendHit"></param>
  951. /// <param name="hitMessage"></param>
  952. /// force: 忽略无敌效果,强制扣血
  953. public int ReduceHP(int hp, InstanceUnit attacker, AttackSource source, bool sendHit = true, UnitHitEvent hitMessage = null,
  954. bool IngoreHealEffect = false, DamageSource dmgSrc = DamageSource.Def, bool force = false)
  955. {
  956. //1.目标无敌,不扣血
  957. if (IsDead() || hp == 0 || (hp > 0 && this.IsInvincible && !force))
  958. {
  959. return 0;
  960. }
  961. //2. 针对有发送者的伤害来源
  962. if (attacker != null)
  963. {
  964. if (hp < 0 && !IngoreHealEffect)
  965. {
  966. int healedEffect = this.Virtual.GetHealedEffect();
  967. int healEffect = (attacker.Virtual.GetUnitPro() == XmdsUnitPro.Priest) ? attacker.Virtual.GetHealEffect() : 0;
  968. if (healedEffect != 0 || healEffect != 0)
  969. {
  970. hp = CUtils.CastInt(hp * (1 + healEffect * 0.0001) * (1 + healedEffect * 0.0001));
  971. }
  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 && attacker.IsMonster)
  986. {
  987. //2.3 如果玩家受伤,伤害来源存在且为怪物,宠物可以分担伤害
  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. this.Statistic.LogDamage(attacker.Statistic, hp);
  997. }
  998. this.DoAddHP(-hp);
  999. if (attacker != null && sendHit)
  1000. {
  1001. if (hitMessage == null)
  1002. {
  1003. hitMessage = new UnitHitEvent();
  1004. }
  1005. hitMessage.object_id = this.ID;
  1006. hitMessage.senderId = attacker == null ? 0 : attacker.ID;
  1007. hitMessage.senderMasterId = attacker == null ? 0 : attacker.Virtual.GetMasterID();
  1008. hitMessage.hitMasterId = this.Virtual.GetMasterID();
  1009. hitMessage.hp = hp;
  1010. hitMessage.isSpecialHit = dmgSrc != DamageSource.Def;
  1011. hitMessage.dmgSrc = dmgSrc;
  1012. hitMessage.isDead = IsDead();
  1013. queueEvent(hitMessage);
  1014. }
  1015. //if (IsDead())
  1016. //{
  1017. // changeState(new StateDead(this, attacker, false));
  1018. // Parent.cb_unitDeadCallBack(this, attacker);
  1019. // queueEvent(new UnitDeadEvent(ID, attacker == null ? 0 : attacker.ID, false, mInfo.RebirthTimeMS));
  1020. //}
  1021. if (hp > 0 && this.IsDead())
  1022. {
  1023. this.doDeadProcess(attacker, source);
  1024. }
  1025. return hp;
  1026. }
  1027. public void AddMP(int mp, InstanceUnit sender, bool sendMsg = true, bool force = false, AttackSource source = null)
  1028. {
  1029. if (mp == 0 || this.MaxMP <= 0)
  1030. {
  1031. return;
  1032. }
  1033. //传递扣定力事件
  1034. if (mp < 0 && sender != null)
  1035. {
  1036. int finalHP = mp;
  1037. sender.Virtual.DispatchAddMPEvent(mp, this, out finalHP, source);
  1038. mp = finalHP;
  1039. }
  1040. this.DoAddMP(mp, force);
  1041. }
  1042. public void removeFromParent()
  1043. {
  1044. Parent.RemoveObjectByID(ID);
  1045. }
  1046. public void kill(InstanceUnit killer = null, bool sendHit = true, UnitHitEvent hitMessage = null)
  1047. {
  1048. ReduceHP(this.CurrentHP, killer, null, sendHit, hitMessage, true);
  1049. }
  1050. virtual protected void onResetAI()
  1051. {
  1052. this.doSomething();
  1053. }
  1054. public void resetAI()
  1055. {
  1056. next_state_queue.Clear();
  1057. onResetAI();
  1058. }
  1059. public override void faceTo(float d)
  1060. {
  1061. if (Info.IsTurnable) { base.faceTo(d); }
  1062. }
  1063. public override void faceTo(float x, float y)
  1064. {
  1065. if (Info.IsTurnable) { base.faceTo(x, y); }
  1066. }
  1067. /// <summary>
  1068. /// 瞬移
  1069. /// </summary>
  1070. /// <param name="x"></param>
  1071. /// <param name="y"></param>
  1072. public void transport(float x, float y, bool setDirction = false, float direction = 0)
  1073. {
  1074. this.SetActionStatus(UnitActionStatus.Transport);
  1075. this.changeState(new StateTransport(this), true);
  1076. setPos(x, y);
  1077. if (setDirction)
  1078. {
  1079. this.faceTo(direction);
  1080. }
  1081. SendForceSync();
  1082. if (mOnRegionTransport != null)
  1083. {
  1084. mOnRegionTransport.Invoke();
  1085. }
  1086. }
  1087. //-----------------------------------------------------------------------------------------------------//
  1088. public virtual void doSomething()
  1089. {
  1090. startIdle();
  1091. }
  1092. /// <summary>
  1093. /// 单位待机
  1094. /// </summary>
  1095. public bool startIdle(bool force = false)
  1096. {
  1097. if (current_state is StateIdle)
  1098. {
  1099. }
  1100. else
  1101. {
  1102. return changeState(new StateIdle(this), force);
  1103. }
  1104. return false;
  1105. }
  1106. /// <summary>
  1107. /// 直接复活
  1108. /// </summary>
  1109. public bool startRebirth(int max_hp = 0, int max_mp = 0)
  1110. {
  1111. if (IsDead())
  1112. {
  1113. //this.DeadHide = false;
  1114. this.mProcessDeadCallbackTime = 0;
  1115. return changeState(new StateRebirth(this, max_hp, max_mp));
  1116. }
  1117. return false;
  1118. }
  1119. /// <summary>
  1120. /// 单位移动
  1121. /// </summary>
  1122. /// <param name="x"></param>
  1123. /// <param name="y"></param>
  1124. public bool startMoveTo(float x, float y)
  1125. {
  1126. return changeState(new StateMove(this, x, y));
  1127. }
  1128. /// <summary>
  1129. /// 调整射击位置
  1130. /// </summary>
  1131. /// <param name="expect_skill"></param>
  1132. /// <param name="target"></param>
  1133. /// <param name="onEndAction"></param>
  1134. public bool startAdjustLaunchSkill(SkillTemplate expect_skill, InstanceUnit target, Predicate<StateMove> onEndAction)
  1135. {
  1136. if (expect_skill.AttackKeepRange > 0 && expect_skill.AttackRange > expect_skill.AttackKeepRange)
  1137. {
  1138. float keep_range = GetSkillAttackRange(expect_skill.AttackKeepRange) + target.BodyBlockSize;
  1139. float distance = MathVector.getDistance(this.X, this.Y, target.X, target.Y);
  1140. if (distance < keep_range)
  1141. {
  1142. var half = (expect_skill.AttackRange - expect_skill.AttackKeepRange) / 2;
  1143. var md = (keep_range - distance) + (float)(RandomN.NextDouble() * half);
  1144. var target_pos = new Vector2(this.X, this.Y);
  1145. var rd = (CMath.PI_DIV_2 / 4);
  1146. var target_direction = this.Direction + CMath.PI_F - (rd / 2 + (float)(RandomN.NextDouble() * rd));
  1147. MathVector.movePolar(target_pos, target_direction, md);
  1148. if (!Parent.TryTouchMap(this, target_pos.X, target_pos.Y))
  1149. {
  1150. var back_pos = new Vector2(this.X, this.Y);
  1151. MathVector.movePolar(back_pos, target_direction, this.BodyBlockSize);
  1152. if (!Parent.TryTouchMap(this, back_pos.X, back_pos.Y))
  1153. {
  1154. var move = new StateMove(this, target_pos.X, target_pos.Y);
  1155. move.MinStepCheckCount = 0;
  1156. move.StopOnTouchMap = true;
  1157. move.EndMoveAction = onEndAction;
  1158. return this.changeState(move);
  1159. }
  1160. }
  1161. }
  1162. }
  1163. return false;
  1164. }
  1165. /// <summary>
  1166. /// 单位移动
  1167. /// </summary>
  1168. /// <param name="obj"></param>
  1169. public bool startFollowTo(InstanceZoneObject obj)
  1170. {
  1171. return changeState(new StateFollowObject(this, obj));
  1172. }
  1173. /// <summary>
  1174. /// 单位逃跑
  1175. /// </summary>
  1176. /// <param name="timeMS"></param>
  1177. /// <param name="distance"></param>
  1178. public bool startEscape(int timeMS, float distance = 0)
  1179. {
  1180. return changeState(new StateEscape(this, timeMS, distance));
  1181. }
  1182. /// <summary>
  1183. /// 和自身交互(搓炉石)
  1184. /// </summary>
  1185. /// <param name="timeMS"></param>
  1186. /// <param name="done"></param>
  1187. /// <param name="status"></param>
  1188. public StatePickObject startPickProgressSelf(int timeMS, StatePickObject.OnPickDone done, string status = null)
  1189. {
  1190. StatePickObject picking = new StatePickObject(this, this, timeMS, status, done);
  1191. changeState(picking);
  1192. return picking;
  1193. }
  1194. /// <summary>
  1195. /// 和目标交互
  1196. /// </summary>
  1197. /// <param name="item"></param>
  1198. /// <param name="timeMS"></param>
  1199. /// <param name="done"></param>
  1200. /// <param name="status"></param>
  1201. /// <returns></returns>
  1202. public StatePickObject startPickProgressObject(InstanceZoneObject item, int timeMS, StatePickObject.OnPickDone done, string status = null)
  1203. {
  1204. StatePickObject picking = new StatePickObject(this, item, timeMS, status, done);
  1205. changeState(picking);
  1206. return picking;
  1207. }
  1208. //-----------------------------------------------------------------------------------------------------------------
  1209. #region _SKILL_
  1210. //-----------------------------------------------------------------------------------------------------------------
  1211. public float GetSkillAttackRange(SkillTemplate skill, InstanceUnit targetUnit)
  1212. {
  1213. return mIsAttackRangeIncludeBodySize ? (BodyBlockSize + skill.AttackRange + targetUnit.BodyBlockSize) : skill.AttackRange;
  1214. //return mIsAttackRangeIncludeBodySize ? (BodyBlockSize + skill.AttackRange) : skill.AttackRange;
  1215. }
  1216. public float GetSkillAttackRange(SkillTemplate skill)
  1217. {
  1218. return mIsAttackRangeIncludeBodySize ? (BodyBlockSize + skill.AttackRange) : skill.AttackRange;
  1219. }
  1220. public float GetSkillAttackRange(float range)
  1221. {
  1222. return (mIsAttackRangeIncludeBodySize ? BodyBlockSize + range : range);
  1223. }
  1224. /// <summary>
  1225. /// 判断当前目标在攻击范围内
  1226. /// </summary>
  1227. /// <param name="skill"></param>
  1228. /// <param name="unit"></param>
  1229. /// <returns></returns>
  1230. public bool IsTargetInSkillRange(SkillTemplate skill, InstanceUnit unit)
  1231. {
  1232. float rg = GetSkillAttackRange(skill);
  1233. float dr = skill.AttackAngle / 2;
  1234. if (Collider.Object_HitBody_TouchFan(unit, X, Y, rg, Direction - dr, Direction + dr))
  1235. {
  1236. return true;
  1237. }
  1238. return false;
  1239. }
  1240. [Obsolete]
  1241. public List<InstanceUnit> getSkillAttackableTargets(SkillTemplate skill, AttackReason reason)
  1242. {
  1243. float rg = GetSkillAttackRange(skill);
  1244. List<InstanceUnit> list = Parent.getObjectsRoundRange<InstanceUnit>(Collider.Object_HitBody_TouchRound, X, Y, rg, this.AoiStatus);
  1245. Parent.getAttackableUnits(this, list, skill.ExpectTarget, reason, skill);
  1246. return list;
  1247. }
  1248. public void getSkillAttackableTargets(SkillTemplate skill, List<InstanceUnit> list, AttackReason reason)
  1249. {
  1250. float rg = GetSkillAttackRange(skill);
  1251. Parent.getObjectsRoundRange<InstanceUnit>(Collider.Object_HitBody_TouchRound, X, Y, rg, list, this.AoiStatus);
  1252. Parent.getAttackableUnits(this, list, skill.ExpectTarget, reason, skill);
  1253. }
  1254. /// <summary>
  1255. /// 获得可用的技能
  1256. /// </summary>
  1257. /// <param name="expect"></param>
  1258. /// <returns></returns>
  1259. public SkillState getAvailableSkill(SkillTemplate.CastTarget expect)
  1260. {
  1261. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1262. {
  1263. SkillTemplate st = mAllSkills[si];
  1264. if (st.ExpectTarget == expect)
  1265. {
  1266. SkillState sst = mSkillStatus.Get(st.GetID());
  1267. if (sst.IsActive && sst.IsDone)
  1268. {
  1269. return sst;
  1270. }
  1271. }
  1272. }
  1273. return null;
  1274. }
  1275. /** 获取当前可自动释放的技能 */
  1276. public SkillState getAvailableAutoLaunchSkill(SkillTemplate.CastTarget expect)
  1277. {
  1278. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1279. {
  1280. SkillTemplate st = mAllSkills[si];
  1281. if (st.ExpectTarget == expect)
  1282. {
  1283. SkillState sst = mSkillStatus.Get(st.GetID());
  1284. if (sst.IsActive && sst.IsDone && sst.LaunchSkill.AutoLaunch && sst.CanAutoLaunch())
  1285. {
  1286. return sst;
  1287. }
  1288. }
  1289. }
  1290. return null;
  1291. }
  1292. /// <summary>
  1293. /// 获取当前最适合攻击的目标
  1294. /// </summary>
  1295. /// <param name="skill"></param>
  1296. /// <param name="reason"></param>
  1297. /// <param name="directionChange"></param>
  1298. /// <returns></returns>
  1299. public InstanceUnit getSkillAttackableFirstTarget(SkillTemplate skill, AttackReason reason, ref bool directionChange)
  1300. {
  1301. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1302. {
  1303. getSkillAttackableTargets(skill, list, reason);
  1304. if (list.Count > 0)
  1305. {
  1306. float rg = GetSkillAttackRange(skill);
  1307. // 检测攻击范围内的单位 //
  1308. float dr = skill.AttackAngle / 2;
  1309. for (int i = 0; i < list.Count; i++)
  1310. {
  1311. InstanceUnit u = list[i];
  1312. if (Collider.Object_HitBody_TouchFan(u, X, Y, rg, Direction - dr, Direction + dr))
  1313. {
  1314. directionChange = false;
  1315. return u;
  1316. }
  1317. }
  1318. // 优先当前朝向的目标 //
  1319. dr = CMath.PI_DIV_2;
  1320. directionChange = true;
  1321. for (int i = 0; i < list.Count; i++)
  1322. {
  1323. InstanceUnit u = list[i];
  1324. if (Collider.Object_HitBody_TouchFan(u, X, Y, rg, Direction - dr, Direction + dr))
  1325. {
  1326. return u;
  1327. }
  1328. }
  1329. // 最后选取最近的目标 //
  1330. InstanceUnit min = null;
  1331. float min_len = float.MaxValue;
  1332. for (int i = 0; i < list.Count; i++)
  1333. {
  1334. InstanceUnit u = list[i];
  1335. float len = MathVector.getDistanceSquare(u.X, u.Y, X, Y);
  1336. if (min_len > len)
  1337. {
  1338. min_len = len;
  1339. min = u;
  1340. }
  1341. }
  1342. return min;
  1343. }
  1344. }
  1345. return null;
  1346. }
  1347. public struct LaunchSkillParam
  1348. {
  1349. public uint TargetUnitID;
  1350. public Vector2 SpellTargetPos;
  1351. public bool AutoFocusNearTarget;
  1352. public bool LaunchSetDirection; // 释放时是否重新设置方向
  1353. public float direction;
  1354. public bool IsAutoLaunch; // 是否自动战斗时释放
  1355. public bool IsInGuard; // 是否是自动战斗
  1356. public LaunchSkillParam(uint targetUnitID = 0, Vector2 target_pos = null, bool autoFocusNearTarget = false,
  1357. bool LaunchSetDirection = false, float direction = 0, bool IsAutoLaunch = false, bool IsInGuard = false)
  1358. {
  1359. this.TargetUnitID = targetUnitID;
  1360. this.SpellTargetPos = target_pos;
  1361. this.AutoFocusNearTarget = autoFocusNearTarget;
  1362. this.LaunchSetDirection = LaunchSetDirection;
  1363. this.direction = direction;
  1364. this.IsAutoLaunch = IsAutoLaunch;
  1365. this.IsInGuard = IsInGuard;
  1366. }
  1367. }
  1368. public StateSkill launchSkill(SkillState ss, LaunchSkillParam param)
  1369. {
  1370. if (ss == null) return null;
  1371. StateSkill current = CurrentState as StateSkill;
  1372. if (current != null && current.SkillData.IsManuallyCancelable)
  1373. {
  1374. //如果当前技能为手动取消//
  1375. if (current.Skill.ID == ss.ID)
  1376. {
  1377. //判断停止当前技能//
  1378. if (current.IsCancelableBySkill)
  1379. {
  1380. current.block();
  1381. }
  1382. }
  1383. //手动取消技能禁止其他技能打断//
  1384. return null;
  1385. }
  1386. if (ss.TryLaunch() && mFormula.TryLaunchSkill(this, ss, ref param))
  1387. {
  1388. if (param.IsAutoLaunch && !ss.CanAutoLaunch())
  1389. {
  1390. return null;
  1391. }
  1392. //沉默不能释放其他技能//
  1393. if (IsSilent && ss != mDefaultSkill)
  1394. {
  1395. return null;
  1396. }
  1397. // 自动战斗下,释放技能要在范围内
  1398. InstanceUnit target = null;
  1399. if (ss.Data.AttackMustBeInRange)
  1400. {
  1401. target = Parent.getUnit(param.TargetUnitID);
  1402. if (!ss.checkTargetRange(target))
  1403. {
  1404. return null;
  1405. }
  1406. }
  1407. //是否在合理的攻击范围内
  1408. if (param.IsInGuard && !ss.checkTargetInAttackRange(target, param))
  1409. {
  1410. return null;
  1411. }
  1412. if (ss.GetSkillType() == XmdsSkillType.petGiveAcitve)
  1413. {
  1414. mFormula.TriggerPetSkill(this, ss, ref param);
  1415. return null;
  1416. }
  1417. StateSkill state = new StateSkill(this, ss, param, ss.ActionSpeed, (st) =>
  1418. {
  1419. overLaunchSkill(ss, st);
  1420. if (mOnLaunchSkill != null)
  1421. {
  1422. mOnLaunchSkill.Invoke(this, ss);
  1423. }
  1424. });
  1425. if (state.tryLaunch())
  1426. {
  1427. this.Virtual.SetCombatState(BattleStatus.ReadyBattle);
  1428. Parent.cb_unitLaunchSkill(this, ss);
  1429. if (changeState(state))
  1430. {
  1431. //释放成功
  1432. if (param.LaunchSetDirection)
  1433. {
  1434. //Console.WriteLine("launchSkill - 设置方向: " + param.direction + ", " + this.ID);
  1435. //this.faceTo(param.direction);
  1436. //this.SendForceSync();
  1437. }
  1438. return state;
  1439. }
  1440. }
  1441. }
  1442. return null;
  1443. }
  1444. protected virtual void overLaunchSkill(SkillState ss, StateSkill state)
  1445. {
  1446. this.AddHP(-ss.Data.CostHP, null);
  1447. this.AddMP(-ss.Data.CostMP, null);
  1448. }
  1449. /// <summary>
  1450. /// 单位释放技能
  1451. /// </summary>
  1452. public StateSkill launchSkill(int skillID, LaunchSkillParam param)
  1453. {
  1454. SkillState skill = getSkillState(skillID);
  1455. return launchSkill(skill, param);
  1456. }
  1457. /// <summary>
  1458. /// 释放随机技能,一般用于AI
  1459. /// </summary>
  1460. /// <param name="expectTarget"></param>
  1461. /// <param name="param"></param>
  1462. /// <returns></returns>
  1463. public virtual StateSkill launchRandomSkill(SkillTemplate.CastTarget expectTarget, LaunchSkillParam param)
  1464. {
  1465. StateSkill current = CurrentState as StateSkill;
  1466. if (current != null && !current.IsCancelableBySkill)
  1467. {
  1468. return null;
  1469. }
  1470. int rand = RandomN.Next(0, mAllSkills.Count);
  1471. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1472. {
  1473. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  1474. if (st.ExpectTarget == expectTarget)
  1475. {
  1476. SkillState sst = mSkillStatus.Get(st.GetID());
  1477. if (sst.LaunchSkill.AutoLaunch && sst.TryLaunch())
  1478. {
  1479. StateSkill state = launchSkill(sst, param);
  1480. if (state != null)
  1481. {
  1482. return state;
  1483. }
  1484. }
  1485. }
  1486. }
  1487. return null;
  1488. }
  1489. public virtual StateSkill launchRandomSkillForAll(LaunchSkillParam param)
  1490. {
  1491. StateSkill current = CurrentState as StateSkill;
  1492. if (current != null && !current.IsCancelableBySkill)
  1493. {
  1494. return null;
  1495. }
  1496. int rand = RandomN.Next(0, mAllSkills.Count);
  1497. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1498. {
  1499. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  1500. SkillState sst = mSkillStatus.Get(st.GetID());
  1501. if (sst.LaunchSkill.AutoLaunch && sst.TryLaunch())
  1502. {
  1503. StateSkill state = launchSkill(sst, param);
  1504. if (state != null)
  1505. {
  1506. return state;
  1507. }
  1508. }
  1509. }
  1510. return null;
  1511. }
  1512. /// <summary>
  1513. /// 尝试取消当前技能,打出连击
  1514. /// </summary>
  1515. /// <param name="target"></param>
  1516. /// <param name="autoFocusNearTarget"></param>
  1517. /// <returns></returns>
  1518. public virtual bool tryLaunchRandomSkillAndCancelCurrentSkill(InstanceUnit target, bool autoFocusNearTarget = false)
  1519. {
  1520. StateSkill current = CurrentState as StateSkill;
  1521. if (current != null && !current.IsChanting && current.IsCancelableBySkill)
  1522. {
  1523. LaunchSkillParam param = new LaunchSkillParam(target.ID, null, autoFocusNearTarget);
  1524. //优先多段攻击//
  1525. if (current.SkillData.IsSingleAction)
  1526. {
  1527. if (IsTargetInSkillRange(current.SkillData, target))
  1528. {
  1529. StateSkill st = launchSkill(current.SkillData.ID, param);
  1530. if (st != null)
  1531. {
  1532. return true;
  1533. }
  1534. }
  1535. }
  1536. //随机其他技能//
  1537. int rand = RandomN.Next(0, mAllSkills.Count);
  1538. for (int si = mAllSkills.Count - 1; si >= 0; --si)
  1539. {
  1540. SkillTemplate st = mAllSkills[CMath.cycNum(rand, si, mAllSkills.Count)];
  1541. if (Parent.IsAttackable(this, target, st.ExpectTarget, AttackReason.Attack, st))
  1542. {
  1543. SkillState sst = mSkillStatus.Get(st.GetID());
  1544. if (sst.LaunchSkill.AutoLaunch && IsTargetInSkillRange(st, target))
  1545. {
  1546. StateSkill state = launchSkill(sst, param);
  1547. if (state != null)
  1548. {
  1549. return true;
  1550. }
  1551. }
  1552. }
  1553. }
  1554. }
  1555. return false;
  1556. }
  1557. /// <summary>
  1558. /// 尝试在范围内找到目标释放技能,并进入StateFollowAndAttack,自己走过去打。
  1559. /// </summary>
  1560. /// <param name="range"></param>
  1561. /// <param name="skill_auto_launchable"></param>
  1562. /// <returns></returns>
  1563. public virtual bool tryFollowAndLaunchRandomSkillToTargetInRange(float range, bool skill_auto_launchable = false)
  1564. {
  1565. if (!IsNoneSkill)
  1566. {
  1567. SkillState[] skills = mSkillStatus.SkillsArray;
  1568. CUtils.RandomArray(RandomN, skills);
  1569. using (var list = ListObjectPool<InstanceUnit>.AllocAutoRelease())
  1570. {
  1571. //随机找个目标施法//
  1572. Parent.getObjectsRoundRange<InstanceUnit>(Collider.Object_Pos_IncludeInRound, X, Y, Info.GuardRange, list, this.AoiStatus);
  1573. DoAndRemoveCollection.UpdateAndRemove<InstanceUnit>(list, (InstanceUnit u) =>
  1574. {
  1575. return !u.IsActive;
  1576. });
  1577. if (list.Count == 0) { return false; }
  1578. CUtils.RandomList(Parent.RandomN, list);
  1579. foreach (SkillState skill in skills)
  1580. {
  1581. if ((!skill_auto_launchable || skill.LaunchSkill.AutoLaunch) && skill.TryLaunch())
  1582. {
  1583. for (int i = 0; i < list.Count; i++)
  1584. {
  1585. InstanceUnit u = list[i];
  1586. if (Parent.IsAttackable(this, u, skill.Data.ExpectTarget, AttackReason.Attack, skill.Data))
  1587. {
  1588. //检测是否有可释放技能//
  1589. changeState(new StateFollowAndAttack(this, u, skill.Data.ExpectTarget));
  1590. return true;
  1591. }
  1592. }
  1593. }
  1594. }
  1595. }
  1596. }
  1597. return false;
  1598. }
  1599. #endregion
  1600. //-----------------------------------------------------------------------------------------------------------------
  1601. internal void deadDropItems(int force)
  1602. {
  1603. foreach (KeyValuePair<ItemTemplate, DropItem> e in mDropItems.Drop(Parent, Parent.RandomN))
  1604. {
  1605. float x;
  1606. float y;
  1607. CMath.RandomPosInRound(RandomN, X, Y, e.Value.DropPosRange, out x, out y);
  1608. Parent.AddItem(e.Key, e.Key.Name, x, y, CMath.RandomAngle(RandomN), force, e.Key.Name, this);
  1609. }
  1610. }
  1611. protected bool tryPickObject(InstanceUnit unit)
  1612. {
  1613. if (!Parent.IsVisibleAOI(this, unit))
  1614. {
  1615. return false;
  1616. }
  1617. bool ret = true;
  1618. if (mOnTryPickUnit != null)
  1619. {
  1620. foreach (TryPickUnitHandler trypick in mOnTryPickUnit.GetInvocationList())
  1621. {
  1622. if (!trypick.Invoke(this, unit))
  1623. {
  1624. ret = false;
  1625. }
  1626. }
  1627. }
  1628. return ret;
  1629. }
  1630. public void PickUnit(InstanceUnit pickable)
  1631. {
  1632. if (tryPickObject(pickable))
  1633. {
  1634. Parent.cb_unitPickUnitCallBack(this, pickable);
  1635. if (mOnPickUnit != null)
  1636. {
  1637. mOnPickUnit.Invoke(this, pickable);
  1638. }
  1639. }
  1640. }
  1641. public CommonAI.Data.SceneType GetSceneType()
  1642. {
  1643. return mZone.SceneType;
  1644. }
  1645. //-----------------------------------------------------------------------------------------------------//
  1646. #region Environment
  1647. //-----------------------------------------------------------------------------------------------------//
  1648. private HashMap<string, EnvironmentVar> EnvironmentVarMap = new HashMap<string, EnvironmentVar>();
  1649. public void SetEnvironmentVar(string key, object value, bool syncToClient = false)
  1650. {
  1651. if (!string.IsNullOrEmpty(key))
  1652. {
  1653. EnvironmentVar var = EnvironmentVarMap.Get(key);
  1654. if (var != null)
  1655. {
  1656. if (EnvironmentVar.ALWAYS_SYNC_ENVIRONMENT_VAR || (var.SyncToClient && var.Value != value))
  1657. {
  1658. queueEvent(new PlayerSyncEnvironmentVarEvent(ID, key, value));
  1659. }
  1660. var.Value = value;
  1661. }
  1662. else
  1663. {
  1664. var = new EnvironmentVar(key, syncToClient, value);
  1665. EnvironmentVarMap.Add(key, var);
  1666. if (EnvironmentVar.ALWAYS_SYNC_ENVIRONMENT_VAR || var.SyncToClient)
  1667. {
  1668. queueEvent(new PlayerSyncEnvironmentVarEvent(ID, key, value));
  1669. }
  1670. }
  1671. }
  1672. }
  1673. public T GetEnvironmentVarAs<T>(string key)
  1674. {
  1675. if (!string.IsNullOrEmpty(key))
  1676. {
  1677. EnvironmentVar var = EnvironmentVarMap.Get(key);
  1678. if (var != null)
  1679. {
  1680. try
  1681. {
  1682. return (T)var.Value;
  1683. }
  1684. catch (Exception err)
  1685. {
  1686. log.Warn("GetEnvironmentVarAs : " + key + ", catch: " + err);
  1687. }
  1688. }
  1689. }
  1690. return default(T);
  1691. }
  1692. public int ListEnvironmentVars(List<EnvironmentVar> list)
  1693. {
  1694. list.AddRange(EnvironmentVarMap.Values);
  1695. return EnvironmentVarMap.Count;
  1696. }
  1697. public List<EnvironmentVar> ListEnvironmentVars()
  1698. {
  1699. return new List<EnvironmentVar>(EnvironmentVarMap.Values);
  1700. }
  1701. public ClientStruct.ZoneEnvironmentVar[] GetCurrentUnitVars()
  1702. {
  1703. ClientStruct.ZoneEnvironmentVar[] ret = new ClientStruct.ZoneEnvironmentVar[EnvironmentVarMap.Count];
  1704. int i = 0;
  1705. try
  1706. {
  1707. foreach (EnvironmentVar var in EnvironmentVarMap.Values)
  1708. {
  1709. ret[i].Key = var.Key;
  1710. ret[i].Value = var.Value;
  1711. ret[i].SyncToClient = var.SyncToClient;
  1712. i++;
  1713. }
  1714. }
  1715. catch (Exception e)
  1716. {
  1717. log.Warn("GetCurrentUnitVars catch: " + ret.Length + ", " + EnvironmentVarMap.Count + ", " + i + ", e: " + e);
  1718. }
  1719. return ret;
  1720. }
  1721. #endregion
  1722. //-----------------------------------------------------------------------------------------------------//
  1723. //-----------------------------------------------------------------------------------------------------//
  1724. #region SceneEvents
  1725. //-----------------------------------------------------------------------------------------------------//
  1726. private HashMap<int, UnitEventTriggerCollection> mBindEvents = new HashMap<int, UnitEventTriggerCollection>();
  1727. public void BindUnitEvent(int unit_event_id)
  1728. {
  1729. if (!mBindEvents.ContainsKey(unit_event_id))
  1730. {
  1731. UnitEventTemplate uet = Templates.getUnitEvent(unit_event_id);
  1732. if (uet != null)
  1733. {
  1734. uet = uet.Clone() as UnitEventTemplate;
  1735. UnitEventTriggerCollection bind_event = new UnitEventTriggerCollection(this);
  1736. bind_event.Bind(uet.Events);
  1737. mBindEvents.Add(unit_event_id, bind_event);
  1738. }
  1739. }
  1740. }
  1741. protected virtual void clearBindEvents()
  1742. {
  1743. foreach (var evt in mBindEvents.Values)
  1744. {
  1745. evt.Dispose();
  1746. }
  1747. mBindEvents.Clear();
  1748. }
  1749. /** 计算并获得伤害 */
  1750. public virtual void PetShareDamage(int baseDmgValue, InstanceUnit sender)
  1751. {
  1752. }
  1753. #endregion
  1754. //-----------------------------------------------------------------------------------------------------//
  1755. //-----------------------------------------------------------------------------------------------------//
  1756. }
  1757. }