/Assets/Thirdparty/Gamelogic/Grids/Plugins/Scripts/GridTypes/FlatHex/PolarFlatBrickMap.cs

https://bitbucket.org/tomtominc/rainbogeddon · C# · 167 lines · 98 code · 32 blank · 37 comment · 1 complexity · a075b552925e21ef6fe722c9a5f06fdb MD5 · raw file

  1. //----------------------------------------------//
  2. // Gamelogic Grids //
  3. // http://www.gamelogic.co.za //
  4. // Copyright (c) 2013 Gamelogic (Pty) Ltd //
  5. //----------------------------------------------//
  6. using Gamelogic.Extensions.Internal;
  7. using UnityEngine;
  8. namespace Gamelogic.Grids
  9. {
  10. /// <summary>
  11. /// This map can be used with a horizontally wrapped FlatHexGrid.
  12. /// For now, alignment does not work as with the other maps.
  13. /// </summary>
  14. /// <example>
  15. /// <code>
  16. /// IGrid&lt;FlatHexPoint&gt; grid;
  17. /// IMap3D map;
  18. ///
  19. /// private void BuildGrid()
  20. /// {
  21. /// grid = FlatHexGrid&lt;TCell&gt;.HorizontallyWrappedParallelogram(width, height);
  22. /// map = new PolarFlatBrickMap(Vector3.zero, 10, 110, newRectPoint(5, 10).To3DXY();
  23. ///
  24. /// foreach (var point in grid)
  25. /// {
  26. /// var cell = Instantiate(cellPrefab);
  27. /// cell.transform.localPosition = map[point];
  28. /// }
  29. /// }
  30. ///
  31. /// public void Click(Vector3 worldPoint)
  32. /// {
  33. /// var gridPoint = map[worldPoint]
  34. ///
  35. /// if (grid.Contains(gridPoint))
  36. /// {
  37. /// ClickCell(grid[gridPoint]);
  38. /// }
  39. /// }
  40. /// </code>
  41. /// </example>
  42. [Version(1,7)]
  43. [Experimental]
  44. public class PolarFlatBrickMap : AbstractMap<FlatHexPoint>, IPolarMap<FlatHexPoint>
  45. {
  46. #region Fields
  47. private readonly float sectorAngleRad;
  48. #endregion
  49. #region Properties
  50. public float SectorAngle
  51. {
  52. get;
  53. private set;
  54. }
  55. public Vector2 Center
  56. {
  57. get;
  58. private set;
  59. }
  60. public VectorPoint SectorsAndBands
  61. {
  62. get;
  63. private set;
  64. }
  65. public float InnerRadius
  66. {
  67. get;
  68. private set;
  69. }
  70. public float OuterRadius
  71. {
  72. get;
  73. private set;
  74. }
  75. #endregion
  76. #region Construction
  77. public PolarFlatBrickMap(Vector2 center, float innerRadius, float outerRadius, VectorPoint sectorsAndBands)
  78. : base(Vector2.one)
  79. {
  80. Center = center;
  81. InnerRadius = innerRadius;
  82. OuterRadius = outerRadius;
  83. SectorsAndBands = sectorsAndBands;
  84. sectorAngleRad = (2f*Mathf.PI)/SectorsAndBands.X;
  85. SectorAngle = 360f/SectorsAndBands.X;
  86. }
  87. #endregion
  88. #region AbstractMap Implementation
  89. public override FlatHexPoint RawWorldToGrid(Vector2 worldPoint)
  90. {
  91. float angleRad = Mathf.Atan2(worldPoint.y - Center.y, worldPoint.x - Center.x);
  92. if (angleRad < 0)
  93. {
  94. angleRad += 2*Mathf.PI;
  95. }
  96. int m = Mathf.FloorToInt(angleRad/sectorAngleRad);
  97. float radius = (new Vector2(worldPoint.x - Center.x, worldPoint.y - Center.y)).magnitude;
  98. int n = Mathf.FloorToInt((radius - InnerRadius)/(OuterRadius - InnerRadius)*SectorsAndBands.Y - (m/2f));
  99. return new FlatHexPoint(m, n);
  100. }
  101. public override Vector2 GridToWorld(FlatHexPoint gridPoint)
  102. {
  103. float m = gridPoint.X;
  104. float n = gridPoint.Y;
  105. float angleRad = (m/SectorsAndBands.X)*2f*Mathf.PI + (Mathf.PI/SectorsAndBands.X);
  106. float radius = ((n + m/2)/SectorsAndBands.Y)*(OuterRadius - InnerRadius) + InnerRadius +
  107. (OuterRadius - InnerRadius)/(2f*SectorsAndBands.Y);
  108. float x = radius*Mathf.Cos(angleRad) + Center.x;
  109. float y = radius*Mathf.Sin(angleRad) + Center.y;
  110. return new Vector2(x, y);
  111. }
  112. #endregion
  113. #region Interface
  114. public float GetStartAngleZ(FlatHexPoint gridPoint)
  115. {
  116. float m = gridPoint.X;
  117. float angle = m*SectorAngle;
  118. return angle;
  119. }
  120. public float GetEndAngleZ(FlatHexPoint gridPoint)
  121. {
  122. float angle = GetStartAngleZ(gridPoint) + SectorAngle;
  123. return angle;
  124. }
  125. public float GetInnerRadius(FlatHexPoint gridPoint)
  126. {
  127. return ((gridPoint.Y + gridPoint.X/2f)/SectorsAndBands.Y)*(OuterRadius - InnerRadius) + InnerRadius;
  128. }
  129. public float GetOuterRadius(FlatHexPoint gridPoint)
  130. {
  131. return ((gridPoint.Y + gridPoint.X/2f + 1)/SectorsAndBands.Y)*(OuterRadius - InnerRadius) + InnerRadius;
  132. }
  133. #endregion
  134. }
  135. }