InstanceFlag.cs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonAI.RTS;
  5. using CommonLang.Vector;
  6. using CommonLang;
  7. using CommonAI.Zone.Helper;
  8. using CommonAI.RTS.Manhattan;
  9. using CommonAI.Zone.ZoneEditor;
  10. namespace CommonAI.Zone.Instance
  11. {
  12. public abstract class InstanceFlag : GameEntity, IPositionObject
  13. {
  14. public abstract float Direction { get; }
  15. public abstract float RadiusSize { get; }
  16. public InstanceZone Parent { get { return mParent; } }
  17. public string Name { get { return mName; } }
  18. public string Alias { get { return mAlias; } }
  19. public Vector2 Pos { get { return mPos; } }
  20. public float X { get { return mPos.X; } }
  21. public float Y { get { return mPos.Y; } }
  22. private Vector2 mPos = new Vector2();
  23. private readonly string mName;
  24. private readonly string mAlias;
  25. private readonly string mSrcTag;
  26. private bool mEnableTriggerd = false;
  27. private bool mEnable = true;
  28. private InstanceZone mParent;
  29. private string mTag;
  30. public bool Enable
  31. {
  32. get { return mEnable; }
  33. set
  34. {
  35. if (!mEnableTriggerd || mEnable != value)
  36. {
  37. mEnableTriggerd = true;
  38. mEnable = value;
  39. if (mEnable)
  40. {
  41. if (mOnFlagEnabled != null)
  42. mOnFlagEnabled.Invoke(this);
  43. }
  44. else
  45. {
  46. if (mOnFlagDisabled != null)
  47. mOnFlagDisabled.Invoke(this);
  48. }
  49. }
  50. }
  51. }
  52. public string Tag
  53. {
  54. get { return mTag; }
  55. set
  56. {
  57. if (mTag != value)
  58. {
  59. mTag = value;
  60. Parent.queueEvent(new FlagTagChangedEvent(this.mName, this.mTag));
  61. }
  62. }
  63. }
  64. public string SrcTag
  65. {
  66. get { return mSrcTag; }
  67. }
  68. public InstanceFlag(InstanceZone zone, ZoneEditor.SceneObjectData data)
  69. {
  70. this.mParent = zone;
  71. this.mName = data.Name;
  72. this.mAlias = data.Alias;
  73. this.mPos = new Vector2(data.X, data.Y);
  74. this.mTag = data.Tag;
  75. }
  76. protected override void Disposing()
  77. {
  78. base.Disposing();
  79. this.clearEvents();
  80. }
  81. virtual internal void onAdded()
  82. {
  83. if (mOnInit != null)
  84. {
  85. mOnInit.Invoke(this);
  86. }
  87. }
  88. protected internal virtual void OnStart()
  89. {
  90. }
  91. virtual internal void update()
  92. {
  93. }
  94. abstract public TVector2 getRandomPos(Random random);
  95. //----------------------------------------------------------------------------
  96. #region IFlagAPI
  97. public bool get_enable()
  98. {
  99. return Enable;
  100. }
  101. public bool set_enable(bool e)
  102. {
  103. Enable = e;
  104. return e;
  105. }
  106. public string get_name()
  107. {
  108. return Name;
  109. }
  110. public float get_x()
  111. {
  112. return X;
  113. }
  114. public float get_y()
  115. {
  116. return Y;
  117. }
  118. /// <summary>
  119. /// Show Check Point
  120. /// </summary>
  121. /// <param name="target"></param>
  122. public void look_at_target(string target)
  123. {
  124. mParent.queueEvent(new LookAtEvent(target, 0, 0));
  125. }
  126. #endregion
  127. //----------------------------------------------------------------------------
  128. #region Delegate
  129. /// <summary>
  130. /// 触发器开始
  131. /// </summary>
  132. /// <param name="flag"></param>
  133. public delegate void InitHandler(InstanceFlag flag);
  134. /// <summary>
  135. /// 触发器被开启
  136. /// </summary>
  137. /// <param name="flag"></param>
  138. public delegate void FlagEnabledHandler(InstanceFlag flag);
  139. /// <summary>
  140. /// 触发器被关闭
  141. /// </summary>
  142. /// <param name="flag"></param>
  143. public delegate void FlagDisabledHandler(InstanceFlag flag);
  144. private InitHandler mOnInit;
  145. private FlagEnabledHandler mOnFlagEnabled;
  146. private FlagDisabledHandler mOnFlagDisabled;
  147. public event InitHandler OnInit { add { mOnInit += value; } remove { mOnInit -= value; } }
  148. public event FlagEnabledHandler OnFlagEnabled { add { mOnFlagEnabled += value; } remove { mOnFlagEnabled -= value; } }
  149. public event FlagDisabledHandler OnFlagDisabled { add { mOnFlagDisabled += value; } remove { mOnFlagDisabled -= value; } }
  150. protected virtual void clearEvents()
  151. {
  152. mOnInit = null;
  153. mOnFlagEnabled = null;
  154. mOnFlagDisabled = null;
  155. }
  156. #endregion
  157. //----------------------------------------------------------------------------
  158. }
  159. public class ZoneRegion : InstanceFlag, ViewTrigger.ViewTriggerListener
  160. {
  161. public ZoneEditor.RegionData.Shape RegionType { get { return mShape; } }
  162. /// <summary>
  163. /// Center of the position
  164. /// </summary>
  165. public override float RadiusSize { get { return R; } }
  166. public override float Direction { get { return 0; } }
  167. public float Width { get { return W; } }
  168. public float Height { get { return H; } }
  169. public float Radius { get { return R; } }
  170. readonly private ZoneEditor.RegionData.Shape mShape;
  171. private float W;
  172. private float H;
  173. private float R;
  174. private ViewTrigger mViewTrigger;
  175. private bool mAllTriggerOver = false;
  176. private bool mAllTriggerNoneAlive = false;
  177. private List<AbstractSpawnTrigger> mSpawnTriggers = new List<AbstractSpawnTrigger>();
  178. public ZoneRegion(InstanceZone zone, ZoneEditor.RegionData data)
  179. : base(zone, data)
  180. {
  181. this.mShape = data.RegionType;
  182. this.W = data.W;
  183. this.H = data.H;
  184. this.R = data.Radius;
  185. switch (mShape)
  186. {
  187. case ZoneEditor.RegionData.Shape.RECTANGLE:
  188. this.mViewTrigger = new ViewTriggerRectCenter(Parent, W, H);
  189. break;
  190. case ZoneEditor.RegionData.Shape.STRIP:
  191. this.mViewTrigger = new ViewTriggerStripCenter(Parent, W, H, data.StripDirection);
  192. break;
  193. case ZoneEditor.RegionData.Shape.ROUND:
  194. default:
  195. this.mViewTrigger = new ViewTriggerRoundCenter(Parent, R);
  196. break;
  197. }
  198. this.mViewTrigger.setListener(this);
  199. }
  200. public override TVector2 getRandomPos(Random random)
  201. {
  202. if (mShape == ZoneEditor.RegionData.Shape.ROUND)
  203. {
  204. float angle = (float)(random.NextDouble() * CMath.PI_MUL_2);
  205. float len = (float)(random.NextDouble() * R);
  206. float x = X + (float)(Math.Cos(angle) * len);
  207. float y = Y + (float)(Math.Sin(angle) * len);
  208. return new TVector2(x, y);
  209. }
  210. else
  211. {
  212. float x = X + (float)((-W / 2f) + random.NextDouble() * W);
  213. float y = Y + (float)((-H / 2f) + random.NextDouble() * H);
  214. return new TVector2(x, y);
  215. }
  216. }
  217. public bool IsUnitInRegion(InstanceZoneObject unit)
  218. {
  219. return Collider.Object_BlockBody_TouchRound(unit, this.get_x(), this.get_y(), this.RadiusSize); ;
  220. }
  221. public void keepInRegion(ref float x, ref float y)
  222. {
  223. switch (mShape)
  224. {
  225. case ZoneEditor.RegionData.Shape.RECTANGLE:
  226. x = CMath.getInRange(x, this.X - this.W / 2, this.X + this.W / 2);
  227. y = CMath.getInRange(y, this.Y - this.H / 2, this.Y + this.H / 2);
  228. break;
  229. case ZoneEditor.RegionData.Shape.ROUND:
  230. float d = MathVector.getDistance(x, y, this.X, this.Y);
  231. if (d > this.R)
  232. {
  233. float a = MathVector.getDegree(x, y, this.X, this.Y);
  234. MathVector.movePolar(ref x, ref y, a, d - this.R);
  235. }
  236. break;
  237. }
  238. }
  239. protected override void Disposing()
  240. {
  241. base.Disposing();
  242. mViewTrigger.Dispose();
  243. foreach (var e in mSpawnTriggers)
  244. {
  245. e.Dispose();
  246. }
  247. mSpawnTriggers.Clear();
  248. mOnUnitEnter = null;
  249. mOnUnitLeave = null;
  250. mOnZoneUpdate = null;
  251. mOnSpawnOver = null;
  252. }
  253. internal void AddSpawnTrigger(AbstractSpawnTrigger trigger)
  254. {
  255. trigger.OnLastObjectRemoved += onTriggerLastObjectRemoved;
  256. mSpawnTriggers.Add(trigger);
  257. mAllTriggerOver = false;
  258. }
  259. private void onTriggerLastObjectRemoved(ZoneRegion region, AbstractSpawnTrigger trigger, InstanceZoneObject unit)
  260. {
  261. if (mOnSpawnOver != null && IsSpawnOver)
  262. {
  263. mOnSpawnOver.Invoke(this);
  264. }
  265. }
  266. /// <summary>
  267. /// 获取此区域绑定的怪物刷新点
  268. /// </summary>
  269. /// <returns></returns>
  270. public IEnumerable<AbstractSpawnTrigger> GetSpawnTriggers()
  271. {
  272. return mSpawnTriggers;
  273. }
  274. /// <summary>
  275. /// 获取此区域绑定的怪物刷新点
  276. /// </summary>
  277. /// <param name="name"></param>
  278. /// <returns></returns>
  279. public AbstractSpawnTrigger GetSpawnTrigger(string name)
  280. {
  281. for (int i = mSpawnTriggers.Count - 1; i >= 0; --i)
  282. {
  283. AbstractSpawnTrigger tg = mSpawnTriggers[i];
  284. if (string.Equals(tg.Name, name))
  285. {
  286. return tg;
  287. }
  288. }
  289. return null;
  290. }
  291. /// <summary>
  292. /// 检测是否所有刷怪点都结束
  293. /// </summary>
  294. public bool IsSpawnOver
  295. {
  296. get
  297. {
  298. if (!mAllTriggerOver)
  299. {
  300. for (int i = mSpawnTriggers.Count - 1; i >= 0; --i)
  301. {
  302. AbstractSpawnTrigger tg = mSpawnTriggers[i];
  303. if (!tg.IsSpawnOver)
  304. {
  305. return false;
  306. }
  307. }
  308. mAllTriggerOver = true;
  309. }
  310. return true;
  311. }
  312. }
  313. /// <summary>
  314. /// 检测是否所有刷怪点都无存活
  315. /// </summary>
  316. public bool IsSpawnNoneAlive
  317. {
  318. get
  319. {
  320. if (!mAllTriggerNoneAlive)
  321. {
  322. for (int i = mSpawnTriggers.Count - 1; i >= 0; --i)
  323. {
  324. AbstractSpawnTrigger tg = mSpawnTriggers[i];
  325. if (!tg.IsSpawnOver)
  326. {
  327. return false;
  328. }
  329. if (tg.AliveCount > 0)
  330. {
  331. return false;
  332. }
  333. }
  334. mAllTriggerNoneAlive = true;
  335. }
  336. return true;
  337. }
  338. }
  339. internal override void update()
  340. {
  341. base.update();
  342. if (Enable && (mOnUnitEnter != null || mOnUnitLeave != null || mEnterOnceList.Count > 0 || mLeaveOnceList.Count > 0))
  343. {
  344. mViewTrigger.Enable = true;
  345. mViewTrigger.onLookUpdate(X, Y);
  346. }
  347. else
  348. {
  349. mViewTrigger.Enable = false;
  350. }
  351. if (Enable && mOnZoneUpdate != null)
  352. {
  353. mOnZoneUpdate.Invoke(this);
  354. }
  355. }
  356. void ViewTrigger.ViewTriggerListener.onObjectEnterView(ViewTrigger src, InstanceZoneObject obj)
  357. {
  358. if (mOnUnitEnter != null)
  359. {
  360. mOnUnitEnter.Invoke(this, obj as InstanceUnit);
  361. }
  362. mEnterOnceList.Invoke(this, obj as InstanceUnit);
  363. }
  364. void ViewTrigger.ViewTriggerListener.onObjectLeaveView(ViewTrigger src, InstanceZoneObject obj)
  365. {
  366. if (mOnUnitLeave != null)
  367. {
  368. mOnUnitLeave.Invoke(this, obj as InstanceUnit);
  369. }
  370. mLeaveOnceList.Invoke(this, obj as InstanceUnit);
  371. }
  372. bool ViewTrigger.ViewTriggerListener.select(ViewTrigger src, InstanceZoneObject obj)
  373. {
  374. return obj is InstanceUnit;
  375. }
  376. public void addInRegionViewed(InstanceUnit unit)
  377. {
  378. if (mViewTrigger != null)
  379. {
  380. mViewTrigger.addViewed(unit);
  381. }
  382. }
  383. /// <summary>
  384. /// 获得所有由此刷新点产生的单位
  385. /// </summary>
  386. /// <typeparam name="T"></typeparam>
  387. /// <param name="list"></param>
  388. public void getSpawnedObjectsInRegion<T>(List<T> list) where T : InstanceZoneObject
  389. {
  390. foreach (var spawn in mSpawnTriggers)
  391. {
  392. spawn.getSpawnedObjects<T>(list);
  393. }
  394. }
  395. //[Obsolete]
  396. //private List<T> getObjectsInRegion<T>(ObjectAoiStatus aoi) where T : InstanceZoneObject
  397. //{
  398. // if (this.RegionType == ZoneEditor.RegionData.Shape.RECTANGLE)
  399. // {
  400. // return Parent.getObjectsRectRange<T>(
  401. // Collider.Object_Pos_IncludeInRect,
  402. // this.X, this.Y, this.Width, this.Height , aoi);
  403. // }
  404. // else
  405. // {
  406. // return Parent.getObjectsRoundRange<T>(
  407. // Collider.Object_Pos_IncludeInRound,
  408. // this.X, this.Y, this.Radius, aoi);
  409. // }
  410. //}
  411. public void getObjectsInRegion<T>(List<T> list, ObjectAoiStatus aoi = null) where T : InstanceZoneObject
  412. {
  413. if (this.RegionType == ZoneEditor.RegionData.Shape.RECTANGLE)
  414. {
  415. Parent.getObjectsRectRange<T>(
  416. Collider.Object_Pos_IncludeInRect,
  417. this.X - this.Width/2, this.Y- this.Height/2, this.X + this.Width/2, this.Y + this.Height/2,
  418. list, aoi);
  419. }
  420. else
  421. {
  422. Parent.getObjectsRoundRange<T>(
  423. Collider.Object_Pos_IncludeInRound,
  424. this.X, this.Y, this.Radius,
  425. list, aoi);
  426. }
  427. }
  428. public int getObjectsCountInRegion<T>(ObjectAoiStatus aoi = null) where T : InstanceZoneObject
  429. {
  430. if (this.RegionType == ZoneEditor.RegionData.Shape.RECTANGLE)
  431. {
  432. return Parent.getObjectsCountRectRange<T>(Collider.Object_Pos_IncludeInRect,
  433. this.X - this.Width / 2, this.Y - this.Height / 2, this.X + this.Width / 2, this.Y + this.Height / 2, aoi);
  434. }
  435. else
  436. {
  437. return Parent.getObjectsCountRoundRange<T>(
  438. Collider.Object_Pos_IncludeInRound,
  439. this.X, this.Y, this.Radius, aoi);
  440. }
  441. }
  442. //[Obsolete]
  443. //private List<T> getObjectsInRegion<T>(Select<T> select, ObjectAoiStatus aoi) where T : InstanceZoneObject
  444. //{
  445. // List<T> list = getObjectsInRegion<T>();
  446. // for (int i = list.Count - 1; i >= 0; --i)
  447. // {
  448. // if (!select.Invoke(list[i]))
  449. // {
  450. // list.RemoveAt(i);
  451. // }
  452. // }
  453. // return list;
  454. //}
  455. public int getObjectsCountInRegion<T>(Select<T> select , ObjectAoiStatus aoi = null) where T : InstanceZoneObject
  456. {
  457. int ret = 0;
  458. using (var list = ListObjectPool<T>.AllocAutoRelease())
  459. {
  460. getObjectsInRegion<T>(list , aoi);
  461. for (int i = list.Count - 1; i >= 0; --i)
  462. {
  463. if (select.Invoke(list[i]))
  464. {
  465. ret++;
  466. }
  467. }
  468. }
  469. return ret;
  470. }
  471. //--------------------------------------------------------------------------------------------------------------
  472. #region Delegate
  473. /// <summary>
  474. /// 某单位进入此区域
  475. /// </summary>
  476. /// <param name="region"></param>
  477. /// <param name="obj"></param>
  478. public delegate void UnitEnterHandler(ZoneRegion region, InstanceUnit obj);
  479. /// <summary>
  480. /// 某单位离开此区域
  481. /// </summary>
  482. /// <param name="region"></param>
  483. /// <param name="obj"></param>
  484. public delegate void UnitLeaveHandler(ZoneRegion region, InstanceUnit obj);
  485. /// <summary>
  486. /// 区域更新
  487. /// </summary>
  488. /// <param name="region"></param>
  489. public delegate void ZoneUpdateHandler(ZoneRegion region);
  490. /// <summary>
  491. /// 刷新点刷新完毕
  492. /// </summary>
  493. /// <param name="region"></param>
  494. public delegate void SpawnOverHandler(ZoneRegion region);
  495. private UnitEnterHandler mOnUnitEnter;
  496. private UnitLeaveHandler mOnUnitLeave;
  497. private ZoneUpdateHandler mOnZoneUpdate;
  498. private SpawnOverHandler mOnSpawnOver;
  499. public event UnitEnterHandler OnUnitEnter { add { mOnUnitEnter += value; } remove { mOnUnitEnter -= value; } }
  500. public event UnitLeaveHandler OnUnitLeave { add { mOnUnitLeave += value; } remove { mOnUnitLeave -= value; } }
  501. public event ZoneUpdateHandler OnZoneUpdate { add { mOnZoneUpdate += value; } remove { mOnZoneUpdate -= value; } }
  502. public event SpawnOverHandler OnSpawnOver { add { mOnSpawnOver += value; } remove { mOnSpawnOver -= value; } }
  503. //--------------------------------------------------------------------------------------------------------------
  504. private struct OnceEvent : IOnceInvoke
  505. {
  506. public InstanceUnit unit { get; private set; }
  507. public bool IsDone { get { return done || !unit.Enable; } }
  508. private UnitEnterHandler enter_handler;
  509. private UnitLeaveHandler leave_handler;
  510. private bool done;
  511. public OnceEvent(InstanceUnit unit, UnitEnterHandler enter, UnitLeaveHandler leave)
  512. {
  513. this.unit = unit;
  514. this.enter_handler = enter;
  515. this.leave_handler = leave;
  516. this.done = false;
  517. }
  518. public void Invoke(ZoneRegion region)
  519. {
  520. if (enter_handler != null) this.enter_handler.Invoke(region, unit);
  521. if (leave_handler != null) this.leave_handler.Invoke(region, unit);
  522. this.enter_handler = null;
  523. this.leave_handler = null;
  524. this.done = true;
  525. }
  526. }
  527. private class OnceInvokeList : OnceInvokeList<OnceEvent>
  528. {
  529. public void Invoke(ZoneRegion region, InstanceUnit unit)
  530. {
  531. base.Invoke((e) =>
  532. {
  533. if (e.unit == unit)
  534. {
  535. e.Invoke(region);
  536. }
  537. });
  538. }
  539. }
  540. private OnceInvokeList mEnterOnceList = new OnceInvokeList();
  541. private OnceInvokeList mLeaveOnceList = new OnceInvokeList();
  542. /// <summary>
  543. /// 监听单位进入一次,触发器只触发一次
  544. /// </summary>
  545. /// <param name="unit"></param>
  546. /// <param name="handler"></param>
  547. public void ListenUnitEnterOnce(InstanceUnit unit, UnitEnterHandler handler)
  548. {
  549. mEnterOnceList.Add(new OnceEvent(unit, handler, null));
  550. }
  551. /// <summary>
  552. /// 监听单位离开一次,触发器只触发一次
  553. /// </summary>
  554. /// <param name="unit"></param>
  555. /// <param name="handler"></param>
  556. public void ListenUnitLeaveOnce(InstanceUnit unit, UnitLeaveHandler handler)
  557. {
  558. mLeaveOnceList.Add(new OnceEvent(unit, null, handler));
  559. }
  560. //--------------------------------------------------------------------------------------------------------------
  561. protected override void clearEvents()
  562. {
  563. base.clearEvents();
  564. mEnterOnceList.Clear();
  565. mLeaveOnceList.Clear();
  566. mOnUnitEnter = null;
  567. mOnUnitLeave = null;
  568. mOnZoneUpdate = null;
  569. mOnSpawnOver = null;
  570. }
  571. #endregion
  572. }
  573. public class ZoneWayPoint : InstanceFlag
  574. {
  575. public override float RadiusSize { get { return 1; } }
  576. public override float Direction { get { return 0; } }
  577. private IList<ZoneWayPoint> mNexts = new List<ZoneWayPoint>();
  578. public ZoneWayPoint(InstanceZone zone, ZoneEditor.PointData data)
  579. : base(zone, data)
  580. {
  581. }
  582. public override TVector2 getRandomPos(Random random)
  583. {
  584. return new TVector2(X, Y);
  585. }
  586. internal override void update()
  587. {
  588. base.update();
  589. }
  590. internal void AddNext(ZoneWayPoint next)
  591. {
  592. mNexts.Add(next);
  593. }
  594. public ZoneWayPoint GetRandomNext(ZoneWayPoint prev = null)
  595. {
  596. if (mNexts.Count > 0)
  597. {
  598. if (mNexts.Count == 1)
  599. {
  600. return mNexts[0];
  601. }
  602. int nexti = Parent.RandomN.Next(0, mNexts.Count);
  603. for (int i = 0; i < mNexts.Count; i++)
  604. {
  605. int idx = CMath.cycNum(nexti, i, mNexts.Count);
  606. if (mNexts[idx] != prev)
  607. {
  608. return mNexts[idx];
  609. }
  610. }
  611. return mNexts[nexti];
  612. }
  613. return null;
  614. }
  615. public ZoneWayPoint GetNext()
  616. {
  617. if (mNexts.Count > 0)
  618. {
  619. return mNexts[0];
  620. }
  621. return null;
  622. }
  623. public ZoneWayPoint GetTail()
  624. {
  625. if (mNexts.Count == 0)
  626. {
  627. return this;
  628. }
  629. ZoneWayPoint wp = GetNext();
  630. while (true)
  631. {
  632. ZoneWayPoint next = wp.GetNext();
  633. if (next != null)
  634. {
  635. wp = next;
  636. }
  637. else
  638. {
  639. break;
  640. }
  641. }
  642. return wp;
  643. }
  644. }
  645. public class ZoneDecoration : InstanceFlag
  646. {
  647. public override float RadiusSize { get { return this.R; } }
  648. public override float Direction { get { return this.StripDirection; } }
  649. readonly private ZoneEditor.DecorationData.Shape mShape;
  650. private float W;
  651. private float H;
  652. private float R;
  653. private float StripDirection;
  654. private bool Blockable;
  655. private int BlockValue;
  656. public ZoneDecoration(InstanceZone zone, ZoneEditor.DecorationData data)
  657. : base(zone, data)
  658. {
  659. this.mShape = data.RegionType;
  660. this.W = data.W;
  661. this.H = data.H;
  662. this.R = data.Radius;
  663. this.StripDirection = data.StripDirection;
  664. this.Blockable = data.Blockable;
  665. this.BlockValue = data.BlockValue;
  666. this.OnFlagEnabled += this.OnEnabled;
  667. this.OnFlagDisabled += this.OnDisabled;
  668. if (Blockable)
  669. {
  670. this.OnFlagEnabled += this.OnTerrainEnabled;
  671. this.OnFlagDisabled += this.OnTerrainDisabled;
  672. }
  673. }
  674. internal override void update()
  675. {
  676. base.update();
  677. }
  678. public override TVector2 getRandomPos(Random random)
  679. {
  680. if (mShape == ZoneEditor.DecorationData.Shape.ROUND)
  681. {
  682. float angle = (float)(random.NextDouble() * CMath.PI_MUL_2);
  683. float len = (float)(random.NextDouble() * R);
  684. float x = X + (float)(Math.Cos(angle) * len);
  685. float y = Y + (float)(Math.Sin(angle) * len);
  686. return new TVector2(x, y);
  687. }
  688. else
  689. {
  690. float x = X + (float)((-W / 2f) + random.NextDouble() * W);
  691. float y = Y + (float)((-H / 2f) + random.NextDouble() * H);
  692. return new TVector2(x, y);
  693. }
  694. }
  695. private void OnEnabled(InstanceFlag flag)
  696. {
  697. Parent.queueEvent(new DecorationChangedEvent(Name, true));
  698. }
  699. private void OnDisabled(InstanceFlag flag)
  700. {
  701. Parent.queueEvent(new DecorationChangedEvent(Name, false));
  702. }
  703. #region FillTerrain
  704. private void OnTerrainEnabled(InstanceFlag flag)
  705. {
  706. this.FillTerrain(fill_Terrain_value);
  707. }
  708. private void OnTerrainDisabled(InstanceFlag flag)
  709. {
  710. this.FillTerrain(fill_Terrain_reset);
  711. }
  712. private void fill_Terrain_value(int bx, int by)
  713. {
  714. Parent.PathFinder.TryFillTerrain(bx, by, BlockValue);
  715. }
  716. private void fill_Terrain_reset(int bx, int by)
  717. {
  718. Parent.PathFinder.TryFillTerrain(bx, by, Parent.TerrainSrc.TerrainMatrix[bx, by]);
  719. }
  720. protected void FillTerrain(AstarManhattan.ForEachTerrainAction fillAction)
  721. {
  722. var mmap = Parent.PathFinder.MMap;
  723. switch (this.mShape)
  724. {
  725. case ZoneEditor.DecorationData.Shape.RECTANGLE:
  726. AstarManhattan.ForEachTerrainRect(mmap, X - W / 2, Y - H / 2, W, H, fillAction);
  727. break;
  728. case ZoneEditor.DecorationData.Shape.ROUND:
  729. AstarManhattan.ForEachTerrainEllipse(mmap, X - W / 2, Y - H / 2, W, H, fillAction);
  730. break;
  731. case ZoneEditor.DecorationData.Shape.STRIP:
  732. TLine2 line = new TLine2(X, Y, X, Y);
  733. MathVector.movePolarExt(ref line.p, StripDirection, -H / 2);
  734. MathVector.movePolarExt(ref line.q, StripDirection, +H / 2);
  735. AstarManhattan.ForEachTerrainStripWidth(mmap, line.p.X, line.p.Y, line.q.X, line.q.Y, W / 2, fillAction);
  736. break;
  737. }
  738. Parent.PathFinder.BeginFindPath();
  739. }
  740. public bool TouchBlock(float x, float y)
  741. {
  742. if (Blockable && Enable && Parent.PathFinder.MMap.TestBlockValue(this.BlockValue))
  743. {
  744. int bx = (((int)x) / Parent.TerrainSrc.GridCellW);
  745. int by = (((int)y) / Parent.TerrainSrc.GridCellH);
  746. var mmap = Parent.PathFinder.MMap;
  747. switch (this.mShape)
  748. {
  749. case ZoneEditor.DecorationData.Shape.RECTANGLE:
  750. return AstarManhattan.ForEachTerrainRect(mmap, X - W / 2, Y - H / 2, W, H,
  751. (cx, cy) => { return cx == bx && cy == by; });
  752. case ZoneEditor.DecorationData.Shape.ROUND:
  753. return AstarManhattan.ForEachTerrainEllipse(mmap, X - W / 2, Y - H / 2, W, H,
  754. (cx, cy) => { return cx == bx && cy == by; });
  755. case ZoneEditor.DecorationData.Shape.STRIP:
  756. TLine2 line = new TLine2(X, Y, X, Y);
  757. MathVector.movePolarExt(ref line.p, StripDirection, -H / 2);
  758. MathVector.movePolarExt(ref line.q, StripDirection, +H / 2);
  759. return AstarManhattan.ForEachTerrainStripWidth(mmap, line.p.X, line.p.Y, line.q.X, line.q.Y, W / 2,
  760. (cx, cy) => { return cx == bx && cy == by; });
  761. }
  762. }
  763. return false;
  764. }
  765. #endregion
  766. }
  767. public class ZoneArea : InstanceFlag
  768. {
  769. public override float RadiusSize { get { return this.R; } }
  770. public override float Direction { get { return 0; } }
  771. public int CurrentMapNodeValue { get; private set; }
  772. private float W;
  773. private float H;
  774. private float R;
  775. public ZoneArea(InstanceZone zone, ZoneEditor.AreaData data)
  776. : base(zone, data)
  777. {
  778. this.W = data.W;
  779. this.H = data.H;
  780. this.R = data.W / 2;
  781. }
  782. internal void BindMapBlock(AreaData data)
  783. {
  784. //var current_node = Parent.PathFinder.GetMapNodeByPos(X, Y);
  785. //if (current_node != null)
  786. {
  787. this.CurrentMapNodeValue = data.Color;
  788. var list = Parent.TerrainAreaGenerator.GetContinuousMapNode(X, Y, W / 2, H / 2);
  789. foreach (var index in list)
  790. {
  791. var node = Parent.PathFinder.GetMapNode(index.BX, index.BY) as ZoneMapNode;
  792. if (node != null)
  793. {
  794. node.SetServerArea(this);
  795. }
  796. }
  797. }
  798. }
  799. public override TVector2 getRandomPos(Random random)
  800. {
  801. return new TVector2(X, Y);
  802. }
  803. internal void do_onUnitEnter(InstanceZoneObject obj)
  804. {
  805. if (obj is InstanceUnit)
  806. {
  807. if (mOnUnitEnter != null) mOnUnitEnter.Invoke(this, obj as InstanceUnit);
  808. if (mEnterOnceList.Count > 0) mEnterOnceList.Invoke(this, obj as InstanceUnit);
  809. }
  810. }
  811. internal void do_onUnitLeave(InstanceZoneObject obj)
  812. {
  813. if (obj is InstanceUnit)
  814. {
  815. if (mOnUnitLeave != null) mOnUnitLeave.Invoke(this, obj as InstanceUnit);
  816. if (mLeaveOnceList.Count > 0) mLeaveOnceList.Invoke(this, obj as InstanceUnit);
  817. }
  818. }
  819. //----------------------------------------------------------------------------
  820. #region Delegate
  821. /// <summary>
  822. /// 某单位进入此区域
  823. /// </summary>
  824. /// <param name="area"></param>
  825. /// <param name="obj"></param>
  826. public delegate void UnitEnterHandler(ZoneArea area, InstanceUnit obj);
  827. /// <summary>
  828. /// 某单位离开此区域
  829. /// </summary>
  830. /// <param name="area"></param>
  831. /// <param name="obj"></param>
  832. public delegate void UnitLeaveHandler(ZoneArea area, InstanceUnit obj);
  833. private UnitEnterHandler mOnUnitEnter;
  834. private UnitLeaveHandler mOnUnitLeave;
  835. public event UnitEnterHandler OnUnitEnter { add { mOnUnitEnter += value; } remove { mOnUnitEnter -= value; } }
  836. public event UnitLeaveHandler OnUnitLeave { add { mOnUnitLeave += value; } remove { mOnUnitLeave -= value; } }
  837. //----------------------------------------------------------------------------
  838. private struct OnceEvent : IOnceInvoke
  839. {
  840. public InstanceUnit unit { get; private set; }
  841. public bool IsDone { get { return done || !unit.Enable; } }
  842. private UnitEnterHandler enter_handler;
  843. private UnitLeaveHandler leave_handler;
  844. private bool done;
  845. public OnceEvent(InstanceUnit unit, UnitEnterHandler enter, UnitLeaveHandler leave)
  846. {
  847. this.unit = unit;
  848. this.enter_handler = enter;
  849. this.leave_handler = leave;
  850. this.done = false;
  851. }
  852. public void Invoke(ZoneArea area)
  853. {
  854. if (enter_handler != null) this.enter_handler.Invoke(area, unit);
  855. if (leave_handler != null) this.leave_handler.Invoke(area, unit);
  856. this.enter_handler = null;
  857. this.leave_handler = null;
  858. this.done = true;
  859. }
  860. }
  861. private class OnceInvokeList : OnceInvokeList<OnceEvent>
  862. {
  863. public void Invoke(ZoneArea area, InstanceUnit unit)
  864. {
  865. base.Invoke((e) =>
  866. {
  867. if (e.unit == unit)
  868. {
  869. e.Invoke(area);
  870. }
  871. });
  872. }
  873. }
  874. private OnceInvokeList mEnterOnceList = new OnceInvokeList();
  875. private OnceInvokeList mLeaveOnceList = new OnceInvokeList();
  876. /// <summary>
  877. /// 监听单位进入一次,触发器只触发一次
  878. /// </summary>
  879. /// <param name="unit"></param>
  880. /// <param name="handler"></param>
  881. public void ListenUnitEnterOnce(InstanceUnit unit, UnitEnterHandler handler)
  882. {
  883. mEnterOnceList.Add(new OnceEvent(unit, handler, null));
  884. }
  885. /// <summary>
  886. /// 监听单位离开一次,触发器只触发一次
  887. /// </summary>
  888. /// <param name="unit"></param>
  889. /// <param name="handler"></param>
  890. public void ListenUnitLeaveOnce(InstanceUnit unit, UnitLeaveHandler handler)
  891. {
  892. mLeaveOnceList.Add(new OnceEvent(unit, null, handler));
  893. }
  894. //----------------------------------------------------------------------------
  895. protected override void clearEvents()
  896. {
  897. base.clearEvents();
  898. mEnterOnceList.Clear();
  899. mLeaveOnceList.Clear();
  900. mOnUnitEnter = null;
  901. mOnUnitLeave = null;
  902. }
  903. #endregion
  904. //----------------------------------------------------------------------------
  905. }
  906. }