rework dimensions and orientation into Placement, optimise requirements to work on previous candidates, use vec3 for positions
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using EscapeRoomEngine.Engine.Runtime.Modules;
|
||||
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
||||
using UnityEngine;
|
||||
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
|
||||
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;
|
||||
@@ -13,7 +14,7 @@ namespace EscapeRoomEngine.Engine.Runtime
|
||||
/// <summary>
|
||||
/// The room relative (<i>RR</i>) dimensions of this space.
|
||||
/// </summary>
|
||||
internal readonly Dimensions rrDimensions;
|
||||
internal readonly Placement rrPlacement;
|
||||
internal readonly Room room;
|
||||
|
||||
/// <summary>
|
||||
@@ -42,10 +43,10 @@ namespace EscapeRoomEngine.Engine.Runtime
|
||||
internal Space(Room room, Passage entrance)
|
||||
{
|
||||
this.room = room;
|
||||
rrDimensions = GenerateSpaceDimensions(
|
||||
rrPlacement = GenerateSpaceDimensions(
|
||||
entrance,
|
||||
Engine.DefaultEngine.theme.minRoomSize,
|
||||
Engine.DefaultEngine.theme.playSpace);
|
||||
Engine.DefaultEngine.theme.minRoomSize.ProjectAtFloor(),
|
||||
Engine.DefaultEngine.theme.playSpace.ProjectAtFloor());
|
||||
|
||||
// connect the space to its passage
|
||||
entrance.ConnectTo(new DoorModule(this,
|
||||
@@ -94,20 +95,20 @@ namespace EscapeRoomEngine.Engine.Runtime
|
||||
{
|
||||
_spaceObject = new GameObject($"Space {name}");
|
||||
_spaceObject.transform.SetParent(parent, false);
|
||||
_spaceObject.transform.localPosition = new Vector3(rrDimensions.x, 0, rrDimensions.z);
|
||||
_spaceObject.transform.localPosition = new Vector3(rrPlacement.position.x, 0, rrPlacement.position.z);
|
||||
|
||||
// build the space floor out of tiles
|
||||
_spaceTiles = new GameObject($"Space Geometry");
|
||||
_spaceTiles.transform.SetParent(_spaceObject.transform, false);
|
||||
_spaceTiles.isStatic = true;
|
||||
for (var z = 0; z < rrDimensions.length; z++)
|
||||
for (var z = 0; z < rrPlacement.size.y; z++)
|
||||
{
|
||||
for (var x = 0; x < rrDimensions.width; x++)
|
||||
for (var x = 0; x < rrPlacement.size.x; x++)
|
||||
{
|
||||
var left = x == 0;
|
||||
var right = x == rrDimensions.width - 1;
|
||||
var right = x == rrPlacement.size.x - 1;
|
||||
var bottom = z == 0;
|
||||
var top = z == rrDimensions.length - 1;
|
||||
var top = z == rrPlacement.size.y - 1;
|
||||
|
||||
TileLocation location;
|
||||
if (bottom)
|
||||
@@ -131,40 +132,40 @@ namespace EscapeRoomEngine.Engine.Runtime
|
||||
/// Convert a position relative to this space to one relative to the room.
|
||||
/// </summary>
|
||||
/// <param name="srPosition">The space relative (<i>SR</i>) position that should be converted to a room relative (<i>RR</i>) position.</param>
|
||||
internal Vector2Int ToRoomRelative(Vector2Int srPosition) => srPosition + rrDimensions.Position;
|
||||
internal Vector3Int ToRoomRelative(Vector3Int srPosition) => srPosition + rrPlacement.position;
|
||||
/// <summary>
|
||||
/// Convert a position relative to the room to one relative to this space.
|
||||
/// </summary>
|
||||
/// <param name="rrPosition">The room relative (<i>RR</i>) position that should be converted to a space relative (<i>SR</i>) position.</param>
|
||||
internal Vector2Int ToSpaceRelative(Vector2Int rrPosition) => rrPosition - rrDimensions.Position;
|
||||
internal Vector3Int ToSpaceRelative(Vector3Int rrPosition) => rrPosition - rrPlacement.position;
|
||||
|
||||
/// <summary>
|
||||
/// Generate space dimensions that fit the required size constraints and cover the position of the entrance.
|
||||
/// </summary>
|
||||
/// <returns>The generated room relative (<i>RR</i>) dimensions.</returns>
|
||||
private static Dimensions GenerateSpaceDimensions(Passage entrance, Vector2Int minSize, Vector2Int availableSpace)
|
||||
/// <returns>A room relative (<i>RR</i>) placement with the generated dimensions. Always faces North.</returns>
|
||||
private static Placement GenerateSpaceDimensions(Passage entrance, Vector3Int minSize, Vector3Int availableSpace)
|
||||
{
|
||||
var xMin = -1;
|
||||
var xMax = -1;
|
||||
var zMin = -1;
|
||||
var zMax = -1;
|
||||
var position = entrance.rrPosition;
|
||||
var door = entrance.fromOut;
|
||||
var position = door.RrPosition;
|
||||
|
||||
// fix the side the door is facing away from
|
||||
switch (door.orientation)
|
||||
switch (door.Orientation)
|
||||
{
|
||||
case Orientation.North:
|
||||
zMin = position.y;
|
||||
zMax = Utilities.Utilities.RandomInclusive(zMin + minSize.y, availableSpace.y);
|
||||
zMin = position.z;
|
||||
zMax = Utilities.Utilities.RandomInclusive(zMin + minSize.z, availableSpace.z);
|
||||
break;
|
||||
case Orientation.East:
|
||||
xMin = position.x;
|
||||
xMax = Utilities.Utilities.RandomInclusive(xMin + minSize.x, availableSpace.x);
|
||||
break;
|
||||
case Orientation.South:
|
||||
zMax = position.y + 1;
|
||||
zMin = Utilities.Utilities.RandomInclusive(0, zMax - minSize.y);
|
||||
zMax = position.z + 1;
|
||||
zMin = Utilities.Utilities.RandomInclusive(0, zMax - minSize.z);
|
||||
break;
|
||||
case Orientation.West:
|
||||
xMax = position.x + 1;
|
||||
@@ -180,16 +181,14 @@ namespace EscapeRoomEngine.Engine.Runtime
|
||||
if(xMax == -1)
|
||||
xMax = Utilities.Utilities.RandomInclusive(Math.Max(position.x + 1, xMin + minSize.x), availableSpace.x);
|
||||
if(zMin == -1)
|
||||
zMin = Utilities.Utilities.RandomInclusive(0, Math.Min(position.y, availableSpace.y - minSize.y));
|
||||
zMin = Utilities.Utilities.RandomInclusive(0, Math.Min(position.z, availableSpace.z - minSize.z));
|
||||
if(zMax == -1)
|
||||
zMax = Utilities.Utilities.RandomInclusive(Math.Max(position.y + 1, zMin + minSize.y), availableSpace.y);
|
||||
zMax = Utilities.Utilities.RandomInclusive(Math.Max(position.z + 1, zMin + minSize.z), availableSpace.z);
|
||||
|
||||
var dimensions = new Dimensions
|
||||
var dimensions = new Placement
|
||||
{
|
||||
width = xMax - xMin,
|
||||
length = zMax - zMin,
|
||||
x = xMin,
|
||||
z = zMin
|
||||
position = new Vector3Int(xMin, 0, zMin),
|
||||
size = new Vector2Int(xMax - xMin, zMax - zMin)
|
||||
};
|
||||
|
||||
Logger.Log($"Generated space dimensions {dimensions} from entrance position {position}", LogType.RoomGeneration);
|
||||
|
||||
Reference in New Issue
Block a user