//Run once per level, to create initial lists public void InitializeSceneList() { //If our scene list is empty, make a new List if (savedLists == null) { savedLists = new List <CollectiblePlacedList>(); } bool found = false; //Boolean for flagging when we've found the list we're looking for //We need to find if we already have a list of saved items for this level: for (int i = 0; i < savedLists.Count; i++) { //Go through our lists until we find a match to our current sceneID if (savedLists[i].sceneID == SceneManager.GetActiveScene().buildIndex) { found = true; //We found it! } } //If not, we need to create it: if (!found) { //Make a new list and add it to our Save Lists CollectiblePlacedList newList = new CollectiblePlacedList(SceneManager.GetActiveScene().buildIndex); savedLists.Add(newList); } }
/// <summary> /// Disable objects in this level that are contained within our 'saved despawn list' /// </summary> void DestroyAlreadyCollected() { //Check to make sure a list for our current level exists GameManager.instance.InitializeSceneList(); playerManager.InitializeUnsavedSceneList(); //Create a new local list, and fill it with the collectibles from our saved list CollectiblePlacedList localList = GameManager.instance.GetListForScene(); //Destroy small collectibles //Destroy large collectibles //Destroy keys //Destroy doors //If we have things in our list, if (localList != null) { //Debug.Log("DESTRUCTION!"); //Search and compare Small Collectibles //Go through each object in our scene, with the saveState script attached for (int i = 0; i < collectiblesInScene.Length; i++) { //Debug.Log("Checking collectible - " + collectiblesInScene[i].uID); //Iterate through our saved uID's, comparing to the one we're checking in the world for (int j = 0; j < localList.placedCollectibles.Count; j++) { //Debug.Log("Comparing collectible - " + localList.placedCollectibles[j].uID); //If we find a Unique ID match between our saved list and our spawn objects list, Despawn the object if (collectiblesInScene[i].uID == localList.placedCollectibles[j].uID) { //Debug.Log("It's a match! Destroy - " + collectiblesInScene[i].uID); //It's a match! despawn the object collectiblesInScene[i].gameObject.SetActive(false); //Quit comparing this scene collectible, move to the next break; } } } //Object Destruction completed } }
//Copy the player's unsaved list of collected items into our Game Manager permanent list public void SavePlayerInventory() { //Create reference to player's unsaved collected objects list playerManager = FindObjectOfType <PlayerManager>(); //Create a new local list, and fill it with the collectibles from our saved list CollectiblePlacedList localList = playerManager.GetUnsavedListForScene(); //Save and clear the player's Small Collectible List //Iterate through all of the objects in the list, adding each one, and then removing it from the list CollectiblePlaced collectible = new CollectiblePlaced(); for (int i = 0; i < localList.placedCollectibles.Count; i++) { //Get a reference to the current object in our unsaved list iteration collectible = localList.placedCollectibles[i]; //Debug.Log("Saving " + collectible.uID + " into GameManager"); //Add this object to the GameManager saved list GetListForScene().placedCollectibles.Add(collectible); } //Now that we've iterated through our list, it's time to clear this list. We've passed the items to permanence, no longer need to hold on to them playerManager.GetUnsavedListForScene().placedCollectibles.Clear(); }