| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 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 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<SpriteRenderer>();
- _sprite.sprite = RoomView.SharedSquareSprite;
- _sprite.color = color;
- _sprite.sortingOrder = 3;
- }
- /// <summary>Visually calls out this agent as the one the camera is currently following.</summary>
- public void SetFollowed(bool followed)
- {
- transform.localScale = Vector3.one * (followed ? FollowedScale : BaseScale);
- if (_sprite != null) _sprite.sortingOrder = followed ? 5 : 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;
- }
- }
- }
|