connect multiple rooms with doors

This commit is contained in:
2022-11-03 21:20:00 +01:00
parent bce88d0504
commit 807eae1c62
24 changed files with 391 additions and 88 deletions

View File

@@ -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));
/// <summary>
/// The room relative (<i>RR</i>) dimensions of this space.
/// </summary>
internal readonly Dimensions rrDimensions;
private readonly Dimensions _dimensions;
private GameObject _spaceObject;
private List<Module> _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>();
meshFilter.mesh = GenerateMesh();
@@ -41,6 +41,19 @@ namespace Escape_Room_Engine.Engine.Scripts
meshRenderer.material = Engine.DefaultEngine.roomMaterial;
}
/// <summary>
/// Convert a position relative to this space to one relative to the room.
/// </summary>
/// <param name="srPosition">The space relative (<i>SR</i>) position that should be converted to a room relative (<i>RR</i>) position.</param>
/// <returns></returns>
internal Vector2Int ToRoomRelative(Vector2Int srPosition) => srPosition + rrDimensions.Position;
/// <summary>
/// Convert a position relative to the room to one relative to this space.
/// </summary>
/// <param name="rrPosition">The room relative (<i>RR</i>) position that should be converted to a space relative (<i>SR</i>) position.</param>
/// <returns></returns>
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[]