make sure space entrance door is always at edge of space
This commit is contained in:
@@ -2,12 +2,10 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Escape_Room_Engine.Engine.Scripts.Modules;
|
||||
using Escape_Room_Engine.Engine.Scripts.Requirements;
|
||||
using Escape_Room_Engine.Engine.Scripts.Utilities;
|
||||
using UnityEngine;
|
||||
using Logger = Escape_Room_Engine.Engine.Scripts.Utilities.Logger;
|
||||
using LogType = Escape_Room_Engine.Engine.Scripts.Utilities.LogType;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Escape_Room_Engine.Engine.Scripts
|
||||
{
|
||||
@@ -68,8 +66,7 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
Logger.Log("Generating space...", LogType.RoomGeneration);
|
||||
|
||||
// create space
|
||||
var rrDimensions = GenerateSpaceDimensions(entrance.rrPosition);
|
||||
var space = new Space(rrDimensions, entrance);
|
||||
var space = new Space(entrance);
|
||||
|
||||
// add exit
|
||||
var exitDoor = new DoorModule(space, exitDoorTypes.RandomElement());
|
||||
@@ -86,37 +83,6 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
room.AddSpace(space, exit);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate space dimensions that fit the required size constraints and cover the position of the entrance.
|
||||
/// </summary>
|
||||
/// <param name="entranceRrPosition">The room relative (<i>RR</i>) position of the entrance.</param>
|
||||
/// <returns>The generated room relative (<i>RR</i>) dimensions.</returns>
|
||||
private Dimensions GenerateSpaceDimensions(Vector2Int entranceRrPosition)
|
||||
{
|
||||
var xMin = Utilities.Utilities.RandomInclusive(
|
||||
0,
|
||||
Math.Min(entranceRrPosition.x, playSpace.x - minRoomSize.x)
|
||||
);
|
||||
var xMax = Utilities.Utilities.RandomInclusive(
|
||||
Math.Max(entranceRrPosition.x + 1, xMin + minRoomSize.x),
|
||||
playSpace.x
|
||||
);
|
||||
var zMin = Utilities.Utilities.RandomInclusive(
|
||||
0,
|
||||
Math.Min(entranceRrPosition.y, playSpace.y - minRoomSize.y)
|
||||
);
|
||||
var zMax = Utilities.Utilities.RandomInclusive(
|
||||
Math.Max(entranceRrPosition.y + 1, zMin + minRoomSize.y),
|
||||
playSpace.y
|
||||
);
|
||||
|
||||
var dimensions = new Dimensions(xMax - xMin, zMax - zMin, xMin, zMin);
|
||||
|
||||
Logger.Log($"Generated space dimensions {dimensions} from entrance position {entranceRrPosition}", LogType.RoomGeneration);
|
||||
|
||||
return dimensions;
|
||||
}
|
||||
|
||||
public void HideOldestRoom()
|
||||
{
|
||||
if (NumberOfRooms > 0)
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Escape_Room_Engine.Engine.Scripts.Modules;
|
||||
using Escape_Room_Engine.Engine.Scripts.Requirements;
|
||||
using UnityEngine;
|
||||
using LogType = Escape_Room_Engine.Engine.Scripts.Utilities.LogType;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Escape_Room_Engine.Engine.Scripts
|
||||
{
|
||||
@@ -15,9 +18,9 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
|
||||
private GameObject _spaceObject;
|
||||
|
||||
internal Space(Dimensions rrDimensions, Passage entrance)
|
||||
internal Space(Passage entrance)
|
||||
{
|
||||
this.rrDimensions = rrDimensions;
|
||||
rrDimensions = GenerateSpaceDimensions(entrance, Engine.DefaultEngine.minRoomSize, Engine.DefaultEngine.playSpace);
|
||||
|
||||
// connect the space to its passage
|
||||
entrance.ConnectTo(new DoorModule(this,
|
||||
@@ -109,5 +112,58 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
{
|
||||
Object.Destroy(_spaceObject);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
var xMin = -1;
|
||||
var xMax = -1;
|
||||
var zMin = -1;
|
||||
var zMax = -1;
|
||||
var position = entrance.rrPosition;
|
||||
var door = entrance.fromOut;
|
||||
|
||||
// fix the side the door is facing away from
|
||||
switch (door.orientation)
|
||||
{
|
||||
case Orientation.North:
|
||||
zMin = position.y;
|
||||
zMax = Utilities.Utilities.RandomInclusive(zMin + minSize.y, availableSpace.y);
|
||||
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);
|
||||
break;
|
||||
case Orientation.West:
|
||||
xMax = position.x + 1;
|
||||
xMin = Utilities.Utilities.RandomInclusive(0, xMax - minSize.x);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
// calculate remaining values if they haven't been covered by the switch statement yet
|
||||
if(xMin == -1)
|
||||
xMin = Utilities.Utilities.RandomInclusive(0, Math.Min(position.x, availableSpace.x - minSize.x));
|
||||
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));
|
||||
if(zMax == -1)
|
||||
zMax = Utilities.Utilities.RandomInclusive(Math.Max(position.y + 1, zMin + minSize.y), availableSpace.y);
|
||||
|
||||
var dimensions = new Dimensions(xMax - xMin, zMax - zMin, xMin, zMin);
|
||||
|
||||
Utilities.Logger.Log($"Generated space dimensions {dimensions} from entrance position {position}", LogType.RoomGeneration);
|
||||
|
||||
return dimensions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user