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-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-24 16:59:41 +02:00
|
|
|
[SerializeField] private int RoomDistance = 5;
|
|
|
|
|
[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-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Instantiate the player at the starting position
|
2025-06-23 16:30:18 +02:00
|
|
|
GameObject player = Instantiate(Player, new Vector3(startPos.x, 1, startPos.z), 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++)
|
|
|
|
|
{
|
2025-06-24 16:59:41 +02:00
|
|
|
Vector3 roomPos = GetGridPosition();
|
2025-06-23 16:30:18 +02:00
|
|
|
GameObject roomPrefab = mapPrefab[Random.Range(0, mapPrefab.Count)];
|
|
|
|
|
GameObject room = Instantiate(roomPrefab, roomPos, Quaternion.identity, transform);
|
|
|
|
|
placedRooms.Add(room);
|
|
|
|
|
roomPositions.Add(roomPos);
|
|
|
|
|
}
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Add End Point
|
|
|
|
|
GameObject endPoint = Instantiate(EndPoint, GetGridPosition(), Quaternion.identity, transform);
|
2025-06-23 16:30:18 +02:00
|
|
|
roomPositions.Add(endPoint.transform.position);
|
|
|
|
|
placedRooms.Add(endPoint);
|
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(
|
|
|
|
|
startRoomPos.x + startRoom.prefabSize.x / 2,
|
|
|
|
|
startRoomPos.y,
|
|
|
|
|
startRoomPos.z
|
|
|
|
|
);
|
|
|
|
|
Vector3 lastCorridorPos = new Vector3(
|
|
|
|
|
endRoomPos.x - endRoom.prefabSize.x / 2,
|
|
|
|
|
endRoomPos.y,
|
|
|
|
|
endRoomPos.z
|
|
|
|
|
);
|
|
|
|
|
CreateCorridor(firstCorridorPos, lastCorridorPos);
|
2025-06-23 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
private Vector3 GetGridPosition()
|
2025-06-23 16:30:18 +02:00
|
|
|
{
|
|
|
|
|
Vector3 lastRoomPos = roomPositions[roomPositions.Count - 1];
|
2025-06-24 16:59:41 +02:00
|
|
|
|
|
|
|
|
// Convert grid units to world position
|
|
|
|
|
Vector3 roomPos = new Vector3(
|
|
|
|
|
lastRoomPos.x + RoomDistance,
|
|
|
|
|
lastRoomPos.y,
|
|
|
|
|
lastRoomPos.z
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
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>();
|
|
|
|
|
|
|
|
|
|
// 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++)
|
2025-06-23 16:30:18 +02:00
|
|
|
{
|
2025-06-24 16:59:41 +02:00
|
|
|
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);
|
2025-06-23 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|