split into multiple assemblies
This commit is contained in:
106
Assets/Engine/Runtime/Engine.cs
Normal file
106
Assets/Engine/Runtime/Engine.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
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<Engine>();
|
||||
}
|
||||
return _foundEngine;
|
||||
}
|
||||
}
|
||||
private static Engine _foundEngine;
|
||||
|
||||
public delegate void UpdateUIHandler();
|
||||
public event UpdateUIHandler UpdateUIEvent;
|
||||
|
||||
[Required] public EngineTheme theme;
|
||||
|
||||
public int NumberOfRooms => _rooms.Count;
|
||||
public IOption<Room> CurrentRoom => NumberOfRooms > 0 ? Some<Room>.Of(_rooms[^1]) : None<Room>.New();
|
||||
|
||||
private readonly List<Room> _rooms = new();
|
||||
private GameObject _playSpaceOrigin, _environment;
|
||||
|
||||
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);
|
||||
|
||||
if (theme.environment)
|
||||
{
|
||||
_environment = Instantiate(theme.environment, _playSpaceOrigin.transform, false);
|
||||
}
|
||||
|
||||
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), 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());
|
||||
|
||||
UpdateUI();
|
||||
}
|
||||
|
||||
private void GenerateSpace(Room room, Passage entrance)
|
||||
{
|
||||
Logger.Log("Generating space...", LogType.RoomGeneration);
|
||||
|
||||
// create space
|
||||
var space = new Space(room, entrance);
|
||||
|
||||
// add exit
|
||||
var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement());
|
||||
if (!space.AddModuleWithRequirements(exitDoor))
|
||||
throw new Exception("Could not satisfy requirements for exit door.");
|
||||
var exit = new Passage(exitDoor);
|
||||
|
||||
// add puzzles
|
||||
for (var i = 0; i < Utilities.Utilities.RandomInclusive(theme.puzzleCount.x, theme.puzzleCount.y); i++)
|
||||
{
|
||||
space.AddModuleWithRequirements(new PuzzleModule(space, theme.puzzleTypes.RandomElement()));
|
||||
}
|
||||
|
||||
room.AddSpace(space, exit);
|
||||
}
|
||||
|
||||
public void HidePreviousRoom()
|
||||
{
|
||||
if (NumberOfRooms > 1)
|
||||
{
|
||||
_rooms[NumberOfRooms - 2].roomObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateUI() => UpdateUIEvent?.Invoke();
|
||||
|
||||
// ReSharper disable once SuggestBaseTypeForParameter
|
||||
// ReSharper disable once UnusedMember.Local
|
||||
private bool IsNotEmpty(List<DoorModuleDescription> modules) => modules.Count > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user