From 807eae1c629160c2e487554af0c4b24dcdcca273 Mon Sep 17 00:00:00 2001 From: milan Date: Thu, 3 Nov 2022 21:20:00 +0100 Subject: [PATCH] connect multiple rooms with doors --- .../Engine/Scripts/Dimensions.cs | 48 +++++++--- .../Engine/Scripts/DoorModule.cs | 18 ---- .../Engine/Scripts/Editor/EngineEditor.cs | 8 +- .../Engine/Scripts/Engine.cs | 87 ++++++++++++++++--- .../Engine/Scripts/Module.cs | 14 --- .../Engine/Scripts/ModuleType.cs | 7 -- .../Engine/Scripts/Modules.meta | 3 + .../Engine/Scripts/Modules/DoorModule.cs | 26 ++++++ .../Scripts/{ => Modules}/DoorModule.cs.meta | 0 .../Engine/Scripts/Modules/Module.cs | 68 +++++++++++++++ .../Scripts/{ => Modules}/Module.cs.meta | 0 .../Engine/Scripts/Modules/ModuleType.cs | 7 ++ .../Scripts/{ => Modules}/ModuleType.cs.meta | 0 .../Engine/Scripts/Passage.cs | 34 +++++++- .../Escape Room Engine/Engine/Scripts/Room.cs | 10 +-- .../Engine/Scripts/Space.cs | 39 ++++++--- .../Engine/Scripts/SpawnPassage.cs | 23 +++++ .../Engine/Scripts/SpawnPassage.cs.meta | 3 + .../Engine/Scripts/Utilities.meta | 3 + .../Engine/Scripts/Utilities/Logger.cs | 37 ++++++++ .../Engine/Scripts/Utilities/Logger.cs.meta | 3 + .../Engine/Scripts/Utilities/Utilities.cs | 23 +++++ .../Scripts/Utilities/Utilities.cs.meta | 3 + Assets/Scenes/TestScene.unity | 15 ++++ 24 files changed, 391 insertions(+), 88 deletions(-) delete mode 100644 Assets/Escape Room Engine/Engine/Scripts/DoorModule.cs delete mode 100644 Assets/Escape Room Engine/Engine/Scripts/Module.cs delete mode 100644 Assets/Escape Room Engine/Engine/Scripts/ModuleType.cs create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Modules.meta create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Modules/DoorModule.cs rename Assets/Escape Room Engine/Engine/Scripts/{ => Modules}/DoorModule.cs.meta (100%) create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Modules/Module.cs rename Assets/Escape Room Engine/Engine/Scripts/{ => Modules}/Module.cs.meta (100%) create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Modules/ModuleType.cs rename Assets/Escape Room Engine/Engine/Scripts/{ => Modules}/ModuleType.cs.meta (100%) create mode 100644 Assets/Escape Room Engine/Engine/Scripts/SpawnPassage.cs create mode 100644 Assets/Escape Room Engine/Engine/Scripts/SpawnPassage.cs.meta create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Utilities.meta create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Utilities/Logger.cs create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Utilities/Logger.cs.meta create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Utilities/Utilities.cs create mode 100644 Assets/Escape Room Engine/Engine/Scripts/Utilities/Utilities.cs.meta diff --git a/Assets/Escape Room Engine/Engine/Scripts/Dimensions.cs b/Assets/Escape Room Engine/Engine/Scripts/Dimensions.cs index b44c518..d405985 100644 --- a/Assets/Escape Room Engine/Engine/Scripts/Dimensions.cs +++ b/Assets/Escape Room Engine/Engine/Scripts/Dimensions.cs @@ -1,20 +1,44 @@ -namespace Escape_Room_Engine.Engine.Scripts +using UnityEngine; + +namespace Escape_Room_Engine.Engine.Scripts { - public readonly struct Dimensions + public struct Dimensions { - internal Dimensions(float width, float length, float x, float y) + internal Dimensions(int width, int length, int x, int z) { - Width = width; - Length = length; - X = x; - Y = y; + this.width = width; + this.length = length; + this.x = x; + this.z = z; } - public float Width { get; } - public float Length { get; } - public float X { get; } - public float Y { get; } + public int width; + public int length; + public int x; + public int z; - public override string ToString() => $"({Width}, {Length}) at ({X}, {Y})"; + public Vector2Int Position + { + get => new(x, z); + set + { + x = value.x; + z = value.y; + } + } + + public Vector2Int Size + { + get => new(width, length); + set + { + width = value.x; + length = value.y; + } + } + + public int Area => width * length; + + public override string ToString() => $"({width}, {length}) at ({x}, {z})"; } } \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/DoorModule.cs b/Assets/Escape Room Engine/Engine/Scripts/DoorModule.cs deleted file mode 100644 index ac806ff..0000000 --- a/Assets/Escape Room Engine/Engine/Scripts/DoorModule.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Escape_Room_Engine.Engine.Scripts -{ - public enum DoorType - { - Entrance = ModuleType.DoorEntrance, Exit = ModuleType.DoorExit - } - - public class DoorModule : Module - { - public bool IsEntrance => IsType((ModuleType)DoorType.Entrance); - public bool IsExit => IsType((ModuleType)DoorType.Exit); - - internal DoorModule(DoorType type) - { - _types.Add((ModuleType)type); - } - } -} \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Editor/EngineEditor.cs b/Assets/Escape Room Engine/Engine/Scripts/Editor/EngineEditor.cs index 12d4160..1ae661f 100644 --- a/Assets/Escape Room Engine/Engine/Scripts/Editor/EngineEditor.cs +++ b/Assets/Escape Room Engine/Engine/Scripts/Editor/EngineEditor.cs @@ -17,7 +17,10 @@ namespace Escape_Room_Engine.Engine.Scripts.Editor public void CreateGUI() { - _generateRoomButton = new Button(GenerateRoom); + _generateRoomButton = new Button(GenerateRoom) + { + text = "Generate Room" + }; rootVisualElement.Add(_generateRoomButton); @@ -29,7 +32,7 @@ namespace Escape_Room_Engine.Engine.Scripts.Editor { if (EditorApplication.isPlaying) { - Engine.DefaultEngine.DisposeOldestRoom(); + Engine.DefaultEngine.HideOldestRoom(); Engine.DefaultEngine.GenerateRoom(); UpdateUI(); } @@ -38,7 +41,6 @@ namespace Escape_Room_Engine.Engine.Scripts.Editor private void UpdateUI() { _generateRoomButton.SetEnabled(EditorApplication.isPlaying); - _generateRoomButton.text = Engine.DefaultEngine.NumberOfRooms == 0 ? "Generate Room" : "Regenerate Room"; } } } \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Engine.cs b/Assets/Escape Room Engine/Engine/Scripts/Engine.cs index 89f0886..67e0549 100644 --- a/Assets/Escape Room Engine/Engine/Scripts/Engine.cs +++ b/Assets/Escape Room Engine/Engine/Scripts/Engine.cs @@ -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(); + public static Engine DefaultEngine + { + get + { + if (_foundEngine == null) + { + _foundEngine = FindObjectOfType(); + } + 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 _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() + /// + /// Generate space dimensions that fit the required size constraints and cover the position of the entrance. + /// + /// The room relative (RR) position of the entrance. + /// The generated room relative (RR) dimensions. + 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); + } } /// diff --git a/Assets/Escape Room Engine/Engine/Scripts/Module.cs b/Assets/Escape Room Engine/Engine/Scripts/Module.cs deleted file mode 100644 index aa361a2..0000000 --- a/Assets/Escape Room Engine/Engine/Scripts/Module.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Collections.Generic; - -namespace Escape_Room_Engine.Engine.Scripts -{ - public class Module - { - protected List _types = new(); - - public bool IsType(ModuleType type) - { - return _types.Contains(type); - } - } -} diff --git a/Assets/Escape Room Engine/Engine/Scripts/ModuleType.cs b/Assets/Escape Room Engine/Engine/Scripts/ModuleType.cs deleted file mode 100644 index 9c1e42e..0000000 --- a/Assets/Escape Room Engine/Engine/Scripts/ModuleType.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Escape_Room_Engine.Engine.Scripts -{ - public enum ModuleType - { - DoorEntrance, DoorExit - } -} \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Modules.meta b/Assets/Escape Room Engine/Engine/Scripts/Modules.meta new file mode 100644 index 0000000..64f5d17 --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Modules.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cee3c3328d46482ba3f565eeee492f59 +timeCreated: 1667812846 \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Modules/DoorModule.cs b/Assets/Escape Room Engine/Engine/Scripts/Modules/DoorModule.cs new file mode 100644 index 0000000..b28b47b --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Modules/DoorModule.cs @@ -0,0 +1,26 @@ +using UnityEngine; + +namespace Escape_Room_Engine.Engine.Scripts.Modules +{ + public enum DoorType + { + Entrance = ModuleType.DoorEntrance, Exit = ModuleType.DoorExit + } + + public class DoorModule : Module + { + public bool IsEntrance => IsType((ModuleType)DoorType.Entrance); + public bool IsExit => IsType((ModuleType)DoorType.Exit); + + internal DoorModule(DoorType type, Space space) : base(space) + { + types.Add((ModuleType)type); + srDimensions.Size = Vector2Int.one; // door always has size 1x1 + } + + public override string ToString() + { + return $"{(IsEntrance ? "Entrance" : IsExit ? "Exit" : "Unknown")} door"; + } + } +} \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/DoorModule.cs.meta b/Assets/Escape Room Engine/Engine/Scripts/Modules/DoorModule.cs.meta similarity index 100% rename from Assets/Escape Room Engine/Engine/Scripts/DoorModule.cs.meta rename to Assets/Escape Room Engine/Engine/Scripts/Modules/DoorModule.cs.meta diff --git a/Assets/Escape Room Engine/Engine/Scripts/Modules/Module.cs b/Assets/Escape Room Engine/Engine/Scripts/Modules/Module.cs new file mode 100644 index 0000000..0abebf2 --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Modules/Module.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +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; + +namespace Escape_Room_Engine.Engine.Scripts.Modules +{ + public class Module + { + /// + /// Get the space relative (SR) position of this module. + /// + internal Vector2Int SrPosition => srDimensions.Position; + /// + /// Get the room relative (RR) position of this module. + /// + internal Vector2Int RrPosition => space.ToRoomRelative(SrPosition); + + internal readonly Space space; + + protected GameObject _moduleObject; + protected readonly List types = new(); + /// + /// The space relative (SR) dimensions of this module. + /// + protected Dimensions srDimensions; + + internal Module(Space space) + { + this.space = space; + } + + internal bool IsType(ModuleType type) + { + return types.Contains(type); + } + + + /// + /// Place this module with a position relative to the room. + /// + /// The room relative (RR) position of this module. Must be inside the space dimensions. + /// If the position is not inside the space dimensions. + internal void PlaceRoomRelative(Vector2Int rrPosition) => Place(space.ToSpaceRelative(rrPosition)); + /// + /// Place this module with a position relative to the space it is in. + /// + /// The space relative (SR) position of this module. Must be inside the space dimensions. + /// If the position is not inside the space dimensions. + internal void Place(Vector2Int srPosition) { + if (space != null && !srPosition.IsInsideRelative(space.rrDimensions)) + { + throw new Exception($"Trying to place {this} at {srPosition}, which is outside space dimensions {space.rrDimensions}."); + } + + srDimensions.Position = srPosition; + + Logger.Log($"{this} has been placed at {srPosition} (SR)", LogType.ModulePlacement); + } + + public override string ToString() + { + return $"Module ({string.Join(',', types.ConvertAll(type => type.ToString()))})"; + } + } +} diff --git a/Assets/Escape Room Engine/Engine/Scripts/Module.cs.meta b/Assets/Escape Room Engine/Engine/Scripts/Modules/Module.cs.meta similarity index 100% rename from Assets/Escape Room Engine/Engine/Scripts/Module.cs.meta rename to Assets/Escape Room Engine/Engine/Scripts/Modules/Module.cs.meta diff --git a/Assets/Escape Room Engine/Engine/Scripts/Modules/ModuleType.cs b/Assets/Escape Room Engine/Engine/Scripts/Modules/ModuleType.cs new file mode 100644 index 0000000..0577219 --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Modules/ModuleType.cs @@ -0,0 +1,7 @@ +namespace Escape_Room_Engine.Engine.Scripts.Modules +{ + public enum ModuleType + { + DoorEntrance, DoorExit, // door types + } +} \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/ModuleType.cs.meta b/Assets/Escape Room Engine/Engine/Scripts/Modules/ModuleType.cs.meta similarity index 100% rename from Assets/Escape Room Engine/Engine/Scripts/ModuleType.cs.meta rename to Assets/Escape Room Engine/Engine/Scripts/Modules/ModuleType.cs.meta diff --git a/Assets/Escape Room Engine/Engine/Scripts/Passage.cs b/Assets/Escape Room Engine/Engine/Scripts/Passage.cs index 7bcb685..45ff311 100644 --- a/Assets/Escape Room Engine/Engine/Scripts/Passage.cs +++ b/Assets/Escape Room Engine/Engine/Scripts/Passage.cs @@ -1,17 +1,47 @@ -namespace Escape_Room_Engine.Engine.Scripts +using System; +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; + +namespace Escape_Room_Engine.Engine.Scripts { public class Passage { internal DoorModule fromOut, toIn; + /// + /// The room relative (RR) position of this passage. + /// + internal Vector2Int rrPosition; + + internal Passage(DoorModule from) + { + ConnectFrom(from); + } + + protected Passage() {} internal void ConnectFrom(DoorModule door) { fromOut = door; + rrPosition = fromOut.RrPosition; + + Logger.Log($"Connected passage from {door} at {rrPosition} (RR).", LogType.PassageConnection); } - internal void ConnectTo(DoorModule door) + internal virtual void ConnectTo(DoorModule door) { + if (fromOut == null) + { + throw new Exception("Cannot connect passage to entrance if it hasn't been connected to an exit first."); + } + toIn = door; + + // to make sure the origin of the player doesn't move, the two doors must be placed in the same location + toIn.PlaceRoomRelative(rrPosition); + + Logger.Log($"Connected passage to {door} at {rrPosition} (RR).", LogType.PassageConnection); } } } \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Room.cs b/Assets/Escape Room Engine/Engine/Scripts/Room.cs index 60c4c25..c099eac 100644 --- a/Assets/Escape Room Engine/Engine/Scripts/Room.cs +++ b/Assets/Escape Room Engine/Engine/Scripts/Room.cs @@ -6,8 +6,8 @@ namespace Escape_Room_Engine.Engine.Scripts public class Room { internal Passage entrance, exit; + internal GameObject roomObject; - private GameObject _roomObject; private List _spaces = new(); internal Room(Passage entrance) @@ -23,12 +23,12 @@ namespace Escape_Room_Engine.Engine.Scripts internal void InstantiateRoom(Transform parent, string name) { - _roomObject = new GameObject($"Room {name}"); - _roomObject.transform.SetParent(parent, false); + 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()); + _spaces[i].InstantiateSpace(roomObject.transform, i.ToString()); } } @@ -38,7 +38,7 @@ namespace Escape_Room_Engine.Engine.Scripts { space.Destroy(); } - Object.Destroy(_roomObject); + Object.Destroy(roomObject); } } } diff --git a/Assets/Escape Room Engine/Engine/Scripts/Space.cs b/Assets/Escape Room Engine/Engine/Scripts/Space.cs index c6c685c..a875015 100644 --- a/Assets/Escape Room Engine/Engine/Scripts/Space.cs +++ b/Assets/Escape Room Engine/Engine/Scripts/Space.cs @@ -1,26 +1,26 @@ using System.Collections.Generic; +using Escape_Room_Engine.Engine.Scripts.Modules; using UnityEngine; namespace Escape_Room_Engine.Engine.Scripts { public class Space { - // public DoorModule Entrance => (DoorModule)_modules.Find(module => module.IsType((ModuleType)DoorType.Entrance)); - // public DoorModule Exit => (DoorModule)_modules.Find(module => module.IsType((ModuleType)DoorType.Exit)); + /// + /// The room relative (RR) dimensions of this space. + /// + internal readonly Dimensions rrDimensions; - private readonly Dimensions _dimensions; private GameObject _spaceObject; private List _modules = new(2); - internal Space(Dimensions dimensions, Passage entrance, Passage exit) + internal Space(Dimensions rrDimensions, Passage entrance) { - _dimensions = dimensions; + this.rrDimensions = rrDimensions; - // connect the space to its passages - entrance.ConnectTo(new DoorModule(DoorType.Entrance)); + // connect the space to its passage + entrance.ConnectTo(new DoorModule(DoorType.Entrance, this)); AddModule(entrance.toIn); - exit.ConnectFrom(new DoorModule(DoorType.Exit)); - AddModule(exit.fromOut); } internal void AddModule(Module module) @@ -32,7 +32,7 @@ namespace Escape_Room_Engine.Engine.Scripts { _spaceObject = new GameObject($"Space {name}", typeof(MeshFilter), typeof(MeshRenderer)); _spaceObject.transform.SetParent(parent, false); - _spaceObject.transform.localPosition = new Vector3(_dimensions.X, 0, _dimensions.Y); + _spaceObject.transform.localPosition = new Vector3(rrDimensions.x, 0, rrDimensions.z); var meshFilter = _spaceObject.GetComponent(); meshFilter.mesh = GenerateMesh(); @@ -41,6 +41,19 @@ namespace Escape_Room_Engine.Engine.Scripts meshRenderer.material = Engine.DefaultEngine.roomMaterial; } + /// + /// Convert a position relative to this space to one relative to the room. + /// + /// The space relative (SR) position that should be converted to a room relative (RR) position. + /// + internal Vector2Int ToRoomRelative(Vector2Int srPosition) => srPosition + rrDimensions.Position; + /// + /// Convert a position relative to the room to one relative to this space. + /// + /// The room relative (RR) position that should be converted to a space relative (SR) position. + /// + internal Vector2Int ToSpaceRelative(Vector2Int rrPosition) => rrPosition - rrDimensions.Position; + private Mesh GenerateMesh() { var mesh = new Mesh(); @@ -48,9 +61,9 @@ namespace Escape_Room_Engine.Engine.Scripts mesh.vertices = new[] { new Vector3(0, 0, 0), - new Vector3(_dimensions.Width, 0, 0), - new Vector3(0, 0, _dimensions.Length), - new Vector3(_dimensions.Width, 0, _dimensions.Length) + new Vector3(rrDimensions.width, 0, 0), + new Vector3(0, 0, rrDimensions.length), + new Vector3(rrDimensions.width, 0, rrDimensions.length) }; mesh.triangles = new[] diff --git a/Assets/Escape Room Engine/Engine/Scripts/SpawnPassage.cs b/Assets/Escape Room Engine/Engine/Scripts/SpawnPassage.cs new file mode 100644 index 0000000..6975de7 --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/SpawnPassage.cs @@ -0,0 +1,23 @@ +using Escape_Room_Engine.Engine.Scripts.Modules; +using UnityEngine; + +namespace Escape_Room_Engine.Engine.Scripts +{ + /// + /// A SpawnPassage is a type of passage used solely for entering the very fist space. It always places the entrance door at (0, 0). + /// + public class SpawnPassage : Passage + { + internal SpawnPassage() {} + + internal override void ConnectTo(DoorModule door) + { + // this door module does not really exist, but is required since a passage must include an entry + // it also defines, where the exit will be placed (in this case always at the play space origin) + fromOut = new DoorModule(DoorType.Exit, null); + fromOut.Place(new Vector2Int(0, 0)); + + base.ConnectTo(door); + } + } +} \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/SpawnPassage.cs.meta b/Assets/Escape Room Engine/Engine/Scripts/SpawnPassage.cs.meta new file mode 100644 index 0000000..9447334 --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/SpawnPassage.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0a168ccfeb4e46b9a2c04442afef6c4c +timeCreated: 1667813431 \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Utilities.meta b/Assets/Escape Room Engine/Engine/Scripts/Utilities.meta new file mode 100644 index 0000000..20469cb --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Utilities.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ec385c62be0244e78c6a13de6c7df71f +timeCreated: 1667815385 \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Utilities/Logger.cs b/Assets/Escape Room Engine/Engine/Scripts/Utilities/Logger.cs new file mode 100644 index 0000000..f4eeda9 --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Utilities/Logger.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace Escape_Room_Engine.Engine.Scripts.Utilities +{ + public enum LogType + { + Important, ModulePlacement, PassageConnection, RoomGeneration + } + + public class Logger : MonoBehaviour + { + public static Logger DefaultLogger + { + get + { + if (_foundLogger == null) + { + _foundLogger = FindObjectOfType(); + } + return _foundLogger; + } + } + private static Logger _foundLogger; + + public bool loggingEnabled; + public List typeFilter; + + public static void Log(string message, LogType type = LogType.Important) + { + if (DefaultLogger.loggingEnabled && DefaultLogger.typeFilter.Contains(type)) + { + Debug.Log($"[{type}] {message}"); + } + } + } +} \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Utilities/Logger.cs.meta b/Assets/Escape Room Engine/Engine/Scripts/Utilities/Logger.cs.meta new file mode 100644 index 0000000..f29c9f1 --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Utilities/Logger.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: debf8d65bda642bc938a94c7639e01a4 +timeCreated: 1667815421 \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Utilities/Utilities.cs b/Assets/Escape Room Engine/Engine/Scripts/Utilities/Utilities.cs new file mode 100644 index 0000000..7d77017 --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Utilities/Utilities.cs @@ -0,0 +1,23 @@ +using UnityEngine; + +namespace Escape_Room_Engine.Engine.Scripts.Utilities +{ + public static class Utilities + { + #region Math + + public static int RandomInclusive(int from, int to) => Random.Range(from, to + 1); + + /// + /// Returns whether a position relative to some dimensions is inside those dimensions. + /// + /// The position to check, relative to the dimensions. + /// The dimensions to check the position against. + public static bool IsInsideRelative(this Vector2Int position, Dimensions dimensions) + { + return position.x >= 0 && position.y >= 0 && position.x < dimensions.width && position.y < dimensions.length; + } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/Escape Room Engine/Engine/Scripts/Utilities/Utilities.cs.meta b/Assets/Escape Room Engine/Engine/Scripts/Utilities/Utilities.cs.meta new file mode 100644 index 0000000..72df3af --- /dev/null +++ b/Assets/Escape Room Engine/Engine/Scripts/Utilities/Utilities.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 092cc048ba0e4bb7a7862083fb702884 +timeCreated: 1667812284 \ No newline at end of file diff --git a/Assets/Scenes/TestScene.unity b/Assets/Scenes/TestScene.unity index b7c2d5e..c3f528a 100644 --- a/Assets/Scenes/TestScene.unity +++ b/Assets/Scenes/TestScene.unity @@ -1212,6 +1212,7 @@ GameObject: m_Component: - component: {fileID: 1568048335} - component: {fileID: 1568048334} + - component: {fileID: 1568048336} m_Layer: 0 m_Name: Engine m_TagString: Untagged @@ -1249,6 +1250,20 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1568048336 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1568048333} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: debf8d65bda642bc938a94c7639e01a4, type: 3} + m_Name: + m_EditorClassIdentifier: + loggingEnabled: 1 + typeFilter: 00000000010000000200000003000000 --- !u!1 &1718957584 GameObject: m_ObjectHideFlags: 0