ExitMarker.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. Colored relative to the agent's current room so the
  7. /// doors that actually matter right now stand out among a cluster of nearby rooms:
  8. /// green = usable from here, not yet leading anywhere explored; blue = usable from here
  9. /// but leads to an already-visited room; transparent gray = not on the current room at
  10. /// all (regardless of its own used/unused state). Click detection is driven externally
  11. /// (see MazeSceneBuilder) via a raycast, not the legacy OnMouseDown message - that
  12. /// message silently does nothing when the project's Active Input Handling is set to
  13. /// "Input System Package (New)" only.
  14. /// </summary>
  15. [RequireComponent(typeof(SpriteRenderer), typeof(CircleCollider2D))]
  16. public class ExitMarker : MonoBehaviour
  17. {
  18. private static readonly Color UnexploredColor = Color.green;
  19. private static readonly Color ExploredColor = new Color(0.25f, 0.55f, 1f);
  20. private static readonly Color OtherRoomColor = new Color(0.5f, 0.5f, 0.5f, 0.6f);
  21. public Exit ExitData { get; private set; }
  22. public void Initialize(Room room, Exit exit)
  23. {
  24. ExitData = exit;
  25. transform.position = (Vector2)room.Position + exit.Direction * (room.Radius + 0.35f);
  26. transform.localScale = Vector3.one * 0.3f;
  27. var sr = GetComponent<SpriteRenderer>();
  28. sr.sprite = RoomView.SharedSquareSprite;
  29. sr.sortingOrder = 1;
  30. var col = GetComponent<CircleCollider2D>();
  31. col.radius = 0.5f;
  32. col.isTrigger = true;
  33. UpdateVisual(null, null);
  34. }
  35. /// <summary>
  36. /// Recolors this marker relative to whichever room the agent is currently standing
  37. /// in. `currentRoom`/`agent` may be null before the agent has spawned yet.
  38. /// </summary>
  39. public void UpdateVisual(Room currentRoom, MazeAgent agent)
  40. {
  41. var sr = GetComponent<SpriteRenderer>();
  42. bool isOnCurrentRoom = currentRoom != null && ExitData.OwnerRoomId == currentRoom.Id;
  43. if (!isOnCurrentRoom)
  44. {
  45. sr.color = OtherRoomColor;
  46. return;
  47. }
  48. bool leadsToExploredRoom = ExitData.ConnectedRoomId.HasValue
  49. && agent != null && agent.HasVisited(ExitData.ConnectedRoomId.Value);
  50. sr.color = leadsToExploredRoom ? ExploredColor : UnexploredColor;
  51. }
  52. }
  53. }