2025-06-23 16:30:18 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
public class MapGenManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[Header("Room Prefabs")]
|
|
|
|
|
[SerializeField] private List<GameObject> mapPrefab = new List<GameObject>();
|
|
|
|
|
[SerializeField] private GameObject StartPoint;
|
|
|
|
|
[SerializeField] private GameObject EndPoint;
|
2025-06-24 16:59:41 +02:00
|
|
|
|
2025-06-23 16:30:18 +02:00
|
|
|
[Header("Player")]
|
|
|
|
|
[SerializeField] private GameObject Player;
|
2025-06-24 16:59:41 +02:00
|
|
|
|
2025-06-23 16:30:18 +02:00
|
|
|
[Header("Corridor Prefabs")]
|
|
|
|
|
[SerializeField] private GameObject CorridorStraight;
|
2025-06-25 11:13:05 +02:00
|
|
|
[SerializeField] private GameObject CorridorStraightUnlit;
|
|
|
|
|
[SerializeField] private GameObject DoorCorridor;
|
2025-06-24 16:59:41 +02:00
|
|
|
/*[SerializeField] private GameObject CorridorL;
|
2025-06-23 16:30:18 +02:00
|
|
|
[SerializeField] private GameObject CorridorT;
|
|
|
|
|
[SerializeField] private GameObject CorridorCross;
|
2025-06-24 16:59:41 +02:00
|
|
|
[SerializeField] private GameObject CorridorEnd;*/
|
|
|
|
|
|
2025-06-23 16:30:18 +02:00
|
|
|
[Header("Generation Settings")]
|
2025-06-25 11:13:05 +02:00
|
|
|
[SerializeField] private int RoomDistance = 3;
|
2025-06-24 16:59:41 +02:00
|
|
|
[SerializeField] private int minRoomsNumber = 3;
|
|
|
|
|
[SerializeField] private int maxRoomsNumber = 7;
|
2025-06-23 16:30:18 +02:00
|
|
|
|
|
|
|
|
private List<Vector3> roomPositions = new List<Vector3>();
|
|
|
|
|
private List<GameObject> placedRooms = new List<GameObject>();
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
MapGen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MapGen()
|
|
|
|
|
{
|
2025-06-24 16:59:41 +02:00
|
|
|
// Clear previous rooms and positions
|
2025-06-23 16:30:18 +02:00
|
|
|
roomPositions.Clear();
|
|
|
|
|
placedRooms.Clear();
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Add Start Point
|
|
|
|
|
Vector3 startPos = new Vector3(0, 0, 0);
|
2025-06-23 16:30:18 +02:00
|
|
|
GameObject startPoint = Instantiate(StartPoint, startPos, Quaternion.identity, transform);
|
|
|
|
|
roomPositions.Add(startPos);
|
|
|
|
|
placedRooms.Add(startPoint);
|
2025-06-25 13:19:20 +02:00
|
|
|
startPoint.GetComponent<CreateEntrances>().SetEntrances(false, false, true, false);
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Instantiate the player at the starting position
|
2025-06-25 15:12:15 +02:00
|
|
|
GameObject player = Instantiate(Player, new Vector3(startPos.x, 1, startPos.z - 5), Quaternion.identity, transform);
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Generate a random number of rooms
|
|
|
|
|
int roomCount = Random.Range(minRoomsNumber, maxRoomsNumber);
|
|
|
|
|
|
|
|
|
|
// Place Generate Rooms
|
2025-06-23 16:30:18 +02:00
|
|
|
for (int i = 0; i < roomCount; i++)
|
|
|
|
|
{
|
|
|
|
|
GameObject roomPrefab = mapPrefab[Random.Range(0, mapPrefab.Count)];
|
2025-06-25 11:13:05 +02:00
|
|
|
Vector3 roomPos = GetGridPosition(roomPrefab);
|
2025-06-23 16:30:18 +02:00
|
|
|
GameObject room = Instantiate(roomPrefab, roomPos, Quaternion.identity, transform);
|
|
|
|
|
placedRooms.Add(room);
|
|
|
|
|
roomPositions.Add(roomPos);
|
2025-06-25 13:19:20 +02:00
|
|
|
room.GetComponent<CreateEntrances>().SetEntrances(false, false, true, true);
|
2025-06-23 16:30:18 +02:00
|
|
|
}
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Add End Point
|
2025-06-25 11:13:05 +02:00
|
|
|
GameObject endPoint = Instantiate(EndPoint, GetGridPosition(EndPoint), Quaternion.identity, transform);
|
2025-06-23 16:30:18 +02:00
|
|
|
roomPositions.Add(endPoint.transform.position);
|
|
|
|
|
placedRooms.Add(endPoint);
|
2025-06-25 13:19:20 +02:00
|
|
|
endPoint.GetComponent<CreateEntrances>().SetEntrances(false, false, false, false);
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Create corridors between rooms
|
|
|
|
|
for (int i = 0; i < roomPositions.Count - 1; i++)
|
|
|
|
|
{
|
|
|
|
|
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(
|
2025-06-25 11:13:05 +02:00
|
|
|
startRoomPos.x,
|
2025-06-24 16:59:41 +02:00
|
|
|
startRoomPos.y,
|
2025-06-25 11:13:05 +02:00
|
|
|
startRoomPos.z + startRoom.prefabSize.y / 2
|
2025-06-24 16:59:41 +02:00
|
|
|
);
|
|
|
|
|
Vector3 lastCorridorPos = new Vector3(
|
2025-06-25 11:13:05 +02:00
|
|
|
endRoomPos.x,
|
2025-06-24 16:59:41 +02:00
|
|
|
endRoomPos.y,
|
2025-06-25 11:13:05 +02:00
|
|
|
endRoomPos.z - endRoom.prefabSize.y / 2
|
2025-06-24 16:59:41 +02:00
|
|
|
);
|
|
|
|
|
CreateCorridor(firstCorridorPos, lastCorridorPos);
|
2025-06-23 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-24 16:59:41 +02:00
|
|
|
|
2025-06-25 11:13:05 +02:00
|
|
|
private Vector3 GetGridPosition(GameObject roomPrefab)
|
2025-06-23 16:30:18 +02:00
|
|
|
{
|
|
|
|
|
Vector3 lastRoomPos = roomPositions[roomPositions.Count - 1];
|
2025-06-25 11:13:05 +02:00
|
|
|
PrefabSize lastRoom = placedRooms[placedRooms.Count - 1].GetComponent<PrefabSize>();
|
|
|
|
|
PrefabSize roomSize = roomPrefab.GetComponent<PrefabSize>();
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
Vector3 roomPos = new Vector3(
|
2025-06-25 11:13:05 +02:00
|
|
|
lastRoomPos.x,
|
2025-06-25 13:19:20 +02:00
|
|
|
lastRoomPos.y - 0.01f,
|
2025-06-25 11:13:05 +02:00
|
|
|
lastRoomPos.z + lastRoom.prefabSize.y / 2f + roomSize.prefabSize.y / 2f + RoomDistance
|
2025-06-24 16:59:41 +02:00
|
|
|
);
|
|
|
|
|
|
2025-06-23 16:30:18 +02:00
|
|
|
return roomPos;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 16:59:41 +02:00
|
|
|
private void CreateCorridor(Vector3 start, Vector3 end)
|
2025-06-23 16:30:18 +02:00
|
|
|
{
|
2025-06-24 16:59:41 +02:00
|
|
|
// Calculate the distance
|
|
|
|
|
float distance = Vector3.Distance(start, end);
|
|
|
|
|
PrefabSize corridorSize = CorridorStraight.GetComponent<PrefabSize>();
|
2025-06-25 11:13:05 +02:00
|
|
|
PrefabSize corridorUnlitSize = CorridorStraightUnlit.GetComponent<PrefabSize>();
|
|
|
|
|
PrefabSize doorSize = DoorCorridor.GetComponent<PrefabSize>();
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Calculate the number of corridors needed
|
2025-06-25 11:13:05 +02:00
|
|
|
int corridorCount = Mathf.FloorToInt(distance / corridorSize.prefabSize.y);
|
2025-06-24 16:59:41 +02:00
|
|
|
Debug.Log($"Creating {corridorCount} corridors from {start} to {end}");
|
|
|
|
|
|
|
|
|
|
// Create corridors
|
2025-06-25 11:13:05 +02:00
|
|
|
//Start with door
|
|
|
|
|
if (corridorCount > 0)
|
2025-06-23 16:30:18 +02:00
|
|
|
{
|
2025-06-25 11:13:05 +02:00
|
|
|
Vector3 doorPos = new Vector3(
|
|
|
|
|
start.x,
|
2025-06-24 16:59:41 +02:00
|
|
|
start.y,
|
2025-06-25 11:13:05 +02:00
|
|
|
start.z + doorSize.prefabSize.y * 0.5f
|
2025-06-24 16:59:41 +02:00
|
|
|
);
|
2025-06-25 11:13:05 +02:00
|
|
|
Quaternion doorRotation = Quaternion.Euler(0, 0, 0);
|
|
|
|
|
GameObject doorCorridor = Instantiate(DoorCorridor, doorPos, doorRotation, transform);
|
2025-06-25 13:19:20 +02:00
|
|
|
doorCorridor.GetComponent<DoorController>().OpenDoor();
|
2025-06-25 11:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < corridorCount; i++)
|
|
|
|
|
{
|
2025-06-25 13:19:20 +02:00
|
|
|
if (i % 2 != 0)
|
2025-06-25 11:13:05 +02:00
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-23 16:30:18 +02:00
|
|
|
}
|
2025-06-25 11:13:05 +02:00
|
|
|
// If there is not enough space to create corridors, throw an exception
|
|
|
|
|
else throw new System.Exception("Not enough space to create corridors");
|
2025-06-23 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
}
|