MazeAgentController.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEngine;
  2. namespace MazeGen
  3. {
  4. /// <summary>
  5. /// Scene-facing wrapper around a MazeAgent: positions a GameObject at the agent's
  6. /// current room and drives exit traversal (generating the next room lazily if needed).
  7. /// </summary>
  8. public class MazeAgentController : MonoBehaviour
  9. {
  10. private const float BaseScale = 0.5f;
  11. private const float FollowedScale = 0.7f;
  12. private const float DeadScale = 0.6f;
  13. private MazeGenerator _generator;
  14. private SpriteRenderer _sprite;
  15. private Color _liveColor = Color.white;
  16. public MazeAgent Agent { get; private set; }
  17. public void Initialize(MazeGenerator generator)
  18. {
  19. _generator = generator;
  20. }
  21. public void Spawn(Room startRoom, Color color)
  22. {
  23. Agent = new MazeAgent(gameObject, startRoom);
  24. transform.position = startRoom.Position;
  25. transform.localScale = Vector3.one * BaseScale;
  26. Agent.RoomEntered += room => transform.position = room.Position;
  27. Agent.Died += OnDied;
  28. _sprite = gameObject.AddComponent<SpriteRenderer>();
  29. _sprite.sprite = RoomView.SharedSquareSprite;
  30. _sprite.color = color;
  31. _sprite.sortingOrder = 3;
  32. _liveColor = color;
  33. }
  34. /// <summary>Visually calls out this agent as the one the camera is currently following.</summary>
  35. public void SetFollowed(bool followed)
  36. {
  37. float scale = followed ? FollowedScale : BaseScale;
  38. transform.localScale = Vector3.one * (Agent != null && Agent.IsDead ? scale * DeadScale : scale);
  39. if (_sprite != null) _sprite.sortingOrder = followed ? 5 : 3;
  40. }
  41. /// <summary>Dead agents stay on the map as dim, shrunken markers rather than vanishing.</summary>
  42. private void OnDied()
  43. {
  44. transform.localScale *= DeadScale;
  45. if (_sprite != null)
  46. _sprite.color = new Color(_liveColor.r * 0.35f, _liveColor.g * 0.35f, _liveColor.b * 0.35f, 0.55f);
  47. }
  48. /// <summary>
  49. /// Attempts to move the agent through the given exit. Returns false if the exit
  50. /// doesn't belong to the agent's current room, if the next room couldn't be generated
  51. /// (extremely dense packing edge case), or if the exit is blocked. Backtracking through
  52. /// an already-used exit (moving back to a previously generated room) is allowed.
  53. /// </summary>
  54. public bool TryUseExit(Exit exit)
  55. {
  56. if (Agent == null || Agent.CurrentRoom == null) return false;
  57. if (Agent.IsDead || Agent.IsEngaged) return false; // corpses and committed fighters don't walk
  58. if (exit.OwnerRoomId != Agent.CurrentRoom.Id) return false;
  59. Room nextRoom = exit.ConnectedRoomId.HasValue
  60. ? _generator.Graph.Get(exit.ConnectedRoomId.Value)
  61. : _generator.GenerateConnectedRoom(Agent.CurrentRoom, exit, Agent);
  62. if (nextRoom == null) return false; // generation failed or exit is blocked; agent picks again next tick
  63. Agent.EnterRoom(nextRoom, exit);
  64. return true;
  65. }
  66. }
  67. }