BoardLayout.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. namespace TowerDevil
  3. {
  4. // Coordinates are local to the board's top edge, with +Y pointing up in uGUI.
  5. internal static class BoardLayout
  6. {
  7. internal const float Width = 750, FrontCell = 106, Gap = 14;
  8. const int Count = Run.Size;
  9. const float BackCell = FrontCell * .88f;
  10. const float Step = (FrontCell - BackCell) / (Count - 1);
  11. internal static float Size(float row) => BackCell + Step * Mathf.Clamp(row, 0, Count - 1);
  12. internal static float Top(int row) => row * (BackCell + Gap) + Step * row * (row - 1) * .5f;
  13. internal static float Height => Top(Count - 1) + FrontCell;
  14. internal static Vector2 Center(float row, float col)
  15. {
  16. // Project the logical half-row boundaries to the midpoint of each gutter.
  17. int band = Mathf.Clamp(Mathf.FloorToInt(row + .5f), 0, Count - 1);
  18. float y = Top(band) + Size(band) * .5f + (row - band) * (Size(band) + Gap);
  19. if (row >= Count - .5f)
  20. y -= Gap * .5f;
  21. else if (row > Count - 1)
  22. y -= (row - Count + 1) * Gap;
  23. return new Vector2(Width * .5f + (col - (Count - 1) * .5f) * Size(row), -y);
  24. }
  25. internal static bool TryCell(Vector2 point, out int row, out int col)
  26. {
  27. col = -1;
  28. for (row = 0; row < Count; row++)
  29. {
  30. float size = Size(row), y = -point.y - Top(row);
  31. if (y < 0 || y >= size - 3)
  32. continue;
  33. float x = point.x - (Width - Count * size) * .5f;
  34. col = Mathf.FloorToInt(x / size);
  35. float within = x - col * size;
  36. return col >= 0 && col < Count && within >= 1.5f && within < size - 1.5f;
  37. }
  38. return false;
  39. }
  40. }
  41. }