2023-04-22 09:18:21 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Rendering;
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
2023-06-02 06:30:58 +02:00
|
|
|
|
2023-06-01 20:25:46 +02:00
|
|
|
namespace Game
|
|
|
|
{
|
2023-04-22 09:18:21 +02:00
|
|
|
public class InGameManager : MonoBehaviour
|
|
|
|
{
|
2023-06-02 06:30:58 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
[SerializeField] private Volume gameVolume;
|
2023-06-02 06:30:58 +02:00
|
|
|
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
[SerializeField] private Volume pausedVolume;
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
[SerializeField] private bool isPaused;
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
// [SerializeField]
|
2023-05-18 06:08:40 +02:00
|
|
|
//private float pauseTransitionDuration = 1f;
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
[SerializeField] private Canvas gameCanvas;
|
|
|
|
|
|
|
|
[SerializeField] private Canvas pausedCanvas;
|
|
|
|
|
|
|
|
[SerializeField] private List<AudioSource> sounds = new();
|
|
|
|
|
|
|
|
private readonly List<float> initSoundVolumes = new();
|
|
|
|
private bool isTransitioning;
|
|
|
|
|
|
|
|
public bool IsPaused => isPaused;
|
2023-06-02 06:30:58 +02:00
|
|
|
[SerializeField] private Scriptable.GameState state;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
isPaused = false;
|
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
Cursor.visible = isPaused;
|
|
|
|
gameVolume.weight = 1;
|
|
|
|
pausedVolume.weight = 0;
|
|
|
|
gameCanvas.gameObject.SetActive(true);
|
|
|
|
pausedCanvas.gameObject.SetActive(false);
|
|
|
|
|
|
|
|
foreach (var source in sounds) initSoundVolumes.Add(source.volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
private void Update()
|
|
|
|
{
|
2023-06-02 06:30:58 +02:00
|
|
|
state.IsPaused = isPaused;
|
2023-06-01 17:03:48 +02:00
|
|
|
if (Input.GetButtonDown("Pause")) TogglePause();
|
|
|
|
|
|
|
|
if (isTransitioning || true)
|
|
|
|
{
|
|
|
|
if (isPaused)
|
|
|
|
{
|
|
|
|
//transition into pause
|
|
|
|
|
|
|
|
//gameVolume.weight = Mathf.Lerp(gameVolume.weight, 0, Time.deltaTime);
|
|
|
|
//pausedVolume.weight = Mathf.Lerp(pausedVolume.weight, 1, Time.deltaTime);
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
//gameVolume.weight = gameVolume.weight < 0.1 ? 0 : gameVolume.weight;
|
|
|
|
//pausedVolume.weight = pausedVolume.weight > 0.9 ? 1 : pausedVolume.weight;
|
|
|
|
gameVolume.weight = 0;
|
|
|
|
pausedVolume.weight = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//transition out of pause
|
|
|
|
|
|
|
|
gameVolume.weight = 1;
|
|
|
|
pausedVolume.weight = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 09:18:21 +02:00
|
|
|
|
|
|
|
public void TogglePause()
|
|
|
|
{
|
|
|
|
if (!isTransitioning)
|
|
|
|
{
|
|
|
|
isPaused = !isPaused;
|
|
|
|
Cursor.visible = isPaused;
|
|
|
|
if (!isPaused)
|
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
else
|
|
|
|
Cursor.lockState = CursorLockMode.None;
|
|
|
|
StartCoroutine(pauseTransition());
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-22 09:18:21 +02:00
|
|
|
public void UnPause()
|
|
|
|
{
|
|
|
|
if (!isTransitioning)
|
|
|
|
{
|
|
|
|
isPaused = false;
|
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
Cursor.visible = isPaused;
|
|
|
|
StartCoroutine(pauseTransition());
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-22 09:18:21 +02:00
|
|
|
public void Pause()
|
|
|
|
{
|
|
|
|
if (!isTransitioning)
|
|
|
|
{
|
|
|
|
isPaused = true;
|
|
|
|
Cursor.lockState = CursorLockMode.None;
|
|
|
|
Cursor.visible = isPaused;
|
|
|
|
StartCoroutine(pauseTransition());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
private IEnumerator pauseTransition()
|
2023-04-22 09:18:21 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
if (pausedCanvas.gameObject.activeInHierarchy && !isPaused) pausedCanvas.gameObject.SetActive(false);
|
|
|
|
if (gameCanvas.gameObject.activeInHierarchy && isPaused) gameCanvas.gameObject.SetActive(false);
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
isTransitioning = true;
|
2023-04-22 09:18:21 +02:00
|
|
|
yield return new WaitForSeconds(0);
|
2023-06-01 17:03:48 +02:00
|
|
|
isTransitioning = false;
|
|
|
|
print("Unpause canvas?" + isPaused + "," + pausedCanvas.gameObject.activeInHierarchy);
|
|
|
|
if (!pausedCanvas.gameObject.activeInHierarchy && isPaused) pausedCanvas.gameObject.SetActive(true);
|
|
|
|
if (!gameCanvas.gameObject.activeInHierarchy && !isPaused) gameCanvas.gameObject.SetActive(true);
|
2023-05-18 06:08:40 +02:00
|
|
|
}
|
|
|
|
|
2023-04-22 09:18:21 +02:00
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
public void SetVolume(float volume)
|
2023-04-22 09:18:21 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
for (var i = 0; i < sounds.Count; i++) sounds[i].volume = initSoundVolumes[i] * volume;
|
2023-04-22 09:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ExitToMenu()
|
|
|
|
{
|
|
|
|
SceneManager.LoadScene(0);
|
|
|
|
}
|
2023-06-01 20:25:46 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
}
|