room Gen, doors

This commit is contained in:
2025-06-30 16:28:30 +02:00
parent b4d49703d2
commit ba83e64b8d
4 changed files with 188 additions and 162 deletions

View File

@@ -8,6 +8,10 @@ PrefabInstance:
serializedVersion: 3 serializedVersion: 3
m_TransformParent: {fileID: 0} m_TransformParent: {fileID: 0}
m_Modifications: m_Modifications:
- target: {fileID: -600122568662034771, guid: 1a5d554c0c76caf4195cae47e098b79d, type: 3}
propertyPath: prefabSize.x
value: 10
objectReference: {fileID: 0}
- target: {fileID: 3479177569595356638, guid: 1a5d554c0c76caf4195cae47e098b79d, type: 3} - target: {fileID: 3479177569595356638, guid: 1a5d554c0c76caf4195cae47e098b79d, type: 3}
propertyPath: m_LocalPosition.x propertyPath: m_LocalPosition.x
value: 20 value: 20

View File

@@ -5,6 +5,11 @@ public class DoorController : MonoBehaviour
[SerializeField] private Animator doorLAnimator; [SerializeField] private Animator doorLAnimator;
[SerializeField] private Animator doorRAnimator; [SerializeField] private Animator doorRAnimator;
public void Start()
{
//OpenDoor();
}
public void OpenDoor() public void OpenDoor()
{ {
doorLAnimator.SetTrigger("Open"); doorLAnimator.SetTrigger("Open");

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
///<summary> ///<summary>
@@ -6,11 +7,24 @@ using UnityEngine;
/// ///
public class RoomHandler : MonoBehaviour public class RoomHandler : MonoBehaviour
{ {
[Header("Doors")]
[SerializeField] private GameObject wallNorth; [SerializeField] private GameObject wallNorth;
[SerializeField] private GameObject wallSouth; [SerializeField] private GameObject wallSouth;
[SerializeField] private GameObject wallEast; [SerializeField] private GameObject wallEast;
[SerializeField] private GameObject wallWest; [SerializeField] private GameObject wallWest;
public enum Side
{
North,
South,
East,
West
}
private readonly Dictionary<Side, DoorAnimation> doors = new();
public void RegisterDoor(Side dir, DoorAnimation doorAnim) => doors[dir] = doorAnim;
/// <summary> /// <summary>
/// Creates entrances to corridors leading to other rooms /// Creates entrances to corridors leading to other rooms
/// </summary> /// </summary>
@@ -25,4 +39,21 @@ public class RoomHandler : MonoBehaviour
wallEast.SetActive(!eastOpen); wallEast.SetActive(!eastOpen);
wallWest.SetActive(!westOpen); wallWest.SetActive(!westOpen);
} }
public void ToggleAllDoors()
{
foreach (DoorAnimation door in doors.Values)
{
door.ToggleDoor();
}
}
public void SetDoorState(Side dir, bool open)
{
if (doors.TryGetValue(dir, out DoorAnimation da))
{
da.ToggleDoor();
Debug.Log("Door " + dir + " is now " + (open ? "open" : "closed"));
}
}
} }

View File

@@ -1,7 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using UnityEngine;
public class MapGenManager : MonoBehaviour public class MapGenManager : MonoBehaviour
{ {
@@ -24,12 +23,9 @@ public class MapGenManager : MonoBehaviour
[SerializeField] private int RoomDistance = 3; [SerializeField] private int RoomDistance = 3;
private readonly Dictionary<Vector2Int, GameObject> gridToRoom = new(); private readonly Dictionary<Vector2Int, GameObject> gridToRoom = new();
private readonly Vector3 roomOriginOffset = Vector3.zero;
void Start() => GenerateFromLayout(); void Start() => GenerateFromLayout();
private void GenerateFromLayout() private void GenerateFromLayout()
{ {
if (layout == null || string.IsNullOrWhiteSpace(layout.grid)) if (layout == null || string.IsNullOrWhiteSpace(layout.grid))
@@ -37,228 +33,218 @@ public class MapGenManager : MonoBehaviour
Debug.LogError("Layout asset není přiřazený nebo je prázdný!"); Debug.LogError("Layout asset není přiřazený nebo je prázdný!");
return; return;
} }
gridToRoom.Clear(); gridToRoom.Clear();
/* ----------- Create a spawn room ----------- */ /* ----------- Create a spawn room ----------- */
GameObject spawnPrefab = mapPrefab[0]; GameObject spawnPrefab = mapPrefab[0];
Vector3 cellSize = GetPrefabXZ(spawnPrefab); Vector3 cellSize = GetPrefabXZ(spawnPrefab);
/* ---------- Text layout to grid ---------- */ string[] lines = layout.grid.Split('\n')
string[] lines = layout.grid .Select(l => l.TrimEnd('\r'))
.Split('\n') .Where(l => !string.IsNullOrWhiteSpace(l))
.Select(line => line.TrimEnd('\r')) .ToArray();
.Where(line => !string.IsNullOrWhiteSpace(line)) Vector2 first = GetFirstOrLastRoom(lines);
.ToArray(); int bottomRow = (int)first.y;
int spawnX = (int)first.x;
Vector2 firstRoomPos = GetFirstOrLastRoom(lines);
int bottomRowIdx = (int)firstRoomPos.y;
int spawnGridX = (int)firstRoomPos.x;
/* ---------- Create spawn room properly ---------- */
Vector3 spawnPos = roomOriginOffset + new Vector3(
spawnGridX * (cellSize.x + RoomDistance), // X
0,
0);
Vector3 spawnPos = new Vector3(spawnX * (cellSize.x + RoomDistance), 0, 0);
GameObject spawnRoom = Instantiate(spawnPrefab, spawnPos, Quaternion.identity, transform); GameObject spawnRoom = Instantiate(spawnPrefab, spawnPos, Quaternion.identity, transform);
gridToRoom[new Vector2Int(spawnGridX, 0)] = spawnRoom; gridToRoom[new Vector2Int(spawnX, 0)] = spawnRoom;
/* ---------- Instantiate player ---------- */
/* ----------- Spawn the player ----------- */
if (Player) if (Player)
{ {
Vector3 playerPos = spawnPos + new Vector3(0, 1, 3); GameObject p = Instantiate(Player, spawnPos + new Vector3(0, 1, 3), Quaternion.identity, transform);
GameObject playerInstance = Instantiate(Player, playerPos, Quaternion.identity, transform); Transform inner = p.transform.Find("Player");
Transform controllerObj = playerInstance.transform.Find("Player"); if (inner) inner.tag = "Player";
if (controllerObj != null)
{
controllerObj.tag = "Player";
}
}
else
{
Debug.LogWarning("Player prefab není přiřazený, hráč nebude umístěn do mapy.");
} }
/* ---------- Build the rest of rooms ---------- */ /* ----------- Build rooms ----------- */
BuildRooms(lines, bottomRow, cellSize);
BuildRooms(lines, bottomRowIdx, cellSize); /* ----------- Set entrances ----------- */
foreach (var kv in gridToRoom)
/* ---------- Open walls based on aproximity to other rooms ---------- */
foreach (var keyValuePair in gridToRoom)
{ {
Vector2Int g = keyValuePair.Key; Vector2Int g = kv.Key;
RoomHandler rh = keyValuePair.Value.GetComponent<RoomHandler>(); RoomHandler rh = kv.Value.GetComponent<RoomHandler>();
bool north = gridToRoom.ContainsKey(g + Vector2Int.left); bool north = gridToRoom.ContainsKey(g + Vector2Int.left);
bool south = gridToRoom.ContainsKey(g + Vector2Int.right); bool south = gridToRoom.ContainsKey(g + Vector2Int.right);
bool east = gridToRoom.ContainsKey(g + Vector2Int.up); bool east = gridToRoom.ContainsKey(g + Vector2Int.up);
bool west = gridToRoom.ContainsKey(g + Vector2Int.down); bool west = gridToRoom.ContainsKey(g + Vector2Int.down);
rh.SetEntrances(north, south, east, west);
rh.SetEntrances(northOpen: north, southOpen: south, eastOpen: east, westOpen: west);
} }
/* ---------- 5) CHODBY ---------- */ /* ----------- Build corridors ----------- */
BuildCorridors(); BuildCorridors();
/* ----------- Toggle all doors ----------- */
foreach (var keyValuePair in gridToRoom)
{
RoomHandler rh = keyValuePair.Value.GetComponent<RoomHandler>();
rh.ToggleAllDoors();
}
} }
/// <summary> /* ============================================================ */
/// Build corridors between rooms /* CORRIDORS */
/// </summary> /* ============================================================ */
/// <returns></returns>
private void BuildCorridors() private void BuildCorridors()
{ {
float straightZ = CorridorStraight.GetComponent<PrefabSize>().prefabSize.y; // Jediné dva směry, které musíme zkontrolovat z každé místnosti (right/up)
float straightX = CorridorStraight.GetComponent<PrefabSize>().prefabSize.x; Vector2Int[] directions = { Vector2Int.right, Vector2Int.up };
Vector2Int[] stepDirs = { Vector2Int.right, Vector2Int.up };
// Délky prefebů podél X a Z osy
float straightLenX = CorridorStraight.GetComponent<PrefabSize>().prefabSize.x;
float straightLenZ = CorridorStraight.GetComponent<PrefabSize>().prefabSize.y;
float doorLenX = DoorCorridor.GetComponent<PrefabSize>().prefabSize.x;
float doorLenZ = DoorCorridor.GetComponent<PrefabSize>().prefabSize.y;
foreach (var kv in gridToRoom) foreach (var kv in gridToRoom)
{ {
Vector2Int cell = kv.Key; Vector2Int cell = kv.Key;
GameObject roomA = kv.Value; GameObject roomA = kv.Value;
foreach (Vector2Int dir in stepDirs) foreach (Vector2Int dir in directions)
{ {
Vector2Int nKey = cell + dir; Vector2Int nKey = cell + dir;
if (!gridToRoom.TryGetValue(nKey, out GameObject roomB)) continue; if (!gridToRoom.TryGetValue(nKey, out GameObject roomB)) continue; // žádná sousední místnost
Vector3 axis, axisNeg;
float halfA, halfB;
Quaternion rot;
// Geometrie pro daný směr
Vector3 axis; float lenStraight, lenDoor; Quaternion rot;
if (dir == Vector2Int.right) if (dir == Vector2Int.right)
{ {
axis = Vector3.right; axisNeg = Vector3.left; axis = Vector3.right;
halfA = GetPrefabXZ(roomA).x * 0.5f; lenStraight = straightLenX;
halfB = GetPrefabXZ(roomB).x * 0.5f; lenDoor = doorLenX;
rot = Quaternion.Euler(0, 90, 0); rot = Quaternion.Euler(0, 90, 0);
} }
else else // Vector2Int.up
{ {
axis = Vector3.forward; axisNeg = Vector3.back; axis = Vector3.forward;
halfA = GetPrefabXZ(roomA).z * 0.5f; lenStraight = straightLenZ;
halfB = GetPrefabXZ(roomB).z * 0.5f; lenDoor = doorLenZ;
rot = Quaternion.identity; rot = Quaternion.identity;
} }
Vector3 start = roomA.transform.position + axis * halfA; // Pozice stěn (počítáme od středu místnosti k jejímu okraji)
Vector3 end = roomB.transform.position + axisNeg * halfB; float halfA = Vector3.Scale(GetPrefabXZ(roomA), axis).magnitude * 0.5f;
float halfB = Vector3.Scale(GetPrefabXZ(roomB), axis).magnitude * 0.5f;
Vector3 wallA = roomA.transform.position + axis * halfA;
Vector3 wallB = roomB.transform.position - axis * halfB;
CreateStraightCorridor(start, end, axis, rot, straightZ, straightX); // DVEŘE pivot dveří do středu úsečky mezi okraji stěn
Vector3 doorPos = (wallA + wallB) * 0.5f;
GameObject doorGO = Instantiate(DoorCorridor, doorPos, rot, transform);
DoorAnimation anim = doorGO.GetComponent<DoorAnimation>();
// REGISTRACE do obou místností
RoomHandler rhA = roomA.GetComponent<RoomHandler>();
RoomHandler rhB = roomB.GetComponent<RoomHandler>();
if (dir == Vector2Int.right)
{
rhA.RegisterDoor(RoomHandler.Side.East, anim);
rhB.RegisterDoor(RoomHandler.Side.West, anim);
}
else
{
rhA.RegisterDoor(RoomHandler.Side.North, anim);
rhB.RegisterDoor(RoomHandler.Side.South, anim);
}
// ROVNÉ SEGMENTY z obou stran dveří
Vector3 doorEdgeA = doorPos - axis * (lenDoor * 0.5f);
Vector3 doorEdgeB = doorPos + axis * (lenDoor * 0.5f);
PlaceStraightSegments(doorEdgeA, wallA, -axis, rot, lenStraight);
PlaceStraightSegments(doorEdgeB, wallB, axis, rot, lenStraight);
} }
} }
} }
/// <summary> /// <summary>
/// Build all rooms based on the layout grid /// Vyplní úsek mezi startEdge (hrana dveří nebo předchozího dílu)
/// a wallEdge (vnější hrana stěny místnosti) rovnými segmenty tak,
/// aby žádný segment nepřečníval do stěny.
/// </summary> /// </summary>
/// <param name="lines"></param> private void PlaceStraightSegments(
/// <param name="bottomRowIdx"></param> Vector3 startEdge,
/// <param name="cellSize"></param> Vector3 wallEdge,
Vector3 direction, // normalizovaný (+/- axis)
Quaternion rot,
float len) // délka STANDARDNÍHO rovného segmentu
{
const float EPS = 0.01f; // ≈1 cm tolerance
float dist = Vector3.Distance(startEdge, wallEdge);
if (dist < EPS) return; // nic netřeba
int fullCount = Mathf.FloorToInt(dist / len);
float remainder = dist - fullCount * len; // 0 .. len
// -------- 1) CELÉ segmenty --------
Vector3 firstPivot = startEdge + direction * (len * 0.5f); // hrana sedí na startEdge
for (int i = 0; i < fullCount; i++)
{
Vector3 pos = firstPivot + direction * (i * len);
GameObject prefab = (i % 2 == 0) ? CorridorStraight : CorridorStraightUnlit;
Instantiate(prefab, pos, rot, transform);
}
// -------- 2) POSLEDNÍ ZKRÁCENÝ segment (pokud je třeba) --------
if (remainder > EPS) // zbylo něco > 1 cm
{
// pivot tak, aby přední hrana nepřesáhla wallEdge
Vector3 remPivot = wallEdge - direction * (remainder * 0.5f);
GameObject last = Instantiate(CorridorStraightUnlit, remPivot, rot, transform);
// zmenšíme délku po místní ose Z (prefab je "dlouhý" po Z)
Vector3 sc = last.transform.localScale;
sc.z *= remainder / len; // poměr 0 .. 1
last.transform.localScale = sc;
}
}
/* ============================================================ */
/* ROOMS */
/* ============================================================ */
private void BuildRooms(string[] lines, int bottomRowIdx, Vector3 cellSize) private void BuildRooms(string[] lines, int bottomRowIdx, Vector3 cellSize)
{ {
int rows = lines.Length; for (int r = 0; r < lines.Length; r++)
for (int rowsIter = 0; rowsIter < rows; rowsIter++)
{ {
string line = lines[rowsIter]; string line = lines[r];
int gridZ = bottomRowIdx - rowsIter + 1; int gridZ = bottomRowIdx - r + 1;
for (int x = 0; x < line.Length; x++) for (int x = 0; x < line.Length; x++)
{ {
char ch = line[x]; char ch = line[x];
if (ch == '-') continue; if (ch == '-' || !char.IsDigit(ch)) continue;
if (!char.IsDigit(ch))
{
Debug.LogWarning($"Neznámý znak '{ch}' v layoutu ignoruji.");
continue;
}
int idx = ch - '0'; int idx = ch - '0';
if (idx >= mapPrefab.Count) if (idx >= mapPrefab.Count) continue;
{ Vector3 pos = new Vector3(x * (cellSize.x + RoomDistance), 0, gridZ * (cellSize.z + RoomDistance));
Debug.LogWarning($"Index {idx} mimo rozsah mapPrefab!"); GameObject room = Instantiate(mapPrefab[idx], pos, Quaternion.identity, transform);
continue;
}
GameObject prefab = mapPrefab[idx];
Vector3 worldPos = roomOriginOffset + new Vector3(
x * (cellSize.x + RoomDistance),
0,
gridZ * (cellSize.z + RoomDistance));
GameObject room = Instantiate(prefab, worldPos, Quaternion.identity, transform);
gridToRoom[new Vector2Int(x, gridZ)] = room; gridToRoom[new Vector2Int(x, gridZ)] = room;
} }
} }
} }
/// <summary> /* ============================================================ */
/// Returns the (x, y) coordinates of the room with closest aproximity to spawn or the furthest aproximity to spawn /* HELPERS */
/// </summary> /* ============================================================ */
/// <param name="lines"></param>
/// <param name="last"></param>
/// <returns>Vector2(x, y) where x is the line index and y is the column index</returns>
private Vector2 GetFirstOrLastRoom(string[] lines, bool last = false)
{
int colIdx = -1;
int spawnGridX = 0;
int rows = lines.Length;
for (int rowsIter = rows - 1; rowsIter >= 0; rowsIter--)
{
string line = lines[rowsIter];
for (int c = 0; c < line.Length; c++)
{
if (char.IsDigit(line[c]))
{
colIdx = rowsIter;
spawnGridX = c;
break;
}
}
if (colIdx != -1) break;
}
if (colIdx == -1)
{
Debug.LogError("Layout neobsahuje žádnou číslici (místnost)!");
return Vector2.zero;
}
return new Vector2(spawnGridX, colIdx);
}
private void CreateStraightCorridor(
Vector3 start, Vector3 end, Vector3 axis, Quaternion rot,
float stepLenZ, float stepLenX)
{
float dist = Vector3.Distance(start, end);
float step = Mathf.Approximately(Mathf.Abs(axis.z), 1f) ? stepLenZ : stepLenX;
int count = Mathf.Max(1, Mathf.FloorToInt(dist / step));
Vector3 segPos = start + axis * (step * 0.5f);
var door = Instantiate(DoorCorridor, segPos, rot, transform);
door.GetComponent<DoorAnimation>()?.ToggleDoor();
for (int i = 1; i < count; i++)
{
GameObject prefab = (i % 2 == 0) ? CorridorStraight : CorridorStraightUnlit;
segPos = start + axis * (i * step + step * 0.5f);
Instantiate(prefab, segPos, rot, transform);
}
}
/// <summary>
/// Returns the size of a prefab room
/// </summary>
/// <param name="prefab"> Choose a prefab from available ones</param>
/// <returns>Size of the prefab as Vector3</returns>
private static Vector3 GetPrefabXZ(GameObject prefab) private static Vector3 GetPrefabXZ(GameObject prefab)
{ {
var ps = prefab.GetComponent<PrefabSize>(); var ps = prefab.GetComponent<PrefabSize>();
return ps ? new Vector3(ps.prefabSize.x, 0, ps.prefabSize.y) : new Vector3(10, 0, 10); return ps ? new Vector3(ps.prefabSize.x, 0, ps.prefabSize.y) : new Vector3(10, 0, 10);
} }
private Vector2 GetFirstOrLastRoom(string[] lines)
{
for (int r = lines.Length - 1; r >= 0; r--)
{
string l = lines[r];
int c = l.ToCharArray().ToList().FindIndex(char.IsDigit);
if (c != -1) return new Vector2(c, r);
}
Debug.LogError("Layout neobsahuje žádnou číslici (místnost)!");
return Vector2.zero;
}
} }