improve room generation
This commit is contained in:
20
Assets/Escape Room Engine/Engine/Scripts/Dimensions.cs
Normal file
20
Assets/Escape Room Engine/Engine/Scripts/Dimensions.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace Escape_Room_Engine.Engine.Scripts
|
||||
{
|
||||
public readonly struct Dimensions
|
||||
{
|
||||
internal Dimensions(float width, float length, float x, float y)
|
||||
{
|
||||
Width = width;
|
||||
Length = length;
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
public float Width { get; }
|
||||
public float Length { get; }
|
||||
public float X { get; }
|
||||
public float Y { get; }
|
||||
|
||||
public override string ToString() => $"({Width}, {Length}) at ({X}, {Y})";
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,6 @@ namespace Escape_Room_Engine.Engine.Scripts.Editor
|
||||
{
|
||||
if (EditorApplication.isPlaying)
|
||||
{
|
||||
Debug.Log("Generating new room...");
|
||||
Engine.DefaultEngine.DisposeOldestRoom();
|
||||
Engine.DefaultEngine.GenerateRoom();
|
||||
UpdateUI();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Escape_Room_Engine.Engine.Scripts
|
||||
{
|
||||
@@ -10,9 +10,21 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
|
||||
public Material roomMaterial;
|
||||
|
||||
[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 int NumberOfRooms => _rooms.Count;
|
||||
|
||||
private List<Room> _rooms = new(1);
|
||||
private readonly List<Room> _rooms = new(1);
|
||||
private GameObject _playSpaceOrigin;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_playSpaceOrigin = new GameObject("Play Space Origin");
|
||||
_playSpaceOrigin.transform.SetParent(transform);
|
||||
_playSpaceOrigin.transform.localPosition = new Vector3(-playSpace.x / 2f, 0, -playSpace.y / 2f);
|
||||
}
|
||||
|
||||
public void GenerateRoom()
|
||||
{
|
||||
@@ -23,17 +35,25 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
_rooms.Add(room);
|
||||
|
||||
GenerateSpace(room, entrance); // TODO: rooms with more than one space
|
||||
room.InstantiateRoom(transform, (_rooms.Count - 1).ToString());
|
||||
room.InstantiateRoom(_playSpaceOrigin.transform, (_rooms.Count - 1).ToString());
|
||||
}
|
||||
|
||||
void GenerateSpace(Room room, Passage from)
|
||||
private void GenerateSpace(Room room, Passage from)
|
||||
{
|
||||
var exit = new Passage();
|
||||
var size = new SpaceSize(Random.Range(2, 6), Random.Range(2, 6)); // TODO: no hardcoded space size
|
||||
var space = new Space(size, from, exit);
|
||||
var space = new Space(GenerateSpaceDimensions(), from, exit);
|
||||
room.AddSpace(space, exit);
|
||||
}
|
||||
|
||||
private Dimensions GenerateSpaceDimensions()
|
||||
{
|
||||
var xMin = Random.Range(0, playSpace.x - minRoomSize.x + 1);
|
||||
var xMax = Random.Range(xMin + minRoomSize.x, playSpace.x + 1);
|
||||
var yMin = Random.Range(0, playSpace.y - minRoomSize.y + 1);
|
||||
var yMax = Random.Range(yMin + minRoomSize.y, playSpace.y + 1);
|
||||
return new Dimensions(xMax - xMin, yMax - yMin, xMin, yMin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose of the oldest room to save resources.
|
||||
/// </summary>
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
internal void InstantiateRoom(Transform parent, string name)
|
||||
{
|
||||
_roomObject = new GameObject($"Room {name}");
|
||||
_roomObject.transform.SetParent(parent);
|
||||
_roomObject.transform.SetParent(parent, false);
|
||||
|
||||
for (var i = 0; i < _spaces.Count; i++)
|
||||
{
|
||||
|
||||
@@ -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[]
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
namespace Escape_Room_Engine.Engine.Scripts
|
||||
{
|
||||
public readonly struct SpaceSize
|
||||
{
|
||||
internal SpaceSize(int width, int height)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
}
|
||||
|
||||
// TODO: intersections and unions of rectangles
|
||||
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
|
||||
public override string ToString() => $"({Width}, {Height})";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user