generic doors

This commit is contained in:
2022-11-04 09:46:00 +01:00
parent 807eae1c62
commit ba4fa9430c
37 changed files with 1071 additions and 77 deletions

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Escape_Room_Engine.Engine.Scripts.Modules;
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;
@@ -27,6 +29,9 @@ namespace Escape_Room_Engine.Engine.Scripts
[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;
private int NumberOfRooms => _rooms.Count;
@@ -45,12 +50,13 @@ namespace Escape_Room_Engine.Engine.Scripts
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 SpawnPassage();
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());
}
@@ -63,7 +69,7 @@ namespace Escape_Room_Engine.Engine.Scripts
var space = new Space(rrDimensions, entrance);
// add exit
var exitDoor = new DoorModule(DoorType.Exit, space);
var exitDoor = new DoorModule(space, exitDoorTypes.RandomElement());
exitDoor.Place(new Vector2Int(
Random.Range(0, rrDimensions.width),
Random.Range(0, rrDimensions.length)
@@ -86,7 +92,7 @@ namespace Escape_Room_Engine.Engine.Scripts
Math.Min(entranceRrPosition.x, playSpace.x - minRoomSize.x)
);
var xMax = Utilities.Utilities.RandomInclusive(
Math.Max(entranceRrPosition.x, xMin + minRoomSize.x),
Math.Max(entranceRrPosition.x + 1, xMin + minRoomSize.x),
playSpace.x
);
var zMin = Utilities.Utilities.RandomInclusive(
@@ -94,13 +100,13 @@ namespace Escape_Room_Engine.Engine.Scripts
Math.Min(entranceRrPosition.y, playSpace.y - minRoomSize.y)
);
var zMax = Utilities.Utilities.RandomInclusive(
Math.Max(entranceRrPosition.y, zMin + minRoomSize.y),
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}", LogType.RoomGeneration);
Logger.Log($"Generated space dimensions {dimensions} from entrance position {entranceRrPosition}", LogType.RoomGeneration);
return dimensions;
}
@@ -112,17 +118,5 @@ namespace Escape_Room_Engine.Engine.Scripts
_rooms[NumberOfRooms - 1].roomObject.SetActive(false);
}
}
/// <summary>
/// Dispose of the oldest room to save resources.
/// </summary>
public void DisposeOldestRoom()
{
if (NumberOfRooms > 0)
{
_rooms[NumberOfRooms - 1].Destroy();
_rooms.RemoveAt(NumberOfRooms - 1);
}
}
}
}

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using System;
using UnityEngine;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
@@ -7,14 +8,15 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
Entrance = ModuleType.DoorEntrance, Exit = ModuleType.DoorExit
}
[Serializable]
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)
internal DoorModule(Space space, DoorModuleDescription description) : base(space, description)
{
types.Add((ModuleType)type);
_description.types.Add((ModuleType)description.doorType);
srDimensions.Size = Vector2Int.one; // door always has size 1x1
}

View File

@@ -0,0 +1,15 @@
using UnityEngine;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
[CreateAssetMenu(menuName = "Door Module")]
public class DoorModuleDescription : ModuleDescription
{
public DoorType doorType;
/// <summary>
/// The description for the door that should be connected with this one.
/// <example>If this is a teleporter entrance, the connected door should be a teleporter exit.</example>
/// </summary>
public DoorModuleDescription connectedDoorDescription;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f5c1202346c34ebc9c3f701a98b50877
timeCreated: 1667833660

View File

@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
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 Object = UnityEngine.Object;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
@@ -16,43 +17,43 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
/// <summary>
/// Get the room relative (<i>RR</i>) position of this module.
/// </summary>
internal Vector2Int RrPosition => space.ToRoomRelative(SrPosition);
internal Vector2Int RrPosition => _space.ToRoomRelative(SrPosition);
internal readonly Space space;
internal readonly ModuleDescription _description;
protected GameObject _moduleObject;
protected readonly List<ModuleType> types = new();
/// <summary>
/// The space relative (<i>SR</i>) dimensions of this module.
/// </summary>
protected Dimensions srDimensions;
internal Module(Space space)
private readonly Space _space;
internal Module(Space space, ModuleDescription description)
{
this.space = space;
_space = space;
_description = description;
}
internal bool IsType(ModuleType type)
{
return types.Contains(type);
return _description.types.Contains(type);
}
/// <summary>
/// Place this module with a position relative to the room.
/// </summary>
/// <param name="rrPosition">The room relative (<i>RR</i>) position of this module. Must be inside the space dimensions.</param>
/// <exception cref="Exception">If the position is not inside the space dimensions.</exception>
internal void PlaceRoomRelative(Vector2Int rrPosition) => Place(space.ToSpaceRelative(rrPosition));
internal void PlaceRoomRelative(Vector2Int rrPosition) => Place(_space.ToSpaceRelative(rrPosition));
/// <summary>
/// Place this module with a position relative to the space it is in.
/// </summary>
/// <param name="srPosition">The space relative (<i>SR</i>) position of this module. Must be inside the space dimensions.</param>
/// <exception cref="Exception">If the position is not inside the space dimensions.</exception>
internal void Place(Vector2Int srPosition) {
if (space != null && !srPosition.IsInsideRelative(space.rrDimensions))
if (_space != null && !srPosition.IsInsideRelative(_space.rrDimensions))
{
throw new Exception($"Trying to place {this} at {srPosition}, which is outside space dimensions {space.rrDimensions}.");
throw new Exception($"Trying to place {this} at {srPosition}, which is outside space dimensions {_space.rrDimensions}.");
}
srDimensions.Position = srPosition;
@@ -60,9 +61,18 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
Logger.Log($"{this} has been placed at {srPosition} (SR)", LogType.ModulePlacement);
}
internal void InstantiateModule(Transform parent)
{
_moduleObject = new GameObject(ToString());
_moduleObject.transform.SetParent(parent, false);
_moduleObject.transform.localPosition = new Vector3(srDimensions.x, 0, srDimensions.z);
Object.Instantiate(_description.modulePrefab, _moduleObject.transform, false);
}
public override string ToString()
{
return $"Module ({string.Join(',', types.ConvertAll(type => type.ToString()))})";
return $"Module ({string.Join(',', _description.types.ToList().ConvertAll(type => type.ToString()))})";
}
}
}

View File

@@ -0,0 +1,12 @@
using System.Collections.Generic;
using UnityEngine;
namespace Escape_Room_Engine.Engine.Scripts.Modules
{
[CreateAssetMenu(menuName = "Generic Module")]
public class ModuleDescription : ScriptableObject
{
public readonly HashSet<ModuleType> types = new();
public GameObject modulePrefab;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: abf4a405f6c64073995bded39977563e
timeCreated: 1667831630

View File

@@ -1,5 +1,4 @@
using System;
using Escape_Room_Engine.Engine.Scripts.Modules;
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;
@@ -14,34 +13,35 @@ namespace Escape_Room_Engine.Engine.Scripts
/// </summary>
internal Vector2Int rrPosition;
internal Passage(DoorModule from)
internal Passage(DoorModule from, bool spawnPassage = false)
{
ConnectFrom(from);
if (spawnPassage)
{
fromOut = from;
rrPosition = Vector2Int.zero;
}
else
{
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);
Logger.Log($"Connected passage from {door} at {rrPosition} (RR)", LogType.PassageConnection);
}
internal virtual void ConnectTo(DoorModule door)
internal 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);
Logger.Log($"Connected passage to {door} at {rrPosition} (RR)", LogType.PassageConnection);
}
}
}

View File

@@ -19,7 +19,8 @@ namespace Escape_Room_Engine.Engine.Scripts
this.rrDimensions = rrDimensions;
// connect the space to its passage
entrance.ConnectTo(new DoorModule(DoorType.Entrance, this));
entrance.ConnectTo(new DoorModule(this,
((DoorModuleDescription)entrance.fromOut._description).connectedDoorDescription));
AddModule(entrance.toIn);
}
@@ -39,6 +40,9 @@ namespace Escape_Room_Engine.Engine.Scripts
var meshRenderer = _spaceObject.GetComponent<MeshRenderer>();
meshRenderer.material = Engine.DefaultEngine.roomMaterial;
// instantiate all modules inside this space
_modules.ForEach(module => module.InstantiateModule(_spaceObject.transform));
}
/// <summary>

View File

@@ -1,23 +0,0 @@
using Escape_Room_Engine.Engine.Scripts.Modules;
using UnityEngine;
namespace Escape_Room_Engine.Engine.Scripts
{
/// <summary>
/// A SpawnPassage is a type of passage used solely for entering the very fist space. It always places the entrance door at (0, 0).
/// </summary>
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);
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 0a168ccfeb4e46b9a2c04442afef6c4c
timeCreated: 1667813431

View File

@@ -1,12 +1,11 @@
using UnityEngine;
using System.Collections.Generic;
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);
/// <summary>
/// Returns whether a position relative to some dimensions is inside those dimensions.
@@ -19,5 +18,13 @@ namespace Escape_Room_Engine.Engine.Scripts.Utilities
}
#endregion
#region Randomness
public static int RandomInclusive(int from, int to) => Random.Range(from, to + 1);
public static T RandomElement<T>(this List<T> list) => list[Random.Range(0, list.Count)];
#endregion
}
}