85 lines
2.5 KiB
C#
85 lines
2.5 KiB
C#
using System.Collections.Generic;
|
|
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));
|
|
|
|
private readonly SpaceSize _size;
|
|
private GameObject _spaceObject;
|
|
private List<Module> _modules = new(2);
|
|
|
|
internal Space(SpaceSize size, Passage entrance, Passage exit)
|
|
{
|
|
_size = size;
|
|
|
|
// connect the space to its passages
|
|
entrance.ConnectTo(new DoorModule(DoorType.Entrance));
|
|
AddModule(entrance.toIn);
|
|
exit.ConnectFrom(new DoorModule(DoorType.Exit));
|
|
AddModule(exit.fromOut);
|
|
}
|
|
|
|
internal void AddModule(Module module)
|
|
{
|
|
_modules.Add(module);
|
|
}
|
|
|
|
internal void InstantiateSpace(Transform parent, string name)
|
|
{
|
|
_spaceObject = new GameObject($"Space {name}");
|
|
_spaceObject.transform.SetParent(parent);
|
|
|
|
var meshFilter = _spaceObject.AddComponent<MeshFilter>();
|
|
meshFilter.mesh = GenerateMesh();
|
|
|
|
var meshRenderer = _spaceObject.AddComponent<MeshRenderer>();
|
|
meshRenderer.material = Engine.DefaultEngine.roomMaterial;
|
|
}
|
|
|
|
internal 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)
|
|
};
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|