Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. /// <summary>
  5. /// A behaviour for managing TreasureChests.
  6. /// </summary>
  7. public class TreasureChestBehaviour : MonoBehaviour
  8. {
  9.     [Tooltip("The unqiue Id of this chest. " +
  10.              "This Id is used for dealing with the chest in the scene. " +
  11.              "If two chests have the same Id, unwanted behaviour may occur.")]
  12.     public int ChestId = 0;
  13.  
  14.     /// <summary>
  15.     /// Loaded chest data for the whole game.
  16.     /// The key of the dictionary is the level, the hashset are all the chest ids we've already obtained.
  17.     /// </summary>
  18.     private static Dictionary<int, HashSet<int>> _chestLevelDataDictionary;
  19.  
  20.     /// <summary>
  21.     /// Key used to access player prefs.
  22.     /// </summary>
  23.     private const string ChestDataPlayerPrefsKey = "ChestData_{0}";
  24.    
  25.     void Start ()
  26.     {
  27.         // I'm pretending here that I have a way to get what level the chest is currently in when it starts.
  28.         // You want to pass that into the LoadChestData so we load the requested level.
  29.         LoadChestData(LevelController.Level);
  30.  
  31.         if (IsChestAlreadyObtained())
  32.         {
  33.             // Disable the chest entirely if it's already obtained.
  34.             this.gameObject.SetActive(false);
  35.         }
  36.     }
  37.  
  38.     private static void LoadChestData(int level)
  39.     {
  40.         // If we haven't created the static dictionary yet, set it up.
  41.         if (_chestLevelDataDictionary == null)
  42.         {
  43.             _chestLevelDataDictionary = new Dictionary<int, HashSet<int>>();
  44.         }
  45.  
  46.         // If we don't have the chest data for this level, that means we haven't loaded it yet.
  47.         HashSet<int> chestData;
  48.         if (!_chestLevelDataDictionary.TryGetValue(level, out chestData))
  49.         {
  50.             chestData = new HashSet<int>();
  51.  
  52.             // We store each level as an Array on an individual key for that level. Get the string and deserialize the array.
  53.             var chestDataJson = PlayerPrefs.GetString(string.Format(ChestDataPlayerPrefsKey, level), null);
  54.  
  55.             // If we haven't saved anything yet for this level, we'll get null.
  56.             if (chestDataJson != null)
  57.             {
  58.                 var chestDataArray = JsonUtility.FromJson<int[]>(chestDataJson);
  59.  
  60.                 // Add all the entries to the hashset.
  61.                 foreach (int chestDataId in chestDataArray)
  62.                 {
  63.                     // 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.
  64.                     // Shouldn't happen, but the redundancy check isn't expensive.
  65.                     if (!chestData.Contains(chestDataId))
  66.                     {
  67.                         chestData.Add(chestDataId);
  68.                     }
  69.                 }
  70.  
  71.                 // And finally add the hashset to our static dictionary, so we know what chests we've already obtained.
  72.                 _chestLevelDataDictionary.Add(level, chestData);
  73.             }
  74.         }
  75.     }
  76.  
  77.     /// <summary>
  78.     /// Obtains this chest. This chest will not be able to be obtained again.
  79.     /// </summary>
  80.     public void ObtainChest()
  81.     {
  82.         HashSet<int> chestData;
  83.         if (!_chestLevelDataDictionary.TryGetValue(LevelController.Level, out chestData))
  84.         {
  85.             throw new System.Exception("You need to load the chest data before asking if a chest is obtained...");
  86.         }
  87.  
  88.         if (!chestData.Contains(this.ChestId))
  89.         {
  90.             chestData.Add(this.ChestId);
  91.  
  92.             // Convert it to JSON and save it as a string, keyed onto the specific level.
  93.             var chestIdArrayJson = JsonUtility.ToJson(chestData);a
  94.             // TODO : I didn't test the ToJson, so if it doesn't convert it to an int array, do that manually here.
  95.             PlayerPrefs.SetString(string.Format(ChestDataPlayerPrefsKey, LevelController.Level), chestIdArrayJson);
  96.         }
  97.         else
  98.         {
  99.             Debug.LogError("You've assigned at least 2 chests the id of: " + this.ChestId);
  100.         }
  101.     }
  102.  
  103.     /// <summary>
  104.     /// Returns if the chest has already been obtained or not.
  105.     /// </summary>
  106.     private bool IsChestAlreadyObtained()
  107.     {
  108.         HashSet<int> chestData;
  109.         if (!_chestLevelDataDictionary.TryGetValue(LevelController.Level, out chestData))
  110.         {
  111.             throw new System.Exception("You need to load the chest data before asking if a chest is obtained...");
  112.         }
  113.  
  114.         return chestData.Contains(this.ChestId);
  115.     }
  116. }
  117.  
  118. public class LevelController
  119. {
  120.     public static int Level { get; private set; }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement