private void AddAmmo() { vp_ItemIdentifier item = inventory.CurrentWeaponIdentifier; if (item == null) { return; } if (currentClip) { vp_UnitBankInstance bank = (inventory.GetItem(item.Type, item.ID) as vp_UnitBankInstance); bank.Count += 1; bank.ClampToCapacity(); } else { vp_UnitBankType type = (vp_UnitBankType)item.Type; if (m_Player.AddAmmo.Try(type.Unit)) { inventory.TryGiveUnits(type.Unit, 1); } } }
/// <summary> /// NOTE: for use with item unitbanks (such as weapons) /// </summary> public virtual bool TryDeduct(vp_UnitBankType unitBankType, int unitBankId, int amount) { vp_UnitBankInstance bank = ((unitBankId < 1) ? GetItem(unitBankType) as vp_UnitBankInstance : GetItem(unitBankType, unitBankId) as vp_UnitBankInstance); if (bank == null) { return(false); } if (!DoRemoveUnits(bank, amount)) { return(false); } // if this operation emptied the unitbank and it's not an // internal unitbank, see if it should be depleted if ((bank.Count <= 0) && ((bank.Type as vp_UnitBankType).RemoveWhenDepleted)) { DoRemoveUnitBank(bank); } return(true); }
/// <summary> /// /// </summary> protected virtual void OnStop_SetWeapon() { // clear the cached unitbank because it will not always be the same m_UnitBank = null; }
/// <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); }
/// <summary> /// /// </summary> public virtual void SetUnitCount(vp_UnitBankInstance bank, int amount) { if (bank == null) return; amount = Mathf.Max(0, amount); if (amount == bank.Count) return; int prevInventoryCount = bank.Count; if (!DoRemoveUnits(bank, bank.Count)) bank.Count = prevInventoryCount; if (amount == 0) return; if (!DoAddUnits(bank, amount)) bank.Count = prevInventoryCount; }
/// <summary> /// /// </summary> public override bool DoRemoveUnits(vp_UnitBankInstance bank, int amount) { bool result = base.DoRemoveUnits(bank, amount); if (bank.Count == 0) vp_Timer.In(0.3f, delegate() { Player.AutoReload.Try(); }); // try to auto-reload (success determined by weaponhandler) return result; }
/// <summary> /// /// </summary> protected override void DoRemoveUnitBank(vp_UnitBankInstance bank) { Unwield(bank); base.DoRemoveUnitBank(bank); }
/// <summary> /// NOTE: only for item unit banks (such as weapons) /// </summary> public virtual bool TryReload(vp_UnitBankInstance bank, int amount) { if ((bank == null) || (bank.IsInternal) || (bank.ID == UNIDENTIFIED)) { Debug.LogWarning("Warning (" + vp_Utility.GetErrorLocation() + ") 'TryReloadUnitBank' could not identify a target item. If you are trying to add units to the main inventory please instead use 'TryGiveUnits'."); return(false); } // fetch the amount of units in the unitbank prior to reloading int preLoadedUnits = bank.Count; // if unitbank is full, there's no point in reloading if (preLoadedUnits >= bank.Capacity) { return(false); } // fetch the current amount of suitable units in the inventory int prevInventoryCount = GetUnitCount(bank.UnitType); // if inventory is empty, there's not much more to do if (prevInventoryCount < 1) { return(false); } // an amount of -1 means 'fill her up' if (amount == MAXCAPACITY) { amount = bank.Capacity; } // remove as many units as we can from the inventory TryRemoveUnits(bank.UnitType, amount); // figure out how many units we managed to remove from inventory int unitsRemoved = Mathf.Max(0, (prevInventoryCount - GetUnitCount(bank.UnitType))); // try to load the target unit bank with the removed units // NOTE: we can never fail on space limit here (since the units we // reload with were already in the inventory). however, space may // have been deducted when we removed from the internal bank so // we need to refresh space when moving units over to the unitbank if (!DoAddUnits(bank, unitsRemoved)) { return(false); } // let's see how many units we managed to transfer to the unitbank int unitsLoaded = Mathf.Max(0, (bank.Count - preLoadedUnits)); // if we managed to load zero units, report failure if (unitsLoaded < 1) { return(false); } // if we succeeded in loading some units - but not as many // as we removed from the inventory - put the excess units // back in the internal unit bank if ((unitsLoaded > 0) && (unitsLoaded < unitsRemoved)) { TryGiveUnits(bank.UnitType, (unitsRemoved - unitsLoaded)); } return(true); }
/// <summary> /// /// </summary> public virtual bool DoRemoveUnits(vp_UnitBankInstance bank, int amount) { return(bank.DoRemoveUnits(amount)); }
/// <summary> /// tries to remove a unitbank instance directly /// </summary> public virtual bool TryRemoveUnitBank(vp_UnitBankInstance unitBank) { if (unitBank == null) return false; DoRemoveUnitBank(unitBank); SetDirty(); return true; }
/// <summary> /// /// </summary> public virtual bool TryGiveUnits(vp_UnitBankInstance bank, int amount) { if (bank == null) return false; amount = Mathf.Max(0, amount); if (SpaceEnabled && (bank.IsInternal || (SpaceMode == Mode.Weight))) { if (RemainingSpace < (amount * bank.UnitType.Space)) { amount = ((int)(RemainingSpace / bank.UnitType.Space)); return DoAddUnits(bank, amount); } } return DoAddUnits(bank, amount); }
/// <summary> /// /// </summary> public virtual bool DoRemoveUnits(vp_UnitBankInstance bank, int amount) { return bank.DoRemoveUnits(amount); }
/// <summary> /// /// </summary> protected virtual void DoRemoveUnitBank(vp_UnitBankInstance bank) { if (!bank.IsInternal) { m_UnitBankInstances.RemoveAt(m_UnitBankInstances.IndexOf(bank)); m_FirstItemsDirty = true; m_ItemDictionaryDirty = true; if (SpaceEnabled) { m_UsedSpace -= bank.Type.Space; // release inventory space for the unitbank if (SpaceMode == Mode.Weight) m_UsedSpace -= (bank.UnitType.Space * bank.Count); // release inventory space for the loaded units } } else m_InternalUnitBanks.RemoveAt(m_InternalUnitBanks.IndexOf(bank)); SetDirty(); }
/// <summary> /// /// </summary> protected virtual void DoAddUnitBank(vp_UnitBankType unitBankType, int id, int unitsLoaded) { vp_UnitBankInstance bank = new vp_UnitBankInstance(unitBankType, id, this); m_UnitBankInstances.Add(bank); m_FirstItemsDirty = true; m_ItemDictionaryDirty = true; if ((SpaceEnabled && !bank.IsInternal)) m_UsedSpace += unitBankType.Space; // consume inventory space for the unitbank bank.TryGiveUnits(unitsLoaded); if (((SpaceEnabled && !bank.IsInternal) && (SpaceMode == Mode.Weight) && (unitBankType.Unit != null))) m_UsedSpace += (unitBankType.Unit.Space * bank.Count); // consume inventory space for the loaded units SetDirty(); }
/// <summary> /// /// </summary> public virtual bool TrySetUnitCount(vp_UnitBankInstance bank, int amount) { if (bank == null) return false; amount = Mathf.Max(0, amount); if (amount == bank.Count) return true; int prevInventoryCount = bank.Count; if (!DoRemoveUnits(bank, bank.Count)) bank.Count = prevInventoryCount; if (amount == 0) return true; if(bank.IsInternal) { m_Result = TryGiveUnits(bank.UnitType, amount); if (m_Result == false) bank.Count = prevInventoryCount; return m_Result; } m_Result = TryGiveUnits(bank, amount); if (m_Result == false) bank.Count = prevInventoryCount; return m_Result; }
/// <summary> /// NOTE: only for item unit banks (such as weapons) /// </summary> public virtual bool TryReload(vp_UnitBankInstance bank) { return TryReload(bank, MAXCAPACITY); }
/// <summary> /// NOTE: only for item unit banks (such as weapons) /// </summary> public virtual bool TryReload(vp_UnitBankInstance bank) { return(TryReload(bank, MAXCAPACITY)); }
/// <summary> /// NOTE: only for item unit banks (such as weapons) /// </summary> public virtual bool TryReload(vp_UnitBankInstance bank, int amount) { if ((bank == null) || (bank.IsInternal) || (bank.ID == UNIDENTIFIED)) { Debug.LogWarning("Warning (" + vp_Utility.GetErrorLocation() + ") 'TryReloadUnitBank' could not identify a target item. If you are trying to add units to the main inventory please instead use 'TryGiveUnits'."); return false; } // fetch the amount of units in the unitbank prior to reloading int preLoadedUnits = bank.Count; // if unitbank is full, there's no point in reloading if (preLoadedUnits >= bank.Capacity) return false; // fetch the current amount of suitable units in the inventory int prevInventoryCount = GetUnitCount(bank.UnitType); // if inventory is empty, there's not much more to do if (prevInventoryCount < 1) return false; // an amount of -1 means 'fill her up' if (amount == MAXCAPACITY) amount = bank.Capacity; // remove as many units as we can from the inventory TryRemoveUnits(bank.UnitType, amount); // figure out how many units we managed to remove from inventory int unitsRemoved = Mathf.Max(0, (prevInventoryCount - GetUnitCount(bank.UnitType))); // try to load the target unit bank with the removed units // NOTE: we can never fail on space limit here (since the units we // reload with were already in the inventory). however, space may // have been deducted when we removed from the internal bank so // we need to refresh space when moving units over to the unitbank if (!DoAddUnits(bank, unitsRemoved)) return false; // let's see how many units we managed to transfer to the unitbank int unitsLoaded = Mathf.Max(0, (bank.Count - preLoadedUnits)); // if we managed to load zero units, report failure if (unitsLoaded < 1) return false; // if we succeeded in loading some units - but not as many // as we removed from the inventory - put the excess units // back in the internal unit bank if ((unitsLoaded > 0) && (unitsLoaded < unitsRemoved)) TryGiveUnits(bank.UnitType, (unitsRemoved - unitsLoaded)); return true; }
/// <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); }
/// <summary> /// /// </summary> public override bool DoAddUnits(vp_UnitBankInstance bank, int amount) { bool result = base.DoAddUnits(bank, amount); // if units were added to the inventory (and not to a weapon) if ((result == true && bank.IsInternal) && !((Application.isPlaying) && WeaponHandler.CurrentWeaponIndex == 0)) { // fetch the inventory record for the current weapon to see // if we should reload it straight away vp_UnitBankInstance weapon = (CurrentWeaponInstance as vp_UnitBankInstance); if (weapon != null) { // if the currently wielded weapon uses the same kind of units, // and is currently out of ammo ... if ((bank.UnitType == weapon.UnitType) && (weapon.Count == 0)) Player.AutoReload.Try(); // try to auto-reload (success determined by weaponhandler) } } return result; }
/// <summary> /// /// </summary> public override bool DoAddUnits(vp_UnitBankInstance bank, int amount) { if(bank == null) return false; int prevUnitCount = GetUnitCount(bank.UnitType); bool result = base.DoAddUnits(bank, amount); // if units were added to the inventory (and not to a weapon) if ((result == true && bank.IsInternal) ) { try { TryWieldNewItem(bank.UnitType, (prevUnitCount != 0)); } catch { // DEBUG: uncomment on elusive item wielding issues //Debug.LogError("Error (" + this + ") Failed to wield new item."); } // --- auto reload firearms --- if(!((Application.isPlaying) && WeaponHandler.CurrentWeaponIndex == 0)) { // fetch the inventory record for the current weapon to see // if we should reload it straight away vp_UnitBankInstance curBank = (CurrentWeaponInstance as vp_UnitBankInstance); if (curBank != null) { // if the currently wielded weapon uses the same kind of units, // and is currently out of ammo ... if ((bank.UnitType == curBank.UnitType) && (curBank.Count == 0)) { Player.AutoReload.Try(); // try to auto-reload (success determined by weaponhandler) } } } } return result; }
/// <summary> /// /// </summary> public virtual bool DoRemoveUnits(vp_UnitBankInstance bank, int amount) { //Debug.Log("DoRemoveUnits"); return(bank.DoRemoveUnits(amount)); }
/// <summary> /// NOTE: only for internal inventory unit banks! /// </summary> public virtual vp_UnitBankInstance GetInternalUnitBank(vp_UnitType unitType) { for (int v = 0; v < m_InternalUnitBanks.Count; v++) { // is item a unit bank? if (m_InternalUnitBanks[v].GetType() != typeof(vp_UnitBankInstance)) continue; // is item internal? (has no vp_ItemType) if (m_InternalUnitBanks[v].Type != null) continue; vp_UnitBankInstance b = (vp_UnitBankInstance)m_InternalUnitBanks[v]; if(b.UnitType != unitType) continue; return b; } SetDirty(); vp_UnitBankInstance bank = new vp_UnitBankInstance(unitType, this); // set capacity to the item cap set for the unit type (if any) // if no such cap exists, capacity will be unlimited bank.Capacity = GetItemCap(unitType); m_InternalUnitBanks.Add(bank); return bank; }