TeamFormationHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using CommonAI.RTS;
  2. using CommonLang.Vector;
  3. using CommonAI.Zone.Instance;
  4. using CommonLang;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. namespace CommonAI.Zone.Helper
  10. {
  11. public class TeamFormationGroup
  12. {
  13. private readonly ZoneRegion mRegion;
  14. private readonly TeamFormation mData;
  15. public ZoneRegion Region { get { return mRegion; } }
  16. public InstanceZone Zone { get { return mRegion.Parent; } }
  17. public TeamFormation Data { get { return mData; } }
  18. public TeamFormation.Formation TFormation { get { return mData.TFormation; } }
  19. // 单位和预期坐标对应关系 //
  20. private List<TeamMember> mObjects = new List<TeamMember>();
  21. public TeamFormationGroup(TeamFormation team, ZoneRegion region)
  22. {
  23. this.mData = team;
  24. this.mRegion = region;
  25. }
  26. public void AddPos(float x, float y, float dr)
  27. {
  28. mObjects.Add(new TeamMember(x, y, dr));
  29. }
  30. /// <summary>
  31. /// 初始化,直接放置坐标位置
  32. /// </summary>
  33. public void ResetPos()
  34. {
  35. IVector2 center = mRegion.Pos;
  36. switch (mData.TFormation)
  37. {
  38. case TeamFormation.Formation.Random:
  39. ResetRandom(center);
  40. break;
  41. case TeamFormation.Formation.Square:
  42. ResetSquare(center);
  43. break;
  44. case TeamFormation.Formation.Round:
  45. ResetRound(center);
  46. break;
  47. case TeamFormation.Formation.Cycle:
  48. ResetCycle(center);
  49. break;
  50. case TeamFormation.Formation.Beehive:
  51. ResetBeehive(center);
  52. break;
  53. case TeamFormation.Formation.Line:
  54. ResetLine(center);
  55. break;
  56. default:
  57. ResetRandom(center);
  58. break;
  59. }
  60. for (int i = mObjects.Count - 1; i >= 0; i--)
  61. {
  62. var tm = mObjects[i];
  63. tm.SetToExpect(mRegion);
  64. }
  65. }
  66. public TeamMember PopPos()
  67. {
  68. if (mObjects.Count > 0)
  69. {
  70. var index = mObjects.Count - 1;
  71. var ret = mObjects[index];
  72. mObjects.RemoveAt(index);
  73. return ret;
  74. }
  75. return null;
  76. }
  77. public void Clear()
  78. {
  79. mObjects.Clear();
  80. }
  81. private void ResetRandom(IVector2 center)
  82. {
  83. VectorGroupHelper.DistributeSpacingSizeRandom(center, mObjects, mData.SpacingSize, Zone.RandomN);
  84. }
  85. private void ResetSquare(IVector2 center)
  86. {
  87. VectorGroupHelper.DistributeSpacingSizeSquare(center, mObjects, mData.SpacingSize);
  88. }
  89. private void ResetRound(IVector2 center)
  90. {
  91. VectorGroupHelper.DistributeSpacingSizeRound(center, mObjects, mData.SpacingSize);
  92. }
  93. private void ResetCycle(IVector2 center)
  94. {
  95. VectorGroupHelper.DistributeSpacingSizeCycle(center, mObjects, mData.SpacingSize);
  96. }
  97. private void ResetBeehive(IVector2 center)
  98. {
  99. VectorGroupHelper.DistributeSpacingSizeBeehive(center, mObjects, mData.SpacingSize);
  100. }
  101. private void ResetLine(IVector2 center)
  102. {
  103. VectorGroupHelper.DistributeSpacingSizeLine(center, mObjects, mData.SpacingSize);
  104. }
  105. public class TeamMember : IPositionObject, IVector2
  106. {
  107. //private InstanceZoneObject unit;
  108. private float expect_x;
  109. private float expect_y;
  110. private float expect_direction;
  111. public TeamMember(float x, float y, float dr)
  112. {
  113. //this.unit = obj;
  114. this.expect_x = x;
  115. this.expect_y = y;
  116. this.expect_direction = dr;
  117. }
  118. public float X { get { return expect_x; } set { expect_x = value; } }
  119. public float Y { get { return expect_y; } set { expect_y = value; } }
  120. public float Direction { get { return expect_direction; } set { expect_direction = value; } }
  121. public float RadiusSize { get { return 1; } }
  122. //public InstanceZoneObject Unit { get { return unit; } }
  123. internal void SetToExpect(ZoneRegion region)
  124. {
  125. region.keepInRegion(ref expect_x, ref expect_y);
  126. }
  127. public void SetX(float value)
  128. {
  129. expect_x = value;
  130. }
  131. public void SetY(float value)
  132. {
  133. expect_y = value;
  134. }
  135. public void AddX(float value)
  136. {
  137. expect_x += value;
  138. }
  139. public void AddY(float value)
  140. {
  141. expect_y += value;
  142. }
  143. }
  144. }
  145. }