/** * Assigns the game data by the given save game */ private static void AssignGameData(SaveGame saveGame) { ScoreManager.score = saveGame.game.score; Camera.main.transform.position = new Vector3( saveGame.game.cameraPositionX, saveGame.game.cameraPositionY, saveGame.game.cameraPositionZ ); }
/** * Assigns the player data (health, position, rotation) by the given save game */ private static void AssignPlayerData(SaveGame saveGame) { var playerPosition = new Vector3( saveGame.player.positionX, saveGame.player.positionY, saveGame.player.positionZ ); var playerRotation = new Quaternion( saveGame.player.rotationX, saveGame.player.rotationY, saveGame.player.rotationZ, saveGame.player.rotationW ); SaveGameManager.playerHealth.currentHealth = saveGame.player.health; SaveGameManager.playerHealth.healthSlider.value = saveGame.player.health; SaveGameManager.playerTransform.position = playerPosition; SaveGameManager.playerTransform.rotation = playerRotation; }
/** * Spawns and assigns the enemy data by the given save game and destroies the current enemies * TODO: Add spwan / load pooling so the game objects will not be destroied? */ private static void AssignEnemyData(SaveGame saveGame) { var enemies = GameObject.FindGameObjectsWithTag("Enemy"); for (var index = 0; index < enemies.Length; index ++) { GameObject.Destroy(enemies[index].gameObject); } for (var index = 0; index < saveGame.enemies.Count; index ++) { var enemySpawnPosition = new Vector3( saveGame.enemies[index].positionX, saveGame.enemies[index].positionY, saveGame.enemies[index].positionZ ); var enemySpawnRotation = new Quaternion( saveGame.enemies[index].rotationX, saveGame.enemies[index].rotationY, saveGame.enemies[index].rotationZ, saveGame.enemies[index].rotationW ); var enemyHealth = saveGame.enemies[index].health; EnemyManager.instances[saveGame.enemies[index].type].Spawn( enemySpawnPosition, enemySpawnRotation, enemyHealth ); } }
/** * Adds the relevant player data to the given save game */ private static void AddPlayerData(SaveGame saveGame) { saveGame.player.health = playerHealth.currentHealth; saveGame.player.positionX = playerTransform.position.x; saveGame.player.positionY = playerTransform.position.y; saveGame.player.positionZ = playerTransform.position.z; saveGame.player.rotationX = playerTransform.rotation.x; saveGame.player.rotationY = playerTransform.rotation.y; saveGame.player.rotationZ = playerTransform.rotation.z; saveGame.player.rotationW = playerTransform.rotation.w; }
/** * Adds the relevant game data to the given save game */ private static void AddGameData(SaveGame saveGame) { saveGame.game.score = ScoreManager.score; saveGame.game.cameraPositionX = Camera.main.transform.position.x; saveGame.game.cameraPositionY = Camera.main.transform.position.y; saveGame.game.cameraPositionZ = Camera.main.transform.position.z; }
/** * Adds the relevant enemy data to the given save game */ private static void AddEnemyData(SaveGame saveGame) { var enemies = GameObject.FindGameObjectsWithTag("Enemy"); saveGame.enemies = new List<SaveGameEnemyData>(); for (var index = 0; index < enemies.Length; index ++) { var enemyHealth = enemies[index].gameObject.GetComponent<EnemyHealth>(); if (!enemyHealth.IsDead()) { var enemyTransform = enemies[index].gameObject.transform; var saveGameEnemyData = new SaveGameEnemyData(); saveGameEnemyData.health = enemyHealth.currentHealth; saveGameEnemyData.positionX = enemyTransform.position.x; saveGameEnemyData.positionY = enemyTransform.position.y; saveGameEnemyData.positionZ = enemyTransform.position.z; saveGameEnemyData.rotationX = enemyTransform.rotation.x; saveGameEnemyData.rotationY = enemyTransform.rotation.y; saveGameEnemyData.rotationZ = enemyTransform.rotation.z; saveGameEnemyData.rotationW = enemyTransform.rotation.w; saveGameEnemyData.type = enemyHealth.name; saveGame.enemies.Add(saveGameEnemyData); } } }
/** * Saves all game relevant data (player-, game- and enemy-data) to a save game named like the given string */ public static void Save(string saveGameName) { var binaryFormatter = new BinaryFormatter(); var saveGameFile = File.Create(Application.persistentDataPath + "/" + saveGameName + ".savegame"); var saveGame = new SaveGame(); SaveGameManager.AddPlayerData(saveGame); SaveGameManager.AddGameData(saveGame); SaveGameManager.AddEnemyData(saveGame); binaryFormatter.Serialize(saveGameFile, saveGame); saveGameFile.Close(); Debug.Log("The savegame " + saveGameName + " was successfully saved!"); }