This commit is contained in:
2022-12-08 10:24:00 +01:00
parent 99187bc752
commit b3c53031c1
8 changed files with 90 additions and 36 deletions

View File

@@ -123,7 +123,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0 m_HorizontalOverflow: 0
m_VerticalOverflow: 0 m_VerticalOverflow: 0
m_LineSpacing: 1 m_LineSpacing: 1
m_Text: +1min m_Text: +10sec
--- !u!1 &4522052737580626021 --- !u!1 &4522052737580626021
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -352,7 +352,7 @@ GameObject:
- component: {fileID: 4522052738465728852} - component: {fileID: 4522052738465728852}
- component: {fileID: 4522052738465728855} - component: {fileID: 4522052738465728855}
m_Layer: 8 m_Layer: 8
m_Name: Add Minute Button m_Name: Add Time Button
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@@ -469,7 +469,7 @@ MonoBehaviour:
m_Arguments: m_Arguments:
m_ObjectArgument: {fileID: 0} m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 60 m_IntArgument: 10
m_FloatArgument: 0 m_FloatArgument: 0
m_StringArgument: m_StringArgument:
m_BoolArgument: 0 m_BoolArgument: 0
@@ -1484,7 +1484,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0 m_HorizontalOverflow: 0
m_VerticalOverflow: 0 m_VerticalOverflow: 0
m_LineSpacing: 1 m_LineSpacing: 1
m_Text: -1min m_Text: -10sec
--- !u!1 &4522052739295172462 --- !u!1 &4522052739295172462
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -1498,7 +1498,7 @@ GameObject:
- component: {fileID: 4522052739295172461} - component: {fileID: 4522052739295172461}
- component: {fileID: 4522052739295172460} - component: {fileID: 4522052739295172460}
m_Layer: 8 m_Layer: 8
m_Name: Remove Minute Button m_Name: Remove Time Button
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@@ -1615,7 +1615,7 @@ MonoBehaviour:
m_Arguments: m_Arguments:
m_ObjectArgument: {fileID: 0} m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: -60 m_IntArgument: -10
m_FloatArgument: 0 m_FloatArgument: 0
m_StringArgument: m_StringArgument:
m_BoolArgument: 0 m_BoolArgument: 0

View File

@@ -49,8 +49,6 @@ namespace EscapeRoomEngine.Engine.Runtime
_playSpaceOrigin = new GameObject("Play Space Origin"); _playSpaceOrigin = new GameObject("Play Space Origin");
_playSpaceOrigin.transform.SetParent(transform); _playSpaceOrigin.transform.SetParent(transform);
_playSpaceOrigin.transform.localPosition = new Vector3(-theme.playSpace.x / 2f, 0, -theme.playSpace.y / 2f); _playSpaceOrigin.transform.localPosition = new Vector3(-theme.playSpace.x / 2f, 0, -theme.playSpace.y / 2f);
GenerateRoom();
} }
public void GenerateRoom() public void GenerateRoom()

View File

@@ -1,4 +1,5 @@
using System; using System;
using EscapeRoomEngine.Engine.Runtime.Measurements;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@@ -11,38 +12,26 @@ namespace EscapeRoomEngine.Engine.Runtime
public class GameControl : MonoBehaviour public class GameControl : MonoBehaviour
{ {
private const int InitialTime = 5 * 60;
[SerializeField] private Button startButton, stopButton, pauseButton, resumeButton, addMinuteButton, removeMinuteButton; [SerializeField] private Button startButton, stopButton, pauseButton, resumeButton, addMinuteButton, removeMinuteButton;
[SerializeField] private Text timeText; [SerializeField] private Text timeText;
[HideInInspector] public GameState gameState = GameState.Stopped; [HideInInspector] public GameState gameState = GameState.Stopped;
public float TimeRemaining => _totalTime - _timeElapsed; private float _timeElapsed;
private float _timeElapsed, _totalTime;
private void Start() private void Start()
{ {
SetGamemasterTimeText(_totalTime); SetGamemasterTimeText(_timeElapsed);
} }
private void Update() private void Update()
{ {
// Update time // Update time
if (gameState == GameState.Running) if (gameState == GameState.Running)
{
if (Time.deltaTime <= TimeRemaining)
{ {
_timeElapsed += Time.deltaTime; _timeElapsed += Time.deltaTime;
}
else
{
_timeElapsed = _totalTime;
StopGame();
}
SetGamemasterTimeText(TimeRemaining); SetGamemasterTimeText(_timeElapsed);
} }
// Enable or disable buttons // Enable or disable buttons
@@ -51,7 +40,7 @@ namespace EscapeRoomEngine.Engine.Runtime
pauseButton.interactable = gameState == GameState.Running; pauseButton.interactable = gameState == GameState.Running;
resumeButton.interactable = gameState == GameState.Paused; resumeButton.interactable = gameState == GameState.Paused;
addMinuteButton.interactable = gameState != GameState.Stopped; addMinuteButton.interactable = gameState != GameState.Stopped;
removeMinuteButton.interactable = gameState != GameState.Stopped && TimeRemaining >= 60; removeMinuteButton.interactable = gameState != GameState.Stopped && _timeElapsed >= 10;
} }
#region Time Controls #region Time Controls
@@ -60,12 +49,23 @@ namespace EscapeRoomEngine.Engine.Runtime
{ {
gameState = GameState.Running; gameState = GameState.Running;
_totalTime = InitialTime;
_timeElapsed = 0; _timeElapsed = 0;
// generate the first room if it hasn't been generated yet
Engine.DefaultEngine.CurrentRoom.Match(none: () => Engine.DefaultEngine.GenerateRoom());
// start a new session
Measure.StartSession();
} }
public void StopGame() public void StopGame()
{ {
if (gameState != GameState.Stopped)
{
// was running
Measure.EndSession(_timeElapsed);
}
gameState = GameState.Stopped; gameState = GameState.Stopped;
} }
@@ -85,9 +85,9 @@ namespace EscapeRoomEngine.Engine.Runtime
/// <param name="seconds">The amount of seconds that will be added to the time. Can be negative to remove time.</param> /// <param name="seconds">The amount of seconds that will be added to the time. Can be negative to remove time.</param>
public void ChangeTime(int seconds) public void ChangeTime(int seconds)
{ {
if (_totalTime + seconds >= 0) if (_timeElapsed + seconds >= 0)
{ {
_totalTime += seconds; _timeElapsed += seconds;
} }
} }

View File

@@ -31,12 +31,22 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
Logger.Log($"Solved {puzzle} with measurement {measurement}", LogType.Measuring); Logger.Log($"Solved {puzzle} with measurement {measurement}", LogType.Measuring);
} }
public static void StartSession() => PuzzleStorage.Instance.StartSession();
public static void EndSession(float time)
{
PuzzleStorage.Instance.EndSession(time);
Logger.Log(PuzzleStorage.Instance.Session.ToString(), LogType.Measuring);
}
public static void LogAllMeasurements() public static void LogAllMeasurements()
{ {
Engine.DefaultEngine.theme.puzzleTypes.ForEach(puzzle => Engine.DefaultEngine.theme.puzzleTypes.ForEach(puzzle =>
{ {
Logger.Log(PuzzleStorage.Instance.LoadOrNew(puzzle).ToString(), LogType.Measuring); Logger.Log(PuzzleStorage.Instance.LoadOrNew(puzzle).ToString(), LogType.Measuring);
}); });
Logger.Log(PuzzleStorage.Instance.Session.ToString(), LogType.Measuring);
} }
public static void Clear() => _runningMeasurements = new Dictionary<int, PuzzleMeasurement>(); public static void Clear() => _runningMeasurements = new Dictionary<int, PuzzleMeasurement>();

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using EscapeRoomEngine.Engine.Runtime.Modules; using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Utilities; using EscapeRoomEngine.Engine.Runtime.Utilities;
@@ -7,6 +8,7 @@ using Realms;
namespace EscapeRoomEngine.Engine.Runtime.Measurements namespace EscapeRoomEngine.Engine.Runtime.Measurements
{ {
[SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")]
public class Puzzle : RealmObject public class Puzzle : RealmObject
{ {
[PrimaryKey] [PrimaryKey]

View File

@@ -24,6 +24,8 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
[SerializeField] [SerializeField]
private string databasePath = "measurements.realm"; private string databasePath = "measurements.realm";
public Session Session { get; private set; }
private Realm _realm; private Realm _realm;
private void OnEnable() private void OnEnable()
@@ -41,23 +43,28 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
_realm?.Dispose(); _realm?.Dispose();
} }
#region Session
public void StartSession() => _realm.Write(() => Session = _realm.Add(new Session()));
public void EndSession(float time) =>_realm.Write(() => Session.Time = time);
#endregion
#region Puzzles
public Puzzle New(PuzzleModuleDescription puzzle) public Puzzle New(PuzzleModuleDescription puzzle)
{ {
Puzzle created = null; Puzzle created = null;
_realm.Write(() => _realm.Write(() => created = _realm.Add(new Puzzle(puzzle)));
{
created = _realm.Add(new Puzzle(puzzle));
});
return created; return created;
} }
public Puzzle LoadOrNew(PuzzleModuleDescription puzzle) => public Puzzle LoadOrNew(PuzzleModuleDescription puzzle) => _realm.Find<Puzzle>(puzzle.Id) ?? New(puzzle);
_realm.Find<Puzzle>(puzzle.Id) ?? New(puzzle);
public Puzzle Load(PuzzleModuleDescription puzzle) => public Puzzle Load(PuzzleModuleDescription puzzle) => _realm.Find<Puzzle>(puzzle.Id);
_realm.Find<Puzzle>(puzzle.Id);
public void Delete(PuzzleModuleDescription puzzle) public void Delete(PuzzleModuleDescription puzzle)
{ {
@@ -76,7 +83,12 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
{ {
measurement.TimeSolved = Time.time; measurement.TimeSolved = Time.time;
found.Measurements.Add(measurement); found.Measurements.Add(measurement);
// add solved puzzle to session
Session.PuzzlesSolved.Add(found);
}); });
} }
#endregion
} }
} }

View File

@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using JetBrains.Annotations;
using MongoDB.Bson;
using Realms;
namespace EscapeRoomEngine.Engine.Runtime.Measurements
{
[SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")]
public class Session : RealmObject
{
[PrimaryKey]
public ObjectId ID { get; set; }
public float Time { get; set; }
public IList<Puzzle> PuzzlesSolved { get; }
[UsedImplicitly]
public Session()
{
ID = ObjectId.GenerateNewId();
}
public override string ToString()
{
return $"Session {ID}: {PuzzlesSolved.Count} puzzles solved in {Time.ToTimeSpan():m':'ss}";
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 94ad8a288d6b4794b6485f026e94267e
timeCreated: 1670892444