StationObscurum/Assets/Scripts/MainMenu/MainMenuManager.cs
2023-11-14 22:11:47 -05:00

141 lines
4.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Steamworks;
public class MainMenuManager : MonoBehaviour
{
[SerializeField] private TMP_Text title;
[SerializeField] private List<TMP_Text> textList = new();
[SerializeField] private float dilateSpeed = 0.1f;
[SerializeField] private Animator cover;
private readonly string component = "_FaceDilate";
private float dilate = -1;
private readonly List<float> dilates = new();
private float initDilate;
private readonly List<float> initDilates = new();
private bool transitioning;
[SerializeField] private Animator cameraAnimator;
[SerializeField] private MainMenuSettingsManager settingsManager;
[SerializeField] private Canvas creditsCanvas;
private bool isAnimating = false;
private bool isDown = false;
private static MainMenuManager instance;
public static MainMenuManager Instance { get { return instance; } }
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
private void Start()
{
//initDilate = title.fontSharedMaterials[0].GetFloat(component);
initDilate = title.fontMaterials[0].GetFloat(component);
foreach (var text in textList)
{
initDilates.Add(text.fontMaterials[0].GetFloat(component));
dilates.Add(-1f);
}
textList[0].GetComponent<Button>().onClick.AddListener(LoadFirstLevel);
textList[1].GetComponent<Button>().onClick.AddListener(LoadSettings);
textList[2].GetComponent<Button>().onClick.AddListener(ExitApp);
textList[3].GetComponent<Button>().onClick.AddListener(LoadCredits);
Cursor.visible = true;
Cursor.lockState = CursorLockMode.Confined;
this.creditsCanvas.gameObject.SetActive(false);
this.settingsManager.gameObject.SetActive(false);
}
// Update is called once per frame
private void Update()
{
//dilate = Mathf.Min(initDilate, dilate += Time.deltaTime);
dilate = Mathf.Lerp(dilate, initDilate, Time.deltaTime * dilateSpeed);
title.fontMaterials[0].SetFloat(component, dilate);
for (var i = 0; i < textList.Count; i++)
{
dilates[i] = Mathf.Lerp(dilates[i], initDilates[i], Time.deltaTime * dilateSpeed);
textList[i].fontMaterials[0].SetFloat(component, dilates[i]);
}
}
private void LoadFirstLevel()
{
if (SteamManager.Initialized)
{
Steamworks.SteamUserStats.GetAchievement("ENTER_THE_STATION", out bool achievementCompleted);
if (!achievementCompleted)
{
SteamUserStats.SetAchievement("ENTER_THE_STATION");
SteamUserStats.StoreStats();
SteamAPI.RunCallbacks();
}
}
if (!transitioning)
{
cover.Play("Cover_load_out");
transitioning = true;
StartCoroutine(_LoadFirstLevel());
}
}
private IEnumerator _LoadFirstLevel()
{
yield return new WaitForSeconds(4);
SceneManager.LoadScene(1);
}
private void ExitApp()
{
Application.Quit();
}
private void LoadSettings()
{
ToggleCamera();
this.creditsCanvas.gameObject.SetActive(false);
this.settingsManager.gameObject.SetActive(true);
}
private void LoadCredits()
{
ToggleCamera();
this.creditsCanvas.gameObject.SetActive(true);
this.settingsManager.gameObject.SetActive(false);
}
public void ToggleCamera()
{
if(!isAnimating)
StartCoroutine(_ToggleCamera());
}
private IEnumerator _ToggleCamera()
{
cameraAnimator.Play(isDown?"CameraUp":"CameraDown");
isAnimating = true;
yield return new WaitForSeconds(1.5f);
isAnimating = false;
isDown = !isDown;
}
}