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

@@ -49,8 +49,6 @@ 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);
GenerateRoom();
}
public void GenerateRoom()

View File

@@ -1,4 +1,5 @@
using System;
using EscapeRoomEngine.Engine.Runtime.Measurements;
using UnityEngine;
using UnityEngine.UI;
@@ -11,20 +12,16 @@ namespace EscapeRoomEngine.Engine.Runtime
public class GameControl : MonoBehaviour
{
private const int InitialTime = 5 * 60;
[SerializeField] private Button startButton, stopButton, pauseButton, resumeButton, addMinuteButton, removeMinuteButton;
[SerializeField] private Text timeText;
[HideInInspector] public GameState gameState = GameState.Stopped;
public float TimeRemaining => _totalTime - _timeElapsed;
private float _timeElapsed, _totalTime;
private float _timeElapsed;
private void Start()
{
SetGamemasterTimeText(_totalTime);
SetGamemasterTimeText(_timeElapsed);
}
private void Update()
@@ -32,17 +29,9 @@ namespace EscapeRoomEngine.Engine.Runtime
// Update time
if (gameState == GameState.Running)
{
if (Time.deltaTime <= TimeRemaining)
{
_timeElapsed += Time.deltaTime;
}
else
{
_timeElapsed = _totalTime;
StopGame();
}
_timeElapsed += Time.deltaTime;
SetGamemasterTimeText(TimeRemaining);
SetGamemasterTimeText(_timeElapsed);
}
// Enable or disable buttons
@@ -51,7 +40,7 @@ namespace EscapeRoomEngine.Engine.Runtime
pauseButton.interactable = gameState == GameState.Running;
resumeButton.interactable = gameState == GameState.Paused;
addMinuteButton.interactable = gameState != GameState.Stopped;
removeMinuteButton.interactable = gameState != GameState.Stopped && TimeRemaining >= 60;
removeMinuteButton.interactable = gameState != GameState.Stopped && _timeElapsed >= 10;
}
#region Time Controls
@@ -60,12 +49,23 @@ namespace EscapeRoomEngine.Engine.Runtime
{
gameState = GameState.Running;
_totalTime = InitialTime;
_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()
{
if (gameState != GameState.Stopped)
{
// was running
Measure.EndSession(_timeElapsed);
}
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>
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);
}
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()
{
Engine.DefaultEngine.theme.puzzleTypes.ForEach(puzzle =>
{
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>();

View File

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

View File

@@ -24,6 +24,8 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
[SerializeField]
private string databasePath = "measurements.realm";
public Session Session { get; private set; }
private Realm _realm;
private void OnEnable()
@@ -41,23 +43,28 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
_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)
{
Puzzle created = null;
_realm.Write(() =>
{
created = _realm.Add(new Puzzle(puzzle));
});
_realm.Write(() => created = _realm.Add(new Puzzle(puzzle)));
return created;
}
public Puzzle LoadOrNew(PuzzleModuleDescription puzzle) =>
_realm.Find<Puzzle>(puzzle.Id) ?? New(puzzle);
public Puzzle LoadOrNew(PuzzleModuleDescription puzzle) => _realm.Find<Puzzle>(puzzle.Id) ?? New(puzzle);
public Puzzle Load(PuzzleModuleDescription puzzle) =>
_realm.Find<Puzzle>(puzzle.Id);
public Puzzle Load(PuzzleModuleDescription puzzle) => _realm.Find<Puzzle>(puzzle.Id);
public void Delete(PuzzleModuleDescription puzzle)
{
@@ -76,7 +83,12 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
{
measurement.TimeSolved = Time.time;
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