public void LoadGame() { ResetGame(); SaveGameData data = SaveLoadController.LoadGame(); if (data == null) { return; } locationsStates = data.locationsStates; Values = data.globalValues; foreach (string id in data.itemsId) { InventoryController.Instance.AddItem(id); } foreach (string id in data.notesId) { JournalController.Instance.AddNote(id, false); } foreach (string id in data.tasksId) { JournalController.Instance.AddTask(id); } foreach (string id in data.completeTasksId) { JournalController.Instance.AddTask(id); JournalController.Instance.CompleteTask(id); } LoadLevel(data.locationName, false, () => { PlayerController.Instance.MoveToPoint(data.playerPosition.GetVector3(), data.playerRotation.GetQuaternion()); PlayerController.Instance.Health = data.playerHealth; if (data.equipedWearId != "") { ItemsController.UseItem(ItemsManager.Instance.GetItemById(data.equipedWearId)); } }); }
/// <summary> /// Generates block types with hp and hp level. /// Chunks and their objects (if first run = true). /// And calculates faces. /// </summary> public IEnumerator LoadWorld(bool firstRun, Action callback) { MainController.InitializeOnWorldSizeChange(); ResetProgressBarVariables(); _stopwatch.Restart(); ProgressDescription = "Loading data..."; Status = WorldGeneratorStatus.NotReady; SaveLoadController.LoadGame(out PlayerLoadedPosition, out PlayerLoadedRotation); AlreadyGenerated += _progressStep; yield return(null); // return control ProgressDescription = "Creating game objects..."; if (firstRun) { _worldScene = SceneManager.CreateScene(name); } Status = WorldGeneratorStatus.TerrainReady; AlreadyGenerated += _progressStep; yield return(null); // return control if (firstRun) { ProgressDescription = "Chunk data initialization..."; // chunkData need to be initialized earlier in order to allow main loop iterate over chunks before their meshes are ready // thanks to this we can display chunk as soon as they become ready GlobalVariables.Chunks = new ChunkData[GlobalVariables.Settings.WorldSizeX, Constants.WORLD_SIZE_Y, GlobalVariables.Settings.WorldSizeZ]; for (int x = 0; x < GlobalVariables.Settings.WorldSizeX; x++) { for (int z = 0; z < GlobalVariables.Settings.WorldSizeZ; z++) { for (int y = 0; y < Constants.WORLD_SIZE_Y; y++) { GlobalVariables.Chunks[x, y, z] = new ChunkData( new ReadonlyVector3Int(x, y, z), new ReadonlyVector3Int(x * Constants.CHUNK_SIZE, y * Constants.CHUNK_SIZE, z * Constants.CHUNK_SIZE)); } } } ChunkObjects = new ChunkObject[GlobalVariables.Settings.WorldSizeX, Constants.WORLD_SIZE_Y, GlobalVariables.Settings.WorldSizeZ]; AlreadyGenerated += _progressStep; yield return(null); // return control } ProgressDescription = "Calculating faces..."; MeshGenerationAbstractionLayer.CalculateFaces(); AlreadyGenerated += _progressStep; yield return(null); // return control ProgressDescription = "World boundaries check..."; MeshGenerationAbstractionLayer.WorldBoundariesCheck(); Status = WorldGeneratorStatus.FacesReady; AlreadyGenerated += _progressStep; yield return(null); // return control ProgressDescription = "Creating chunks..."; // create chunk objects one by one for (int x = 0; x < GlobalVariables.Settings.WorldSizeX; x++) { for (int z = 0; z < GlobalVariables.Settings.WorldSizeZ; z++) { for (int y = 0; y < Constants.WORLD_SIZE_Y; y++) { ChunkData chunkData = GlobalVariables.Chunks[x, y, z]; MeshGenerationAbstractionLayer.CalculateMeshes(in chunkData.Position, out Mesh terrainMesh, out Mesh waterMesh); if (firstRun) { var chunkObject = new ChunkObject(); CreateChunkGameObjects(ref chunkData, ref chunkObject, terrainMesh, waterMesh); SceneManager.MoveGameObjectToScene(chunkObject.Terrain.gameObject, _worldScene); SceneManager.MoveGameObjectToScene(chunkObject.Water.gameObject, _worldScene); ChunkObjects[x, y, z] = chunkObject; } else { SetMeshesAndCollider(ref chunkData, ref ChunkObjects[x, y, z], terrainMesh, waterMesh); } GlobalVariables.Chunks[x, y, z].Status = ChunkStatus.NeedToBeRedrawn; AlreadyGenerated++; yield return(null); // return control } } } Status = WorldGeneratorStatus.AllReady; _stopwatch.Stop(); UnityEngine.Debug.Log($"It took {_stopwatch.ElapsedTicks / TimeSpan.TicksPerMillisecond} ms to load all terrain."); AlreadyGenerated = TerrainProgressSteps + MeshProgressSteps; // hardcoded end indicator ProgressDescription = "Game Load Completed"; callback?.Invoke(); }