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)
|
if (EditorApplication.isPlaying)
|
||||||
{
|
{
|
||||||
Debug.Log("Generating new room...");
|
|
||||||
Engine.DefaultEngine.DisposeOldestRoom();
|
Engine.DefaultEngine.DisposeOldestRoom();
|
||||||
Engine.DefaultEngine.GenerateRoom();
|
Engine.DefaultEngine.GenerateRoom();
|
||||||
UpdateUI();
|
UpdateUI();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
namespace Escape_Room_Engine.Engine.Scripts
|
namespace Escape_Room_Engine.Engine.Scripts
|
||||||
{
|
{
|
||||||
@@ -10,9 +10,21 @@ namespace Escape_Room_Engine.Engine.Scripts
|
|||||||
|
|
||||||
public Material roomMaterial;
|
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;
|
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()
|
public void GenerateRoom()
|
||||||
{
|
{
|
||||||
@@ -23,17 +35,25 @@ namespace Escape_Room_Engine.Engine.Scripts
|
|||||||
_rooms.Add(room);
|
_rooms.Add(room);
|
||||||
|
|
||||||
GenerateSpace(room, entrance); // TODO: rooms with more than one space
|
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 exit = new Passage();
|
||||||
var size = new SpaceSize(Random.Range(2, 6), Random.Range(2, 6)); // TODO: no hardcoded space size
|
var space = new Space(GenerateSpaceDimensions(), from, exit);
|
||||||
var space = new Space(size, from, exit);
|
|
||||||
room.AddSpace(space, 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>
|
/// <summary>
|
||||||
/// Dispose of the oldest room to save resources.
|
/// Dispose of the oldest room to save resources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ namespace Escape_Room_Engine.Engine.Scripts
|
|||||||
internal void InstantiateRoom(Transform parent, string name)
|
internal void InstantiateRoom(Transform parent, string name)
|
||||||
{
|
{
|
||||||
_roomObject = new GameObject($"Room {name}");
|
_roomObject = new GameObject($"Room {name}");
|
||||||
_roomObject.transform.SetParent(parent);
|
_roomObject.transform.SetParent(parent, false);
|
||||||
|
|
||||||
for (var i = 0; i < _spaces.Count; i++)
|
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 Entrance => (DoorModule)_modules.Find(module => module.IsType((ModuleType)DoorType.Entrance));
|
||||||
// public DoorModule Exit => (DoorModule)_modules.Find(module => module.IsType((ModuleType)DoorType.Exit));
|
// public DoorModule Exit => (DoorModule)_modules.Find(module => module.IsType((ModuleType)DoorType.Exit));
|
||||||
|
|
||||||
private readonly SpaceSize _size;
|
private readonly Dimensions _dimensions;
|
||||||
private GameObject _spaceObject;
|
private GameObject _spaceObject;
|
||||||
private List<Module> _modules = new(2);
|
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
|
// connect the space to its passages
|
||||||
entrance.ConnectTo(new DoorModule(DoorType.Entrance));
|
entrance.ConnectTo(new DoorModule(DoorType.Entrance));
|
||||||
@@ -30,27 +30,27 @@ namespace Escape_Room_Engine.Engine.Scripts
|
|||||||
|
|
||||||
internal void InstantiateSpace(Transform parent, string name)
|
internal void InstantiateSpace(Transform parent, string name)
|
||||||
{
|
{
|
||||||
_spaceObject = new GameObject($"Space {name}");
|
_spaceObject = new GameObject($"Space {name}", typeof(MeshFilter), typeof(MeshRenderer));
|
||||||
_spaceObject.transform.SetParent(parent);
|
_spaceObject.transform.SetParent(parent, false);
|
||||||
|
_spaceObject.transform.localPosition = new Vector3(_dimensions.X, 0, _dimensions.Y);
|
||||||
var meshFilter = _spaceObject.AddComponent<MeshFilter>();
|
|
||||||
|
var meshFilter = _spaceObject.GetComponent<MeshFilter>();
|
||||||
meshFilter.mesh = GenerateMesh();
|
meshFilter.mesh = GenerateMesh();
|
||||||
|
|
||||||
var meshRenderer = _spaceObject.AddComponent<MeshRenderer>();
|
var meshRenderer = _spaceObject.GetComponent<MeshRenderer>();
|
||||||
meshRenderer.material = Engine.DefaultEngine.roomMaterial;
|
meshRenderer.material = Engine.DefaultEngine.roomMaterial;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal Mesh GenerateMesh()
|
private Mesh GenerateMesh()
|
||||||
{
|
{
|
||||||
var mesh = new Mesh();
|
var mesh = new Mesh();
|
||||||
|
|
||||||
float halfWidth = _size.Width / 2f, halfHeight = _size.Height / 2f;
|
|
||||||
mesh.vertices = new[]
|
mesh.vertices = new[]
|
||||||
{
|
{
|
||||||
new Vector3(-halfWidth, 0, -halfHeight),
|
new Vector3(0, 0, 0),
|
||||||
new Vector3(halfWidth, 0, -halfHeight),
|
new Vector3(_dimensions.Width, 0, 0),
|
||||||
new Vector3(-halfWidth, 0, halfHeight),
|
new Vector3(0, 0, _dimensions.Length),
|
||||||
new Vector3(halfWidth, 0, halfHeight)
|
new Vector3(_dimensions.Width, 0, _dimensions.Length)
|
||||||
};
|
};
|
||||||
|
|
||||||
mesh.triangles = new[]
|
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})";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1232,6 +1232,8 @@ MonoBehaviour:
|
|||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
roomMaterial: {fileID: 2100000, guid: 39e2ed014eda5d6408c16fbf0fa80781, type: 2}
|
roomMaterial: {fileID: 2100000, guid: 39e2ed014eda5d6408c16fbf0fa80781, type: 2}
|
||||||
|
minRoomSize: {x: 3, y: 3}
|
||||||
|
playSpace: {x: 4, y: 6}
|
||||||
--- !u!4 &1568048335
|
--- !u!4 &1568048335
|
||||||
Transform:
|
Transform:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
Reference in New Issue
Block a user