| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using UnityEngine;
- namespace MazeGen
- {
- /// <summary>
- /// 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).
- /// </summary>
- 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<SpriteRenderer>();
- sr.sprite = RoomView.SharedSquareSprite;
- sr.color = Color.white;
- sr.sortingOrder = 3;
- }
- /// <summary>
- /// 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.
- /// </summary>
- 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;
- }
- }
- }
|