InstanceUnit.cs 71 KB

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