MazeFogOfWar.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. /// <summary>
  4. /// Manages fog of war for the maze - only shows explored areas
  5. /// AI entities can only see areas they've visited or are within their vision range
  6. /// </summary>
  7. public class MazeFogOfWar : MonoBehaviour
  8. {
  9. [Header("Fog Settings")]
  10. [SerializeField] private MazeController mazeController;
  11. [Header("Rendering")]
  12. [SerializeField] private MeshMazeRenderer meshRenderer;
  13. [SerializeField] private Color fogColor = new Color(0.1f, 0.1f, 0.1f, 0.8f);
  14. [SerializeField] private Material fogMaterial;
  15. private HashSet<Vector2Int> exploredTiles = new();
  16. private Dictionary<GameObject, HashSet<Vector2Int>> entityVisionRanges = new();
  17. void Start()
  18. {
  19. if (fogMaterial == null)
  20. {
  21. CreateFogMaterial();
  22. }
  23. if (meshRenderer == null)
  24. {
  25. meshRenderer = GetComponent<MeshMazeRenderer>() ?? FindAnyObjectByType<MeshMazeRenderer>();
  26. }
  27. }
  28. /// <summary>
  29. /// Marks a tile as explored
  30. /// </summary>
  31. public void ExploreTile(Vector2Int tilePos)
  32. {
  33. exploredTiles.Add(tilePos);
  34. UpdateRendering();
  35. }
  36. /// <summary>
  37. /// Marks multiple tiles as explored
  38. /// </summary>
  39. public void ExploreTiles(IEnumerable<Vector2Int> tiles)
  40. {
  41. foreach (var tile in tiles)
  42. {
  43. exploredTiles.Add(tile);
  44. }
  45. UpdateRendering();
  46. }
  47. /// <summary>
  48. /// Updates vision range for an entity
  49. /// </summary>
  50. public void UpdateEntityVision(GameObject entity, Vector2Int position, float range)
  51. {
  52. HashSet<Vector2Int> visibleTiles = GetVisibleTiles(position, range);
  53. if (!entityVisionRanges.ContainsKey(entity))
  54. {
  55. entityVisionRanges[entity] = new HashSet<Vector2Int>();
  56. }
  57. // Remove old vision
  58. foreach (var tile in entityVisionRanges[entity])
  59. {
  60. if (!exploredTiles.Contains(tile) && !IsTileVisibleByOtherEntities(tile))
  61. {
  62. // Tile should be hidden
  63. }
  64. }
  65. // Add new vision
  66. entityVisionRanges[entity] = visibleTiles;
  67. foreach (var tile in visibleTiles)
  68. {
  69. exploredTiles.Add(tile);
  70. }
  71. UpdateRendering();
  72. }
  73. /// <summary>
  74. /// Gets all tiles visible from a position within range
  75. /// </summary>
  76. private HashSet<Vector2Int> GetVisibleTiles(Vector2Int center, float range)
  77. {
  78. HashSet<Vector2Int> visible = new();
  79. var maze = mazeController.GetCurrentMaze();
  80. if (maze == null) return visible;
  81. int rangeInt = Mathf.CeilToInt(range);
  82. for (int x = center.x - rangeInt; x <= center.x + rangeInt; x++)
  83. {
  84. for (int y = center.y - rangeInt; y <= center.y + rangeInt; y++)
  85. {
  86. Vector2Int tilePos = new Vector2Int(x, y);
  87. if (Vector2Int.Distance(center, tilePos) <= range)
  88. {
  89. if (maze.IsInBounds(x, y) && HasLineOfSight(center, tilePos))
  90. {
  91. visible.Add(tilePos);
  92. }
  93. }
  94. }
  95. }
  96. return visible;
  97. }
  98. /// <summary>
  99. /// Checks if there's line of sight between two points
  100. /// </summary>
  101. private bool HasLineOfSight(Vector2Int from, Vector2Int to)
  102. {
  103. var maze = mazeController.GetCurrentMaze();
  104. if (maze == null) return false;
  105. // Simple line of sight check - can be enhanced with proper raycasting
  106. Vector2Int delta = to - from;
  107. int steps = Mathf.Max(Mathf.Abs(delta.x), Mathf.Abs(delta.y));
  108. for (int i = 1; i < steps; i++)
  109. {
  110. float t = (float)i / steps;
  111. Vector2Int checkPos = new Vector2Int(
  112. Mathf.RoundToInt(from.x + delta.x * t),
  113. Mathf.RoundToInt(from.y + delta.y * t)
  114. );
  115. if (!maze.IsWalkable(checkPos.x, checkPos.y))
  116. {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. /// <summary>
  123. /// Checks if a tile is visible by any other entity
  124. /// </summary>
  125. private bool IsTileVisibleByOtherEntities(Vector2Int tile)
  126. {
  127. foreach (var vision in entityVisionRanges.Values)
  128. {
  129. if (vision.Contains(tile))
  130. {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. /// <summary>
  137. /// Updates the rendering to show only explored areas
  138. /// </summary>
  139. private void UpdateRendering()
  140. {
  141. if (meshRenderer != null)
  142. {
  143. meshRenderer.RefreshFogOfWar();
  144. }
  145. }
  146. /// <summary>
  147. /// Creates a default fog material
  148. /// </summary>
  149. private void CreateFogMaterial()
  150. {
  151. fogMaterial = new Material(Shader.Find("Sprites/Default"));
  152. fogMaterial.color = fogColor;
  153. }
  154. /// <summary>
  155. /// Checks if a tile is explored
  156. /// </summary>
  157. public bool IsTileExplored(Vector2Int tile)
  158. {
  159. return exploredTiles.Contains(tile);
  160. }
  161. /// <summary>
  162. /// Gets all explored tiles
  163. /// </summary>
  164. public HashSet<Vector2Int> GetExploredTiles()
  165. {
  166. return new HashSet<Vector2Int>(exploredTiles);
  167. }
  168. /// <summary>
  169. /// Removes an entity from vision tracking
  170. /// </summary>
  171. public void RemoveEntity(GameObject entity)
  172. {
  173. entityVisionRanges.Remove(entity);
  174. UpdateRendering();
  175. }
  176. }