Esempio n. 1
0
 /// <summary>
 /// Houses a representation of player's currently held object, inventory items, audio diaries, and note entries from their inventory
 /// </summary>
 /// <param name="heldObjectData">Object data for held object</param>
 /// <param name="inventoryItems">Inventory item data</param>
 /// <param name="audioDiaries">Audio diary data</param>
 /// <param name="notes">note entry data</param>
 public FPEInventorySaveData(FPEHeldObjectSaveData heldObjectData, FPEInventoryItemSaveData[] inventoryItems, FPEAudioDiaryEntrySaveData[] audioDiaries, FPENoteSaveData[] notes)
 {
     heldObjData    = heldObjectData;
     invItems       = inventoryItems;
     audioDiaryData = audioDiaries;
     noteData       = notes;
 }
        /// <summary>
        /// Gathers data from player's inventory (items, audio diaries, notes)
        /// </summary>
        /// <returns>A saveable data package for the inventory at save time</returns>
        public FPEInventorySaveData gatherInventorySaveData()
        {
            #region HELD_OBJECT

            FPEInteractableBaseScript.eInteractionType heldObjectType = FPEInteractionManagerScript.Instance.getHeldObjectType();
            GameObject heldObject = FPEInteractionManagerScript.Instance.getHeldObject();

            FPEHeldObjectSaveData heldObjectData = null;

            if (heldObjectType == FPEInteractableBaseScript.eInteractionType.PICKUP)
            {
                string scrubbedName = heldObject.gameObject.name.Split(FPEObjectTypeLookup.PickupPrefabDelimiter)[0];
                // Here, we pass in the first eInventoryItems value since we'll be ignoring it anyway, and it's "better" than exposing a "NO TYPE" in the enum list in the Inspector.
                heldObjectData = new FPEHeldObjectSaveData(true, scrubbedName, FPEInventoryManagerScript.eInventoryItems.APPLE, heldObject.transform.localRotation);
            }
            else if (heldObjectType == FPEInteractableBaseScript.eInteractionType.INVENTORY)
            {
                heldObjectData = new FPEHeldObjectSaveData(true, "", heldObject.GetComponent <FPEInteractableInventoryItemScript>().InventoryItemType, heldObject.transform.localRotation);
            }
            else
            {
                heldObjectData = new FPEHeldObjectSaveData(false, "", FPEInventoryManagerScript.eInventoryItems.APPLE, Quaternion.identity);
            }

            #endregion

            FPEInventoryManagerScript invManager = FPEInventoryManagerScript.Instance;

            #region INVENTORY_ITEMS

            List <FPEInteractableInventoryItemScript> invItems = invManager.getInventoryItems();
            int[] invQty = invManager.getInventoryQuantities();
            List <FPEInventoryItemSaveData> invData = new List <FPEInventoryItemSaveData>();
            int tempQty = 0;

            for (int i = 0; i < invItems.Count; i++)
            {
                if (invItems[i].Stackable)
                {
                    tempQty = invQty[(int)invItems[i].InventoryItemType];

                    for (int q = 0; q < tempQty; q++)
                    {
                        invData.Add(new FPEInventoryItemSaveData(invItems[i].InventoryItemType));
                    }
                }
                else
                {
                    invData.Add(new FPEInventoryItemSaveData(invItems[i].InventoryItemType));
                }
            }

            #endregion

            #region NOTES_AND_AUDIO_DIARIES

            // Notes
            FPENoteEntry[]    notes    = invManager.getNoteDataForSavedGame();
            FPENoteSaveData[] noteData = new FPENoteSaveData[notes.Length];

            for (int n = 0; n < notes.Length; n++)
            {
                noteData[n] = new FPENoteSaveData(notes[n].NoteTitle, notes[n].NoteBody);
            }

            // Audio Diaries
            FPEAudioDiaryEntry[]         diaries   = invManager.getAudioDiaryDataForSavedGame();
            FPEAudioDiaryEntrySaveData[] diaryData = new FPEAudioDiaryEntrySaveData[diaries.Length];

            for (int a = 0; a < diaries.Length; a++)
            {
                diaryData[a] = new FPEAudioDiaryEntrySaveData(diaries[a].DiaryTitle, diaries[a].getAudioDiaryClipPath(), diaries[a].ShowDiaryTitle);
            }

            #endregion

            FPEInventorySaveData inventoryData = new FPEInventorySaveData(heldObjectData, invData.ToArray(), diaryData, noteData);

            return(inventoryData);
        }
        /// <summary>
        /// Restores player's inventory (items, audio diaries, notes)
        /// </summary>
        /// <param name="data">The data from which to base restored inventory</param>
        public void restoreInventorySaveData(FPEInventorySaveData data)
        {
            FPEInventoryManagerScript invManager = FPEInventoryManagerScript.Instance;

            // Clear existing inventory
            invManager.clearInventoryItems();

            // Held Object
            FPEHeldObjectSaveData heldObjData = data.HeldObjectData;

            #region HELD_OBJECT
            if (heldObjData.HeldSomething)
            {
                GameObject tempLoadedObject = null;

                // Pickup Type
                if (heldObjData.PickupPrefabName != "")
                {
                    Object tempObject = Resources.Load(FPEObjectTypeLookup.PickupResourcePath + heldObjData.PickupPrefabName);

                    if (tempObject != null)
                    {
                        tempLoadedObject      = Instantiate(tempObject) as GameObject;
                        tempLoadedObject.name = heldObjData.PickupPrefabName;
                        tempLoadedObject.transform.localRotation = heldObjData.LocalRotation;
                    }
                    else
                    {
                        Debug.LogError("FPESaveLoadLogic:: Loading data encountered unknown Pickup named '" + heldObjData.PickupPrefabName + "'. No prefab was found with this name. This object will NOT be loaded.  Ensure that all Pickup prefabs are located in the '" + FPEObjectTypeLookup.PickupResourcePath + "' sub folder of a Resources folder.");
                    }
                }
                // Inventory Type
                else
                {
                    if (inventoryLookupTable.ContainsKey(heldObjData.InventoryItemType))
                    {
                        string tempPath = "";

                        if (inventoryLookupTable.TryGetValue(heldObjData.InventoryItemType, out tempPath))
                        {
                            tempLoadedObject = Instantiate(Resources.Load(FPEObjectTypeLookup.InventoryResourcePath + tempPath)) as GameObject;
                            tempLoadedObject.transform.localRotation = heldObjData.LocalRotation;
                        }
                        else
                        {
                            Debug.LogError("FPESaveLoadLogic:: Loading data could not get value for InventoryItemType '" + heldObjData.InventoryItemType + "'");
                        }
                    }
                    else
                    {
                        Debug.LogError("FPESaveLoadLogic:: Loading data encountered unknown InventoryItemType '" + heldObjData.InventoryItemType + "'. This object will NOT be loaded.  Ensure that all Inventory Item prefabs are located in the '" + FPEObjectTypeLookup.InventoryResourcePath + "' sub folder of a Resources folder. Also ensure that there is an entry in the FPEObjectTypeLookup 'inventoryItemsLookup' Dictionary for type '" + heldObjData.InventoryItemType + "'");
                    }
                }

                // Lastly, put the object into player's hand
                FPEInteractablePickupScript pickup = tempLoadedObject.GetComponent <FPEInteractablePickupScript>();

                if (pickup)
                {
                    FPEInteractionManagerScript.Instance.holdObjectFromGameLoad(pickup);
                }
                else
                {
                    Debug.LogError("FPESaveLoadLogic:: Loading held object for object '" + tempLoadedObject.gameObject.name + "', but its prefab had no attached FPEInteractablePickupScript component. Object will not be loaded. Check prefab.");
                }
            }
            #endregion

            #region INVENTORY_ITEMS

            FPEInventoryItemSaveData[] loadedItemData = data.InventoryItemData;

            // Create all the items, and add each to inventory
            GameObject tempInvObject = null;
            string     tempInvPath;
            FPEInteractableInventoryItemScript tempInvItem = null;

            // We start at index 1 to skip over our padded 0th value
            for (int i = 0; i < loadedItemData.Length; i++)
            {
                if (inventoryLookupTable.ContainsKey(loadedItemData[i].InventoryItemType))
                {
                    // Added this as an if, did that break things?
                    if (inventoryLookupTable.TryGetValue(loadedItemData[i].InventoryItemType, out tempInvPath))
                    {
                        tempInvObject = Instantiate(Resources.Load(FPEObjectTypeLookup.InventoryResourcePath + tempInvPath)) as GameObject;
                        tempInvItem   = tempInvObject.GetComponent <FPEInteractableInventoryItemScript>();

                        if (tempInvItem != null)
                        {
                            FPEInteractionManagerScript.Instance.putObjectIntoInventory(tempInvItem, false);
                        }
                        else
                        {
                            Debug.LogError("FPESaveLoadLogic:: Loaded Inventory Item prefab '" + FPEObjectTypeLookup.InventoryResourcePath + tempInvPath + "' had no attached FPEInteractableInventoryItemScript component. Item will NOT be restored into player inventory");
                        }
                    }
                    else
                    {
                        Debug.LogError("FPESaveLoadLogic:: Loading data could not get value for InventoryItemType '" + loadedItemData[i].InventoryItemType + "'");
                    }
                }
                else
                {
                    Debug.LogError("FPESaveLoadLogic:: Loading data encountered unknown InventoryItemType '" + loadedItemData[i].InventoryItemType + "'. This object will NOT be restored into player inventory.  Ensure that there is an entry in the FPEObjectTypeLookup 'inventoryItemsLookup' Dictionary for type '" + loadedItemData[i].InventoryItemType + "'");
                }
            }

            #endregion

            #region NOTES_AND_AUDIO_DIARIES

            FPENoteSaveData[]   loadedNoteData = data.NoteData;
            List <FPENoteEntry> noteEntries    = new List <FPENoteEntry>();

            for (int n = 0; n < loadedNoteData.Length; n++)
            {
                noteEntries.Add(loadedNoteData[n].getNoteEntry());
            }

            invManager.setNoteDataFromSavedGame(noteEntries);

            FPEAudioDiaryEntrySaveData[] loadedDiaryData = data.AudioDiaryData;
            List <FPEAudioDiaryEntry>    diaryEntries    = new List <FPEAudioDiaryEntry>();

            for (int a = 0; a < loadedDiaryData.Length; a++)
            {
                diaryEntries.Add(loadedDiaryData[a].getAudioDiaryEntry());
            }

            invManager.setAudioDiaryDataFromSavedGame(diaryEntries);

            #endregion
        }