diff --git a/Assets/Engine/Runtime/Placement.cs b/Assets/Engine/Runtime/Placement.cs index 6fe2e56..6999ae0 100644 --- a/Assets/Engine/Runtime/Placement.cs +++ b/Assets/Engine/Runtime/Placement.cs @@ -30,24 +30,48 @@ namespace EscapeRoomEngine.Engine.Runtime }; } } + + /// + /// Get the geometric center of this placement taking into account its orientation. + /// + 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 + }; + } + } /// - /// Get the positions of the two back corners. The first one is just the position of the placement. + /// Get the integer positions of the two back corners. The first one is just the position of the placement. /// - /// public (Vector3Int, Vector3Int) BackCorners { get { var width = size.x - 1; var horizontal = new Vector3Int(width, 0, 0); var vertical = new Vector3Int(0, 0, width); +#pragma warning disable CS8524 var otherCorner = orientation switch +#pragma warning restore CS8524 { Orientation.North => position + horizontal, Orientation.East => position - vertical, Orientation.South => position - horizontal, - Orientation.West => position + vertical, - _ => throw new ArgumentOutOfRangeException() + Orientation.West => position + vertical }; return (position, otherCorner); } diff --git a/Assets/Engine/Runtime/Requirements/FaceSpaceCenter.cs b/Assets/Engine/Runtime/Requirements/FaceSpaceCenter.cs index 0def07c..99ebc11 100644 --- a/Assets/Engine/Runtime/Requirements/FaceSpaceCenter.cs +++ b/Assets/Engine/Runtime/Requirements/FaceSpaceCenter.cs @@ -14,20 +14,40 @@ namespace EscapeRoomEngine.Engine.Runtime.Requirements candidates.RemoveAll(candidate => { - var bottomLeft = candidate.BottomLeft; - var center = new Vector2(bottomLeft.x, bottomLeft.z) + - new Vector2(candidate.size.x / 2f, candidate.size.y / 2f); + var orientation = candidate.orientation; + var center = candidate.GeometricCenter; var xRel = center.x / width; - var zRel = center.y / length; + var zRel = center.z / length; + bool keep; + + // check the space diagonals + if (Mathf.Approximately(xRel, zRel)) + { + keep = Mathf.Approximately(xRel, 0.5f) || + (xRel < 0.5f + ? orientation is Orientation.East or Orientation.North // on bottom left diagonal of space + : orientation is Orientation.West or Orientation.South); // on top right diagonal of space + } + 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 candidate.orientation != - (zRel > xRel - ? zRel > 1 - xRel - ? Orientation.South - : Orientation.East - : zRel > 1 - xRel - ? Orientation.West - : Orientation.North); + return !keep; }); return candidates;