rework dimensions and orientation into Placement, optimise requirements to work on previous candidates, use vec3 for positions
This commit is contained in:
@@ -10,37 +10,27 @@ using Object = UnityEngine.Object;
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.Modules
|
||||
{
|
||||
public enum Orientation
|
||||
{
|
||||
North = 0, East = 90, South = 180, West = 270
|
||||
}
|
||||
|
||||
public class Module
|
||||
{
|
||||
public static HashSet<Orientation> EveryOrientation => new(new[]
|
||||
{
|
||||
Orientation.North, Orientation.East, Orientation.South, Orientation.West
|
||||
});
|
||||
|
||||
public readonly List<Module> relatedModules = new();
|
||||
|
||||
public ModuleState State { get; private set; }
|
||||
internal Orientation Orientation => srPlacement.orientation;
|
||||
/// <summary>
|
||||
/// Get the space relative (<i>SR</i>) position of this module.
|
||||
/// </summary>
|
||||
internal Vector2Int SrPosition => srDimensions.Position;
|
||||
internal Vector3Int SrPosition => srPlacement.position;
|
||||
/// <summary>
|
||||
/// Get the room relative (<i>RR</i>) position of this module.
|
||||
/// </summary>
|
||||
internal Vector2Int RrPosition => space.ToRoomRelative(SrPosition);
|
||||
internal Vector3Int RrPosition => space?.ToRoomRelative(SrPosition) ?? SrPosition;
|
||||
|
||||
internal readonly ModuleDescription description;
|
||||
internal Orientation orientation;
|
||||
|
||||
/// <summary>
|
||||
/// The space relative (<i>SR</i>) dimensions of this module.
|
||||
/// The space relative (<i>SR</i>) placement of this module.
|
||||
/// </summary>
|
||||
protected Dimensions srDimensions;
|
||||
public Placement srPlacement;
|
||||
|
||||
protected readonly Space space;
|
||||
|
||||
@@ -58,49 +48,55 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
||||
internal bool CheckRequirements()
|
||||
{
|
||||
return PreconditionRequirement.CheckPreconditions(this, space) &&
|
||||
PlacementRequirement.TryPlacing(this, space) &&
|
||||
OrientationRequirement.TryOrienting(this, space);
|
||||
PlacementRequirement.TryPlacing(this, space);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Place this module with a position relative to the room.
|
||||
/// </summary>
|
||||
/// <param name="rrPosition">The room relative (<i>RR</i>) position of this module. Must be inside the space dimensions.</param>
|
||||
/// <param name="orientation">The orientation of this module.</param>
|
||||
/// <exception cref="EngineException">If the position is not inside the space dimensions.</exception>
|
||||
internal void PlaceRoomRelative(Vector2Int rrPosition) => Place(space.ToSpaceRelative(rrPosition));
|
||||
|
||||
internal void PlaceRoomRelative(Vector3Int rrPosition, Orientation orientation) =>
|
||||
Place(new Placement
|
||||
{
|
||||
position = space.ToSpaceRelative(rrPosition),
|
||||
size = description.modulePrefab.size,
|
||||
orientation = orientation
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Place this module with a position relative to the space it is in.
|
||||
/// </summary>
|
||||
/// <param name="srPosition">The space relative (<i>SR</i>) position of this module. Must be inside the space dimensions.</param>
|
||||
/// <param name="placement">The space relative (<i>SR</i>) placement of this module. Must be inside the space dimensions.</param>
|
||||
/// <exception cref="EngineException">If the position is not inside the space dimensions.</exception>
|
||||
internal void Place(Vector2Int srPosition) {
|
||||
if (space != null && !srPosition.IsInsideRelative(space.rrDimensions))
|
||||
internal void Place(Placement placement) {
|
||||
if (space != null && !placement.position.IsInsideRelative(space.rrPlacement))
|
||||
{
|
||||
throw new EngineException($"Trying to place {this} at {srPosition}, which is outside space dimensions {space.rrDimensions}.");
|
||||
throw new EngineException($"Trying to place {this} at {placement.position} which is outside space dimensions {space.rrPlacement}.");
|
||||
}
|
||||
|
||||
srPlacement = placement;
|
||||
|
||||
srDimensions.Position = srPosition;
|
||||
|
||||
Logger.Log($"{this} has been placed at {srPosition} (SR)", LogType.ModulePlacement);
|
||||
Logger.Log($"{this} has been placed at {placement.position} (SR)", LogType.ModulePlacement);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a position relative to this module to one relative to its space.
|
||||
/// <example>The module relative position <c>(0, 1)</c> should always be in front of the module, wherever it faces.</example>
|
||||
/// <example>The module relative position <c>(0, 0, 1)</c> should always be in front of the module, wherever it faces.</example>
|
||||
/// </summary>
|
||||
/// <param name="mrPosition">The module relative (<i>MR</i>) position that should be converted to a space relative (<i>SR</i>) position.</param>
|
||||
/// <returns></returns>
|
||||
internal Vector2Int ToSpaceRelative(Vector2Int mrPosition)
|
||||
internal Vector3Int ToSpaceRelative(Vector3Int mrPosition)
|
||||
{
|
||||
return srDimensions.Position + orientation switch
|
||||
// ReSharper disable once ArrangeRedundantParentheses
|
||||
return srPlacement.position + (Orientation switch
|
||||
{
|
||||
Orientation.North => mrPosition,
|
||||
Orientation.East => new Vector2Int(mrPosition.y, -mrPosition.x),
|
||||
Orientation.East => new Vector3Int(mrPosition.z, 0, -mrPosition.x),
|
||||
Orientation.South => -mrPosition,
|
||||
Orientation.West => new Vector2Int(-mrPosition.y, mrPosition.x),
|
||||
Orientation.West => new Vector3Int(-mrPosition.z, 0, mrPosition.x),
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
internal virtual void InstantiateModule(Transform parent)
|
||||
@@ -108,8 +104,8 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
||||
Logger.Log($"Instantiating {this}", LogType.ModuleInstantiation);
|
||||
|
||||
State = Object.Instantiate(description.modulePrefab, parent, false);
|
||||
State.transform.localPosition = new Vector3(srDimensions.x + .5f, 0, srDimensions.z + .5f);
|
||||
State.transform.Rotate(Vector3.up, (float)orientation);
|
||||
State.transform.localPosition = new Vector3(SrPosition.x + .5f, 0, SrPosition.z + .5f);
|
||||
State.transform.Rotate(Vector3.up, Orientation.Angle());
|
||||
State.name = ToString();
|
||||
State.SetModule(this);
|
||||
}
|
||||
|
||||
@@ -14,8 +14,6 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
|
||||
public List<PreconditionRequirement> preconditionRequirements = new();
|
||||
[BoxGroup("Requirements")]
|
||||
public List<PlacementRequirement> placementRequirements = new();
|
||||
[BoxGroup("Requirements")]
|
||||
public List<OrientationRequirement> orientationRequirements = new();
|
||||
|
||||
internal bool HasType(ModuleType type)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user