using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace MoreMountains.Tools { [ExecuteAlways] /// /// This static class is in charge of storing the current state of the achievements, unlocking/locking them, and saving them to data files /// public static class MMAchievementManager { public static List AchievementsList { get { return _achievements; }} private static List _achievements; private static MMAchievement _achievement = null; private const string _defaultFileName = "Achievements"; private const string _saveFolderName = "MMAchievements/"; private const string _saveFileExtension = ".achievements"; private static string _saveFileName; private static string _listID; /// /// You'll need to call this method to initialize the manager /// public static void LoadAchievementList() { _achievements = new List (); // the Achievement List scriptable object must be in a Resources folder inside your project, like so : Resources/Achievements/PUT_SCRIPTABLE_OBJECT_HERE MMAchievementList achievementList = (MMAchievementList) Resources.Load("Achievements/AchievementList"); if (achievementList == null) { return; } // we store the ID for save purposes _listID = achievementList.AchievementsListID; foreach (MMAchievement achievement in achievementList.Achievements) { _achievements.Add (achievement.Copy()); } } /// /// Unlocks the specified achievement (if found). /// /// Achievement I. public static void UnlockAchievement(string achievementID) { _achievement = AchievementManagerContains(achievementID); if (_achievement != null) { _achievement.UnlockAchievement(); } } /// /// Locks the specified achievement (if found). /// /// Achievement ID. public static void LockAchievement(string achievementID) { _achievement = AchievementManagerContains(achievementID); if (_achievement != null) { _achievement.LockAchievement(); } } /// /// Adds progress to the specified achievement (if found). /// /// Achievement ID. /// New progress. public static void AddProgress(string achievementID, int newProgress) { _achievement = AchievementManagerContains(achievementID); if (_achievement != null) { _achievement.AddProgress(newProgress); } } /// /// Sets the progress of the specified achievement (if found) to the specified progress. /// /// Achievement ID. /// New progress. public static void SetProgress(string achievementID, int newProgress) { _achievement = AchievementManagerContains(achievementID); if (_achievement != null) { _achievement.SetProgress(newProgress); } } /// /// Determines if the achievement manager contains an achievement of the specified ID. Returns it if found, otherwise returns null /// /// The achievement corresponding to the searched ID if found, otherwise null. /// Searched I. private static MMAchievement AchievementManagerContains(string searchedID) { if (_achievements.Count == 0) { return null; } foreach(MMAchievement achievement in _achievements) { if (achievement.AchievementID == searchedID) { return achievement; } } return null; } // SAVE ------------------------------------------------------------------------------------------------------------------------------------ /// /// Removes saved data and resets all achievements from a list /// /// The ID of the achievement list to reset. public static void ResetAchievements(string listID) { if (_achievements != null) { foreach(MMAchievement achievement in _achievements) { achievement.ProgressCurrent = 0; achievement.UnlockedStatus = false; } } DeterminePath (listID); MMSaveLoadManager.DeleteSave(_saveFileName + _saveFileExtension, _saveFolderName); Debug.LogFormat ("Achievements Reset"); } public static void ResetAllAchievements() { LoadAchievementList (); ResetAchievements (_listID); } /// /// Loads the saved achievements file and updates the array with its content. /// public static void LoadSavedAchievements() { DeterminePath (); SerializedMMAchievementManager serializedMMAchievementManager = (SerializedMMAchievementManager)MMSaveLoadManager.Load(typeof(SerializedMMAchievementManager), _saveFileName+ _saveFileExtension, _saveFolderName); ExtractSerializedMMAchievementManager(serializedMMAchievementManager); } /// /// Saves the achievements current status to a file on disk /// public static void SaveAchievements() { DeterminePath (); SerializedMMAchievementManager serializedMMAchievementManager = new SerializedMMAchievementManager(); FillSerializedMMAchievementManager(serializedMMAchievementManager); MMSaveLoadManager.Save(serializedMMAchievementManager, _saveFileName+_saveFileExtension, _saveFolderName); } /// /// Determines the path the achievements save file should be saved to. /// private static void DeterminePath(string specifiedFileName = "") { string tempFileName = (!string.IsNullOrEmpty(_listID)) ? _listID : _defaultFileName; if (!string.IsNullOrEmpty(specifiedFileName)) { tempFileName = specifiedFileName; } _saveFileName = tempFileName; } /// /// Serializes the contents of the achievements array to a serialized, ready to save object /// /// Serialized inventory. public static void FillSerializedMMAchievementManager(SerializedMMAchievementManager serializedAchievements) { serializedAchievements.Achievements = new SerializedMMAchievement[_achievements.Count]; for (int i = 0; i < _achievements.Count(); i++) { SerializedMMAchievement newAchievement = new SerializedMMAchievement (_achievements[i].AchievementID, _achievements[i].UnlockedStatus, _achievements[i].ProgressCurrent); serializedAchievements.Achievements [i] = newAchievement; } } /// /// Extracts the serialized achievements into our achievements array if the achievements ID match. /// /// Serialized achievements. public static void ExtractSerializedMMAchievementManager(SerializedMMAchievementManager serializedAchievements) { if (serializedAchievements == null) { return; } for (int i = 0; i < _achievements.Count(); i++) { for (int j=0; j