orientation requirements
This commit is contained in:
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Escape_Room_Engine.Engine.Scripts.Modules;
|
||||
using Escape_Room_Engine.Engine.Scripts.Requirements;
|
||||
using Escape_Room_Engine.Engine.Scripts.Utilities;
|
||||
using UnityEngine;
|
||||
using Logger = Escape_Room_Engine.Engine.Scripts.Utilities.Logger;
|
||||
@@ -94,17 +95,40 @@ namespace Escape_Room_Engine.Engine.Scripts
|
||||
|
||||
// place puzzle
|
||||
var placementCandidates = space.rrDimensions.EveryPosition;
|
||||
puzzle._description.PlacementRequirements.ForEach(requirement =>
|
||||
puzzle._description.RequirementsOfType<PlacementRequirement>().ForEach(requirement =>
|
||||
placementCandidates.IntersectWith(requirement.PlacementCandidates(puzzle, space)));
|
||||
|
||||
Logger.Log($"placement candidates: {string.Join(", ", placementCandidates.ToList().ConvertAll(c => c.ToString()))}", LogType.RequirementResolution);
|
||||
|
||||
if (placementCandidates.Count > 0)
|
||||
{
|
||||
puzzle.Place(placementCandidates.RandomElement());
|
||||
space.AddModule(puzzle);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Log("Could not find suitable placement for puzzle", LogType.PuzzleGeneration);
|
||||
return;
|
||||
}
|
||||
|
||||
// orient puzzle
|
||||
HashSet<Orientation> orientationCandidates = Module.EveryOrientation;
|
||||
Logger.Log($"orientation candidatesA: {string.Join(",", orientationCandidates.ToList().ConvertAll(c => c.ToString()))}", LogType.RequirementResolution);
|
||||
puzzle._description.RequirementsOfType<OrientationRequirement>().ForEach(requirement =>
|
||||
orientationCandidates.IntersectWith(requirement.OrientationCandidates(puzzle, space)));
|
||||
|
||||
Logger.Log($"orientation candidates: {string.Join(",", orientationCandidates.ToList().ConvertAll(c => c.ToString()))}", LogType.RequirementResolution);
|
||||
|
||||
if (orientationCandidates.Count > 0)
|
||||
{
|
||||
puzzle.Orient(orientationCandidates.RandomElement());
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Log("Could not find suitable orientation for puzzle", LogType.PuzzleGeneration);
|
||||
return;
|
||||
}
|
||||
|
||||
space.AddModule(puzzle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Escape_Room_Engine.Engine.Scripts.Utilities;
|
||||
using UnityEngine;
|
||||
@@ -15,6 +16,11 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
|
||||
public class Module
|
||||
{
|
||||
public static HashSet<Orientation> EveryOrientation => new(new[]
|
||||
{
|
||||
Orientation.North, Orientation.East, Orientation.South, Orientation.West
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Get the space relative (<i>SR</i>) position of this module.
|
||||
/// </summary>
|
||||
@@ -67,6 +73,11 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
Logger.Log($"{this} has been placed at {srPosition} (SR)", LogType.ModulePlacement);
|
||||
}
|
||||
|
||||
internal void Orient(Orientation o)
|
||||
{
|
||||
orientation = o;
|
||||
}
|
||||
|
||||
internal void InstantiateModule(Transform parent)
|
||||
{
|
||||
_moduleObject = new GameObject(ToString());
|
||||
@@ -79,7 +90,7 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Module ({string.Join(',', _description.types.ToList().ConvertAll(type => type.ToString()))})";
|
||||
return $"Module ({string.Join(", ", _description.types.ToList().ConvertAll(type => type.ToString()))})";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,9 @@ namespace Escape_Room_Engine.Engine.Scripts.Modules
|
||||
public GameObject modulePrefab;
|
||||
public List<Requirement> requirements = new();
|
||||
|
||||
public List<PlacementRequirement> PlacementRequirements => requirements
|
||||
.FindAll(requirement => requirement is PlacementRequirement)
|
||||
.ConvertAll(requirement => (PlacementRequirement)requirement);
|
||||
public List<T> RequirementsOfType<T>() where T : Requirement
|
||||
{
|
||||
return requirements.FindAll(requirement => requirement is T).ConvertAll(requirement => (T)requirement);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using Escape_Room_Engine.Engine.Scripts.Modules;
|
||||
using Unity.XR.CoreUtils;
|
||||
using UnityEngine;
|
||||
using Logger = Escape_Room_Engine.Engine.Scripts.Utilities.Logger;
|
||||
using LogType = Escape_Room_Engine.Engine.Scripts.Utilities.LogType;
|
||||
|
||||
namespace Escape_Room_Engine.Engine.Scripts.Requirements
|
||||
{
|
||||
[CreateAssetMenu(menuName = "Requirements/Face Space Center")]
|
||||
public class FaceSpaceCenter : OrientationRequirement
|
||||
{
|
||||
public override IEnumerable<Orientation> OrientationCandidates(Module module, Space space)
|
||||
{
|
||||
var orientation = new HashSet<Orientation>(1);
|
||||
float width = space.rrDimensions.width;
|
||||
float length = space.rrDimensions.length;
|
||||
var xRel = module.SrPosition.x / (width - 1);
|
||||
var zRel = module.SrPosition.y / (length - 1);
|
||||
|
||||
Debug.Log($"{xRel}, {zRel}, {1 - xRel}");
|
||||
|
||||
if (zRel > xRel)
|
||||
{
|
||||
orientation.Add(zRel > 1 - xRel ? Orientation.South : Orientation.East);
|
||||
}
|
||||
else
|
||||
{
|
||||
orientation.Add(zRel > 1 - xRel ? Orientation.West : Orientation.North);
|
||||
}
|
||||
|
||||
return orientation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 872da92bb04647e3bd6e741e6bb0a976
|
||||
timeCreated: 1667878978
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using Escape_Room_Engine.Engine.Scripts.Modules;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Escape_Room_Engine.Engine.Scripts.Requirements
|
||||
{
|
||||
public abstract class OrientationRequirement : Requirement
|
||||
{
|
||||
public abstract IEnumerable<Orientation> OrientationCandidates(Module module, Space space);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0704ea5393394baf921f68d2dcbdfaec
|
||||
timeCreated: 1667878220
|
||||
@@ -7,19 +7,19 @@ namespace Escape_Room_Engine.Engine.Scripts.Requirements
|
||||
[CreateAssetMenu(menuName = "Requirements/Place Along Space Edges")]
|
||||
public class PlaceAlongSpaceEdges : PlacementRequirement
|
||||
{
|
||||
public override HashSet<Vector2Int> PlacementCandidates(Module module, Space space)
|
||||
public override IEnumerable<Vector2Int> PlacementCandidates(Module module, Space space)
|
||||
{
|
||||
var edgePositions = new HashSet<Vector2Int>();
|
||||
|
||||
for (var x = 0; x < space.rrDimensions.width; x++)
|
||||
{
|
||||
edgePositions.Add(new Vector2Int(x, 0));
|
||||
edgePositions.Add(new Vector2Int(x, space.rrDimensions.length));
|
||||
edgePositions.Add(new Vector2Int(x, space.rrDimensions.length - 1));
|
||||
}
|
||||
for (var z = 0; z < space.rrDimensions.length; z++)
|
||||
{
|
||||
edgePositions.Add(new Vector2Int(0, z));
|
||||
edgePositions.Add(new Vector2Int(space.rrDimensions.width, z));
|
||||
edgePositions.Add(new Vector2Int(space.rrDimensions.width - 1, z));
|
||||
}
|
||||
|
||||
return edgePositions;
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace Escape_Room_Engine.Engine.Scripts.Requirements
|
||||
[CreateAssetMenu(menuName = "Requirements/Place Anywhere")]
|
||||
public class PlaceAnywhere : PlacementRequirement
|
||||
{
|
||||
public override HashSet<Vector2Int> PlacementCandidates(Module module, Space space)
|
||||
public override IEnumerable<Vector2Int> PlacementCandidates(Module module, Space space)
|
||||
{
|
||||
return space.rrDimensions.EveryPosition;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,6 @@ namespace Escape_Room_Engine.Engine.Scripts.Requirements
|
||||
{
|
||||
public abstract class PlacementRequirement : Requirement
|
||||
{
|
||||
public abstract HashSet<Vector2Int> PlacementCandidates(Module module, Space space);
|
||||
public abstract IEnumerable<Vector2Int> PlacementCandidates(Module module, Space space);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace Escape_Room_Engine.Engine.Scripts.Utilities
|
||||
{
|
||||
public enum LogType
|
||||
{
|
||||
Important, ModulePlacement, PassageConnection, RoomGeneration, PuzzleGeneration
|
||||
Important, ModulePlacement, PassageConnection, RoomGeneration, PuzzleGeneration, RequirementResolution
|
||||
}
|
||||
|
||||
public class Logger : MonoBehaviour
|
||||
|
||||
Reference in New Issue
Block a user