//Shady using Fusion; using System; using UnityEngine; using Fusion.Sockets; using Shady.Core.Singletons; using Sirenix.OdinInspector; using DedicatedServer.Utils; using System.Threading.Tasks; using System.Collections.Generic; using UnityEngine.SceneManagement; using ReadOnly = Sirenix.OdinInspector.ReadOnlyAttribute; using Random = UnityEngine.Random; public class GameManager : Singleton { //=================================================== // FIELDS //=================================================== [Title("GAME MANAGER", titleAlignment: TitleAlignments.Centered)] [DisplayAsString] [SerializeField] string _sessionName = "SESSION_NAME"; [ReadOnly] [SerializeField] UserData _userData; [Space] [SerializeField] NetworkRunner _runnerPrefab = null; [ReadOnly] [SerializeField] NetworkRunner _networkRunner = null; [ReadOnly] [SerializeField] NetworkSceneManagerDefault _sceneManager = null; [HideInInspector] public List currentAppliedCustomizations; //=================================================== // PROPERTIES //=================================================== public string SessionName => _sessionName; public string UserName{get => _userData.UserName; set => _userData.UserName = value;} public Gender Gender {get => _userData.Gender; set => _userData.Gender = value;} //=================================================== // METHODS //=================================================== internal override void Init() { base.Init(); if(InstanceNotSelf) return; FadeSystem.Instance.Splash(); QualitySettings.vSyncCount = 0; Application.targetFrameRate = 60; if(GameData.Instance.DebugConsole) { GameObject obj = Resources.Load("IngameDebugConsole"); if(obj) GameObject.Instantiate(obj); }//if end Splash(); }//Init() end public void Splash() { AudioManager.Instance.StartGame(); UI_Manager.Instance.OpenPanel(); FadeSystem.Instance.FadeOut(); }//Splash() end public async void CreateUser(string userName) { SaveData.Instance.UserData.UserName = userName; SaveData.Instance.UserData.Gender = Gender; SaveSystem.SaveProgress(); InitRunner(); // switch(await SaveSystem.CreateUserRequest()) // { // case CreateUserStatus.EmptyUserName: // GameLog.ShowMessage("UserName Empty!"); // break; // case CreateUserStatus.Success: // InitRunner(); // break; // case CreateUserStatus.Exists: // GameLog.ShowMessage("Username already exists!"); // break; // case CreateUserStatus.RequestFailed: // GameLog.ShowWarning("Request Failed to Create New User!"); // break; // }//switch end }//CreateUser() end public async void InitRunner() { _networkRunner = Instantiate(_runnerPrefab); _sceneManager = _networkRunner.GetComponent(); StartGameResult result = await _networkRunner.JoinSessionLobby(SessionLobby.ClientServer); if(result.Ok) { // FadeSystem.Instance.Fade(()=>UI_Manager.Instance.OpenPanel()); StartSession("Zone", SaveData.Instance.UserData.UserName); // if(SaveData.Instance.HasID is false) // else // { // // switch(await SaveSystem.GetUserRequest()) // // { // // case GetUserStatus.Exists: // // StartSession("Zone", SaveData.Instance.UserData.UserName); // // break; // // case GetUserStatus.NotExists: // // GameLog.ShowMessage("Currently saved user does not exist on server!"); // // break; // // case GetUserStatus.RequestFailed: // // GameLog.ShowWarning("Request Failed to Get User!"); // // break; // // }//switch end // }//else end }//if end }//InitRunner() end private void StartSession(string sessionName, string userName) { UserName = userName; _sessionName = sessionName; Gender = SaveData.Instance.UserData.Gender; FadeSystem.Instance.FadeIn(StartGame); }//StartSession() end private async void StartGame() { _networkRunner.ProvideInput = true; // Create the NetworkSceneInfo from the current scene SceneRef sceneRef = SceneRef.FromIndex(SceneManager.GetActiveScene().buildIndex); NetworkSceneInfo sceneInfo = new(); if (sceneRef.IsValid) sceneInfo.AddSceneRef(sceneRef, LoadSceneMode.Additive); StartGameArgs startGameArgs = new() { SessionName = _sessionName, GameMode = GameMode.Client, Address = NetAddress.Any(), PlayerCount = 5, OnGameStarted = OnGameStarted(), Scene = sceneRef.IsValid ? sceneRef : null, SceneManager = _sceneManager, // CustomPhotonAppSettings = appSettings, }; StartGameResult result = await _networkRunner.StartGame(startGameArgs); if(result.Ok) { GameLog.ShowMessage($"Game started successfully. Requesting server to spawn Local Player."); if(_networkRunner.IsRunning) { PlayerData playerData = new() { gender = SaveData.Instance.UserData.Gender, position = new Vector3(Random.Range(-8, 8), 0f, Random.Range(-8, 8)), rotation = Quaternion.Euler(0f, 90f, 0f), }; var key = ReliableKey.FromInts(95, 0, 0, 0); _networkRunner.SendReliableDataToServer(key, playerData.Serialize()); await Task.Delay(1000); FadeSystem.Instance.FadeOut(); // GameLog.ShowMessage(PhotonAppSettings.Global.AppSettings.BestRegionSummaryFromStorage); }//if end }//if end }//InitializeNetworkRunner() end // private Action RunnerInitialized() Action OnGameStarted() { GameLog.ShowMessage($"Starting Client NetworkRunner on Session: {_sessionName}"); return null; }//RunnerInitialized() end // public static string GetRandomName() // { // int playerNumber = UnityEngine.Random.Range(0, 1000); // return $"Player {playerNumber.ToString("0000")}"; // }//GetRandomName() end public void Shutdown() { _networkRunner.Shutdown(); FadeSystem.Instance.FadeIn(async ()=> { SceneManager.LoadScene((int)SceneDefs.MENU, LoadSceneMode.Single); await Task.Delay(100); Splash(); }); }//Shutdown() end public void GoOffline() { _networkRunner.Shutdown(); FadeSystem.Instance.FadeIn(async ()=> { SceneManager.LoadScene((int)SceneDefs.OFFLINE, LoadSceneMode.Single); await Task.Delay(3000); FadeSystem.Instance.FadeOut(); }); }//GoOffline() end public void GoOnline() { FadeSystem.Instance.FadeIn(async ()=> { SceneManager.LoadScene((int)SceneDefs.MENU, LoadSceneMode.Single); await Task.Delay(1000); InitRunner(); FadeSystem.Instance.FadeOut(); }); }//GoOnline() end }//class end