Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// A behaviour for managing TreasureChests.
- /// </summary>
- public class TreasureChestBehaviour : MonoBehaviour
- {
- [Tooltip("The unqiue Id of this chest. " +
- "This Id is used for dealing with the chest in the scene. " +
- "If two chests have the same Id, unwanted behaviour may occur.")]
- public int ChestId = 0;
- /// <summary>
- /// Loaded chest data for the whole game.
- /// The key of the dictionary is the level, the hashset are all the chest ids we've already obtained.
- /// </summary>
- private static Dictionary<int, HashSet<int>> _chestLevelDataDictionary;
- /// <summary>
- /// Key used to access player prefs.
- /// </summary>
- private const string ChestDataPlayerPrefsKey = "ChestData_{0}";
- void Start ()
- {
- // I'm pretending here that I have a way to get what level the chest is currently in when it starts.
- // You want to pass that into the LoadChestData so we load the requested level.
- LoadChestData(LevelController.Level);
- if (IsChestAlreadyObtained())
- {
- // Disable the chest entirely if it's already obtained.
- this.gameObject.SetActive(false);
- }
- }
- private static void LoadChestData(int level)
- {
- // If we haven't created the static dictionary yet, set it up.
- if (_chestLevelDataDictionary == null)
- {
- _chestLevelDataDictionary = new Dictionary<int, HashSet<int>>();
- }
- // If we don't have the chest data for this level, that means we haven't loaded it yet.
- HashSet<int> chestData;
- if (!_chestLevelDataDictionary.TryGetValue(level, out chestData))
- {
- chestData = new HashSet<int>();
- // We store each level as an Array on an individual key for that level. Get the string and deserialize the array.
- var chestDataJson = PlayerPrefs.GetString(string.Format(ChestDataPlayerPrefsKey, level), null);
- // If we haven't saved anything yet for this level, we'll get null.
- if (chestDataJson != null)
- {
- var chestDataArray = JsonUtility.FromJson<int[]>(chestDataJson);
- // Add all the entries to the hashset.
- foreach (int chestDataId in chestDataArray)
- {
- // In the event you mess up and assign 2 chests the same id and it gets saved, we'll make sure the hashset doesn't have it yet.
- // Shouldn't happen, but the redundancy check isn't expensive.
- if (!chestData.Contains(chestDataId))
- {
- chestData.Add(chestDataId);
- }
- }
- // And finally add the hashset to our static dictionary, so we know what chests we've already obtained.
- _chestLevelDataDictionary.Add(level, chestData);
- }
- }
- }
- /// <summary>
- /// Obtains this chest. This chest will not be able to be obtained again.
- /// </summary>
- public void ObtainChest()
- {
- HashSet<int> chestData;
- if (!_chestLevelDataDictionary.TryGetValue(LevelController.Level, out chestData))
- {
- throw new System.Exception("You need to load the chest data before asking if a chest is obtained...");
- }
- if (!chestData.Contains(this.ChestId))
- {
- chestData.Add(this.ChestId);
- // Convert it to JSON and save it as a string, keyed onto the specific level.
- var chestIdArrayJson = JsonUtility.ToJson(chestData);a
- // TODO : I didn't test the ToJson, so if it doesn't convert it to an int array, do that manually here.
- PlayerPrefs.SetString(string.Format(ChestDataPlayerPrefsKey, LevelController.Level), chestIdArrayJson);
- }
- else
- {
- Debug.LogError("You've assigned at least 2 chests the id of: " + this.ChestId);
- }
- }
- /// <summary>
- /// Returns if the chest has already been obtained or not.
- /// </summary>
- private bool IsChestAlreadyObtained()
- {
- HashSet<int> chestData;
- if (!_chestLevelDataDictionary.TryGetValue(LevelController.Level, out chestData))
- {
- throw new System.Exception("You need to load the chest data before asking if a chest is obtained...");
- }
- return chestData.Contains(this.ChestId);
- }
- }
- public class LevelController
- {
- public static int Level { get; private set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement