using System.Collections.Generic; using Escape_Room_Engine.Engine.Scripts.Modules; using UnityEngine; namespace Escape_Room_Engine.Engine.Scripts { public class Space { /// /// The room relative (RR) dimensions of this space. /// internal readonly Dimensions rrDimensions; internal List Modules { get; } = new(2); private GameObject _spaceObject; internal Space(Dimensions rrDimensions, Passage entrance) { this.rrDimensions = rrDimensions; // connect the space to its passage entrance.ConnectTo(new DoorModule(this, ((DoorModuleDescription)entrance.fromOut.description).connectedDoorDescription)); AddModule(entrance.toIn); } internal void AddModule(Module module) { Modules.Add(module); } internal void InstantiateSpace(Transform parent, string name) { _spaceObject = new GameObject($"Space {name}", typeof(MeshFilter), typeof(MeshRenderer)); _spaceObject.transform.SetParent(parent, false); _spaceObject.transform.localPosition = new Vector3(rrDimensions.x, 0, rrDimensions.z); var meshFilter = _spaceObject.GetComponent(); meshFilter.mesh = GenerateMesh(); var meshRenderer = _spaceObject.GetComponent(); meshRenderer.material = Engine.DefaultEngine.roomMaterial; // instantiate all modules inside this space Modules.ForEach(module => module.InstantiateModule(_spaceObject.transform)); } /// /// 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(); mesh.vertices = new[] { new Vector3(0, 0, 0), new Vector3(rrDimensions.width, 0, 0), new Vector3(0, 0, rrDimensions.length), new Vector3(rrDimensions.width, 0, rrDimensions.length) }; mesh.triangles = new[] { 0, 2, 1, 2, 3, 1 }; var normal = Vector3.up; mesh.normals = new[] { normal, normal, normal, normal }; mesh.uv = new[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1) }; return mesh; } internal void Destroy() { Object.Destroy(_spaceObject); } } }