Advertisement
Guest User

Untitled

a guest
Mar 24th, 2025
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | Gaming | 0 0
  1. using UnityEngine;
  2.  
  3.  
  4. public class GridManager : MonoBehaviour
  5. {
  6. public static GridManager Instance; // Singleton for easy access
  7. public int width = 10, height = 10; // Define grid size
  8. public float cellSize = 1.0f; // Size of each grid cell
  9. private Transform[,] gridArray; // 2D array for grid storage
  10. public Vector2 gridOrigin = Vector2.zero; // Adjust this in Unity Inspector
  11. private void Awake()
  12. {
  13. if (Instance == null)
  14. Instance = this;
  15. else
  16. Destroy(gameObject);
  17.  
  18. gridArray = new Transform[width, height]; // Initialize grid array
  19. Debug.Log("GridManager Loaded. Grid Size: " + width + "x" + height);
  20. }
  21.  
  22. void OnDrawGizmos()
  23. {
  24. Gizmos.color = Color.green; // Grid color
  25.  
  26. for (int x = 0; x < width; x++)
  27. {
  28. for (int y = 0; y < height; y++)
  29. {
  30. Vector2 cellPosition = new Vector2(x * cellSize, y * cellSize) + gridOrigin;
  31. Gizmos.DrawWireCube(cellPosition, Vector2.one * (cellSize * 0.9f));
  32. }
  33. }
  34. }
  35.  
  36. // Converts world position to the nearest grid cell position
  37. public Vector2 SnapToGrid(Vector2 worldPosition)
  38. {
  39. Debug.Log($"World Position: {worldPosition}");
  40.  
  41. float relativeX = worldPosition.x - gridOrigin.x;
  42. float relativeY = worldPosition.y - gridOrigin.y;
  43. Debug.Log($"Relative Position (after subtracting origin): {new Vector2(relativeX, relativeY)}");
  44.  
  45. int x = Mathf.RoundToInt(relativeX / cellSize);
  46. int y = Mathf.RoundToInt(relativeY / cellSize);
  47. Debug.Log($"Snapped Grid Coordinates: ({x}, {y})");
  48.  
  49. Vector2 snappedPosition = new Vector2(x * cellSize + gridOrigin.x, y * cellSize + gridOrigin.y);
  50. Debug.Log($"Final Snapped Position: {snappedPosition}");
  51.  
  52. return snappedPosition;
  53. }
  54.  
  55.  
  56.  
  57. // Check if a grid cell is empty
  58. public bool IsCellEmpty(Vector2 worldPosition)
  59. {
  60. int x = Mathf.RoundToInt(worldPosition.x / cellSize);
  61. int y = Mathf.RoundToInt(worldPosition.y / cellSize);
  62.  
  63. if (x >= 0 && x < width && y >= 0 && y < height)
  64. return gridArray[x, y] == null; // True if empty
  65.  
  66. return false; // Out of bounds
  67. }
  68.  
  69. // Place block on the grid and log its position
  70. public void PlaceBlock(Vector2 worldPosition, Transform block)
  71. {
  72. int x = Mathf.RoundToInt(worldPosition.x / cellSize);
  73. int y = Mathf.RoundToInt(worldPosition.y / cellSize);
  74.  
  75. if (x < 0 || x >= width || y < 0 || y >= height)
  76. {
  77. Debug.LogWarning($"Out of Bounds! Tried to place block at ({x}, {y})");
  78. return;
  79. }
  80.  
  81. if (gridArray[x, y] == null)
  82. {
  83. gridArray[x, y] = block;
  84. Debug.Log($"✅ Block locked at Grid Position ({x}, {y})");
  85. }
  86. else
  87. {
  88. Debug.LogWarning($"⚠️ Cell ({x}, {y}) is already occupied!");
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement