BoardLayout.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using UnityEngine;
  2. namespace TowerDevil
  3. {
  4. // Coordinates are measured from the back edge of the courtyard; uGUI Y increases upwards.
  5. internal static class BoardLayout
  6. {
  7. internal const float Width = 750, FrontCell = 116;
  8. const int Count = Run.Size;
  9. const float BackCell = 107, BackDepth = 91, FrontDepth = 98;
  10. const float DepthStep = (FrontDepth - BackDepth) / (Count - 1);
  11. internal static float Size(float row) => Mathf.Lerp(BackCell, FrontCell, row / (Count - 1));
  12. internal static float Depth(int row) => BackDepth + DepthStep * row;
  13. internal static float Top(int row) => row * BackDepth + DepthStep * row * (row - 1) * .5f;
  14. internal static float Height => Top(Count);
  15. internal static Vector2 Center(float row, float col)
  16. {
  17. int band = Mathf.Clamp(Mathf.FloorToInt(row + .5f), 0, Count - 1);
  18. float y = Top(band) + (row - band + .5f) * Depth(band);
  19. return new Vector2(Width * .5f + (col - (Count - 1) * .5f) * Size(row), -y);
  20. }
  21. internal static bool TryCell(Vector2 point, out int row, out int col)
  22. {
  23. col = -1;
  24. float depth = -point.y;
  25. for (row = 0; row < Count; row++)
  26. {
  27. if (depth < Top(row) || depth >= Top(row + 1)) continue;
  28. float size = Size(row);
  29. col = Mathf.FloorToInt((point.x - Width * .5f) / size + Count * .5f);
  30. return col >= 0 && col < Count;
  31. }
  32. return false;
  33. }
  34. }
  35. }