using UnityEngine;
namespace MazeGen
{
///
/// Placeholder visual for a Room: a flat tinted square sized to the room's radius.
/// Swap this out for real prefabs later without touching generation logic.
///
[RequireComponent(typeof(SpriteRenderer))]
public class RoomView : MonoBehaviour
{
private static Sprite _sharedSquareSprite;
public static Sprite SharedSquareSprite
{
get
{
if (_sharedSquareSprite == null)
{
var tex = Texture2D.whiteTexture;
_sharedSquareSprite = Sprite.Create(
tex, new Rect(0, 0, tex.width, tex.height),
new Vector2(0.5f, 0.5f), tex.width);
}
return _sharedSquareSprite;
}
}
public Room RoomData { get; private set; }
public void Initialize(Room room)
{
RoomData = room;
// Elbow-connector hallways know both doors they join - render as a stretched
// strip along that path instead of a small square, so the corridor actually
// looks like it spans the gap between the two rooms it connects.
if (room.Kind == RoomKind.Hallway && room.CorridorWaypoints != null && room.CorridorWaypoints.Length >= 2)
{
GetComponent().enabled = false;
Color color = ColorFor(room.Kind);
for (int i = 0; i < room.CorridorWaypoints.Length - 1; i++)
SpawnCorridorSegment(room.CorridorWaypoints[i], room.CorridorWaypoints[i + 1], room.Radius, color);
return;
}
transform.position = room.Position;
transform.localScale = Vector3.one * (room.Radius * 2f);
var sr = GetComponent();
sr.sprite = SharedSquareSprite;
sr.color = ColorFor(room.Kind);
}
private void SpawnCorridorSegment(Vector2 a, Vector2 b, float radius, Color color)
{
Vector2 mid = (a + b) * 0.5f;
float length = Vector2.Distance(a, b);
if (length < 0.01f) return;
float angle = Mathf.Atan2(b.y - a.y, b.x - a.x) * Mathf.Rad2Deg;
var segmentGO = new GameObject("CorridorSegment");
segmentGO.transform.SetParent(transform, false);
segmentGO.transform.position = new Vector3(mid.x, mid.y, transform.position.z);
segmentGO.transform.rotation = Quaternion.Euler(0f, 0f, angle);
segmentGO.transform.localScale = new Vector3(length, radius * 2f, 1f);
var sr = segmentGO.AddComponent();
sr.sprite = SharedSquareSprite;
sr.color = color;
}
private static Color ColorFor(RoomKind kind)
{
switch (kind)
{
case RoomKind.Start: return new Color(0.2f, 0.6f, 1f);
case RoomKind.Exit: return new Color(1f, 0.3f, 0.3f);
case RoomKind.Hallway: return new Color(0.6f, 0.6f, 0.6f);
default: return new Color(0.3f, 0.8f, 0.4f);
}
}
}
}