93 lines
2.5 KiB
C#
93 lines
2.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using EscapeRoomEngine.Engine.Runtime.Modules;
|
|
using UnityEngine;
|
|
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
|
|
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;
|
|
|
|
namespace EscapeRoomEngine.Engine.Runtime
|
|
{
|
|
public class Room
|
|
{
|
|
internal Passage entrance, exit;
|
|
internal GameObject roomObject;
|
|
|
|
private readonly List<Space> _spaces = new();
|
|
private readonly List<PuzzleModule> _puzzles = new();
|
|
private readonly List<DoorModule> _doors = new();
|
|
|
|
internal Room(Passage entrance)
|
|
{
|
|
this.entrance = entrance;
|
|
}
|
|
|
|
internal void AddSpace(Space space, Passage spaceExit)
|
|
{
|
|
_spaces.Add(space);
|
|
exit = spaceExit;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Solves all puzzles in this room.
|
|
/// </summary>
|
|
public void SkipRoom()
|
|
{
|
|
Logger.Log($"Skipping {this}...", LogType.PuzzleFlow);
|
|
|
|
_puzzles.ForEach(puzzle => puzzle.PuzzleState.Solve());
|
|
|
|
if (_puzzles.Count == 0)
|
|
{
|
|
exit.fromOut.DoorState.Unlock();
|
|
}
|
|
}
|
|
|
|
internal void AddPuzzle(PuzzleModule puzzle)
|
|
{
|
|
_puzzles.Add(puzzle);
|
|
puzzle.PuzzleState.PuzzleEvent += OnPuzzleEvent;
|
|
}
|
|
|
|
private void OnPuzzleEvent(PuzzleModule puzzle, PuzzleEventType type)
|
|
{
|
|
if (type == PuzzleEventType.Solved)
|
|
{
|
|
if (_puzzles.All(p => p.PuzzleState.Solved))
|
|
{
|
|
exit.fromOut.DoorState.Unlock();
|
|
}
|
|
}
|
|
}
|
|
|
|
internal void AddDoor(DoorModule door)
|
|
{
|
|
_doors.Add(door);
|
|
door.DoorState.DoorEvent += OnDoorEvent;
|
|
}
|
|
|
|
private void OnDoorEvent(DoorModule door, DoorEventType type)
|
|
{
|
|
if (type == DoorEventType.Unlocked && door.Equals(exit.fromOut))
|
|
{
|
|
Engine.DefaultEngine.GenerateRoom();
|
|
}
|
|
}
|
|
|
|
internal void InstantiateRoom(Transform parent, string name)
|
|
{
|
|
roomObject = new GameObject($"Room {name}");
|
|
roomObject.transform.SetParent(parent, false);
|
|
|
|
for (var i = 0; i < _spaces.Count; i++)
|
|
{
|
|
_spaces[i].InstantiateSpace(roomObject.transform, i.ToString());
|
|
}
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return roomObject.name;
|
|
}
|
|
}
|
|
}
|