improve room generation

This commit is contained in:
2022-11-01 18:02:00 +01:00
parent 484244c249
commit bce88d0504
8 changed files with 63 additions and 40 deletions

View File

@@ -8,13 +8,13 @@ namespace Escape_Room_Engine.Engine.Scripts
// public DoorModule Entrance => (DoorModule)_modules.Find(module => module.IsType((ModuleType)DoorType.Entrance));
// public DoorModule Exit => (DoorModule)_modules.Find(module => module.IsType((ModuleType)DoorType.Exit));
private readonly SpaceSize _size;
private readonly Dimensions _dimensions;
private GameObject _spaceObject;
private List<Module> _modules = new(2);
internal Space(SpaceSize size, Passage entrance, Passage exit)
internal Space(Dimensions dimensions, Passage entrance, Passage exit)
{
_size = size;
_dimensions = dimensions;
// connect the space to its passages
entrance.ConnectTo(new DoorModule(DoorType.Entrance));
@@ -30,27 +30,27 @@ namespace Escape_Room_Engine.Engine.Scripts
internal void InstantiateSpace(Transform parent, string name)
{
_spaceObject = new GameObject($"Space {name}");
_spaceObject.transform.SetParent(parent);
var meshFilter = _spaceObject.AddComponent<MeshFilter>();
_spaceObject = new GameObject($"Space {name}", typeof(MeshFilter), typeof(MeshRenderer));
_spaceObject.transform.SetParent(parent, false);
_spaceObject.transform.localPosition = new Vector3(_dimensions.X, 0, _dimensions.Y);
var meshFilter = _spaceObject.GetComponent<MeshFilter>();
meshFilter.mesh = GenerateMesh();
var meshRenderer = _spaceObject.AddComponent<MeshRenderer>();
var meshRenderer = _spaceObject.GetComponent<MeshRenderer>();
meshRenderer.material = Engine.DefaultEngine.roomMaterial;
}
internal Mesh GenerateMesh()
private Mesh GenerateMesh()
{
var mesh = new Mesh();
float halfWidth = _size.Width / 2f, halfHeight = _size.Height / 2f;
mesh.vertices = new[]
{
new Vector3(-halfWidth, 0, -halfHeight),
new Vector3(halfWidth, 0, -halfHeight),
new Vector3(-halfWidth, 0, halfHeight),
new Vector3(halfWidth, 0, halfHeight)
new Vector3(0, 0, 0),
new Vector3(_dimensions.Width, 0, 0),
new Vector3(0, 0, _dimensions.Length),
new Vector3(_dimensions.Width, 0, _dimensions.Length)
};
mesh.triangles = new[]