RNG generace dungeonu
This commit is contained in:
@@ -627,6 +627,7 @@ GameObject:
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 976286785544496788}
|
||||
- component: {fileID: 3997441805795631177}
|
||||
m_Layer: 0
|
||||
m_Name: Arena_1
|
||||
m_TagString: Untagged
|
||||
@@ -643,7 +644,7 @@ Transform:
|
||||
m_GameObject: {fileID: 2783560673348224804}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 90}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
@@ -663,6 +664,19 @@ Transform:
|
||||
- {fileID: 6594277869342524614}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &3997441805795631177
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2783560673348224804}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 2fd110cfa76ce1444a9e8de41c1f8896, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
prefabSize: {x: 30, y: 50}
|
||||
--- !u!1 &2837174233086072514
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -118,6 +118,7 @@ GameObject:
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1895486018500882413}
|
||||
- component: {fileID: 6297990339078496104}
|
||||
m_Layer: 0
|
||||
m_Name: Arena_2
|
||||
m_TagString: Untagged
|
||||
@@ -134,7 +135,7 @@ Transform:
|
||||
m_GameObject: {fileID: 7069902989848030098}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 50, y: 0, z: 90}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
@@ -149,6 +150,19 @@ Transform:
|
||||
- {fileID: 3262639963771063369}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &6297990339078496104
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 7069902989848030098}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 2fd110cfa76ce1444a9e8de41c1f8896, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
prefabSize: {x: 30, y: 50}
|
||||
--- !u!1001 &627855985815857216
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -118,6 +118,7 @@ GameObject:
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 6920336211645667070}
|
||||
- component: {fileID: 3990037672578072316}
|
||||
m_Layer: 0
|
||||
m_Name: Arena_3
|
||||
m_TagString: Untagged
|
||||
@@ -134,7 +135,7 @@ Transform:
|
||||
m_GameObject: {fileID: 8524395191423973796}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 125.4, y: 0, z: 90}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
@@ -146,6 +147,19 @@ Transform:
|
||||
- {fileID: 3087047028094329201}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &3990037672578072316
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 8524395191423973796}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 2fd110cfa76ce1444a9e8de41c1f8896, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
prefabSize: {x: 50, y: 50}
|
||||
--- !u!1001 &572179306618835033
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -9,6 +9,7 @@ GameObject:
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1670384370540582}
|
||||
- component: {fileID: 8788175465630764883}
|
||||
m_Layer: 0
|
||||
m_Name: Arena_4
|
||||
m_TagString: Untagged
|
||||
@@ -25,7 +26,7 @@ Transform:
|
||||
m_GameObject: {fileID: 4362243528253867212}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 180, y: 0, z: 90}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
@@ -38,6 +39,19 @@ Transform:
|
||||
- {fileID: 3874663274240414718}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &8788175465630764883
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 4362243528253867212}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 2fd110cfa76ce1444a9e8de41c1f8896, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
prefabSize: {x: 50, y: 50}
|
||||
--- !u!1001 &3066405850367996388
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -9,6 +9,7 @@ GameObject:
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2981424240143364775}
|
||||
- component: {fileID: 2787345659745357017}
|
||||
m_Layer: 0
|
||||
m_Name: Arena_5
|
||||
m_TagString: Untagged
|
||||
@@ -25,7 +26,7 @@ Transform:
|
||||
m_GameObject: {fileID: 5061069654894918266}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 250, y: 0, z: 90}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
@@ -38,6 +39,19 @@ Transform:
|
||||
- {fileID: 3431484422084717758}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &2787345659745357017
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 5061069654894918266}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 2fd110cfa76ce1444a9e8de41c1f8896, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
prefabSize: {x: 50, y: 50}
|
||||
--- !u!1001 &2611697662717533622
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -134,7 +134,7 @@ Transform:
|
||||
m_GameObject: {fileID: 9041356366999144539}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 63.960274, y: 1.110223e-16, z: -1.3195934}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
|
||||
@@ -9,6 +9,7 @@ GameObject:
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 5209095924313359579}
|
||||
- component: {fileID: 9201002091790163399}
|
||||
m_Layer: 0
|
||||
m_Name: "Odpo\u010D\xEDv\xE1rna_1"
|
||||
m_TagString: Untagged
|
||||
@@ -25,7 +26,7 @@ Transform:
|
||||
m_GameObject: {fileID: 819094401162878122}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 25}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
@@ -37,6 +38,19 @@ Transform:
|
||||
- {fileID: 7513404745493509246}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &9201002091790163399
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 819094401162878122}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 2fd110cfa76ce1444a9e8de41c1f8896, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
prefabSize: {x: 30, y: 30}
|
||||
--- !u!1 &1015967461472900312
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -705,6 +705,7 @@ GameObject:
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 3479177569595356638}
|
||||
- component: {fileID: -600122568662034771}
|
||||
m_Layer: 0
|
||||
m_Name: Corridor_1
|
||||
m_TagString: Untagged
|
||||
@@ -732,6 +733,19 @@ Transform:
|
||||
- {fileID: 3981932374261552881}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &-600122568662034771
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 8047827979703692770}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 2fd110cfa76ce1444a9e8de41c1f8896, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
prefabSize: {x: 10, y: 10}
|
||||
--- !u!1 &8648776681170881870
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
8
3D blobici/Assets/Prefabs/Scripts.meta
Normal file
8
3D blobici/Assets/Prefabs/Scripts.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f63549004eeb0e143b3aa6c81398015b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
6
3D blobici/Assets/Prefabs/Scripts/PrefabSize.cs
Normal file
6
3D blobici/Assets/Prefabs/Scripts/PrefabSize.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PrefabSize : MonoBehaviour
|
||||
{
|
||||
public Vector2Int prefabSize = new Vector2Int(3, 5);
|
||||
}
|
||||
2
3D blobici/Assets/Prefabs/Scripts/PrefabSize.cs.meta
Normal file
2
3D blobici/Assets/Prefabs/Scripts/PrefabSize.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2fd110cfa76ce1444a9e8de41c1f8896
|
||||
@@ -152,17 +152,13 @@ MonoBehaviour:
|
||||
- {fileID: 2783560673348224804, guid: b7111ccd107e56741a9790a50ab8e190, type: 3}
|
||||
- {fileID: 7069902989848030098, guid: e8e0ba1faefe88f47b927a477b87ed22, type: 3}
|
||||
- {fileID: 8524395191423973796, guid: 37b1623de3a4b8140bc4fd60f59c6329, type: 3}
|
||||
StartPoint: {fileID: 6288531490932494907, guid: 03f2147e5a186fc408b959faa2f97e86, type: 3}
|
||||
EndPoint: {fileID: 6288531490932494907, guid: 03f2147e5a186fc408b959faa2f97e86, type: 3}
|
||||
StartPoint: {fileID: 819094401162878122, guid: 03f2147e5a186fc408b959faa2f97e86, type: 3}
|
||||
EndPoint: {fileID: 819094401162878122, guid: 03f2147e5a186fc408b959faa2f97e86, type: 3}
|
||||
Player: {fileID: 6983871523237736218, guid: f0df263e5be65a041848d5a8bab85af1, type: 3}
|
||||
CorridorStraight: {fileID: 8047827979703692770, guid: 1a5d554c0c76caf4195cae47e098b79d, type: 3}
|
||||
CorridorL: {fileID: 9041356366999144539, guid: 03969bf1ebc35b649a56caa0c7ecc01b, type: 3}
|
||||
CorridorT: {fileID: 1810040309676791451, guid: 9d329c9ef1d131b48b86fda839e87cf4, type: 3}
|
||||
CorridorCross: {fileID: 1941837422573887459, guid: 7c3fab57d0af04944b229b354210a5a5, type: 3}
|
||||
CorridorEnd: {fileID: 3057623685151502433, guid: c675bab0a21d7014d8010d0e0fb01754, type: 3}
|
||||
minRoomDistance: 60
|
||||
maxRoomDistance: 80
|
||||
corridorWidth: 5
|
||||
RoomDistance: 70
|
||||
minRoomsNumber: 3
|
||||
maxRoomsNumber: 7
|
||||
--- !u!4 &23489964
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -172,7 +168,7 @@ Transform:
|
||||
m_GameObject: {fileID: 23489962}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: -17.590492, y: -78.82353, z: 130.80498}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
|
||||
@@ -8,21 +8,21 @@ public class MapGenManager : MonoBehaviour
|
||||
[SerializeField] private List<GameObject> mapPrefab = new List<GameObject>();
|
||||
[SerializeField] private GameObject StartPoint;
|
||||
[SerializeField] private GameObject EndPoint;
|
||||
|
||||
|
||||
[Header("Player")]
|
||||
[SerializeField] private GameObject Player;
|
||||
|
||||
|
||||
[Header("Corridor Prefabs")]
|
||||
[SerializeField] private GameObject CorridorStraight;
|
||||
[SerializeField] private GameObject CorridorL;
|
||||
/*[SerializeField] private GameObject CorridorL;
|
||||
[SerializeField] private GameObject CorridorT;
|
||||
[SerializeField] private GameObject CorridorCross;
|
||||
[SerializeField] private GameObject CorridorEnd;
|
||||
|
||||
[SerializeField] private GameObject CorridorEnd;*/
|
||||
|
||||
[Header("Generation Settings")]
|
||||
[SerializeField] private float minRoomDistance = 30f;
|
||||
[SerializeField] private float maxRoomDistance = 50f;
|
||||
[SerializeField] private float corridorWidth = 5f;
|
||||
[SerializeField] private int RoomDistance = 5;
|
||||
[SerializeField] private int minRoomsNumber = 3;
|
||||
[SerializeField] private int maxRoomsNumber = 7;
|
||||
|
||||
private List<Vector3> roomPositions = new List<Vector3>();
|
||||
private List<GameObject> placedRooms = new List<GameObject>();
|
||||
@@ -34,540 +34,93 @@ public class MapGenManager : MonoBehaviour
|
||||
|
||||
private void MapGen()
|
||||
{
|
||||
// Clear previous rooms and positions
|
||||
roomPositions.Clear();
|
||||
placedRooms.Clear();
|
||||
|
||||
// Start position should be aligned to grid
|
||||
Vector3 startPos = new Vector3(0, 0, 0); // Grid aligned at origin
|
||||
|
||||
// Add Start Point
|
||||
Vector3 startPos = new Vector3(0, 0, 0);
|
||||
GameObject startPoint = Instantiate(StartPoint, startPos, Quaternion.identity, transform);
|
||||
roomPositions.Add(startPos);
|
||||
placedRooms.Add(startPoint);
|
||||
|
||||
|
||||
// Instantiate the player at the starting position
|
||||
GameObject player = Instantiate(Player, new Vector3(startPos.x, 1, startPos.z), Quaternion.identity, transform);
|
||||
|
||||
int roomCount = Random.Range(3, 7);
|
||||
|
||||
|
||||
// Generate a random number of rooms
|
||||
int roomCount = Random.Range(minRoomsNumber, maxRoomsNumber);
|
||||
|
||||
// Place Generate Rooms
|
||||
for (int i = 0; i < roomCount; i++)
|
||||
{
|
||||
Vector3 roomPos = GetRandomGridPosition();
|
||||
Vector3 roomPos = GetGridPosition();
|
||||
GameObject roomPrefab = mapPrefab[Random.Range(0, mapPrefab.Count)];
|
||||
GameObject room = Instantiate(roomPrefab, roomPos, Quaternion.identity, transform);
|
||||
placedRooms.Add(room);
|
||||
roomPositions.Add(roomPos);
|
||||
}
|
||||
|
||||
GameObject endPoint = Instantiate(EndPoint, GetRandomGridPosition(), Quaternion.identity, transform);
|
||||
|
||||
// Add End Point
|
||||
GameObject endPoint = Instantiate(EndPoint, GetGridPosition(), Quaternion.identity, transform);
|
||||
roomPositions.Add(endPoint.transform.position);
|
||||
placedRooms.Add(endPoint);
|
||||
|
||||
// Generate corridors to connect rooms
|
||||
GenerateCorridors();
|
||||
|
||||
// Add some dead ends for more dynamic layouts
|
||||
AddDeadEndCorridors();
|
||||
}
|
||||
|
||||
private void GenerateCorridors()
|
||||
{
|
||||
// Create a minimum spanning tree to ensure all rooms are connected
|
||||
List<(int, int)> edges = CreateMinimumSpanningTree();
|
||||
|
||||
// Place corridors between connected rooms
|
||||
foreach (var edge in edges)
|
||||
|
||||
// Create corridors between rooms
|
||||
for (int i = 0; i < roomPositions.Count - 1; i++)
|
||||
{
|
||||
ConnectRoomsWithCorridor(roomPositions[edge.Item1], roomPositions[edge.Item2]);
|
||||
}
|
||||
}
|
||||
|
||||
private List<(int, int)> CreateMinimumSpanningTree()
|
||||
{
|
||||
// Using Prim's algorithm to generate a minimum spanning tree
|
||||
List<(int, int)> mstEdges = new List<(int, int)>();
|
||||
List<int> connectedNodes = new List<int>();
|
||||
List<int> unconnectedNodes = new List<int>();
|
||||
|
||||
// Start with node 0 (start room)
|
||||
for (int i = 0; i < roomPositions.Count; i++)
|
||||
{
|
||||
unconnectedNodes.Add(i);
|
||||
}
|
||||
|
||||
// Start with first node
|
||||
connectedNodes.Add(unconnectedNodes[0]);
|
||||
unconnectedNodes.RemoveAt(0);
|
||||
|
||||
// Continue until all nodes are connected
|
||||
while (unconnectedNodes.Count > 0)
|
||||
{
|
||||
float minDistance = float.MaxValue;
|
||||
int closestConnected = -1;
|
||||
int closestUnconnected = -1;
|
||||
|
||||
// Find shortest edge between a connected and unconnected node
|
||||
foreach (int connected in connectedNodes)
|
||||
{
|
||||
foreach (int unconnected in unconnectedNodes)
|
||||
{
|
||||
float distance = Vector3.Distance(roomPositions[connected], roomPositions[unconnected]);
|
||||
if (distance < minDistance)
|
||||
{
|
||||
minDistance = distance;
|
||||
closestConnected = connected;
|
||||
closestUnconnected = unconnected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the edge to our MST
|
||||
mstEdges.Add((closestConnected, closestUnconnected));
|
||||
|
||||
// Move the node from unconnected to connected
|
||||
connectedNodes.Add(closestUnconnected);
|
||||
unconnectedNodes.Remove(closestUnconnected);
|
||||
}
|
||||
|
||||
return mstEdges;
|
||||
}
|
||||
|
||||
private void ConnectRoomsWithCorridor(Vector3 startRoom, Vector3 endRoom)
|
||||
{
|
||||
// Calculate the grid-based path between rooms
|
||||
List<Vector3> path = CalculateGridPath(startRoom, endRoom);
|
||||
|
||||
// Place corridor pieces along the path
|
||||
for (int i = 0; i < path.Count - 1; i++)
|
||||
{
|
||||
PlaceCorridorSegment(path[i], path[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Vector3> CalculateGridPath(Vector3 start, Vector3 end)
|
||||
{
|
||||
List<Vector3> path = new List<Vector3>();
|
||||
path.Add(start);
|
||||
|
||||
// Determine if we go horizontal first or vertical first (50/50 chance)
|
||||
bool horizontalFirst = Random.value < 0.5f;
|
||||
|
||||
Vector3 current = start;
|
||||
|
||||
if (horizontalFirst)
|
||||
{
|
||||
// Move horizontally first, then vertically
|
||||
while (Mathf.Abs(current.x - end.x) >= 5)
|
||||
{
|
||||
float step = current.x < end.x ? 5 : -5;
|
||||
current = new Vector3(current.x + step, 0, current.z);
|
||||
path.Add(current);
|
||||
}
|
||||
|
||||
while (Mathf.Abs(current.z - end.z) >= 5)
|
||||
{
|
||||
float step = current.z < end.z ? 5 : -5;
|
||||
current = new Vector3(current.x, 0, current.z + step);
|
||||
path.Add(current);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Move vertically first, then horizontally
|
||||
while (Mathf.Abs(current.z - end.z) >= 5)
|
||||
{
|
||||
float step = current.z < end.z ? 5 : -5;
|
||||
current = new Vector3(current.x, 0, current.z + step);
|
||||
path.Add(current);
|
||||
}
|
||||
|
||||
while (Mathf.Abs(current.x - end.x) >= 5)
|
||||
{
|
||||
float step = current.x < end.x ? 5 : -5;
|
||||
current = new Vector3(current.x + step, 0, current.z);
|
||||
path.Add(current);
|
||||
}
|
||||
}
|
||||
|
||||
// Add the end position if it's not already there
|
||||
if (Vector3.Distance(current, end) >= 5)
|
||||
{
|
||||
path.Add(end);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
private void PlaceCorridorSegment(Vector3 start, Vector3 end)
|
||||
{
|
||||
// Determine corridor type based on connecting rooms
|
||||
Vector3 direction = end - start;
|
||||
GameObject corridorPrefab = CorridorStraight; // Default to straight
|
||||
Quaternion rotation = Quaternion.identity;
|
||||
Vector3 position = (start + end) / 2;
|
||||
|
||||
// Calculate direction for rotation
|
||||
if (direction.x > 0) // East
|
||||
{
|
||||
rotation = Quaternion.Euler(0, 90, 0);
|
||||
}
|
||||
else if (direction.x < 0) // West
|
||||
{
|
||||
rotation = Quaternion.Euler(0, 90, 0);
|
||||
}
|
||||
else if (direction.z > 0) // North
|
||||
{
|
||||
rotation = Quaternion.Euler(0, 0, 0);
|
||||
}
|
||||
else if (direction.z < 0) // South
|
||||
{
|
||||
rotation = Quaternion.Euler(0, 0, 0);
|
||||
}
|
||||
|
||||
// Check if this segment is part of a turn, T-junction, or crossing
|
||||
List<Direction> connectingDirections = GetConnectingDirections(end);
|
||||
|
||||
// Determine corridor type and rotation based on connections
|
||||
if (connectingDirections.Count == 1) // Straight corridor or dead end
|
||||
{
|
||||
corridorPrefab = CorridorStraight;
|
||||
}
|
||||
else if (connectingDirections.Count == 2)
|
||||
{
|
||||
// Check if it's an L-turn
|
||||
if (IsLTurn(direction, connectingDirections))
|
||||
{
|
||||
corridorPrefab = CorridorL;
|
||||
// Adjust rotation for L-turn
|
||||
rotation = GetLTurnRotation(direction, connectingDirections);
|
||||
}
|
||||
}
|
||||
else if (connectingDirections.Count == 3) // T-junction
|
||||
{
|
||||
corridorPrefab = CorridorT;
|
||||
// Adjust rotation for T-junction
|
||||
rotation = GetTJunctionRotation(direction, connectingDirections);
|
||||
}
|
||||
else if (connectingDirections.Count >= 4) // Cross junction
|
||||
{
|
||||
corridorPrefab = CorridorCross;
|
||||
}
|
||||
|
||||
Instantiate(corridorPrefab, position, rotation, transform);
|
||||
}
|
||||
|
||||
private enum Direction { North, East, South, West }
|
||||
|
||||
private List<Direction> GetConnectingDirections(Vector3 position)
|
||||
{
|
||||
// Check which directions have corridors or rooms from this position
|
||||
List<Direction> connections = new List<Direction>();
|
||||
|
||||
// Check in each cardinal direction
|
||||
Vector3[] offsets = new Vector3[] {
|
||||
new Vector3(0, 0, 5), // North
|
||||
new Vector3(5, 0, 0), // East
|
||||
new Vector3(0, 0, -5), // South
|
||||
new Vector3(-5, 0, 0) // West
|
||||
};
|
||||
|
||||
Direction[] directions = new Direction[] {
|
||||
Direction.North,
|
||||
Direction.East,
|
||||
Direction.South,
|
||||
Direction.West
|
||||
};
|
||||
|
||||
for (int i = 0; i < offsets.Length; i++)
|
||||
{
|
||||
Vector3 checkPos = position + offsets[i];
|
||||
|
||||
// Check if there's a room at this position
|
||||
bool hasConnection = roomPositions.Any(rp => Vector3.Distance(rp, checkPos) < 2.5f);
|
||||
|
||||
// If no room, check for corridor (simplified - in a full implementation,
|
||||
// you'd track placed corridors separately)
|
||||
if (!hasConnection)
|
||||
{
|
||||
// For simplicity, assume there's a corridor if it's part of a path we've calculated
|
||||
// In a complete implementation, you'd track corridor positions
|
||||
}
|
||||
|
||||
if (hasConnection)
|
||||
{
|
||||
connections.Add(directions[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return connections;
|
||||
}
|
||||
|
||||
private bool IsLTurn(Vector3 incomingDirection, List<Direction> connections)
|
||||
{
|
||||
// Check if connections form an L shape (90-degree turn)
|
||||
if (connections.Count != 2) return false;
|
||||
|
||||
Direction incoming = VectorToDirection(incomingDirection);
|
||||
Direction opposite = GetOppositeDirection(incoming);
|
||||
|
||||
// If one of the connections is opposite to the incoming direction,
|
||||
// then it's a straight corridor, not an L-turn
|
||||
return !connections.Contains(opposite);
|
||||
}
|
||||
|
||||
private Direction VectorToDirection(Vector3 vector)
|
||||
{
|
||||
if (vector.x > 0) return Direction.East;
|
||||
if (vector.x < 0) return Direction.West;
|
||||
if (vector.z > 0) return Direction.North;
|
||||
return Direction.South;
|
||||
}
|
||||
|
||||
private Direction GetOppositeDirection(Direction dir)
|
||||
{
|
||||
switch (dir)
|
||||
{
|
||||
case Direction.North: return Direction.South;
|
||||
case Direction.East: return Direction.West;
|
||||
case Direction.South: return Direction.North;
|
||||
case Direction.West: return Direction.East;
|
||||
default: return Direction.North;
|
||||
}
|
||||
}
|
||||
|
||||
private Quaternion GetLTurnRotation(Vector3 incomingDirection, List<Direction> connections)
|
||||
{
|
||||
// Calculate rotation for L-turns based on the directions it connects
|
||||
Direction incoming = VectorToDirection(incomingDirection);
|
||||
|
||||
// Find the other direction (not the incoming and not the opposite of incoming)
|
||||
Direction other = connections.Find(d => d != incoming && d != GetOppositeDirection(incoming));
|
||||
|
||||
switch (incoming)
|
||||
{
|
||||
case Direction.North:
|
||||
return other == Direction.East ? Quaternion.Euler(0, 0, 0) : Quaternion.Euler(0, 270, 0);
|
||||
case Direction.East:
|
||||
return other == Direction.North ? Quaternion.Euler(0, 90, 0) : Quaternion.Euler(0, 0, 0);
|
||||
case Direction.South:
|
||||
return other == Direction.East ? Quaternion.Euler(0, 270, 0) : Quaternion.Euler(0, 180, 0);
|
||||
case Direction.West:
|
||||
return other == Direction.North ? Quaternion.Euler(0, 180, 0) : Quaternion.Euler(0, 90, 0);
|
||||
default:
|
||||
return Quaternion.identity;
|
||||
}
|
||||
}
|
||||
|
||||
private Quaternion GetTJunctionRotation(Vector3 incomingDirection, List<Direction> connections)
|
||||
{
|
||||
// Calculate rotation for T-junctions
|
||||
Direction incoming = VectorToDirection(incomingDirection);
|
||||
Direction opposite = GetOppositeDirection(incoming);
|
||||
|
||||
// If the connection doesn't include the opposite direction, the "T" points in that direction
|
||||
if (!connections.Contains(opposite))
|
||||
{
|
||||
switch (opposite)
|
||||
{
|
||||
case Direction.North: return Quaternion.Euler(0, 0, 0);
|
||||
case Direction.East: return Quaternion.Euler(0, 90, 0);
|
||||
case Direction.South: return Quaternion.Euler(0, 180, 0);
|
||||
case Direction.West: return Quaternion.Euler(0, 270, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// If it does contain the opposite, find the missing direction
|
||||
Direction[] allDirections = new Direction[] { Direction.North, Direction.East, Direction.South, Direction.West };
|
||||
Direction missing = allDirections.First(d => !connections.Contains(d));
|
||||
|
||||
switch (missing)
|
||||
{
|
||||
case Direction.North: return Quaternion.Euler(0, 180, 0);
|
||||
case Direction.East: return Quaternion.Euler(0, 270, 0);
|
||||
case Direction.South: return Quaternion.Euler(0, 0, 0);
|
||||
case Direction.West: return Quaternion.Euler(0, 90, 0);
|
||||
default: return Quaternion.identity;
|
||||
}
|
||||
}
|
||||
|
||||
private void AddDeadEndCorridors()
|
||||
{
|
||||
// Add some random dead ends for more interesting level design
|
||||
int deadEndCount = Random.Range(1, 4); // 1-3 dead ends
|
||||
|
||||
for (int i = 0; i < deadEndCount; i++)
|
||||
{
|
||||
// Pick a random room to extend from
|
||||
int roomIndex = Random.Range(0, roomPositions.Count);
|
||||
Vector3 roomPos = roomPositions[roomIndex];
|
||||
|
||||
// Pick a random direction
|
||||
Vector3[] directions = new Vector3[] {
|
||||
new Vector3(5, 0, 0), // East
|
||||
new Vector3(-5, 0, 0), // West
|
||||
new Vector3(0, 0, 5), // North
|
||||
new Vector3(0, 0, -5) // South
|
||||
};
|
||||
|
||||
Vector3 direction = directions[Random.Range(0, directions.Length)];
|
||||
|
||||
// Create a dead end corridor (1-3 segments long)
|
||||
int segmentCount = Random.Range(1, 4);
|
||||
Vector3 currentPos = roomPos;
|
||||
|
||||
for (int j = 0; j < segmentCount; j++)
|
||||
{
|
||||
Vector3 nextPos = currentPos + direction;
|
||||
|
||||
// Make sure we're not placing corridors where rooms exist
|
||||
bool canPlace = true;
|
||||
foreach (Vector3 roomPosition in roomPositions)
|
||||
{
|
||||
if (Vector3.Distance(nextPos, roomPosition) < 5)
|
||||
{
|
||||
canPlace = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (canPlace)
|
||||
{
|
||||
PlaceCorridorSegment(currentPos, nextPos);
|
||||
currentPos = nextPos;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Place an end cap at the last position if it's not overlapping with a room
|
||||
bool endCapCanBePlaced = true;
|
||||
foreach (Vector3 roomPosition in roomPositions)
|
||||
{
|
||||
if (Vector3.Distance(currentPos, roomPosition) < 5)
|
||||
{
|
||||
endCapCanBePlaced = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endCapCanBePlaced && segmentCount > 0)
|
||||
{
|
||||
// Calculate rotation based on direction
|
||||
Quaternion rotation = Quaternion.identity;
|
||||
if (direction.x > 0) rotation = Quaternion.Euler(0, 90, 0);
|
||||
else if (direction.x < 0) rotation = Quaternion.Euler(0, 270, 0);
|
||||
else if (direction.z > 0) rotation = Quaternion.Euler(0, 0, 0);
|
||||
else if (direction.z < 0) rotation = Quaternion.Euler(0, 180, 0);
|
||||
|
||||
Instantiate(CorridorEnd, currentPos, rotation, transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 GetRandomGridPosition()
|
||||
{
|
||||
Vector3 lastRoomPos = roomPositions[roomPositions.Count - 1];
|
||||
|
||||
// Calculate min and max distances in grid units (multiples of 5)
|
||||
int minGridDistance = Mathf.CeilToInt(minRoomDistance / 5);
|
||||
int maxGridDistance = Mathf.FloorToInt(maxRoomDistance / 5);
|
||||
|
||||
// Get random grid cell offset
|
||||
int xGridOffset = 0;
|
||||
int zGridOffset = 0;
|
||||
Vector3 roomPos = Vector3.zero;
|
||||
|
||||
// Make sure the position is valid
|
||||
int attempts = 0;
|
||||
int maxAttempts = 100; // Prevent infinite loops
|
||||
|
||||
do {
|
||||
// Generate random offsets directly as grid units
|
||||
xGridOffset = Random.Range(-maxGridDistance, maxGridDistance + 1);
|
||||
zGridOffset = Random.Range(-maxGridDistance, maxGridDistance + 1);
|
||||
|
||||
// Ensure we respect minimum distance
|
||||
if (Mathf.Abs(xGridOffset) < minGridDistance && Mathf.Abs(zGridOffset) < minGridDistance)
|
||||
{
|
||||
// Force minimum distance by picking a direction
|
||||
if (Random.value < 0.5f)
|
||||
xGridOffset = Random.value < 0.5f ? minGridDistance : -minGridDistance;
|
||||
else
|
||||
zGridOffset = Random.value < 0.5f ? minGridDistance : -minGridDistance;
|
||||
}
|
||||
|
||||
// Convert grid units to world position (multiply by 5)
|
||||
roomPos = new Vector3(
|
||||
lastRoomPos.x + (xGridOffset * 5),
|
||||
0,
|
||||
lastRoomPos.z + (zGridOffset * 5)
|
||||
Vector3 startRoomPos = roomPositions[i];
|
||||
PrefabSize startRoom = placedRooms[i].GetComponent<PrefabSize>();
|
||||
Vector3 endRoomPos = roomPositions[i + 1];
|
||||
PrefabSize endRoom = placedRooms[i + 1].GetComponent<PrefabSize>();
|
||||
Vector3 firstCorridorPos = new Vector3(
|
||||
startRoomPos.x + startRoom.prefabSize.x / 2,
|
||||
startRoomPos.y,
|
||||
startRoomPos.z
|
||||
);
|
||||
|
||||
attempts++;
|
||||
} while (!IsValidPos(roomPos) && attempts < maxAttempts);
|
||||
|
||||
// If we couldn't find a valid position, use fallback
|
||||
if (attempts >= maxAttempts)
|
||||
{
|
||||
Debug.LogWarning("Couldn't find valid room position after " + maxAttempts + " attempts. Using best approximation.");
|
||||
roomPos = FindNearestValidGridPosition(lastRoomPos);
|
||||
Vector3 lastCorridorPos = new Vector3(
|
||||
endRoomPos.x - endRoom.prefabSize.x / 2,
|
||||
endRoomPos.y,
|
||||
endRoomPos.z
|
||||
);
|
||||
CreateCorridor(firstCorridorPos, lastCorridorPos);
|
||||
}
|
||||
|
||||
return roomPos;
|
||||
}
|
||||
|
||||
private Vector3 FindNearestValidGridPosition(Vector3 startPos)
|
||||
{
|
||||
// Define min and max grid distances in grid units (not world units)
|
||||
int minGridDistance = Mathf.CeilToInt(minRoomDistance / 5);
|
||||
int maxGridDistance = Mathf.FloorToInt(maxRoomDistance / 5);
|
||||
|
||||
// Check each grid distance in increasing order
|
||||
for (int distance = minGridDistance; distance <= maxGridDistance; distance++)
|
||||
{
|
||||
// Try cardinal directions first (more likely to have space)
|
||||
int[] directions = { distance, -distance };
|
||||
|
||||
// Try horizontal directions
|
||||
foreach (int x in directions)
|
||||
{
|
||||
Vector3 testPos = new Vector3(startPos.x + (x * 5), 0, startPos.z);
|
||||
if (IsValidPos(testPos))
|
||||
return testPos;
|
||||
}
|
||||
|
||||
// Try vertical directions
|
||||
foreach (int z in directions)
|
||||
{
|
||||
Vector3 testPos = new Vector3(startPos.x, 0, startPos.z + (z * 5));
|
||||
if (IsValidPos(testPos))
|
||||
return testPos;
|
||||
}
|
||||
|
||||
// Try diagonals
|
||||
foreach (int x in directions)
|
||||
{
|
||||
foreach (int z in directions)
|
||||
{
|
||||
Vector3 testPos = new Vector3(startPos.x + (x * 5), 0, startPos.z + (z * 5));
|
||||
if (IsValidPos(testPos))
|
||||
return testPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If all else fails, return a position at minimum distance
|
||||
return new Vector3(startPos.x + (minGridDistance * 5), 0, startPos.z);
|
||||
}
|
||||
|
||||
private bool IsValidPos(Vector3 pos)
|
||||
private Vector3 GetGridPosition()
|
||||
{
|
||||
foreach (Vector3 roomPos in roomPositions)
|
||||
Vector3 lastRoomPos = roomPositions[roomPositions.Count - 1];
|
||||
|
||||
// Convert grid units to world position
|
||||
Vector3 roomPos = new Vector3(
|
||||
lastRoomPos.x + RoomDistance,
|
||||
lastRoomPos.y,
|
||||
lastRoomPos.z
|
||||
);
|
||||
|
||||
|
||||
return roomPos;
|
||||
}
|
||||
|
||||
private void CreateCorridor(Vector3 start, Vector3 end)
|
||||
{
|
||||
// Calculate the distance
|
||||
float distance = Vector3.Distance(start, end);
|
||||
PrefabSize corridorSize = CorridorStraight.GetComponent<PrefabSize>();
|
||||
|
||||
// Calculate the number of corridors needed
|
||||
int corridorCount = Mathf.FloorToInt(distance / corridorSize.prefabSize.x);
|
||||
Debug.Log($"Creating {corridorCount} corridors from {start} to {end}");
|
||||
|
||||
// Create corridors
|
||||
for (int i = 0; i < corridorCount; i++)
|
||||
{
|
||||
if (Vector3.Distance(pos, roomPos) < minRoomDistance)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Vector3 pos = new Vector3(
|
||||
start.x + i * corridorSize.prefabSize.x + corridorSize.prefabSize.x * 0.5f,
|
||||
start.y,
|
||||
start.z
|
||||
);
|
||||
Quaternion rotation = Quaternion.Euler(0, 90, 0);
|
||||
GameObject corridor = Instantiate(CorridorStraight, pos, rotation, transform);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user