示例#1
0
        private bool ListItems(InvCollection invCollection)
        {
            bool isCarrying = false;

            foreach (InvInstance invInstance in invCollection.InvInstances)
            {
                if (InvInstance.IsValid(invInstance))
                {
                    isCarrying = true;

                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("Item:", GUILayout.Width(80f));
                    if (invInstance.InvItem.canCarryMultiple)
                    {
                        EditorGUILayout.LabelField(invInstance.InvItem.label, EditorStyles.boldLabel, GUILayout.Width(135f));
                        EditorGUILayout.LabelField("Count:", GUILayout.Width(50f));
                        EditorGUILayout.LabelField(invInstance.InvItem.count.ToString(), GUILayout.Width(44f));
                    }
                    else
                    {
                        EditorGUILayout.LabelField(invInstance.InvItem.label, EditorStyles.boldLabel);
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }

            return(isCarrying);
        }
示例#2
0
        /**
         * <summary>Transfers all items from another collection</summary>
         * <param name="fromCollection">The collection of inventory item instances to transfer from</param>
         */
        public void TransferAll(InvCollection fromCollection)
        {
            if (fromCollection == null)
            {
                return;
            }

            InvInstance[] transferInstances = fromCollection.InvInstances.ToArray();
            foreach (InvInstance transferInstance in transferInstances)
            {
                Add(transferInstance);
            }
        }
示例#3
0
        /**
         * <summary>Transfer all counts of a given inventory item from another collection</summary>
         * <param name="invID">The ID of the inventory item (InvItem) to transfer</param>
         * <param name="fromCollection">The collection to transfer from</param>
         */
        public void Transfer(int invID, InvCollection fromCollection)
        {
            if (fromCollection == null || !fromCollection.Contains(invID))
            {
                return;
            }

            foreach (InvInstance invInstance in fromCollection.invInstances)
            {
                if (!InvInstance.IsValid(invInstance) || invInstance.ItemID != invID)
                {
                    continue;
                }

                Add(invInstance);
            }
        }
示例#4
0
        private bool HasInvalidItems(InvCollection invCollection)
        {
            if (ingredients.Count == 0)
            {
                return(true);
            }

            // Are any invalid ingredients present?
            List <InvItem> craftingItems = invCollection.InvItems;

            foreach (InvItem craftingItem in craftingItems)
            {
                if (!RequiresItem(craftingItem))
                {
                    return(true);
                }
            }
            return(false);
        }
示例#5
0
        /** Reduces the item instance's own internal amount of associated items, and creates a new one ready to be transferred to another collection */
        public InvInstance CreateTransferInstance()
        {
            InvCollection invCollection = GetSource();

            InvInstance newInstance = new InvInstance(this);

            if (transferCount > 0)
            {
                newInstance.count = transferCount;
            }

            count        -= newInstance.count;
            transferCount = 0;

            if (KickStarter.eventManager)
            {
                KickStarter.eventManager.Call_OnChangeInventory(invCollection, this, InventoryEventType.Remove, newInstance.count);
            }

            return(newInstance);
        }
        /**
         * <summary>Deserialises a string of data, and restores the GameObject to its previous state.</summary>
         * <param name = "stringData">The data, serialised as a string</param>
         */
        public override void LoadData(string stringData)
        {
            ContainerData data = Serializer.LoadScriptData <ContainerData> (stringData);

            if (data == null)
            {
                return;
            }
            SavePrevented = data.savePrevented; if (savePrevented)
            {
                return;
            }

            if (_Container)
            {
                List <InvInstance> invInstances = new List <InvInstance> ();

                if (!string.IsNullOrEmpty(data._linkedIDs))
                {
                    int[] linkedIDs = StringToIntArray(data._linkedIDs);
                    int[] counts    = StringToIntArray(data._counts);

                    if (linkedIDs != null)
                    {
                        for (int i = 0; i < linkedIDs.Length; i++)
                        {
                            invInstances.Add(new InvInstance(linkedIDs[i], counts[i]));
                        }
                    }

                    _Container.InvCollection = new InvCollection(invInstances);
                }
                else if (!string.IsNullOrEmpty(data.collectionData))
                {
                    _Container.InvCollection = InvCollection.LoadData(data.collectionData);
                }
            }
        }
示例#7
0
        /**
         * <summary>Transfer a set count of a given inventory item from another collection</summary>
         * <param name="invID">The ID of the inventory item (InvItem) to transfer</param>
         * <param name="fromCollection">The collection to transfer from</param>
         * <param name="amount">The amount of items to transfer</param>
         */
        public void Transfer(int invID, InvCollection fromCollection, int amount)
        {
            if (fromCollection == null || !fromCollection.Contains(invID))
            {
                return;
            }

            foreach (InvInstance invInstance in fromCollection.invInstances)
            {
                if (!InvInstance.IsValid(invInstance) || invInstance.ItemID != invID)
                {
                    continue;
                }

                int thisItemCount = invInstance.Count;
                if (thisItemCount < amount)
                {
                    // Will need more after this
                    invInstance.TransferCount = thisItemCount;
                    amount -= thisItemCount;
                }
                else
                {
                    // All we need
                    invInstance.TransferCount = amount;
                    amount = 0;
                }

                Add(invInstance);

                if (amount == 0)
                {
                    break;
                }
            }
        }
示例#8
0
 protected void CreateDefaultInstances()
 {
     invCollection = new InvCollection(this);
 }
示例#9
0
        /**
         * <summary>Checks if a collection of inventory item instances has all the items necessary to craft this recipe's item.</summary>
         * <param name="invCollection">The collection of inventory item instances to check</param>
         * <returns>True if the collection has all the items necessary to craft the recipe's item</returns>
         */
        public bool CanBeCrafted(InvCollection invCollection)
        {
            if (HasInvalidItems(invCollection))
            {
                return(false);
            }

            if (useSpecificSlots)
            {
                int maxSlot = invCollection.InvInstances.Count - 1;
                foreach (Ingredient ingredient in ingredients)
                {
                    if (maxSlot < ingredient.CraftingIndex)
                    {
                        maxSlot = ingredient.CraftingIndex;
                    }
                }

                if (maxSlot < 0)
                {
                    return(false);
                }

                for (int i = 0; i <= maxSlot; i++)
                {
                    Ingredient  ingredient  = GetIngredientForIndex(i);
                    InvInstance invInstance = invCollection.GetInstanceAtIndex(i);

                    if (InvInstance.IsValid(invInstance))
                    {
                        // Item in slot

                        if (ingredient == null)
                        {
                            return(false);
                        }

                        if (ingredient.ItemID != invInstance.ItemID)
                        {
                            return(false);
                        }

                        if (ingredient.Amount > invInstance.Count)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        // Slot is empty

                        if (ingredient != null)
                        {
                            return(false);
                        }
                    }
                }
            }
            else
            {
                // Indices don't matter, just check counts are correct

                foreach (Ingredient ingredient in ingredients)
                {
                    int ingredientCount = invCollection.GetCount(ingredient.ItemID);
                    if (ingredientCount < ingredient.Amount)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
示例#10
0
        public override void OnInspectorGUI()
        {
            Player _target = (Player)target;

            SharedGUIOne(_target);

            SettingsManager settingsManager = AdvGame.GetReferences().settingsManager;

            if (settingsManager != null && settingsManager.playerSwitching == PlayerSwitching.Allow)
            {
                NPC_GUI(_target);
            }

            SharedGUITwo(_target);

            if (settingsManager && (settingsManager.hotspotDetection == HotspotDetection.PlayerVicinity || settingsManager.playerSwitching == PlayerSwitching.Allow))
            {
                CustomGUILayout.BeginVertical();
                EditorGUILayout.LabelField("Player settings", EditorStyles.boldLabel);

                if (settingsManager.hotspotDetection == HotspotDetection.PlayerVicinity)
                {
                    _target.hotspotDetector = (DetectHotspots)CustomGUILayout.ObjectField <DetectHotspots> ("Hotspot detector child:", _target.hotspotDetector, true, "", "The DetectHotspots component to rely on for hotspot detection. This should be a child object of the Player.");
                }

                if (settingsManager.playerSwitching == PlayerSwitching.Allow)
                {
                    _target.autoSyncHotspotState = CustomGUILayout.Toggle("Auto-sync Hotspot state?", _target.autoSyncHotspotState, "", "If True, then any attached Hotspot will be made inactive while this character is the current active Player");
                }

                CustomGUILayout.EndVertical();
            }

            if (Application.isPlaying && _target.gameObject.activeInHierarchy)
            {
                CustomGUILayout.BeginVertical();
                EditorGUILayout.LabelField("Current inventory", EditorStyles.boldLabel);

                bool isCarrying = false;

                if (KickStarter.saveSystem != null)
                {
                    if ((_target.IsLocalPlayer() ||
                         KickStarter.settingsManager.playerSwitching == PlayerSwitching.DoNotAllow ||
                         _target.ID == KickStarter.saveSystem.CurrentPlayerID ||
                         KickStarter.settingsManager.shareInventory))
                    {
                        if (KickStarter.runtimeInventory != null && KickStarter.runtimeInventory.localItems != null)
                        {
                            if (ListItems(KickStarter.runtimeInventory.PlayerInvCollection))
                            {
                                isCarrying = true;
                            }
                        }

                        if (KickStarter.inventoryManager != null && KickStarter.runtimeDocuments != null && KickStarter.runtimeDocuments.GetCollectedDocumentIDs() != null)
                        {
                            for (int i = 0; i < KickStarter.runtimeDocuments.GetCollectedDocumentIDs().Length; i++)
                            {
                                Document document = KickStarter.inventoryManager.GetDocument(KickStarter.runtimeDocuments.GetCollectedDocumentIDs()[i]);

                                if (document != null)
                                {
                                    isCarrying = true;

                                    EditorGUILayout.BeginHorizontal();
                                    EditorGUILayout.LabelField("Document:", GUILayout.Width(80f));
                                    EditorGUILayout.LabelField(document.Title, EditorStyles.boldLabel);
                                    EditorGUILayout.EndHorizontal();
                                }
                            }
                        }

                        if (KickStarter.inventoryManager != null && KickStarter.runtimeObjectives != null)
                        {
                            ObjectiveInstance[] objectiveInstances = KickStarter.runtimeObjectives.GetObjectives();
                            foreach (ObjectiveInstance objectiveInstance in objectiveInstances)
                            {
                                EditorGUILayout.BeginHorizontal();
                                EditorGUILayout.LabelField("Objective:", GUILayout.Width(80f));
                                EditorGUILayout.LabelField(objectiveInstance.Objective.GetTitle() + ": " + objectiveInstance.CurrentState.GetLabel(), EditorStyles.boldLabel);
                                EditorGUILayout.EndHorizontal();
                            }
                        }
                    }
                    else
                    {
                        PlayerData playerData = KickStarter.saveSystem.GetPlayerData(_target.ID);
                        if (playerData != null)
                        {
                            if (ListItems(InvCollection.LoadData(playerData.inventoryData)))
                            {
                                isCarrying = true;
                            }

                            if (!string.IsNullOrEmpty(playerData.collectedDocumentData))
                            {
                                EditorGUILayout.LabelField("Documents:", playerData.collectedDocumentData);
                            }
                            if (!string.IsNullOrEmpty(playerData.playerObjectivesData))
                            {
                                EditorGUILayout.LabelField("Objectives:", playerData.playerObjectivesData);
                            }
                        }
                    }
                }

                if (!isCarrying)
                {
                    EditorGUILayout.HelpBox("This Player is not carrying any items.", MessageType.Info);
                }

                CustomGUILayout.EndVertical();
            }

            UnityVersionHandler.CustomSetDirty(_target);
        }
示例#11
0
        /**
         * <summary>Inserts an inventory item instance into a specific index in the collection.  If the item exists in another collection, it will be removed from there automatically.</summary>
         * <param name="addInstance">The inventory item instance to add</param>
         * <param name="index">The index to insert the item at</param>
         * <param name="occupiedSlotBehaviour">How to react if the intended index is already occupied by another item instance.</param>
         * <returns>The new instance of the added item</returns>
         */
        public InvInstance Insert(InvInstance addInstance, int index, OccupiedSlotBehaviour occupiedSlotBehaviour = OccupiedSlotBehaviour.ShiftItems)
        {
            // Adds to a specific index, or the end/first empty slot if -1
            if (!CanAccept(addInstance, index, occupiedSlotBehaviour))
            {
                if (InvInstance.IsValid(addInstance))
                {
                    if (KickStarter.eventManager)
                    {
                        KickStarter.eventManager.Call_OnUseContainerFail(addInstance.GetSourceContainer(), addInstance);
                    }
                }
                return(null);
            }

            InvInstance addedInstance = null;

            if (Contains(addInstance))
            {
                if (!CanReorder())
                {
                    return(addInstance);
                }

                if (MaxSlots > 0 && index >= MaxSlots)
                {
                    return(addInstance);
                }

                occupiedSlotBehaviour = OccupiedSlotBehaviour.SwapItems;
            }

            int numAdded = -1;

            InvCollection fromCollection = (Contains(addInstance)) ? this : addInstance.GetSource();

            if (index >= 0 && index < invInstances.Count)
            {
                // Inside
                InvInstance existingInstance = invInstances[index];

                if (!InvInstance.IsValid(existingInstance))
                {
                    // Empty slot
                    addedInstance = addInstance.CreateTransferInstance();

                    if (InvInstance.IsValid(addedInstance))
                    {
                        numAdded            = addedInstance.Count;
                        invInstances[index] = addedInstance;
                    }
                }
                else
                {
                    if (existingInstance == addInstance)
                    {
                        // Same
                        return(existingInstance);
                    }
                    else if (existingInstance.InvItem == addInstance.InvItem && addInstance.InvItem.canCarryMultiple && existingInstance.Capacity > 0)
                    {
                        // Merge
                        if (addInstance.TransferCount > existingInstance.Capacity)
                        {
                            addInstance.TransferCount = existingInstance.Capacity;
                        }
                        numAdded = Mathf.Min(addInstance.CreateTransferInstance().Count, existingInstance.Capacity);
                        existingInstance.Count += numAdded;
                        addedInstance           = existingInstance;
                    }
                    else
                    {
                        switch (occupiedSlotBehaviour)
                        {
                        case OccupiedSlotBehaviour.ShiftItems:
                            invInstances.Insert(index, addInstance.CreateTransferInstance());
                            addedInstance = invInstances[index];
                            break;

                        case OccupiedSlotBehaviour.FailTransfer:
                            if (InvInstance.IsValid(addInstance))
                            {
                                if (KickStarter.eventManager)
                                {
                                    KickStarter.eventManager.Call_OnUseContainerFail(addInstance.GetSourceContainer(), addInstance);
                                }
                                return(null);
                            }
                            break;

                        case OccupiedSlotBehaviour.SwapItems:
                            if (fromCollection != null)
                            {
                                if (addInstance.IsPartialTransform())
                                {
                                    if (KickStarter.eventManager)
                                    {
                                        KickStarter.eventManager.Call_OnUseContainerFail(addInstance.GetSourceContainer(), addInstance);
                                    }
                                    return(null);
                                }

                                fromCollection.invInstances[fromCollection.IndexOf(addInstance)] = existingInstance;
                                invInstances[index] = addInstance;
                                addedInstance       = invInstances[index];

                                if (KickStarter.runtimeInventory.SelectedInstance == addInstance)
                                {
                                    KickStarter.runtimeInventory.SelectItem(existingInstance);
                                }
                            }
                            break;

                        case OccupiedSlotBehaviour.Overwrite:
                            if (KickStarter.eventManager)
                            {
                                KickStarter.eventManager.Call_OnChangeInventory(this, existingInstance, InventoryEventType.Remove);
                            }
                            invInstances[index] = addInstance.CreateTransferInstance();
                            addedInstance       = invInstances[index];
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            else
            {
                // Add to first empty slot, or end
                bool addedInside = false;

                if (index < 0)
                {
                    // Find first empty slot
                    for (int i = 0; i < invInstances.Count; i++)
                    {
                        if (!InvInstance.IsValid(invInstances[i]))
                        {
                            invInstances[i] = addInstance.CreateTransferInstance();
                            addedInstance   = invInstances[i];
                            index           = i;
                            addedInside     = true;
                            break;
                        }
                        else if (invInstances[i] == addInstance)
                        {
                            return(addInstance);
                        }
                    }
                }

                if (!addedInside)
                {
                    if (maxSlots > 0 && invInstances.Count >= maxSlots)
                    {
                        return(null);
                    }

                    if (index > 0 && CanReorder())
                    {
                        while (invInstances.Count < index)
                        {
                            invInstances.Add(null);
                        }
                    }

                    invInstances.Add(addInstance.CreateTransferInstance());
                    addedInstance = invInstances[invInstances.Count - 1];
                }
            }

            if (fromCollection != null && fromCollection != this)
            {
                fromCollection.Clean();
            }

            Clean();
            PlayerMenus.ResetInventoryBoxes();

            if (KickStarter.eventManager)
            {
                if (numAdded >= 0)
                {
                    KickStarter.eventManager.Call_OnChangeInventory(this, addedInstance, InventoryEventType.Add, numAdded);
                }
                else
                {
                    KickStarter.eventManager.Call_OnChangeInventory(this, addedInstance, InventoryEventType.Add);
                }
            }
            return(addedInstance);
        }
示例#12
0
        /**
         * <summary>Adds an inventory item instance to the collection.  If the item exists in another collection, it will be removed from there automatically.</summary>
         * <param name="addInstance">The inventory item instance to add</param>
         */
        public void Add(InvInstance addInstance)
        {
            // Add to the first-available slot, or a filled slot if the same item

            if (!CanAccept(addInstance))
            {
                if (InvInstance.IsValid(addInstance))
                {
                    if (KickStarter.eventManager)
                    {
                        KickStarter.eventManager.Call_OnUseContainerFail(addInstance.GetSourceContainer(), addInstance);
                    }
                }
                return;
            }

            InvCollection fromCollection = addInstance.GetSource();

            bool added = false;

            for (int i = 0; i < invInstances.Count; i++)
            {
                // Inside

                if (!InvInstance.IsValid(invInstances[i]))
                {
                    // Empty slot
                    invInstances[i] = addInstance.CreateTransferInstance();
                    if (KickStarter.eventManager)
                    {
                        KickStarter.eventManager.Call_OnChangeInventory(this, invInstances[i], InventoryEventType.Add);
                    }
                    added = true;
                    break;
                }
                else if (invInstances[i] == addInstance)
                {
                    // Same
                }
                else if (invInstances[i].InvItem == addInstance.InvItem && addInstance.InvItem.canCarryMultiple && invInstances[i].Capacity > 0)
                {
                    // Merge
                    if (addInstance.TransferCount > invInstances[i].Capacity)
                    {
                        addInstance.TransferCount = invInstances[i].Capacity;
                    }
                    int numAdded = Mathf.Min(addInstance.CreateTransferInstance().Count, invInstances[i].Capacity);
                    invInstances[i].Count += numAdded;
                    if (KickStarter.eventManager)
                    {
                        KickStarter.eventManager.Call_OnChangeInventory(this, invInstances[i], InventoryEventType.Add, numAdded);
                    }
                    added = true;
                    break;
                }
            }

            if (!added)
            {
                AddToEnd(addInstance);
                return;
            }

            if (fromCollection != null)
            {
                fromCollection.Clean();
            }
            Clean();
            PlayerMenus.ResetInventoryBoxes();
        }