174 lines
6.9 KiB
C#
174 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Escape_Room_Engine.Engine.Scripts.Modules;
|
|
using Escape_Room_Engine.Engine.Scripts.Requirements;
|
|
using Escape_Room_Engine.Engine.Scripts.Utilities;
|
|
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
|
|
{
|
|
get
|
|
{
|
|
if (_foundEngine == null)
|
|
{
|
|
_foundEngine = FindObjectOfType<Engine>();
|
|
}
|
|
return _foundEngine;
|
|
}
|
|
}
|
|
private static Engine _foundEngine;
|
|
|
|
public Material roomMaterial;
|
|
|
|
[Range(0, 10)] public int minPuzzleCount, maxPuzzleCount;
|
|
[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 ModuleDescription genericModule;
|
|
public DoorModuleDescription spawnDoor;
|
|
public List<DoorModuleDescription> exitDoorTypes;
|
|
public List<PuzzleModuleDescription> puzzleTypes;
|
|
|
|
private int NumberOfRooms => _rooms.Count;
|
|
|
|
private readonly List<Room> _rooms = new(1);
|
|
private GameObject _playSpaceOrigin;
|
|
|
|
private void Start()
|
|
{
|
|
_playSpaceOrigin = new GameObject("Play Space Origin");
|
|
_playSpaceOrigin.transform.SetParent(transform);
|
|
_playSpaceOrigin.transform.localPosition = new Vector3(-playSpace.x / 2f, 0, -playSpace.y / 2f);
|
|
}
|
|
|
|
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.Last().exit : new Passage(new DoorModule(null, spawnDoor), true);
|
|
|
|
var room = new Room(entrance);
|
|
_rooms.Add(room);
|
|
|
|
GenerateSpace(room, entrance); // TODO: rooms with more than one space
|
|
|
|
room.InstantiateRoom(_playSpaceOrigin.transform, (_rooms.Count - 1).ToString());
|
|
}
|
|
|
|
private void GenerateSpace(Room room, Passage entrance)
|
|
{
|
|
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(space, exitDoorTypes.RandomElement());
|
|
exitDoor.Place(new Vector2Int(
|
|
Random.Range(0, rrDimensions.width),
|
|
Random.Range(0, rrDimensions.length)
|
|
));
|
|
var exit = new Passage(exitDoor);
|
|
space.AddModule(exitDoor);
|
|
|
|
// add puzzles
|
|
for (var i = 0; i < Utilities.Utilities.RandomInclusive(minPuzzleCount, maxPuzzleCount); i++)
|
|
{
|
|
GeneratePuzzle(space);
|
|
}
|
|
|
|
room.AddSpace(space, exit);
|
|
}
|
|
|
|
private void GeneratePuzzle(Space space)
|
|
{
|
|
var puzzle = new PuzzleModule(space, puzzleTypes.RandomElement());
|
|
|
|
// place puzzle
|
|
var placementCandidates = space.rrDimensions.EveryPosition;
|
|
puzzle._description.RequirementsOfType<PlacementRequirement>().ForEach(requirement =>
|
|
placementCandidates.IntersectWith(requirement.PlacementCandidates(puzzle, space)));
|
|
|
|
Logger.Log($"placement candidates: {string.Join(", ", placementCandidates.ToList().ConvertAll(c => c.ToString()))}", LogType.RequirementResolution);
|
|
|
|
if (placementCandidates.Count > 0)
|
|
{
|
|
puzzle.Place(placementCandidates.RandomElement());
|
|
}
|
|
else
|
|
{
|
|
Logger.Log("Could not find suitable placement for puzzle", LogType.PuzzleGeneration);
|
|
return;
|
|
}
|
|
|
|
// orient puzzle
|
|
HashSet<Orientation> orientationCandidates = Module.EveryOrientation;
|
|
Logger.Log($"orientation candidatesA: {string.Join(",", orientationCandidates.ToList().ConvertAll(c => c.ToString()))}", LogType.RequirementResolution);
|
|
puzzle._description.RequirementsOfType<OrientationRequirement>().ForEach(requirement =>
|
|
orientationCandidates.IntersectWith(requirement.OrientationCandidates(puzzle, space)));
|
|
|
|
Logger.Log($"orientation candidates: {string.Join(",", orientationCandidates.ToList().ConvertAll(c => c.ToString()))}", LogType.RequirementResolution);
|
|
|
|
if (orientationCandidates.Count > 0)
|
|
{
|
|
puzzle.Orient(orientationCandidates.RandomElement());
|
|
}
|
|
else
|
|
{
|
|
Logger.Log("Could not find suitable orientation for puzzle", LogType.PuzzleGeneration);
|
|
return;
|
|
}
|
|
|
|
space.AddModule(puzzle);
|
|
}
|
|
|
|
/// <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 = Utilities.Utilities.RandomInclusive(
|
|
0,
|
|
Math.Min(entranceRrPosition.x, playSpace.x - minRoomSize.x)
|
|
);
|
|
var xMax = Utilities.Utilities.RandomInclusive(
|
|
Math.Max(entranceRrPosition.x + 1, 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 + 1, zMin + minRoomSize.y),
|
|
playSpace.y
|
|
);
|
|
|
|
var dimensions = new Dimensions(xMax - xMin, zMax - zMin, xMin, zMin);
|
|
|
|
Logger.Log($"Generated space dimensions {dimensions} from entrance position {entranceRrPosition}", LogType.RoomGeneration);
|
|
|
|
return dimensions;
|
|
}
|
|
|
|
public void HideOldestRoom()
|
|
{
|
|
if (NumberOfRooms > 0)
|
|
{
|
|
_rooms[NumberOfRooms - 1].roomObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|