connect multiple rooms with doors
This commit is contained in:
@@ -1,20 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Escape_Room_Engine.Engine.Scripts.Modules;
|
||||
using UnityEngine;
|
||||
using Logger = Escape_Room_Engine.Engine.Scripts.Utilities.Logger;
|
||||
using LogType = Escape_Room_Engine.Engine.Scripts.Utilities.LogType;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Escape_Room_Engine.Engine.Scripts
|
||||
{
|
||||
public class Engine : MonoBehaviour
|
||||
{
|
||||
public static Engine DefaultEngine => FindObjectOfType<Engine>();
|
||||
public static Engine DefaultEngine
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_foundEngine == null)
|
||||
{
|
||||
_foundEngine = FindObjectOfType<Engine>();
|
||||
}
|
||||
return _foundEngine;
|
||||
}
|
||||
}
|
||||
private static Engine _foundEngine;
|
||||
|
||||
public Material roomMaterial;
|
||||
|
||||
[Tooltip("The minimum size that should be allowed for rooms.")]
|
||||
public Vector2Int minRoomSize;
|
||||
[Tooltip("The minimum size that should be allowed for rooms.")] public Vector2Int minRoomSize;
|
||||
[Tooltip("The size of the physical play space available to the engine.")] public Vector2Int playSpace;
|
||||
|
||||
public int NumberOfRooms => _rooms.Count;
|
||||
private int NumberOfRooms => _rooms.Count;
|
||||
|
||||
private readonly List<Room> _rooms = new(1);
|
||||
private GameObject _playSpaceOrigin;
|
||||
@@ -28,8 +42,10 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
|
||||
public void GenerateRoom()
|
||||
{
|
||||
Logger.Log("Generating room...", LogType.RoomGeneration);
|
||||
|
||||
// get the last entrance from the newest room or create a spawn passage with no entrance door for where the player will start
|
||||
var entrance = NumberOfRooms > 0 ? _rooms[0].exit : new Passage();
|
||||
var entrance = NumberOfRooms > 0 ? _rooms[0].exit : new SpawnPassage();
|
||||
|
||||
var room = new Room(entrance);
|
||||
_rooms.Add(room);
|
||||
@@ -38,20 +54,63 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
room.InstantiateRoom(_playSpaceOrigin.transform, (_rooms.Count - 1).ToString());
|
||||
}
|
||||
|
||||
private void GenerateSpace(Room room, Passage from)
|
||||
private void GenerateSpace(Room room, Passage entrance)
|
||||
{
|
||||
var exit = new Passage();
|
||||
var space = new Space(GenerateSpaceDimensions(), from, exit);
|
||||
Logger.Log("Generating space...", LogType.RoomGeneration);
|
||||
|
||||
// create space
|
||||
var rrDimensions = GenerateSpaceDimensions(entrance.rrPosition);
|
||||
var space = new Space(rrDimensions, entrance);
|
||||
|
||||
// add exit
|
||||
var exitDoor = new DoorModule(DoorType.Exit, space);
|
||||
exitDoor.Place(new Vector2Int(
|
||||
Random.Range(0, rrDimensions.width),
|
||||
Random.Range(0, rrDimensions.length)
|
||||
));
|
||||
var exit = new Passage(exitDoor);
|
||||
space.AddModule(exitDoor);
|
||||
|
||||
room.AddSpace(space, exit);
|
||||
}
|
||||
|
||||
private Dimensions GenerateSpaceDimensions()
|
||||
/// <summary>
|
||||
/// Generate space dimensions that fit the required size constraints and cover the position of the entrance.
|
||||
/// </summary>
|
||||
/// <param name="entranceRrPosition">The room relative (<i>RR</i>) position of the entrance.</param>
|
||||
/// <returns>The generated room relative (<i>RR</i>) dimensions.</returns>
|
||||
private Dimensions GenerateSpaceDimensions(Vector2Int entranceRrPosition)
|
||||
{
|
||||
var xMin = Random.Range(0, playSpace.x - minRoomSize.x + 1);
|
||||
var xMax = Random.Range(xMin + minRoomSize.x, playSpace.x + 1);
|
||||
var yMin = Random.Range(0, playSpace.y - minRoomSize.y + 1);
|
||||
var yMax = Random.Range(yMin + minRoomSize.y, playSpace.y + 1);
|
||||
return new Dimensions(xMax - xMin, yMax - yMin, xMin, yMin);
|
||||
var xMin = Utilities.Utilities.RandomInclusive(
|
||||
0,
|
||||
Math.Min(entranceRrPosition.x, playSpace.x - minRoomSize.x)
|
||||
);
|
||||
var xMax = Utilities.Utilities.RandomInclusive(
|
||||
Math.Max(entranceRrPosition.x, xMin + minRoomSize.x),
|
||||
playSpace.x
|
||||
);
|
||||
var zMin = Utilities.Utilities.RandomInclusive(
|
||||
0,
|
||||
Math.Min(entranceRrPosition.y, playSpace.y - minRoomSize.y)
|
||||
);
|
||||
var zMax = Utilities.Utilities.RandomInclusive(
|
||||
Math.Max(entranceRrPosition.y, zMin + minRoomSize.y),
|
||||
playSpace.y
|
||||
);
|
||||
|
||||
var dimensions = new Dimensions(xMax - xMin, zMax - zMin, xMin, zMin);
|
||||
|
||||
Logger.Log($"Generated space dimensions {dimensions}", LogType.RoomGeneration);
|
||||
|
||||
return dimensions;
|
||||
}
|
||||
|
||||
public void HideOldestRoom()
|
||||
{
|
||||
if (NumberOfRooms > 0)
|
||||
{
|
||||
_rooms[NumberOfRooms - 1].roomObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user