clean up ui
This commit is contained in:
144
Assets/Engine/Runtime/UI/GameControl.cs
Normal file
144
Assets/Engine/Runtime/UI/GameControl.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using EscapeRoomEngine.Engine.Runtime.Measurements;
|
||||
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
||||
using NaughtyAttributes;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.UI
|
||||
{
|
||||
public enum GameState
|
||||
{
|
||||
Stopped, Paused, Running
|
||||
}
|
||||
|
||||
public class GameControl : MonoBehaviour
|
||||
{
|
||||
public static GameControl Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_foundGameControl == null)
|
||||
{
|
||||
_foundGameControl = FindObjectOfType<GameControl>();
|
||||
}
|
||||
return _foundGameControl;
|
||||
}
|
||||
}
|
||||
private static GameControl _foundGameControl;
|
||||
|
||||
[BoxGroup("Internal")] [SerializeField]
|
||||
private Button startButton, stopButton, pauseButton, addTimeButton, removeTimeButton;
|
||||
[BoxGroup("Internal")] [SerializeField]
|
||||
private Text timeText, targetTimeText, estimateTimeText;
|
||||
|
||||
[HideInInspector] public GameState gameState = GameState.Stopped;
|
||||
|
||||
private float _timeElapsed, _targetTime;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_targetTime = Engine.DefaultEngine.initialTargetTime;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
SetTimeText();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Update time
|
||||
if (gameState == GameState.Running)
|
||||
{
|
||||
_timeElapsed += Time.deltaTime;
|
||||
|
||||
SetTimeText();
|
||||
}
|
||||
|
||||
// Enable or disable buttons
|
||||
startButton.interactable = gameState == GameState.Stopped;
|
||||
stopButton.interactable = gameState != GameState.Stopped;
|
||||
pauseButton.interactable = gameState is GameState.Running or GameState.Paused;
|
||||
addTimeButton.interactable = gameState != GameState.Stopped;
|
||||
removeTimeButton.interactable = gameState != GameState.Stopped && _targetTime >= _timeElapsed + 60;
|
||||
}
|
||||
|
||||
#region Time Controls
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
gameState = GameState.Running;
|
||||
|
||||
_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;
|
||||
}
|
||||
|
||||
public void PauseGame()
|
||||
{
|
||||
// ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
|
||||
switch (gameState)
|
||||
{
|
||||
case GameState.Running:
|
||||
gameState = GameState.Paused;
|
||||
pauseButton.GetComponent<PauseButton>().Paused = true;
|
||||
break;
|
||||
case GameState.Paused:
|
||||
gameState = GameState.Running;
|
||||
pauseButton.GetComponent<PauseButton>().Paused = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 (_targetTime + seconds >= _timeElapsed)
|
||||
{
|
||||
_targetTime += seconds;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetTimeText()
|
||||
{
|
||||
timeText.text = TimeToText(_timeElapsed);
|
||||
targetTimeText.text = TimeToText(_targetTime);
|
||||
estimateTimeText.text = TimeToText(_timeElapsed);
|
||||
}
|
||||
|
||||
private static string TimeToText(float time) => $"{time.ToTimeSpan():mm':'ss}";
|
||||
|
||||
#endregion
|
||||
|
||||
public void ExitGame()
|
||||
{
|
||||
StopGame();
|
||||
|
||||
#if UNITY_STANDALONE
|
||||
Application.Quit();
|
||||
#endif
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorApplication.isPlaying = false;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Engine/Runtime/UI/GameControl.cs.meta
Normal file
3
Assets/Engine/Runtime/UI/GameControl.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 676ef7e7d34646dbb24b1978563ab63b
|
||||
timeCreated: 1668937602
|
||||
19
Assets/Engine/Runtime/UI/PauseButton.cs
Normal file
19
Assets/Engine/Runtime/UI/PauseButton.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using NaughtyAttributes;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace EscapeRoomEngine.Engine.Runtime.UI
|
||||
{
|
||||
public class PauseButton : MonoBehaviour
|
||||
{
|
||||
[BoxGroup("Internal")] [SerializeField]
|
||||
private Image icon;
|
||||
[BoxGroup("Internal")] [SerializeField]
|
||||
private Sprite pauseIcon, resumeIcon;
|
||||
|
||||
public bool Paused
|
||||
{
|
||||
set => icon.sprite = value ? resumeIcon : pauseIcon;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Engine/Runtime/UI/PauseButton.cs.meta
Normal file
3
Assets/Engine/Runtime/UI/PauseButton.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 453c346d9a6f46828b7fc21944890efa
|
||||
timeCreated: 1671030945
|
||||
Reference in New Issue
Block a user