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 MazeGenerator _generator;
public MazeAgent Agent { get; private set; }
public void Initialize(MazeGenerator generator)
{
_generator = generator;
}
public void Spawn(Room startRoom)
{
Agent = new MazeAgent(gameObject.name, startRoom);
transform.position = startRoom.Position;
transform.localScale = Vector3.one * 0.5f;
Agent.RoomEntered += room => transform.position = room.Position;
var sr = gameObject.AddComponent();
sr.sprite = RoomView.SharedSquareSprite;
sr.color = Color.white;
sr.sortingOrder = 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;
}
}
}