InstanceUnit.cs 71 KB

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