using System.Collections.Generic; using System.Linq; using EscapeRoomEngine.Engine.Runtime.Measurements; using EscapeRoomEngine.Engine.Runtime.Modules; using EscapeRoomEngine.Engine.Runtime.Utilities; using NaughtyAttributes; using UnityEngine; using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger; using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType; namespace EscapeRoomEngine.Engine.Runtime { public class Engine : MonoBehaviour { public static Engine DefaultEngine { get { if (_foundEngine == null) { _foundEngine = FindObjectOfType(); } return _foundEngine; } } private static Engine _foundEngine; public delegate void UpdateUIHandler(); public event UpdateUIHandler UpdateUIEvent; [InfoBox("If a space was generated without any puzzles in it, the engine will try generating another new space. To prevent infinite loops, the amount of retries is bound.")] public int maxSpaceGenerationTries; public Vector3 roomOffset; [Required] public EngineTheme theme; public int NumberOfRooms => _rooms.Count; public IOption CurrentRoom => NumberOfRooms > 0 ? Some.Of(_rooms[^1]) : None.New(); private readonly List _rooms = new(); private GameObject _playSpaceOrigin; private void Awake() { Measure.Clear(); } private void Start() { _playSpaceOrigin = new GameObject("Play Space Origin"); _playSpaceOrigin.transform.SetParent(transform); _playSpaceOrigin.transform.localPosition = new Vector3(-theme.playSpace.x / 2f, 0, -theme.playSpace.y / 2f); GenerateRoom(); } 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, theme.spawnDoor)); var room = new Room(entrance); _rooms.Add(room); GenerateSpace(room, entrance); var roomId = _rooms.Count - 1; room.InstantiateRoom(_playSpaceOrigin.transform, roomId * roomOffset, roomId.ToString()); if (theme.environment) { Instantiate(theme.environment, room.roomObject.transform, false); } UpdateUI(); } private void GenerateSpace(Room room, Passage entrance) { var puzzlesAdded = 0; var tries = 0; Space space; Passage exit; do { tries++; Logger.Log($"Generating space{(tries > 1 ? $" (try {tries})" : "")}...", LogType.RoomGeneration); // create space space = new Space(room, entrance); // add exit var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement()); if (!space.AddModuleWithRequirements(exitDoor)) throw new EngineException("Could not satisfy requirements for exit door."); exit = new Passage(exitDoor); // add puzzles var puzzleCount = Utilities.Utilities.RandomInclusive(theme.puzzleCount.x, theme.puzzleCount.y); for (var i = 0; i < puzzleCount; i++) { if (space.AddModuleWithRequirements(Module.CreateModuleByType(space, theme.puzzleTypes.RandomElement()))) { puzzlesAdded++; } } } while (puzzlesAdded == 0 && tries < maxSpaceGenerationTries); if (puzzlesAdded == 0) { Logger.Log($"Unable to create space with puzzles after {tries} tries", LogType.Important); } room.AddSpace(space, exit); } public void HidePreviousRoom(bool destroy = true) { if (NumberOfRooms > 2) { var room = _rooms[NumberOfRooms - 3]; // lock the doors that might be used to return to the old room room.exit.toIn.DoorState.Lock(); room.exit.fromOut.DoorState.Lock(); // destroy or hide the old room if (destroy) { Destroy(room.roomObject); } else { room.roomObject.SetActive(false); } } } private void UpdateUI() => UpdateUIEvent?.Invoke(); } }