fix FaceSpaceCenter requirement

This commit is contained in:
2022-12-05 12:04:00 +01:00
parent 4e95765d98
commit 28e6f346f0
2 changed files with 60 additions and 16 deletions

View File

@@ -32,22 +32,46 @@ namespace EscapeRoomEngine.Engine.Runtime
} }
/// <summary> /// <summary>
/// Get the positions of the two back corners. The first one is just the position of the placement. /// Get the geometric center of this placement taking into account its orientation.
/// </summary>
public Vector3 GeometricCenter
{
get
{
var sizeMinusOne = size - Vector2Int.one;
var half = new Vector2(sizeMinusOne.x/2f, sizeMinusOne.y/2f);
var horizontal = new Vector3(half.x, 0, half.y);
var vertical = new Vector3(half.y, 0, -half.x);
var geometricPosition = position + new Vector3(0.5f, 0, 0.5f);
#pragma warning disable CS8524
return orientation switch
#pragma warning restore CS8524
{
Orientation.North => geometricPosition + horizontal,
Orientation.East => geometricPosition + vertical,
Orientation.South => geometricPosition - horizontal,
Orientation.West => geometricPosition - vertical
};
}
}
/// <summary>
/// Get the integer positions of the two back corners. The first one is just the position of the placement.
/// </summary> /// </summary>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public (Vector3Int, Vector3Int) BackCorners { public (Vector3Int, Vector3Int) BackCorners {
get get
{ {
var width = size.x - 1; var width = size.x - 1;
var horizontal = new Vector3Int(width, 0, 0); var horizontal = new Vector3Int(width, 0, 0);
var vertical = new Vector3Int(0, 0, width); var vertical = new Vector3Int(0, 0, width);
#pragma warning disable CS8524
var otherCorner = orientation switch var otherCorner = orientation switch
#pragma warning restore CS8524
{ {
Orientation.North => position + horizontal, Orientation.North => position + horizontal,
Orientation.East => position - vertical, Orientation.East => position - vertical,
Orientation.South => position - horizontal, Orientation.South => position - horizontal,
Orientation.West => position + vertical, Orientation.West => position + vertical
_ => throw new ArgumentOutOfRangeException()
}; };
return (position, otherCorner); return (position, otherCorner);
} }

View File

@@ -14,20 +14,40 @@ namespace EscapeRoomEngine.Engine.Runtime.Requirements
candidates.RemoveAll(candidate => candidates.RemoveAll(candidate =>
{ {
var bottomLeft = candidate.BottomLeft; var orientation = candidate.orientation;
var center = new Vector2(bottomLeft.x, bottomLeft.z) + var center = candidate.GeometricCenter;
new Vector2(candidate.size.x / 2f, candidate.size.y / 2f);
var xRel = center.x / width; var xRel = center.x / width;
var zRel = center.y / length; var zRel = center.z / length;
bool keep;
return candidate.orientation != // check the space diagonals
(zRel > xRel if (Mathf.Approximately(xRel, zRel))
? zRel > 1 - xRel {
? Orientation.South keep = Mathf.Approximately(xRel, 0.5f) ||
: Orientation.East (xRel < 0.5f
: zRel > 1 - xRel ? orientation is Orientation.East or Orientation.North // on bottom left diagonal of space
? Orientation.West : orientation is Orientation.West or Orientation.South); // on top right diagonal of space
: Orientation.North); }
else if (Mathf.Approximately(xRel, 1 - zRel))
{
keep = xRel < 0.5f
? orientation is Orientation.East or Orientation.South // on top left diagonal of space
: orientation is Orientation.West or Orientation.North; // on bottom right diagonal of space
}
else
{
// check the areas between the diagonals
keep = orientation ==
(zRel > xRel
? zRel > 1 - xRel
? Orientation.South
: Orientation.East
: zRel > 1 - xRel
? Orientation.West
: Orientation.North);
}
return !keep;
}); });
return candidates; return candidates;