move puzzle implementations out of engine

This commit is contained in:
2023-03-22 09:44:17 +01:00
parent e1bfecbd4b
commit 30d8f986df
70 changed files with 82 additions and 46 deletions

View File

@@ -0,0 +1,60 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Requirements.Placement
{
/// <summary>
/// This requirement guarantees that the module faces the center of the space.
/// </summary>
[CreateAssetMenu(menuName = "Requirements/Face Space Center")]
public class FaceSpaceCenter : PlacementRequirement
{
protected override List<Utilities.Placement> FilterCandidates(List<Utilities.Placement> candidates, Module module, Space space)
{
float width = space.rrPlacement.size.x;
float length = space.rrPlacement.size.y;
candidates.RemoveAll(candidate =>
{
var orientation = candidate.orientation;
var center = candidate.GeometricCenter;
var xRel = center.x / width;
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 !keep;
});
return candidates;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 872da92bb04647e3bd6e741e6bb0a976
timeCreated: 1667878978

View File

@@ -0,0 +1,23 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Requirements.Placement
{
/// <summary>
/// This requirement forces a specific orientation.
/// </summary>
[CreateAssetMenu(menuName = "Requirements/Lock Orientation")]
public class LockOrientation : PlacementRequirement
{
public Orientation orientation;
protected override List<Utilities.Placement> FilterCandidates(List<Utilities.Placement> candidates, Module module, Space space)
{
candidates.RemoveAll(candidate => candidate.orientation != orientation);
return candidates;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b20fab4af0b74820b7abb1864fef644a
timeCreated: 1669306423

View File

@@ -0,0 +1,35 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Requirements.Placement
{
/// <summary>
/// This requirement prevents modules from overlapping. For two models not to overlap, both of them need this requirement.
/// </summary>
[CreateAssetMenu(menuName = "Requirements/No Overlap")]
public class NoOverlap : PlacementRequirement
{
protected override List<Utilities.Placement> FilterCandidates(List<Utilities.Placement> candidates, Module module, Space space)
{
space.AllModules.ForEach(other => // for all other module ...
other.srPlacement.ForEachPosition(otherPosition => // ... positions ...
candidates.RemoveAll(candidate => // ... remove every candidate that ...
{
var remove = false;
candidate.ForEachPosition(position => // ... anywhere inside its bounds ...
{
if (!remove)
{
remove = position.Equals(otherPosition); // ... overlaps with other modules' position
}
});
return remove;
})));
return candidates;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 58ae9c09c887475d833d2cd4ee4ccffb
timeCreated: 1667881856

View File

@@ -0,0 +1,29 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Requirements.Placement
{
/// <summary>
/// This requirement guarantees that the back side of the module is placed at the edge of the space.
/// </summary>
[CreateAssetMenu(menuName = "Requirements/Place Along Space Edges")]
public class PlaceAlongSpaceEdges : PlacementRequirement
{
protected override List<Utilities.Placement> FilterCandidates(List<Utilities.Placement> candidates, Module module, Space space)
{
var sizeMinusOne = space.rrPlacement.size - Vector2Int.one;
candidates.RemoveAll(candidate =>
{
var (left, right) = candidate.BackCorners;
return !(left.x == 0 && right.x == 0 ||
left.z == 0 && right.z == 0 ||
left.x == sizeMinusOne.x && right.x == sizeMinusOne.x ||
left.z == sizeMinusOne.y && right.z == sizeMinusOne.y);
});
return candidates;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8ec2cdf0145347e18e7c68221333be2c
timeCreated: 1667876484

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Requirements.Placement
{
/// <summary>
/// This requirement allows any placement. Used for testing purposes.
/// </summary>
[CreateAssetMenu(menuName = "Requirements/Place Anywhere")]
public class PlaceAnywhere : PlacementRequirement
{
protected override List<Utilities.Placement> FilterCandidates(List<Utilities.Placement> candidates, Module module, Space space)
{
return candidates;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa4ff365b4e844e782cd12d8aeebd3d4
timeCreated: 1667876850

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Requirements.Placement
{
/// <summary>
/// This requirement places a module exactly in the same position as its first related module.
/// </summary>
[CreateAssetMenu(menuName = "Requirements/Place With Related Module")]
public class PlaceWithRelatedModule : PlacementRequirement
{
protected override List<Utilities.Placement> FilterCandidates(List<Utilities.Placement> candidates, Module module, Space space)
{
return new List<Utilities.Placement> { module.relatedModules[0].srPlacement };
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 406f215d83114f4c9b3a92c8a0ea8d73
timeCreated: 1670537230