ExitMarker.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. namespace MazeGen
  3. {
  4. /// <summary>
  5. /// Placeholder marker for a single Exit, positioned just outside its owning room's
  6. /// edge in the exit's direction. Purely informational now that agents move
  7. /// autonomously - colored relative to whichever agent the camera is currently
  8. /// following, so the doors that actually matter stand out among a cluster of nearby
  9. /// rooms: green = usable from the followed agent's room, not yet leading anywhere
  10. /// explored; blue = usable from there but leads to an already-visited room; transparent
  11. /// gray = not on the followed room at all (or no agent is being followed).
  12. /// </summary>
  13. [RequireComponent(typeof(SpriteRenderer), typeof(CircleCollider2D))]
  14. public class ExitMarker : MonoBehaviour
  15. {
  16. private static readonly Color UnexploredColor = Color.green;
  17. private static readonly Color ExploredColor = new Color(0.25f, 0.55f, 1f);
  18. private static readonly Color OtherRoomColor = new Color(0.5f, 0.5f, 0.5f, 0.6f);
  19. public Exit ExitData { get; private set; }
  20. public void Initialize(Room room, Exit exit)
  21. {
  22. ExitData = exit;
  23. transform.position = (Vector2)room.Position + exit.Direction * (room.Radius + 0.35f);
  24. transform.localScale = Vector3.one * 0.3f;
  25. var sr = GetComponent<SpriteRenderer>();
  26. sr.sprite = RoomView.SharedSquareSprite;
  27. sr.sortingOrder = 1;
  28. var col = GetComponent<CircleCollider2D>();
  29. col.radius = 0.5f;
  30. col.isTrigger = true;
  31. UpdateVisual(null, null);
  32. }
  33. /// <summary>
  34. /// Recolors this marker relative to whichever room the agent is currently standing
  35. /// in. `currentRoom`/`agent` may be null before the agent has spawned yet.
  36. /// </summary>
  37. public void UpdateVisual(Room currentRoom, MazeAgent agent)
  38. {
  39. var sr = GetComponent<SpriteRenderer>();
  40. bool isOnCurrentRoom = currentRoom != null && ExitData.OwnerRoomId == currentRoom.Id;
  41. if (!isOnCurrentRoom)
  42. {
  43. sr.color = OtherRoomColor;
  44. return;
  45. }
  46. bool leadsToExploredRoom = ExitData.ConnectedRoomId.HasValue
  47. && agent != null && agent.HasVisited(ExitData.ConnectedRoomId.Value);
  48. sr.color = leadsToExploredRoom ? ExploredColor : UnexploredColor;
  49. }
  50. }
  51. }