/// <summary> /// returns true if inventory has items of 'itemType', and the /// amount of existing items is modified or unchanged. returns /// false if there are no matching items /// </summary> protected static bool TryMatchExistingItem(vp_Inventory inventory, Transform recipient, vp_ItemType itemType, int amount) { //Debug.Log("--- TryMatchExistingItem ---"); //Debug.Log("recipient: " + recipient); //Debug.Log("item type: " + itemType); //Debug.Log("amount: " + amount); System.Type type = itemType.GetType(); if ((type == typeof(vp_UnitBankType)) || (type.BaseType == typeof(vp_UnitBankType))) { vp_UnitBankInstance ui = inventory.GetItem(itemType) as vp_UnitBankInstance; if (ui != null) { ui.Count = amount; return(true); } } else if ((type == typeof(vp_UnitType)) || (type.BaseType == typeof(vp_UnitType))) { if (inventory.HaveInternalUnitBank(itemType as vp_UnitType)) { vp_UnitBankInstance ui = inventory.GetInternalUnitBank(itemType as vp_UnitType); ui.Count = amount; return(true); } } else if ((type == typeof(vp_ItemType)) || (type.BaseType == typeof(vp_ItemType))) { int existing = inventory.GetItemCount(itemType); //Debug.Log("currently existing in inventory: " + existing); if (existing > 0) { int adjustment = (amount - existing); //Debug.Log("needed adjustment: " + adjustment); if (adjustment < 0) { //Debug.Log("adjustment less than zero. running 'TryRemoveItems' with argument '" + adjustment + "' and result: " + inventory.TryRemoveItems(itemType, Mathf.Abs(adjustment)); //); } else if (adjustment > 0) { //Debug.Log("adjustment more than zero. running 'TryGiveItems' with argument '" + adjustment + "' and result: " + inventory.TryGiveItems(itemType, adjustment); //); } return(true); } } //Debug.Log("------------------------"); return(false); }
/// <summary> /// returns true if inventory has items of 'itemType', and the /// amount of existing items is modified or unchanged. returns /// false if there are no matching items /// </summary> protected static bool TryMatchExistingItem(vp_Inventory inventory, Transform recipient, vp_ItemType itemType, int amount) { //Debug.Log("--- TryMatchExistingItem ---"); //Debug.Log("recipient: " + recipient); //Debug.Log("item type: " + itemType); //Debug.Log("amount: " + amount); System.Type type = itemType.GetType(); if ((type == typeof(vp_UnitBankType)) || (type.BaseType == typeof(vp_UnitBankType))) { vp_UnitBankInstance ui = inventory.GetItem(itemType) as vp_UnitBankInstance; if (ui != null) { ui.Count = amount; return true; } } else if ((type == typeof(vp_UnitType)) || (type.BaseType == typeof(vp_UnitType))) { if (inventory.HaveInternalUnitBank(itemType as vp_UnitType)) { vp_UnitBankInstance ui = inventory.GetInternalUnitBank(itemType as vp_UnitType); ui.Count = amount; return true; } } else if ((type == typeof(vp_ItemType)) || (type.BaseType == typeof(vp_ItemType))) { int existing = inventory.GetItemCount(itemType); //Debug.Log("currently existing in inventory: " + existing); if (existing > 0) { int adjustment = (amount - existing); //Debug.Log("needed adjustment: " + adjustment); if (adjustment < 0) { //Debug.Log("adjustment less than zero. running 'TryRemoveItems' with argument '" + adjustment + "' and result: " + inventory.TryRemoveItems(itemType, Mathf.Abs(adjustment)); //); } else if (adjustment > 0) { //Debug.Log("adjustment more than zero. running 'TryGiveItems' with argument '" + adjustment + "' and result: " + inventory.TryGiveItems(itemType, adjustment); //); } return true; } } //Debug.Log("------------------------"); return false; }
/// <summary> /// /// </summary> protected float DoItemsFoldout(Rect pos, SerializedProperty prop) { m_InitialY = pos.y; pos.height = 16; vp_Inventory inventory = ((vp_Inventory)prop.serializedObject.targetObject); GUI.backgroundColor = Color.white; pos.y += (( (inventory.ItemInstances.Count > 0) || (inventory.UnitBankInstances.Count > 0) || (inventory.InternalUnitBanks.Count > 0)) ? 5 : -5); pos.x += 20; pos.width -= 15; // --- draw internal unit banks --- for (int v = 0; v < inventory.InternalUnitBanks.Count; v++) { vp_UnitBankInstance ibank = inventory.InternalUnitBanks[v]; if ((ibank == null) || (ibank.UnitType == null)) { inventory.InternalUnitBanks.Remove(ibank); inventory.Refresh(); continue; } string name = ibank.UnitType.ToString(); name = name.Remove(name.IndexOf(" (")) + "s (Internal UnitBank)"; int unitCount = inventory.GetItemCount(ibank.UnitType); vp_PropertyDrawerUtility.ItemCard(pos, ((ibank == null) ? null : ibank.UnitType.Icon), name, ((ibank == null) ? null : ibank.UnitType), ref unitCount, "Units", delegate() { inventory.TrySetUnitCount(ibank.UnitType, unitCount); }, ref NO_VALUE, "", null, delegate() { m_UnitBankToRemove = ibank; }); pos.y += 21; } // --- draw item unit bank instances (such as weapons) --- for (int v = 0; v < inventory.UnitBankInstances.Count; v++) { vp_UnitBankInstance bank = inventory.UnitBankInstances[v]; int unitCount = bank.Count; if ((bank == null) || (bank.Type == null)) { inventory.UnitBankInstances.Remove(bank); inventory.Refresh(); continue; } string name = bank.Type.ToString(); if (vp_PropertyDrawerUtility.ItemCard(pos, ((bank == null) ? null : bank.Type.Icon), name, ((bank == null) ? null : bank.Type), ref unitCount, "Units", delegate() { inventory.TrySetUnitCount(bank, unitCount); }, ref bank.ID, "ID", null, // no need to act on value change since it is handled by the ref above delegate() { m_UnitBankToRemove = bank; })) { inventory.ClearItemDictionaries(); inventory.SetDirty(); } pos.y += 21; } // --- draw item instances --- for (int v = 0; v < inventory.ItemInstances.Count; v++) { vp_ItemInstance item = inventory.ItemInstances[v]; if ((item == null) || (item.Type == null)) { inventory.ItemInstances.Remove(item); inventory.Refresh(); continue; } string name = item.Type.ToString(); if (vp_PropertyDrawerUtility.ItemCard(pos, ((item == null) ? null : item.Type.Icon), name, ((item == null) ? null : item.Type), ref item.ID, "ID", null, // no need to act on value change since it is handled by the ref above ref NO_VALUE, "", null, delegate() { m_ItemToRemove = item; }, 45)) { inventory.ClearItemDictionaries(); inventory.SetDirty(); } pos.y += 21; } // --- draw 'add object' box --- pos.y += 16; vp_PropertyDrawerUtility.AddObjectBox(pos, "n ItemType", typeof(vp_ItemType), delegate(Object itemType) { if (itemType is vp_UnitBankType) { vp_UnitBankType bank = (vp_UnitBankType)itemType; int cap = inventory.GetItemCap((vp_UnitBankType)itemType); if ((cap == -1) || (inventory.GetItemCount((vp_UnitBankType)itemType) < cap)) { if (!inventory.TryGiveUnitBank(bank, bank.Capacity, 0)) { EditorUtility.DisplayDialog(m_InventoryFullCaption, string.Format(m_InventoryFullMessage, itemType), "OK"); } } else { EditorUtility.DisplayDialog(m_InventoryFullCaption, string.Format(m_InventoryFullMessage, itemType), "OK"); } } else if (itemType is vp_UnitType) { if (!inventory.HaveInternalUnitBank((vp_UnitType)itemType)) { vp_UnitType unitType = (vp_UnitType)itemType; if (!inventory.TryGiveUnits(unitType, 10)) { EditorUtility.DisplayDialog(m_InventoryFullCaption, string.Format(m_InventoryFullMessage, itemType), "OK"); } } else { EditorUtility.DisplayDialog(m_UnitBankAlreadyExistsCaption, string.Format(m_UnitBankAlreadyExistsMessage, itemType), "OK"); } } else { if (!inventory.TryGiveItem((vp_ItemType)itemType, 0)) { EditorUtility.DisplayDialog(m_InventoryFullCaption, string.Format(m_InventoryFullMessage, itemType), "OK"); } } EditorUtility.SetDirty(inventory); inventory.Refresh(); }); pos.y += vp_PropertyDrawerUtility.CalcAddObjectBoxHeight - 5; // handle removed items if (m_UnitBankToRemove != null) { inventory.TryRemoveUnitBank(m_UnitBankToRemove); m_UnitBankToRemove = null; } else if (m_ItemToRemove != null) { inventory.TryRemoveItem(m_ItemToRemove); m_ItemToRemove = null; } return(pos.y - m_InitialY); }