Better corridor generation

Added door as first corridor
Changing from unlit and lit corridor
This commit is contained in:
2025-06-25 11:13:05 +02:00
parent 9eb7cfa4d1
commit 6d19043ed9
5 changed files with 94 additions and 23 deletions

View File

@@ -156,7 +156,9 @@ MonoBehaviour:
EndPoint: {fileID: 819094401162878122, guid: 03f2147e5a186fc408b959faa2f97e86, type: 3}
Player: {fileID: 6983871523237736218, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
CorridorStraight: {fileID: 8047827979703692770, guid: 1a5d554c0c76caf4195cae47e098b79d, type: 3}
RoomDistance: 70
CorridorStraightUnlit: {fileID: 2016417306107577256, guid: 92d9025262a022a499862d352c2724ee, type: 3}
DoorCorridor: {fileID: 5603417387143431118, guid: 9eccbf5a500a0d2429e6008a713a49fd, type: 3}
RoomDistance: 40
minRoomsNumber: 3
maxRoomsNumber: 7
--- !u!4 &23489964

View File

@@ -779,21 +779,53 @@ PrefabInstance:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 1872598832695960773, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1872598832695960773, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1872598832695960773, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1872598832695960773, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1872598832695960773, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6983871523237736218, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_Name
value: PlayerContainer
objectReference: {fileID: 0}
- target: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalScale.x
value: 1.5
objectReference: {fileID: 0}
- target: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalScale.y
value: 1.5
objectReference: {fileID: 0}
- target: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalScale.z
value: 1.5
objectReference: {fileID: 0}
- target: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: 4.49
objectReference: {fileID: 0}
- target: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalPosition.z
value: 0
value: 15.09
objectReference: {fileID: 0}
- target: {fileID: 7001416999833331379, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
propertyPath: m_LocalRotation.w

View File

@@ -14,13 +14,15 @@ public class MapGenManager : MonoBehaviour
[Header("Corridor Prefabs")]
[SerializeField] private GameObject CorridorStraight;
[SerializeField] private GameObject CorridorStraightUnlit;
[SerializeField] private GameObject DoorCorridor;
/*[SerializeField] private GameObject CorridorL;
[SerializeField] private GameObject CorridorT;
[SerializeField] private GameObject CorridorCross;
[SerializeField] private GameObject CorridorEnd;*/
[Header("Generation Settings")]
[SerializeField] private int RoomDistance = 5;
[SerializeField] private int RoomDistance = 3;
[SerializeField] private int minRoomsNumber = 3;
[SerializeField] private int maxRoomsNumber = 7;
@@ -53,15 +55,15 @@ public class MapGenManager : MonoBehaviour
// Place Generate Rooms
for (int i = 0; i < roomCount; i++)
{
Vector3 roomPos = GetGridPosition();
GameObject roomPrefab = mapPrefab[Random.Range(0, mapPrefab.Count)];
Vector3 roomPos = GetGridPosition(roomPrefab);
GameObject room = Instantiate(roomPrefab, roomPos, Quaternion.identity, transform);
placedRooms.Add(room);
roomPositions.Add(roomPos);
}
// Add End Point
GameObject endPoint = Instantiate(EndPoint, GetGridPosition(), Quaternion.identity, transform);
GameObject endPoint = Instantiate(EndPoint, GetGridPosition(EndPoint), Quaternion.identity, transform);
roomPositions.Add(endPoint.transform.position);
placedRooms.Add(endPoint);
@@ -73,31 +75,31 @@ public class MapGenManager : MonoBehaviour
Vector3 endRoomPos = roomPositions[i + 1];
PrefabSize endRoom = placedRooms[i + 1].GetComponent<PrefabSize>();
Vector3 firstCorridorPos = new Vector3(
startRoomPos.x + startRoom.prefabSize.x / 2,
startRoomPos.x,
startRoomPos.y,
startRoomPos.z
startRoomPos.z + startRoom.prefabSize.y / 2
);
Vector3 lastCorridorPos = new Vector3(
endRoomPos.x - endRoom.prefabSize.x / 2,
endRoomPos.x,
endRoomPos.y,
endRoomPos.z
endRoomPos.z - endRoom.prefabSize.y / 2
);
CreateCorridor(firstCorridorPos, lastCorridorPos);
}
}
private Vector3 GetGridPosition()
private Vector3 GetGridPosition(GameObject roomPrefab)
{
Vector3 lastRoomPos = roomPositions[roomPositions.Count - 1];
PrefabSize lastRoom = placedRooms[placedRooms.Count - 1].GetComponent<PrefabSize>();
PrefabSize roomSize = roomPrefab.GetComponent<PrefabSize>();
// Convert grid units to world position
Vector3 roomPos = new Vector3(
lastRoomPos.x + RoomDistance,
lastRoomPos.x,
lastRoomPos.y,
lastRoomPos.z
lastRoomPos.z + lastRoom.prefabSize.y / 2f + roomSize.prefabSize.y / 2f + RoomDistance
);
return roomPos;
}
@@ -106,21 +108,53 @@ public class MapGenManager : MonoBehaviour
// Calculate the distance
float distance = Vector3.Distance(start, end);
PrefabSize corridorSize = CorridorStraight.GetComponent<PrefabSize>();
PrefabSize corridorUnlitSize = CorridorStraightUnlit.GetComponent<PrefabSize>();
PrefabSize doorSize = DoorCorridor.GetComponent<PrefabSize>();
// Calculate the number of corridors needed
int corridorCount = Mathf.FloorToInt(distance / corridorSize.prefabSize.x);
int corridorCount = Mathf.FloorToInt(distance / corridorSize.prefabSize.y);
Debug.Log($"Creating {corridorCount} corridors from {start} to {end}");
// Create corridors
for (int i = 0; i < corridorCount; i++)
//Start with door
if (corridorCount > 0)
{
Vector3 pos = new Vector3(
start.x + i * corridorSize.prefabSize.x + corridorSize.prefabSize.x * 0.5f,
Vector3 doorPos = new Vector3(
start.x,
start.y,
start.z
start.z + doorSize.prefabSize.y * 0.5f
);
Quaternion rotation = Quaternion.Euler(0, 90, 0);
GameObject corridor = Instantiate(CorridorStraight, pos, rotation, transform);
Quaternion doorRotation = Quaternion.Euler(0, 0, 0);
GameObject doorCorridor = Instantiate(DoorCorridor, doorPos, doorRotation, transform);
for (int i = 1; i < corridorCount; i++)
{
if(i % 2 != 0)
{
// Create straight corridor
Vector3 pos = new Vector3(
start.x,
start.y,
start.z + i * corridorSize.prefabSize.y + corridorSize.prefabSize.y * 0.5f
);
Quaternion rotation = Quaternion.Euler(0, 0, 0);
GameObject corridor = Instantiate(CorridorStraight, pos, rotation, transform);
}
else
{
// Create unlit corridor
Vector3 pos = new Vector3(
start.x,
start.y,
start.z + i * corridorUnlitSize.prefabSize.y + corridorUnlitSize.prefabSize.y * 0.5f
);
Quaternion rotation = Quaternion.Euler(0, 0, 0);
GameObject corridorUnlit = Instantiate(CorridorStraightUnlit, pos, rotation, transform);
}
}
}
// If there is not enough space to create corridors, throw an exception
else throw new System.Exception("Not enough space to create corridors");
}
}

View File

@@ -7,7 +7,7 @@ public class MenuController : MonoBehaviour
public void PlayButton()
{
SceneManager.LoadScene("Podzemi");
SceneManager.LoadScene("GenTest");
}
public void SettingButtonn()