set room size in ui

This commit is contained in:
2023-05-12 02:59:40 +02:00
parent 939c741787
commit 05c952e7c7
12 changed files with 1680 additions and 100 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -63,7 +63,7 @@ namespace EscapeRoomEngine.Engine.Runtime
{
_playSpaceOrigin = new GameObject("Play Space Origin");
_playSpaceOrigin.transform.SetParent(transform);
_playSpaceOrigin.transform.localPosition = new Vector3(-theme.playSpace.x / 2f, 0, -theme.playSpace.y / 2f);
_playSpaceOrigin.transform.localPosition = new Vector3(-GameControl.Instance.RoomSize.x / 2f, 0, -GameControl.Instance.RoomSize.y / 2f);
}
#region Generation

View File

@@ -13,16 +13,6 @@ namespace EscapeRoomEngine.Engine.Runtime
[CreateAssetMenu(menuName = "Engine Theme")]
public class EngineTheme : ScriptableObject
{
#region Size
[BoxGroup("Size")] [Tooltip("The minimum size that should be allowed for rooms.")]
public Vector2Int minRoomSize;
[BoxGroup("Size")] [Tooltip("The size of the physical play space available to the engine.")]
public Vector2Int playSpace;
#endregion
#region Theme
[BoxGroup("Theme")] [Required]

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Modules.Description;
using EscapeRoomEngine.Engine.Runtime.UI;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using UnityEngine;
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
@@ -51,10 +52,11 @@ namespace EscapeRoomEngine.Engine.Runtime
internal Space(Room room, Passage entrance)
{
this.room = room;
rrPlacement = GenerateSpaceDimensions(
entrance,
Engine.Theme.minRoomSize.ProjectAtFloor(),
Engine.Theme.playSpace.ProjectAtFloor());
rrPlacement = new Placement
{
position = Vector3Int.zero,
size = new Vector2Int(GameControl.Instance.RoomSize.x, GameControl.Instance.RoomSize.y)
};
// connect the space to its entrance passage
entrance.PlaceEntrance(new DoorModule(this,
@@ -146,62 +148,5 @@ namespace EscapeRoomEngine.Engine.Runtime
/// </summary>
/// <param name="rrPosition">The room relative (<i>RR</i>) position that should be converted to a space relative (<i>SR</i>) position.</param>
internal Vector3Int ToSpaceRelative(Vector3Int rrPosition) => rrPosition - rrPlacement.position;
/// <summary>
/// Generate space dimensions that fit the required size constraints and cover the position of the entrance.
/// </summary>
/// <returns>A room relative (<i>RR</i>) placement with the generated dimensions. Always faces North.</returns>
private static Placement GenerateSpaceDimensions(Passage entrance, Vector3Int minSize, Vector3Int availableSpace)
{
var x = new Range(-1, -1);
var z = new Range(-1, -1);
var door = entrance.fromOut;
var position = door.RrPosition;
var bottomLeft = door.srPlacement.BottomLeft;
var doorSize = door.Size;
// fix the side the door is facing away from
switch (door.Orientation)
{
case Orientation.North:
z.min = position.z;
z.max = Utilities.Utilities.RandomInclusive(z.min + minSize.z, availableSpace.z);
break;
case Orientation.East:
x.min = position.x;
x.max = Utilities.Utilities.RandomInclusive(x.min + minSize.x, availableSpace.x);
break;
case Orientation.South:
z.max = position.z + 1;
z.min = Utilities.Utilities.RandomInclusive(0, z.max - minSize.z);
break;
case Orientation.West:
x.max = position.x + 1;
x.min = Utilities.Utilities.RandomInclusive(0, x.max - minSize.x);
break;
default:
throw new ArgumentOutOfRangeException();
}
// calculate remaining values if they haven't been covered by the switch statement yet
if(x.min == -1)
x.min = Utilities.Utilities.RandomInclusive(0, Math.Min(bottomLeft.x, availableSpace.x - minSize.x));
if(x.max == -1)
x.max = Utilities.Utilities.RandomInclusive(Math.Max(bottomLeft.x + doorSize.x, x.min + minSize.x), availableSpace.x);
if(z.min == -1)
z.min = Utilities.Utilities.RandomInclusive(0, Math.Min(bottomLeft.z, availableSpace.z - minSize.z));
if(z.max == -1)
z.max = Utilities.Utilities.RandomInclusive(Math.Max(bottomLeft.z + doorSize.x, z.min + minSize.z), availableSpace.z);
var dimensions = new Placement
{
position = new Vector3Int(x.min, 0, z.min),
size = new Vector2Int(x.Length, z.Length)
};
Logger.Log($"Generated space dimensions {dimensions} from entrance position {position}", LogType.RoomGeneration);
return dimensions;
}
}
}

View File

@@ -19,6 +19,8 @@ namespace EscapeRoomEngine.Engine.Runtime.UI
/// </summary>
public class GameControl : MonoBehaviour
{
public static readonly Vector2Int DefaultRoomSize = new Vector2Int(2, 4);
/// <summary>
/// The active instance of the game control.
/// </summary>
@@ -33,7 +35,11 @@ namespace EscapeRoomEngine.Engine.Runtime.UI
[BoxGroup("Internal")] [SerializeField]
private Button startButton, stopButton, pauseButton, addTimeButton, removeTimeButton;
[BoxGroup("Internal")] [SerializeField]
private Text timeText, roomTimeText, estimateTimeText, targetTimeText, percentileText;
private Text timeText, roomTimeText, estimateTimeText, targetTimeText, percentileText, widthText, lengthText;
[BoxGroup("Internal")] [SerializeField]
private Slider roomWidth, roomLength;
[BoxGroup("Internal")] [SerializeField]
private Transform roomSizeElement;
[BoxGroup("Internal")] [SerializeField]
private PuzzlePlan puzzlePlan;
@@ -67,12 +73,15 @@ namespace EscapeRoomEngine.Engine.Runtime.UI
/// The estimated total time the player will spend in the experience.
/// </summary>
public float EstimatedTime { get; private set; }
public Vector2Int RoomSize { get; private set; }
private float _previousUIUpdate, _previousPlanUpdate;
private void Awake()
{
Instance = this;
RoomSize = DefaultRoomSize;
}
private void Start()
@@ -114,6 +123,8 @@ namespace EscapeRoomEngine.Engine.Runtime.UI
pauseButton.interactable = gameState is GameState.Running or GameState.Paused;
addTimeButton.interactable = gameState is GameState.Running or GameState.Paused;
removeTimeButton.interactable = gameState is GameState.Running or GameState.Paused && TargetTime >= TimeElapsed + 60;
roomWidth.interactable = gameState == GameState.Stopped;
roomLength.interactable = gameState == GameState.Stopped;
}
#region Time Controls
@@ -124,6 +135,8 @@ namespace EscapeRoomEngine.Engine.Runtime.UI
TimeElapsed = 0;
Destroy(roomSizeElement.gameObject);
// generate the first room if it hasn't been generated yet
Engine.Instance.CurrentRoom.Match(none: () => Engine.Instance.GenerateRoom());
@@ -217,6 +230,13 @@ namespace EscapeRoomEngine.Engine.Runtime.UI
#endregion
public void SetRoomSize()
{
RoomSize = new Vector2Int((int)roomWidth.value, (int)roomLength.value);
widthText.text = RoomSize.x.ToString();
lengthText.text = RoomSize.y.ToString();
}
public void ExitGame()
{
StopGame();