/// <summary> /// Exports a savegame to the isolated storage and adds it to this tag. /// </summary> /// <param name="cs">The savegame to add.</param> public void AddSavegame(CartridgeSavegame cs) { // Sanity check: a savegame with similar name should // not exist. if (Savegames.Any(c => c.Name == cs.Name)) { System.Diagnostics.Debug.WriteLine("CartridgeTag: Renaming new savegame because an old one with same name exists: " + cs.Name); // What's the last savegame following the pattern "name (n)"? int dbl = GetLastSavegameNameInteger(cs.Name, " ({0})"); // Renames the savegame. using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { cs.Rename(this, cs.Name + " (" + ++dbl + ")", isf); } } // Makes sure the savegame is exported to the cache. cs.ExportToIsoStore(); // Adds the savegame. _savegames.Add(cs); // Notifies of a change. RaisePropertyChanged("Savegames"); }
private static void LoadSavegames() { if (IsFileAccessPossible) { var directoryPath = Path.Combine(Application.persistentDataPath, "Savegames"); if (Directory.Exists(directoryPath)) { foreach (var savegameFile in Directory.EnumerateFiles(directoryPath, "Save*.nerds")) { var gameStateJson = File.ReadAllText(savegameFile); var loadedGameState = UnityEngine.JsonUtility.FromJson <GameState>(gameStateJson); Savegames.Add(new Savegame() { PlanetsVisited = loadedGameState.PlanetsVisited, SavedOn = loadedGameState.SavedOn, RawSource = gameStateJson }); } } while (Savegames.Count < 3) { Savegames.Add(default);
/// <summary> /// Load player game /// Currently only player position /// </summary> /// <param name="saveName"></param> public void LoadGame(string saveName) { if (PlayerPrefs.HasKey("Savegames")) { string allSavegamesString = PlayerPrefs.GetString("Savegames"); Savegames allSavegames = JsonUtility.FromJson <Savegames>(allSavegamesString); Savegame save = null; foreach (Savegame s in allSavegames.savegames) { if (s.name == saveName) { save = s; } } if (save != null) { foreach (string s in save.itemsNamesInventory) { _player.inventory.Add(GetItem(s)); } foreach (string s in save.itemsNamesStock) { _player.stock.Add(GetItem(s)); } Vector3 position = save.position; GameObject.Find("PlayerController").transform.position = position; foreach (AnimalData ad in save.animals) { AnimalBody concernedAnimal = GetAnimal(ad.name); Food eat = GetItem(ad.eat) as Food; int happiness = ad.happiness; int hunger = ad.hunger; Vector3 animalPosition = ad.position; Vector3 animalRotation = ad.rotation; string wearStr = ad.wear; RareItem wear = null; if (wearStr != null) { wear = GetItem(wearStr) as RareItem; } RareItem canWear = GetItem(ad.canWear) as RareItem; concernedAnimal.animal.SetProperties(happiness, hunger, eat, wear, canWear); concernedAnimal.transform.position = animalPosition; concernedAnimal.transform.rotation = Quaternion.Euler(animalRotation); } } } }
private void ExecuteInitializeSavegameList(object obj) { Savegames.Clear(); string gamesavesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "The Witcher 3\\gamesaves"); var filesPaths = Directory.GetFiles(gamesavesPath, "*.sav"); foreach (var filePath in filesPaths) { var thumbnailFilePath = Path.Combine(Path.GetDirectoryName(filePath) ?? "", Path.GetFileNameWithoutExtension(filePath) + ".png"); Savegames.Add(new SavegameModel { Name = Path.GetFileName(filePath), Path = filePath, ThumbnailPath = thumbnailFilePath }); } }
public void BtnLoadGameOnClick() { _canvasPopup.SetActive(true); foreach (Transform child in GameObject.Find("LoadPanel").transform) { if (child.GetComponent <Button>() != null) { GameObject.Destroy(child.gameObject); } } if (PlayerPrefs.HasKey("Savegames")) { string allSavegamesString = PlayerPrefs.GetString("Savegames"); Savegames allSavegames = JsonUtility.FromJson <Savegames>(allSavegamesString); /*GameObject button2 = Resources.Load("UI/WoodButton", typeof(GameObject)) as GameObject; * button2 = Instantiate(button2, GameObject.Find("LoadPanel").transform); * button2.transform.GetChild(0).GetComponent<Text>().text = "baepsae"; * button2.GetComponent<RectTransform>().position = new Vector3(button2.GetComponent<RectTransform>().position.x, button2.GetComponent<RectTransform>().position.y, 0); * button2.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1); * button2.GetComponent<Button>().onClick.AddListener(delegate { BtnSavedGameOnClick("baepsae"); });*/ foreach (Savegame s in allSavegames.savegames) { GameObject button = Resources.Load("UI/WoodButton", typeof(GameObject)) as GameObject; button = Instantiate(button, GameObject.Find("LoadPanel").transform); button.transform.GetChild(0).GetComponent <Text>().text = s.name; button.GetComponent <RectTransform>().position = new Vector3(button.GetComponent <RectTransform>().position.x, button.GetComponent <RectTransform>().position.y, 0); button.GetComponent <RectTransform>().localScale = new Vector3(1, 1, 1); button.GetComponent <Button>().onClick.AddListener(delegate { BtnSavedGameOnClick(s.name); }); } } else { GameObject label = Resources.Load("UI/CaligraphiedLabel", typeof(GameObject)) as GameObject; label = Instantiate(label); label.transform.parent = GameObject.Find("LoadPanel").transform; label.transform.GetComponent <Text>().text = "Aucune partie sauvegardée"; } }
/// <summary> /// Gets the last integer suffix that matches a pattern of names for savegames currently /// added to the tag. /// </summary> /// <param name="name">Name of the savegame root</param> /// <param name="suffixFormat">Format of the integer suffix</param> /// <returns>The last integer to match, or 0.</returns> public int GetLastSavegameNameInteger(string name, string suffixFormat) { // Bakes the regex pattern string regexPattern = Regex.Escape(name + String.Format(suffixFormat, @"(\d+)")); regexPattern = regexPattern.Replace(@"\(\\d\+\)", @"(\d+)"); int dbl = 0; Regex r = new Regex(regexPattern); foreach (string n in Savegames.Where(c => c.Name.StartsWith(name)).Select(c => c.Name)) { foreach (Match match in r.Matches(n)) { int i = -1; if (int.TryParse(match.Groups[1].Value, out i) && i > dbl) { dbl = i; } } } return(dbl); }
/// <summary> /// Save game in the disk /// Currently only player position /// </summary> /// <param name="saveName">Save name</param> public void SaveGame(string saveName) { Vector3 playerPosition = GameObject.Find("PlayerController").transform.position; Savegames saves; if (PlayerPrefs.HasKey("Savegames")) { saves = JsonUtility.FromJson <Savegames>(PlayerPrefs.GetString("Savegames")); } else { saves = new Savegames(); } Savegame existingSave = new Savegame() { name = saveName }; foreach (Savegame save in saves.savegames) { if (save.name == saveName) { existingSave = save; } } saves.savegames.Remove(existingSave); existingSave.position = transform.position; existingSave.animals = new List <AnimalData>(); existingSave.itemsNamesInventory = new List <string>(); existingSave.itemsNamesStock = new List <string>(); foreach (Item item in _player.inventory) { existingSave.itemsNamesInventory.Add(item.name); } foreach (Item item in _player.stock) { existingSave.itemsNamesStock.Add(item.name); } foreach (AnimalBody ab in _animals) { AnimalData ad = new AnimalData(); ad.eat = ab.animal.eat.name; ad.happiness = ab.animal.happiness; ad.hunger = ab.animal.hunger; ad.position = ab.transform.position; ad.rotation = ab.transform.rotation.eulerAngles; ad.wear = ab.animal.WearItem() ? ab.animal.wear.name : ""; ad.canWear = ab.animal.canWear.name; ad.name = ab.name; existingSave.animals.Add(ad); } saves.savegames.Add(existingSave); PlayerPrefs.SetString("Savegames", JsonUtility.ToJson(saves, true)); PlayerPrefs.Save(); }
/// <summary> /// Gets a savegame of this cartridge by name, or null if none /// is found. /// </summary> /// <param name="name">Name of the savegame to find.</param> /// <returns>The savegame, or null if it wasn't found.</returns> public CartridgeSavegame GetSavegameByNameOrDefault(string name) { return(Savegames.SingleOrDefault(cs => cs.Name == name)); }