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 MazeGenerator _generator;
private SpriteRenderer _sprite;
public MazeAgent Agent { get; private set; }
public void Initialize(MazeGenerator generator)
{
_generator = generator;
}
public void Spawn(Room startRoom, Color color)
{
Agent = new MazeAgent(gameObject.name, startRoom);
transform.position = startRoom.Position;
transform.localScale = Vector3.one * BaseScale;
Agent.RoomEntered += room => transform.position = room.Position;
_sprite = gameObject.AddComponent();
_sprite.sprite = RoomView.SharedSquareSprite;
_sprite.color = color;
_sprite.sortingOrder = 3;
}
/// Visually calls out this agent as the one the camera is currently following.
public void SetFollowed(bool followed)
{
transform.localScale = Vector3.one * (followed ? FollowedScale : BaseScale);
if (_sprite != null) _sprite.sortingOrder = followed ? 5 : 3;
}
///
/// Attempts to move the agent through the given exit. Returns false if the exit
/// doesn't belong to the agent's current room. 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 (exit.OwnerRoomId != Agent.CurrentRoom.Id) return false;
Room nextRoom = exit.ConnectedRoomId.HasValue
? _generator.Graph.Get(exit.ConnectedRoomId.Value)
: _generator.GenerateConnectedRoom(Agent.CurrentRoom, exit, Agent);
Agent.EnterRoom(nextRoom, exit);
return true;
}
}
}