HeavenStudioPlus/Assets/Scripts/GlobalGameManager.cs

408 lines
15 KiB
C#
Raw Normal View History

using System.IO;
using System.Collections;
2021-12-19 04:10:43 +00:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using DG.Tweening;
using TMPro;
2021-12-19 04:10:43 +00:00
using HeavenStudio.Common;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
public class GlobalGameManager : MonoBehaviour
{
public static GlobalGameManager instance { get; set; }
[Header("Loading Screen")]
[SerializeField] Image fadeImage;
[SerializeField] TMP_Text loadingText;
2021-12-19 04:10:43 +00:00
[Header("Dialog Box")]
[SerializeField] GameObject messagePanel;
[SerializeField] TMP_Text messageHeader;
[SerializeField] TMP_Text messageBody;
[SerializeField] TMP_Text errorBuild;
[SerializeField] Button errorOkButton;
[SerializeField] Button errorLogButton;
[SerializeField] Slider dialogProgress;
public static bool IsShowingDialog;
public static string buildTime = "00/00/0000 00:00:00";
public static bool HasShutDown = false;
public static bool discordDuringTesting = false;
2021-12-19 04:10:43 +00:00
static string loadedScene;
static string lastLoadedScene;
static AsyncOperation asyncLoad;
2021-12-19 04:10:43 +00:00
2021-12-21 01:10:49 +00:00
public static string levelLocation;
public static bool officialLevel;
2021-12-19 04:10:43 +00:00
public static bool IsFirstBoot = false;
2022-07-09 02:11:55 +00:00
public static int CustomScreenWidth = 1280;
public static int CustomScreenHeight = 720;
public static readonly (int width, int height)[] DEFAULT_SCREEN_SIZES = new[] { (1280, 720), (1920, 1080), (2560, 1440), (3840, 2160)};
public static readonly string[] DEFAULT_SCREEN_SIZES_STRING = new[] { "1280x720", "1920x1080", "2560x1440", "3840x2160", "Custom" };
public static int ScreenSizeIndex = 0;
2022-07-10 21:39:14 +00:00
public static float MasterVolume = 0.8f;
public static int currentDspSize = 512;
Game Overlays (#280) * add accuracy display * temp BG for show * separate overlays prefab make proper shader for star effects * aim shakiness display * implement testing skill star * fully functional skill star * separate section display from editor * fully separate chart section display from timeline * add section to overlays * fix nullreference issues * start game layout settings * add game settings script * fix nonfunctioning scoring * invert y position logic on timing bar * add perfect challenge functionality * fix section not showing up in editor add perfect challenge option * add timing display minimal mode * Update PerfectAndPractice.png * show gismo for minigame bounds in editor * add ability to disable overlays in editor * prepare medals add new timing display graphic * hide screen preview * per-axis camera control added per request * section medals basic functionality * add medal get animations * fix bug with perfect icons * visual enhancements * adjust look of timing display minmode address audio ducking issues(?) * prepare overlay lyt editor add viewport pan, rotate, scale adjust audio setting * add layout editor UI elements * dynamic overlay creation * fix default single timing disp * set up overlay settings controls * start UI events * runtime uuid for component reference * layout editor affects overlay elements * show overlay element previews while editing * advanced audio settings * fix bug in drop-down creation * fallback defaults for the new stuff * fix textbox & overlay visibility bugs
2023-03-11 04:51:22 +00:00
public static int currentSampleRate = 44100;
public static readonly int[] DSP_BUFFER_SIZES =
{
128, 256, 340, 480, 512, 1024
};
public static readonly int[] SAMPLE_RATES =
{
22050, 44100, 48000, 88200, 96000,
};
public static RenderTexture GameRenderTexture;
public static RenderTexture OverlayRenderTexture;
2022-07-10 21:39:14 +00:00
2021-12-21 01:10:49 +00:00
public enum Scenes : int
{
SplashScreen = 0,
Menu = 1,
Editor = 2,
Game = 3
}
2021-12-19 04:10:43 +00:00
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
2021-12-21 01:10:49 +00:00
public static void Init()
{
Application.wantsToQuit += WantsToQuit;
Application.quitting += OnQuitting;
2021-12-21 01:10:49 +00:00
BasicCheck();
2021-12-19 04:10:43 +00:00
Minigames.InitPreprocessor();
loadedScene = SceneManager.GetActiveScene().name;
PersistentDataManager.LoadSettings();
ScreenSizeIndex = PersistentDataManager.gameSettings.resolutionIndex;
CustomScreenWidth = PersistentDataManager.gameSettings.resolutionWidth;
CustomScreenHeight = PersistentDataManager.gameSettings.resolutionHeight;
ChangeMasterVolume(PersistentDataManager.gameSettings.masterVolume);
Game Overlays (#280) * add accuracy display * temp BG for show * separate overlays prefab make proper shader for star effects * aim shakiness display * implement testing skill star * fully functional skill star * separate section display from editor * fully separate chart section display from timeline * add section to overlays * fix nullreference issues * start game layout settings * add game settings script * fix nonfunctioning scoring * invert y position logic on timing bar * add perfect challenge functionality * fix section not showing up in editor add perfect challenge option * add timing display minimal mode * Update PerfectAndPractice.png * show gismo for minigame bounds in editor * add ability to disable overlays in editor * prepare medals add new timing display graphic * hide screen preview * per-axis camera control added per request * section medals basic functionality * add medal get animations * fix bug with perfect icons * visual enhancements * adjust look of timing display minmode address audio ducking issues(?) * prepare overlay lyt editor add viewport pan, rotate, scale adjust audio setting * add layout editor UI elements * dynamic overlay creation * fix default single timing disp * set up overlay settings controls * start UI events * runtime uuid for component reference * layout editor affects overlay elements * show overlay element previews while editing * advanced audio settings * fix bug in drop-down creation * fallback defaults for the new stuff * fix textbox & overlay visibility bugs
2023-03-11 04:51:22 +00:00
if (PersistentDataManager.gameSettings.dspSize == 0)
PersistentDataManager.gameSettings.dspSize = 512;
Game Overlays (#280) * add accuracy display * temp BG for show * separate overlays prefab make proper shader for star effects * aim shakiness display * implement testing skill star * fully functional skill star * separate section display from editor * fully separate chart section display from timeline * add section to overlays * fix nullreference issues * start game layout settings * add game settings script * fix nonfunctioning scoring * invert y position logic on timing bar * add perfect challenge functionality * fix section not showing up in editor add perfect challenge option * add timing display minimal mode * Update PerfectAndPractice.png * show gismo for minigame bounds in editor * add ability to disable overlays in editor * prepare medals add new timing display graphic * hide screen preview * per-axis camera control added per request * section medals basic functionality * add medal get animations * fix bug with perfect icons * visual enhancements * adjust look of timing display minmode address audio ducking issues(?) * prepare overlay lyt editor add viewport pan, rotate, scale adjust audio setting * add layout editor UI elements * dynamic overlay creation * fix default single timing disp * set up overlay settings controls * start UI events * runtime uuid for component reference * layout editor affects overlay elements * show overlay element previews while editing * advanced audio settings * fix bug in drop-down creation * fallback defaults for the new stuff * fix textbox & overlay visibility bugs
2023-03-11 04:51:22 +00:00
if (PersistentDataManager.gameSettings.sampleRate == 0)
PersistentDataManager.gameSettings.sampleRate = 44100;
currentDspSize = PersistentDataManager.gameSettings.dspSize;
currentSampleRate = PersistentDataManager.gameSettings.sampleRate;
ChangeAudioSettings(currentDspSize, currentSampleRate);
Application.targetFrameRate = -1;
QualitySettings.vSyncCount = 0;
QualitySettings.maxQueuedFrames = 1;
if (PersistentDataManager.gameSettings.isFullscreen)
{
Screen.SetResolution(Display.main.systemWidth, Display.main.systemHeight, FullScreenMode.ExclusiveFullScreen);
Screen.fullScreen = true;
}
else
{
Screen.fullScreenMode = FullScreenMode.Windowed;
Screen.fullScreen = false;
ChangeScreenSize();
}
PlayerInput.InitInputControllers();
#if UNITY_EDITOR
Starpelly.OS.ChangeWindowTitle("Heaven Studio UNITYEDITOR ");
buildTime = "(EDITOR) " + System.DateTime.UtcNow.ToString("dd/MM/yyyy hh:mm:ss");
#else
Starpelly.OS.ChangeWindowTitle("Heaven Studio (INDEV) " + Application.buildGUID.Substring(0, 8));
buildTime = Application.buildGUID.Substring(0, 8) + " " + AppInfo.Date.ToString("dd/MM/yyyy hh:mm:ss");
#endif
2021-12-21 01:10:49 +00:00
}
2021-12-19 04:10:43 +00:00
public void Awake()
2021-12-21 01:10:49 +00:00
{
DontDestroyOnLoad(this.gameObject);
instance = this;
fadeImage.gameObject.SetActive(false);
loadingText.enabled = false;
messagePanel.SetActive(false);
IsShowingDialog = false;
}
private void Update()
{
PlayerInput.UpdateInputControllers();
}
IEnumerator LoadSceneAsync(string scene, float fadeOut)
{
Application.backgroundLoadingPriority = ThreadPriority.Normal;
//TODO: create flow mem loading icon
asyncLoad = SceneManager.LoadSceneAsync(scene);
while (!asyncLoad.isDone)
{
yield return null;
}
//TODO: fade out flow mem loading icon
instance.fadeImage.DOKill();
instance.loadingText.enabled = false;
instance.fadeImage.DOFade(0, fadeOut).OnComplete(() =>
{
instance.fadeImage.gameObject.SetActive(false);
});
2021-12-21 01:10:49 +00:00
}
2021-12-19 04:10:43 +00:00
IEnumerator ForceFadeAsync(float hold, float fadeOut)
{
yield return new WaitForSeconds(hold);
instance.fadeImage.DOKill();
instance.loadingText.enabled = false;
instance.fadeImage.DOFade(0, fadeOut).OnComplete(() =>
{
instance.fadeImage.gameObject.SetActive(false);
});
}
2021-12-19 04:10:43 +00:00
public void HideDialog()
{
Debug.Log("Hiding dialog");
messagePanel.SetActive(false);
IsShowingDialog = false;
}
public void OpenLogFolder()
{
// TODO
}
2021-12-21 01:10:49 +00:00
public static void BasicCheck()
2021-12-19 04:10:43 +00:00
{
2021-12-21 01:10:49 +00:00
if (FindGGM() == null)
{
// load the global game manager prefab
GameObject ggm = Instantiate(Resources.Load("Prefabs/GlobalGameManager") as GameObject);
DontDestroyOnLoad(ggm);
2021-12-21 01:10:49 +00:00
}
2021-12-19 04:10:43 +00:00
}
2021-12-21 01:10:49 +00:00
public static GameObject FindGGM()
{
if (instance != null)
return instance.gameObject;
2021-12-21 01:10:49 +00:00
else
return null;
}
2021-12-19 04:10:43 +00:00
public static void LoadScene(string scene, float fadeIn = 0.35f, float fadeOut = 0.35f)
2021-12-21 01:10:49 +00:00
{
if (scene == loadedScene)
return;
lastLoadedScene = loadedScene;
loadedScene = scene;
instance.fadeImage.DOKill();
instance.fadeImage.gameObject.SetActive(true);
instance.fadeImage.color = new Color(0, 0, 0, 0);
instance.fadeImage.DOFade(1, fadeIn).OnComplete(() =>
{
instance.StartCoroutine(instance.LoadSceneAsync(scene, fadeOut));
instance.loadingText.enabled = true;
});
}
2021-12-19 04:10:43 +00:00
public static void ForceFade(float fadeIn, float hold, float fadeOut)
{
instance.fadeImage.DOKill();
instance.fadeImage.gameObject.SetActive(true);
instance.fadeImage.color = new Color(0, 0, 0, 0);
instance.loadingText.enabled = false;
instance.fadeImage.DOFade(1, fadeIn).OnComplete(() =>
{
instance.StartCoroutine(instance.ForceFadeAsync(hold, fadeOut));
});
2021-12-21 01:10:49 +00:00
}
2022-07-08 22:49:15 +00:00
public static void ShowErrorMessage(string header, string message)
{
IsShowingDialog = true;
if (Conductor.instance != null && Conductor.instance.isPlaying)
Conductor.instance.Pause();
instance.messageHeader.text = header;
instance.messageBody.text = message;
instance.errorOkButton.gameObject.SetActive(true);
// instance.errorLogButton.gameObject.SetActive(true);
instance.dialogProgress.gameObject.SetActive(false);
instance.errorBuild.gameObject.SetActive(true);
#if UNITY_EDITOR
instance.errorBuild.text = "(EDITOR) " + System.DateTime.UtcNow.ToString("dd/MM/yyyy hh:mm:ss");
#else
instance.errorBuild.text = Application.buildGUID.Substring(0, 8) + " " + AppInfo.Date.ToString("dd/MM/yyyy hh:mm:ss");
#endif
instance.messagePanel.SetActive(true);
}
public static void ShowLoadingMessage(string header, string message, float progress = -1)
{
Debug.Log("ShowLoadingMessage");
IsShowingDialog = true;
if (Conductor.instance != null && Conductor.instance.isPlaying)
Conductor.instance.Pause();
instance.messageHeader.text = header;
instance.messageBody.text = message;
instance.errorOkButton.gameObject.SetActive(false);
instance.errorBuild.gameObject.SetActive(false);
// instance.errorLogButton.gameObject.SetActive(false);
if (progress >= 0)
{
instance.dialogProgress.gameObject.SetActive(true);
instance.dialogProgress.value = progress;
}
instance.messagePanel.SetActive(true);
}
public static void SetLoadingMessageProgress(float progress)
{
if (IsShowingDialog)
{
instance.dialogProgress.gameObject.SetActive(true);
instance.dialogProgress.value = progress;
}
}
2022-07-08 22:49:15 +00:00
public static void WindowFullScreen()
{
2022-07-09 02:11:55 +00:00
if (!Screen.fullScreen)
2022-07-08 22:49:15 +00:00
{
// Set the resolution to the display's current resolution
Screen.SetResolution(Display.main.systemWidth, Display.main.systemHeight, FullScreenMode.ExclusiveFullScreen);
2022-07-09 02:11:55 +00:00
Screen.fullScreen = true;
PersistentDataManager.gameSettings.isFullscreen = true;
2022-07-08 22:49:15 +00:00
}
else
{
2022-07-09 02:11:55 +00:00
Screen.SetResolution(1280, 720, FullScreenMode.Windowed);
Screen.fullScreen = false;
PersistentDataManager.gameSettings.isFullscreen = false;
2022-07-08 22:49:15 +00:00
}
}
2021-12-21 01:10:49 +00:00
2022-07-09 02:11:55 +00:00
public static void ChangeScreenSize()
{
FullScreenMode mode = Screen.fullScreenMode;
if (ScreenSizeIndex == DEFAULT_SCREEN_SIZES_STRING.Length - 1)
{
Screen.SetResolution(CustomScreenWidth, CustomScreenHeight, mode);
PersistentDataManager.gameSettings.resolutionWidth = CustomScreenWidth;
PersistentDataManager.gameSettings.resolutionHeight = CustomScreenHeight;
PersistentDataManager.gameSettings.resolutionIndex = DEFAULT_SCREEN_SIZES_STRING.Length - 1;
2022-07-09 02:11:55 +00:00
}
else
{
Screen.SetResolution(DEFAULT_SCREEN_SIZES[ScreenSizeIndex].width, DEFAULT_SCREEN_SIZES[ScreenSizeIndex].height, mode);
PersistentDataManager.gameSettings.resolutionWidth = DEFAULT_SCREEN_SIZES[ScreenSizeIndex].width;
PersistentDataManager.gameSettings.resolutionHeight = DEFAULT_SCREEN_SIZES[ScreenSizeIndex].height;
PersistentDataManager.gameSettings.resolutionIndex = ScreenSizeIndex;
2022-07-09 02:11:55 +00:00
}
}
2022-07-10 21:39:14 +00:00
Game Overlays (#280) * add accuracy display * temp BG for show * separate overlays prefab make proper shader for star effects * aim shakiness display * implement testing skill star * fully functional skill star * separate section display from editor * fully separate chart section display from timeline * add section to overlays * fix nullreference issues * start game layout settings * add game settings script * fix nonfunctioning scoring * invert y position logic on timing bar * add perfect challenge functionality * fix section not showing up in editor add perfect challenge option * add timing display minimal mode * Update PerfectAndPractice.png * show gismo for minigame bounds in editor * add ability to disable overlays in editor * prepare medals add new timing display graphic * hide screen preview * per-axis camera control added per request * section medals basic functionality * add medal get animations * fix bug with perfect icons * visual enhancements * adjust look of timing display minmode address audio ducking issues(?) * prepare overlay lyt editor add viewport pan, rotate, scale adjust audio setting * add layout editor UI elements * dynamic overlay creation * fix default single timing disp * set up overlay settings controls * start UI events * runtime uuid for component reference * layout editor affects overlay elements * show overlay element previews while editing * advanced audio settings * fix bug in drop-down creation * fallback defaults for the new stuff * fix textbox & overlay visibility bugs
2023-03-11 04:51:22 +00:00
public static void ResetGameRenderTexture()
{
// keep 16:9 aspect ratio
int width = Screen.width;
int height = Screen.height;
if (width / 16f > height / 9f)
{
width = (int)(height / 9f * 16f);
}
else
{
height = (int)(width / 16f * 9f);
}
GameRenderTexture.Release();
GameRenderTexture.width = width;
GameRenderTexture.height = height;
Game Overlays (#280) * add accuracy display * temp BG for show * separate overlays prefab make proper shader for star effects * aim shakiness display * implement testing skill star * fully functional skill star * separate section display from editor * fully separate chart section display from timeline * add section to overlays * fix nullreference issues * start game layout settings * add game settings script * fix nonfunctioning scoring * invert y position logic on timing bar * add perfect challenge functionality * fix section not showing up in editor add perfect challenge option * add timing display minimal mode * Update PerfectAndPractice.png * show gismo for minigame bounds in editor * add ability to disable overlays in editor * prepare medals add new timing display graphic * hide screen preview * per-axis camera control added per request * section medals basic functionality * add medal get animations * fix bug with perfect icons * visual enhancements * adjust look of timing display minmode address audio ducking issues(?) * prepare overlay lyt editor add viewport pan, rotate, scale adjust audio setting * add layout editor UI elements * dynamic overlay creation * fix default single timing disp * set up overlay settings controls * start UI events * runtime uuid for component reference * layout editor affects overlay elements * show overlay element previews while editing * advanced audio settings * fix bug in drop-down creation * fallback defaults for the new stuff * fix textbox & overlay visibility bugs
2023-03-11 04:51:22 +00:00
GameRenderTexture.Create();
OverlayRenderTexture.Release();
OverlayRenderTexture.width = (int)(width * 1.5f);
OverlayRenderTexture.height = (int)(height * 1.5f);
OverlayRenderTexture.Create();
Game Overlays (#280) * add accuracy display * temp BG for show * separate overlays prefab make proper shader for star effects * aim shakiness display * implement testing skill star * fully functional skill star * separate section display from editor * fully separate chart section display from timeline * add section to overlays * fix nullreference issues * start game layout settings * add game settings script * fix nonfunctioning scoring * invert y position logic on timing bar * add perfect challenge functionality * fix section not showing up in editor add perfect challenge option * add timing display minimal mode * Update PerfectAndPractice.png * show gismo for minigame bounds in editor * add ability to disable overlays in editor * prepare medals add new timing display graphic * hide screen preview * per-axis camera control added per request * section medals basic functionality * add medal get animations * fix bug with perfect icons * visual enhancements * adjust look of timing display minmode address audio ducking issues(?) * prepare overlay lyt editor add viewport pan, rotate, scale adjust audio setting * add layout editor UI elements * dynamic overlay creation * fix default single timing disp * set up overlay settings controls * start UI events * runtime uuid for component reference * layout editor affects overlay elements * show overlay element previews while editing * advanced audio settings * fix bug in drop-down creation * fallback defaults for the new stuff * fix textbox & overlay visibility bugs
2023-03-11 04:51:22 +00:00
}
2022-07-10 21:39:14 +00:00
public static void ChangeMasterVolume(float value)
{
MasterVolume = value;
AudioListener.volume = MasterVolume;
}
Game Overlays (#280) * add accuracy display * temp BG for show * separate overlays prefab make proper shader for star effects * aim shakiness display * implement testing skill star * fully functional skill star * separate section display from editor * fully separate chart section display from timeline * add section to overlays * fix nullreference issues * start game layout settings * add game settings script * fix nonfunctioning scoring * invert y position logic on timing bar * add perfect challenge functionality * fix section not showing up in editor add perfect challenge option * add timing display minimal mode * Update PerfectAndPractice.png * show gismo for minigame bounds in editor * add ability to disable overlays in editor * prepare medals add new timing display graphic * hide screen preview * per-axis camera control added per request * section medals basic functionality * add medal get animations * fix bug with perfect icons * visual enhancements * adjust look of timing display minmode address audio ducking issues(?) * prepare overlay lyt editor add viewport pan, rotate, scale adjust audio setting * add layout editor UI elements * dynamic overlay creation * fix default single timing disp * set up overlay settings controls * start UI events * runtime uuid for component reference * layout editor affects overlay elements * show overlay element previews while editing * advanced audio settings * fix bug in drop-down creation * fallback defaults for the new stuff * fix textbox & overlay visibility bugs
2023-03-11 04:51:22 +00:00
public static void ChangeAudioSettings(int dspSize, int sampleRate)
{
// don't reset audio if no changes are done
AudioConfiguration config = AudioSettings.GetConfiguration();
if (dspSize == config.dspBufferSize && sampleRate == config.sampleRate) return;
currentDspSize = dspSize;
currentSampleRate = sampleRate;
config.dspBufferSize = currentDspSize;
config.sampleRate = currentSampleRate;
AudioSettings.Reset(config);
PersistentDataManager.gameSettings.dspSize = currentDspSize;
PersistentDataManager.gameSettings.sampleRate = currentSampleRate;
}
public static void UpdateDiscordStatus(string details, bool editor = false, bool updateTime = false)
{
if (discordDuringTesting || !Application.isEditor)
{
if (PersistentDataManager.gameSettings.discordRPCEnable)
{
DiscordRPC.DiscordRPC.UpdateActivity(editor ? "In Editor " : "Playing ", details, updateTime);
Debug.Log("Discord status updated");
}
}
}
private static void OnQuitting()
{
if (!HasShutDown)
{
Debug.Log("Disconnecting JoyShocks...");
PlayerInput.CleanUp();
Debug.Log("Clearing RIQ Cache...");
Jukebox.RiqFileHandler.ClearCache();
Debug.Log("Closing Discord GameSDK...");
DiscordRPC.DiscordController.instance?.Disconnect();
HasShutDown = true;
}
}
private static bool WantsToQuit()
{
if (SceneManager.GetActiveScene().name != "Editor") return true;
Editor.Editor.instance.ShowQuitPopUp(true);
return Editor.Editor.instance.ShouldQuit;
}
2022-07-09 02:11:55 +00:00
}
}