add PreconditionRequirement and RelatedModule requirement

This commit is contained in:
2022-11-20 18:05:50 +01:00
parent 1dcd6e67e1
commit 8ee43d6823
21 changed files with 270 additions and 7 deletions

View File

@@ -81,9 +81,10 @@ namespace EscapeRoomEngine.Engine.Runtime
var exit = new Passage(exitDoor);
// add puzzles
for (var i = 0; i < Utilities.Utilities.RandomInclusive(theme.puzzleCount.x, theme.puzzleCount.y); i++)
var puzzleCount = Utilities.Utilities.RandomInclusive(theme.puzzleCount.x, theme.puzzleCount.y);
for (var i = 0; i < puzzleCount; i++)
{
space.AddModuleWithRequirements(new PuzzleModule(space, theme.puzzleTypes.RandomElement()));
space.AddModuleWithRequirements(Module.CreateModuleByType(space, theme.puzzleTypes.RandomElement()));
}
room.AddSpace(space, exit);

View File

@@ -47,7 +47,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
}
else
{
throw new Exception("Tried to set wrong type of module.");
throw new Exception($"Tried to set wrong type of module ({module.GetType()} instead of DoorModule)");
}
}

View File

@@ -103,6 +103,24 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
State.SetModule(this);
}
internal static Module CreateModuleByType(Space space, ModuleDescription description)
{
if (description.HasType(ModuleType.Puzzle) &&
description is PuzzleModuleDescription puzzleModuleDescription)
{
return new PuzzleModule(space, puzzleModuleDescription);
}
else if((description.HasType(ModuleType.DoorEntrance) || description.HasType(ModuleType.DoorExit)) &&
description is DoorModuleDescription doorModuleDescription)
{
return new DoorModule(space, doorModuleDescription);
}
else
{
throw new Exception("Module description does not have fitting type.");
}
}
public override string ToString()
{
return $"Module ({string.Join(", ", description.types.ToList().ConvertAll(type => type.ToString()))})";

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Requirements;
using NaughtyAttributes;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Modules
@@ -9,7 +10,16 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
{
public List<ModuleType> types = new();
public ModuleState modulePrefab;
[BoxGroup("Requirements")]
public List<PreconditionRequirement> preconditionRequirements = new();
[BoxGroup("Requirements")]
public List<PlacementRequirement> placementRequirements = new();
[BoxGroup("Requirements")]
public List<OrientationRequirement> orientationRequirements = new();
internal bool HasType(ModuleType type)
{
return types.Contains(type);
}
}
}

View File

@@ -64,7 +64,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Modules
}
else
{
throw new Exception("Tried to set wrong type of module.");
throw new Exception($"Tried to set wrong type of module ({module.GetType()} instead of PuzzleModule)");
}
}

View File

@@ -11,6 +11,12 @@ namespace EscapeRoomEngine.Engine.Runtime.Requirements
public static bool TryOrienting(Module module, Space space)
{
if (module.description.orientationRequirements.Count == 0)
{
// don't evaluate requirements if there are none
return true;
}
var orientationCandidates = Candidates(
Module.EveryOrientation,
module.description.orientationRequirements,

View File

@@ -14,6 +14,12 @@ namespace EscapeRoomEngine.Engine.Runtime.Requirements
public static bool TryPlacing(Module module, Space space)
{
if (module.description.placementRequirements.Count == 0)
{
// don't evaluate requirements if there are none
return true;
}
var placementCandidates = Candidates(
space.rrDimensions.EveryPosition,
module.description.placementRequirements,

View File

@@ -0,0 +1,44 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using UnityEngine;
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;
namespace EscapeRoomEngine.Engine.Runtime.Requirements
{
public abstract class PreconditionRequirement : Requirement<bool>
{
protected static readonly HashSet<bool> TrueSet = new(new[] { true }), FalseSet = new(new[] { false });
protected abstract override IEnumerable<bool> GenerateCandidates(Module module, Space space);
public static bool CheckPreconditions(Module module, Space space)
{
Debug.Log($"{module}, {module.description.preconditionRequirements}");
if (module.description.preconditionRequirements.Count == 0)
{
// don't evaluate requirements if there are none
return true;
}
var preconditionsMet = Candidates(
TrueSet,
module.description.preconditionRequirements,
module, space)
.Contains(true);
Logger.Log(
preconditionsMet
? $"Preconditions for {module} satisfied"
: $"Could not satisfy preconditions for {module}",
LogType.ModulePlacement);
return preconditionsMet;
}
protected static HashSet<bool> SetFor(bool value)
{
return value ? TrueSet : FalseSet;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a73316a9f52d4c5982f9c7936e322ec4
timeCreated: 1668945634

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using NaughtyAttributes;
using UnityEngine;
namespace EscapeRoomEngine.Engine.Runtime.Requirements
{
[CreateAssetMenu(menuName = "Requirements/Related Module")]
class RelatedModule : PreconditionRequirement
{
[InfoBox("A related module that must be added to the same space successfully before this module is added.")]
[Required]
public ModuleDescription relatedModule;
protected override IEnumerable<bool> GenerateCandidates(Module module, Space space)
{
return new []{ space.AddModuleWithRequirements(Module.CreateModuleByType(space, relatedModule)) };
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6e3f3bf07aae4a03834a7943c255f37d
timeCreated: 1668947627

View File

@@ -42,6 +42,7 @@ namespace EscapeRoomEngine.Engine.Runtime
internal bool AddModuleWithRequirements(Module module)
{
var requirementsFulfilled =
PreconditionRequirement.CheckPreconditions(module, this) &&
PlacementRequirement.TryPlacing(module, this) &&
OrientationRequirement.TryOrienting(module, this);