add gamemaster canvas

This commit is contained in:
2022-11-20 11:15:11 +01:00
parent 1f823813e0
commit def03954a0
10 changed files with 2188 additions and 77 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0e23fe57328d4ca89779b491fb194e72
timeCreated: 1668937570

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 220c55da163c4bc381099af8030cc0d5
timeCreated: 1668937570

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6ab6b46f22ab480fbd1c85d5e8601025
timeCreated: 1668937536

View File

@@ -0,0 +1,129 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace Escape_Room_Engine.Engine.Scripts
{
public enum GameState
{
Stopped, Paused, Running
}
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 void Start()
{
SetGamemasterTimeText(_totalTime);
}
private void Update()
{
// Update time
if (gameState == GameState.Running)
{
if (Time.deltaTime <= TimeRemaining)
{
_timeElapsed += Time.deltaTime;
}
else
{
_timeElapsed = _totalTime;
StopGame();
}
SetGamemasterTimeText(TimeRemaining);
}
// Enable or disable buttons
startButton.interactable = gameState == GameState.Stopped;
stopButton.interactable = gameState != GameState.Stopped;
pauseButton.interactable = gameState == GameState.Running;
resumeButton.interactable = gameState == GameState.Paused;
addMinuteButton.interactable = gameState != GameState.Stopped;
removeMinuteButton.interactable = gameState != GameState.Stopped && TimeRemaining >= 60;
}
#region Time Controls
public void StartGame()
{
gameState = GameState.Running;
_totalTime = InitialTime;
_timeElapsed = 0;
}
public void StopGame()
{
gameState = GameState.Stopped;
}
public void PauseGame()
{
gameState = GameState.Paused;
}
public void ResumeGame()
{
gameState = GameState.Running;
}
/// <summary>
/// Change the allowed time by a specified amount of seconds.
/// </summary>
/// <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)
{
_totalTime += seconds;
}
}
private void SetGamemasterTimeText(float time)
{
if (timeText != null)
{
timeText.text = TimeToText(time);
}
}
private static string TimeToText(float time)
{
var minutes = (int) (time / 60);
var seconds = (int) Math.Ceiling(time - minutes * 60);
if (seconds == 60)
{
minutes += 1;
seconds = 0;
}
return $"{minutes:D2}:{seconds:D2}";
}
#endregion
public void ExitGame()
{
StopGame();
#if UNITY_STANDALONE
Application.Quit();
#endif
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 676ef7e7d34646dbb24b1978563ab63b
timeCreated: 1668937602