using System.Collections; using System.Collections.Generic; using Nox.Game; using Nox.Game.UI; using Nox.Input; using Nox.Platform; using Jovian.SaveSystem; using Nox.EditorCode; using Unity.Profiling; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; namespace Nox.Core { /// /// The main boot class which is initializing the project, loads the platforms settings, initializes the platforms and creates the game states /// public class EntryPoint : MonoBehaviour { public AssetReferenceT initializerSettingsFile; private IPlatform platform; private GameDataState gameDataState; private Dictionary applicationStates; private ProfilerMarker createApplicationStateMarker = new ProfilerMarker("createApplicationState"); private ISceneTransition sceneTransition; private ISaveSystem saveSystem; private InitializerSettingsFile initializerSettings; private BootstrapReferences bootstrapReferences; private MenuGameStateData menuGameStateData; public IEnumerator Start() { DontDestroyOnLoad(this); // scene authoring is helping with making each scene stand-alone playable var activeSceneReference = FindFirstObjectByType(); if(activeSceneReference == null) { Debug.LogWarning("The scene has no SceneReference script. Will start by default in Splash Application State"); var sceneRef = new GameObject { name = "Scene Reference" }; activeSceneReference = sceneRef.AddComponent(); activeSceneReference.gameState = GameState.BootState; yield return null; } // Initialize platform gameDataState = new GameDataState { platformSelector = new PlatformSelector(PlatformSelector.GetDevicePlatform(), PlatformSelector.GetPlatformDefaultInputMode()), }; gameDataState.ChangeGameState(activeSceneReference.gameState); // Load Initialization Settings var initSettingsHandle = Addressables.LoadAssetAsync(initializerSettingsFile); yield return new WaitUntil(() => initSettingsHandle.IsDone); initializerSettings = initSettingsHandle.Result; yield return CreatePlatformFactory(); var bootStrapsSettingsHandle = Addressables.LoadAssetAsync(initializerSettings.bootstrapSettings); yield return new WaitUntil(() => bootStrapsSettingsHandle.IsDone); bootstrapReferences = bootStrapsSettingsHandle.Result; menuGameStateData = new MenuGameStateData(); var fadeObject = new GameObject("ScreenFadeTransition"); sceneTransition = fadeObject.AddComponent(); CreateApplicationStates(); var gameStateRunner = gameObject.AddComponent(); gameStateRunner.Initialize(applicationStates, gameDataState, platform, sceneTransition); } private IEnumerator CreatePlatformFactory() { platform = null; var devicePlatform = gameDataState.platformSelector.devicePlatform; platform = devicePlatform switch { DevicePlatform.Desktop => new DesktopPlatform(initializerSettings.desktopPlatformSettings), DevicePlatform.UnityEditor => new UnityEditorPlatform(initializerSettings.unityEditorPlatformSettings), _ => platform }; if(!Equals(gameDataState.platformSelector.devicePlatform, DevicePlatform.Desktop) && PlatformSelector.GetPlatformDefaultInputMode() == InputMode.Desktop) { platform = new DesktopPlatform(initializerSettings.desktopPlatformSettings); } yield return platform?.Initialize(gameDataState); } private void CreateApplicationStates() { createApplicationStateMarker.Begin(); // Save System var saveSettings = SaveSystemSettings.Load(); ISaveSerializer saveSerializer = saveSettings.saveFormat == SaveFormat.Json ? new JsonSaveSerializer() : new BinarySaveSerializer(saveSettings.obfuscationKey); ISaveStorage saveStorage = new FileSystemSaveStorage(Application.persistentDataPath, saveSettings.saveDirectoryName); ISaveSlotManager saveSlotManager = new SaveSlotManager(saveStorage, saveSettings); saveSystem = new SaveSystem(saveSerializer, saveStorage, saveSlotManager, saveSettings); var adventuredata = new AdventureData(); var characterSystems = DefaultCharacterSystemsFactory.Create(maxPartySize: 8); var partyCreatorModel = new PartyCreatorModel(characterSystems.CharacterFactory, characterSystems.PartyFactory); applicationStates = new Dictionary { [GameState.BootState] = new SplashGameState(bootstrapReferences, gameDataState), [GameState.MainMenu] = new MainMenuGameState(gameDataState, menuGameStateData, bootstrapReferences, saveSystem, partyCreatorModel, adventuredata), [GameState.GameMode] = new GameModeGameState(gameDataState, bootstrapReferences, platform.PlatformSettings, saveSystem, sceneTransition, adventuredata), }; createApplicationStateMarker.End(); } } }