private IEnumerator LoadLevelStateHelper(string levelName) { // Get the saved data string path = LevelSaveStatePrefix + levelName + ".xml"; LevelSaveState levelSave = null; levelSave = LevelSaveState.Load(path); // Do nothing if we couldn't load any data if (levelSave == null) { yield break; } // Restore all the enemies in the level AI.ResetEnemies(); foreach (EnemySaveState enemyState in levelSave.EnemyStates) { string enemyName = enemyState.Type.ToString().Substring(6); // NOTE: WE MAY WANT TO POOL HERE, BUT I DON'T THINK IT HAPPENS ENOUGH TO BE WORTH IT/NOT SURE IT MAKES SENSE GameObject newEnemy = (GameObject)Instantiate(Resources.Load("Prefabs/Characters/" + enemyName), enemyState.Position, enemyState.Rotation); if (newEnemy == null) { Debug.LogWarning("Failed to load enemy: " + enemyName); continue; } newEnemy.transform.parent = AI.transform; newEnemy.GetComponent <CharacterAnimator>().Direction = enemyState.Direction; newEnemy.GetComponentInChildren <EnemyHeartBox>().HitPoints = enemyState.Health; } // Restore all the items in the scene Level.ResetItems(); foreach (ItemSaveState itemState in levelSave.ItemStates) { string itemName = "Prefabs/Items/" + itemState.ItemType.ToString().Substring(5); if (itemState.ItemType == Item.ItemType.Item__Weapon) { itemName = "Prefabs/Items/Weapons/OnField/" + itemState.WeaponType.ToString().Substring(7); } // NOTE: WE MAY WANT TO POOL HERE, BUT I DON'T THINK IT HAPPENS ENOUGH TO BE WORTH IT/NOT SURE IT MAKES SENSE GameObject createdItem = (GameObject)Instantiate(Resources.Load(itemName), itemState.Position, itemState.Rotation); if (createdItem == null) { Debug.LogWarning("Failed to load item: " + itemName); continue; } createdItem.transform.parent = Level.ItemPickups; Item newItem = createdItem.GetComponent <Item>(); newItem.Quantity = itemState.Quantity; } yield return(null); }
public static LevelSaveState LoadFromText(string text) { LevelSaveState levelSave = null; XmlSerializer serializer = new XmlSerializer(typeof(LevelSaveState)); StringReader stream = null; try { stream = new StringReader(text); levelSave = serializer.Deserialize(stream) as LevelSaveState; } catch { // If we fail to load the file just return null levelSave = null; } finally { // Make sure we close the stream if (stream != null) { stream.Close(); } } return(levelSave); }
public static LevelSaveState Load(string path) { LevelSaveState levelSave = null; XmlSerializer serializer = new XmlSerializer(typeof(LevelSaveState)); FileStream stream = null; try { stream = new FileStream(path, FileMode.Open); levelSave = serializer.Deserialize(stream) as LevelSaveState; } catch { // If we fail to load the file just return null levelSave = null; } finally { // Make sure we close the stream if (stream != null) { stream.Close(); } } return(levelSave); }
public static void SaveLevelState(string levelName) { string path = LevelSaveStatePrefix + levelName + ".xml"; LevelSaveState level = new LevelSaveState(); List <EnemySaveState> enemySaves = new List <EnemySaveState>(); foreach (EnemyAI enemyAI in AI.Enemies) { enemySaves.Add(enemyAI.SaveState()); } level.EnemyStates = enemySaves.ToArray(); List <ItemSaveState> itemSaves = new List <ItemSaveState>(); foreach (Item item in Level.Items) { itemSaves.Add(item.SaveState()); } level.ItemStates = itemSaves.ToArray(); level.Save(path); }