using UnityEngine; namespace MazeGen { /// /// Scene-facing wrapper around a MazeAgent: positions a GameObject at the agent's /// current room and drives exit traversal (generating the next room lazily if needed). /// public class MazeAgentController : MonoBehaviour { private const float BaseScale = 0.5f; private const float FollowedScale = 0.7f; private const float DeadScale = 0.6f; private MazeGenerator _generator; private SpriteRenderer _sprite; private Color _liveColor = Color.white; public MazeAgent Agent { get; private set; } public void Initialize(MazeGenerator generator) { _generator = generator; } public void Spawn(Room startRoom, Color color) { Agent = new MazeAgent(gameObject, startRoom); transform.position = startRoom.Position; transform.localScale = Vector3.one * BaseScale; Agent.RoomEntered += room => transform.position = room.Position; Agent.Died += OnDied; _sprite = gameObject.AddComponent(); _sprite.sprite = RoomView.SharedSquareSprite; _sprite.color = color; _sprite.sortingOrder = 3; _liveColor = color; } /// Visually calls out this agent as the one the camera is currently following. public void SetFollowed(bool followed) { float scale = followed ? FollowedScale : BaseScale; transform.localScale = Vector3.one * (Agent != null && Agent.IsDead ? scale * DeadScale : scale); if (_sprite != null) _sprite.sortingOrder = followed ? 5 : 3; } /// Dead agents stay on the map as dim, shrunken markers rather than vanishing. private void OnDied() { transform.localScale *= DeadScale; if (_sprite != null) _sprite.color = new Color(_liveColor.r * 0.35f, _liveColor.g * 0.35f, _liveColor.b * 0.35f, 0.55f); } /// /// Attempts to move the agent through the given exit. Returns false if the exit /// doesn't belong to the agent's current room, if the next room couldn't be generated /// (extremely dense packing edge case), or if the exit is blocked. Backtracking through /// an already-used exit (moving back to a previously generated room) is allowed. /// public bool TryUseExit(Exit exit) { if (Agent == null || Agent.CurrentRoom == null) return false; if (Agent.IsDead || Agent.IsEngaged) return false; // corpses and committed fighters don't walk if (exit.OwnerRoomId != Agent.CurrentRoom.Id) return false; Room nextRoom = exit.ConnectedRoomId.HasValue ? _generator.Graph.Get(exit.ConnectedRoomId.Value) : _generator.GenerateConnectedRoom(Agent.CurrentRoom, exit, Agent); if (nextRoom == null) return false; // generation failed or exit is blocked; agent picks again next tick Agent.EnterRoom(nextRoom, exit); return true; } } }