/// <summary> /// /// </summary> public virtual int GetItemCount(vp_ItemType type) { vp_UnitType unitType = type as vp_UnitType; if (unitType != null) { return(GetUnitCount(unitType)); } int count = 0; for (int v = 0; v < ItemInstances.Count; v++) { if (ItemInstances[v].Type == type) { count++; } } for (int v = 0; v < m_UnitBankInstances.Count; v++) { if (m_UnitBankInstances[v].Type == type) { count++; } } return(count); }
/// <summary> /// /// </summary> public virtual bool HaveInternalUnitBank(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(true); } return(false); }
/// <summary> /// NOTE: only for internal inventory unit banks! /// </summary> protected 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); }
/// <summary> /// returns true if the player rig has a vp_Weapon with the same UnitType and /// 'AnimationType:Thrown'. /// </summary> public virtual bool IsThrowingUnit(vp_UnitType unitType) { if (!m_HaveThrowingWeaponInfo) { StoreThrowingWeaponInfo(); } return(m_ThrowingWeaponUnitTypes.Contains(unitType)); }
/// <summary> /// tries to drop the currenlty loaded throwing weapon unit in the form /// of an ammo unit (rather than in the form of a weapon) /// </summary> protected virtual void TryDropLoadedThrowingUnit(vp_UnitType unitType) { vp_UnitBankType unitBankType = Inventory.GetThrowingWeaponUnitBankType(unitType); if (unitBankType == null) { return; } for (int v = (WeaponHandler.Weapons.Count - 1); v > -1; v--) { if (WeaponHandler.Weapons[v] == null) { continue; } vp_ItemIdentifier identifier = WeaponHandler.Weapons[v].GetComponent <vp_ItemIdentifier>(); if (identifier == null) { continue; } vp_ItemType iType = identifier.GetItemType(); if (iType == null) { continue; } if (iType != unitBankType) { continue; } for (int u = (Inventory.UnitBankInstances.Count - 1); u > -1; u--) { //Debug.Log("unittype: " + Inventory.UnitBankInstances[u].Type + ", count: " + Inventory.UnitBankInstances[u].Count); if (Inventory.UnitBankInstances[u].UnitType == unitBankType.Unit) { // if the throwing weapon is currently loaded with one unit (likely), move that unit // from the throwing weapon to the internal unitbank and drop it as an ammo clip if (Inventory.UnitBankInstances[u].Count == 1) { // move unit to internal unitbank Inventory.UnitBankInstances[u].DoRemoveUnits(1); vp_UnitBankInstance internalUnitBank = Inventory.GetInternalUnitBank(Inventory.UnitBankInstances[u].UnitType); internalUnitBank.DoAddUnits(1); // drop the moved unit if (TryDropAmmoClip(Inventory.UnitBankInstances[u].UnitType.name, 1)) { //Debug.Log("Dropped: " + Inventory.UnitBankInstances[v].UnitType.name + ",1"); } continue; } } } } }
/// <summary> /// add 'amount' units of 'unitType' to the inventory's internal /// unit bank for this type and return the amount successfully added. /// if the inventory does not yet have an internal unit bank for /// 'unitType', one will be created /// </summary> public virtual bool TryGiveUnits(vp_UnitType unitType, int amount) { if (GetItemCap(unitType) == 0) { return(false); } return(TryGiveUnits(GetInternalUnitBank(unitType), amount)); }
public static void CreateItemTypeVpUnitType() { vp_UnitType asset = (vp_UnitType)vp_EditorUtility.CreateAsset("UFPS/Base/Content/ItemTypes", typeof(vp_UnitType)); if (asset != null) { asset.DisplayName = "unit"; } }
/// <summary> /// /// </summary> public virtual int GetUnitCount(vp_UnitType unitType) { vp_UnitBankInstance v = GetInternalUnitBank(unitType); if (v == null) { return(0); } return(v.Count); }
/// <summary> /// add 'amount' units of 'unitType' to the inventory's internal /// unit bank for this type and return the amount successfully added. /// if the inventory does not yet have an internal unit bank for /// 'unitType', one will be created /// </summary> public virtual bool TryGiveUnits(vp_UnitType unitType, int amount) { //Debug.Log("TryGiveUnits: " + unitType + ", " + amount); if (GetItemCap(unitType) == 0) { return(false); } return(TryGiveUnits(GetInternalUnitBank(unitType), amount)); }
/// <summary> /// /// </summary> public virtual bool TryRemoveUnits(vp_UnitType unitType, int amount) { vp_UnitBankInstance bank = GetInternalUnitBank(unitType); if (bank == null) { return(false); } return(DoRemoveUnits(bank, amount)); }
/// <summary> /// returns the UnitBank type of the first known vp_Weapon that has the /// passed UnitType and 'AnimationType:Thrown'. /// </summary> public virtual vp_UnitBankType GetThrowingWeaponUnitBankType(vp_UnitType unitType) { if (!m_HaveThrowingWeaponInfo) { StoreThrowingWeaponInfo(); } vp_UnitBankType unitBankType = null; m_ThrowingWeaponUnitBankTypes.TryGetValue(unitType, out unitBankType); return(unitBankType); }
/// <summary> /// internal method which can add items by type, amount and ID. /// </summary> public virtual bool TryGiveItem(vp_ItemType itemType, int id) { if (itemType == null) { Debug.LogError("Error (" + vp_Utility.GetErrorLocation(2) + ") Item type was null."); return(false); } // forward to the correct method if this was a unit type vp_UnitType unitType = itemType as vp_UnitType; if (unitType != null) { return(TryGiveUnits(unitType, id)); // in this case treat int argument as 'amount' } // forward to the correct method if this was a unitbank type vp_UnitBankType unitBankType = itemType as vp_UnitBankType; if (unitBankType != null) { return(TryGiveUnitBank(unitBankType, unitBankType.Capacity, id)); } // enforce item cap for this type of item if (CapsEnabled) { int capacity = GetItemCap(itemType); if ((capacity != UNLIMITED) && (GetItemCount(itemType) >= capacity)) { return(false); } } // enforce space limitations of the inventory if (SpaceEnabled) { if ((UsedSpace + itemType.Space) > TotalSpace) { return(false); } } DoAddItem(itemType, id); return(true); }
/// <summary> /// tries to drop all the ammo player is carrying. since UFPS has no ammo clip /// concept (all ammo is stored in a 'bullet pool' in the inventory) this script /// matches existing ammo pickups with the type of ammo dropped, and tries to /// drop as large (as few) ammo clips as possible per drop until the inventory /// has no more ammo (or not enough ammo to drop another clip) /// </summary> public virtual void TryDropAllAmmo() { bool retry = true; int iterations = 100; // max iterations to prevent infinite loop for whatever reason while (retry && (iterations > 0)) { retry = false; foreach (vp_ItemType itemType in m_SceneItemTypesByName.Values) { if (itemType is vp_UnitType) { vp_UnitType unitType = (itemType as vp_UnitType); // try to drop as large (as few) clips of every type as possible // until we have no more ammo that fits any available clip size for (int v = (m_AvailableUnitAmounts.Count - 1); v > -1; v--) { if (!(m_AvailableAmmoClips.Contains(new AmmoClipType(unitType.name, m_AvailableUnitAmounts[v])))) { continue; } if (TryDropAmmoClip(unitType.name, m_AvailableUnitAmounts[v])) { //Debug.Log("Dropped: " + itemType.name + "," + m_AvailableUnitAmounts[v]); } if (Inventory.GetUnitCount(unitType) >= m_AvailableUnitAmounts[v]) { retry = true; } } // if ammo was for a throwing weapon, drop the loaded unit too if (Inventory.IsThrowingUnit(unitType)) { TryDropLoadedThrowingUnit(unitType); } } } iterations--; } }
/// <summary> /// /// </summary> protected bool TryWieldByUnit(vp_UnitType unitType) { // try to find a weapon with this unit type List <vp_Weapon> weaponsWithUnitType; if (WeaponsByUnit.TryGetValue(unitType, out weaponsWithUnitType) && (weaponsWithUnitType != null) && (weaponsWithUnitType.Count > 0) ) { // try to set the first weapon we find that uses this unit type foreach (vp_Weapon w in weaponsWithUnitType) { if (m_Player.SetWeapon.TryStart(WeaponHandler.Weapons.IndexOf(w) + 1)) { return(true); // found matching weapon: stop looking } } } return(false); }
/// <summary> /// /// </summary> protected bool TryWieldByUnit(vp_UnitType unitType) { // try to find a weapon with this unit type List<vp_Weapon> weaponsWithUnitType; if(WeaponsByUnit.TryGetValue(unitType, out weaponsWithUnitType) && (weaponsWithUnitType != null) && (weaponsWithUnitType.Count > 0) ) { // try to set the first weapon we find that uses this unit type foreach (vp_Weapon w in weaponsWithUnitType) { if (m_Player.SetWeapon.TryStart(WeaponHandler.Weapons.IndexOf(w) + 1)) return true; // found matching weapon: stop looking } } return false; }
public vp_UnitBankInstance(vp_UnitBankType unitBankType, int id, vp_Inventory inventory) : base(unitBankType, id) { UnitType = unitBankType.Unit; m_Inventory = inventory; }
public vp_UnitBankInstance(vp_UnitType unitType, vp_Inventory inventory) : base(null, 0) { UnitType = unitType; m_Inventory = inventory; }
/// <summary> /// /// </summary> public virtual bool TrySetUnitCount(vp_UnitType unitType, int amount) { return TrySetUnitCount(GetInternalUnitBank((vp_UnitType)unitType), amount); }
/// <summary> /// /// </summary> public virtual void SetUnitCount(vp_UnitType unitType, int amount) { TrySetUnitCount(GetInternalUnitBank((vp_UnitType)unitType), amount); }
/// <summary> /// add 'amount' units of 'unitType' to the inventory's internal /// unit bank for this type and return the amount successfully added. /// if the inventory does not yet have an internal unit bank for /// 'unitType', one will be created /// </summary> public virtual bool TryGiveUnits(vp_UnitType unitType, int amount) { if (GetItemCap(unitType) == 0) return false; return TryGiveUnits(GetInternalUnitBank(unitType), amount); }
/// <summary> /// /// </summary> public virtual bool HaveInternalUnitBank(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 true; } return false; }
/// <summary> /// /// </summary> public virtual bool TrySetUnitCount(vp_UnitType unitType, int amount) { return(TrySetUnitCount(GetInternalUnitBank((vp_UnitType)unitType), amount)); }
/// <summary> /// /// </summary> public virtual bool TryRemoveUnits(vp_UnitType unitType, int amount) { vp_UnitBankInstance bank = GetInternalUnitBank(unitType); if (bank == null) return false; return DoRemoveUnits(bank, amount); }
public virtual void TryDropAmmoClip(vp_UnitType unitType, int clipSize) { TryDropItemInternal(unitType, transform.forward, clipSize); }
/// <summary> /// Syncs Lua to the UFPS inventory. /// </summary> public void SyncLuaToFPInventory() { if (fpInventory != null) { // Update UFPS inventory if any counts have changed: foreach (vp_ItemType itemType in usableItemTypes) { if (itemType != null) { int newCount = DialogueLua.GetVariable(itemType.name).AsInt; int oldCount = fpInventory.GetItemCount(itemType); if (debug) { Debug.Log(string.Format("Item: {0} oldCount={1} newCount={2}", itemType.name, oldCount, newCount)); } if (itemType is vp_UnitBankType) { // Unit Bank (a loadable weapon such as a pistol): if (debug) { Debug.Log(" is UnitBankType"); } vp_UnitBankType unitBankType = itemType as vp_UnitBankType; int unitsLoaded = DialogueLua.GetVariable(itemType.name + "_Units").AsInt; if (newCount < oldCount) // Remove unit banks. { if (debug) { Debug.Log(string.Format(" TryRemoveUnitBanks({0}, {1}", unitBankType.name, oldCount - newCount)); } fpInventory.TryRemoveUnitBanks(unitBankType, oldCount - newCount); } else if (newCount > oldCount) // Add unit banks. { for (int i = 0; i < newCount - oldCount; i++) { if (debug) { Debug.Log(string.Format(" TryGiveUnitBank({0}, 0, 0)", unitBankType.name)); } if (debug) { Debug.Log(string.Format(" TryGiveUnitBank({0}, {1}, 0)", unitBankType.name, unitsLoaded)); } fpInventory.TryGiveUnitBank(unitBankType, unitsLoaded, 0); } } else { foreach (var unitBankInstance in fpInventory.UnitBankInstances) { if (unitBankInstance.Type == itemType) { unitBankInstance.Count = unitsLoaded; } } } } else if (itemType is vp_UnitType) { // Unit Type (ammo): if (debug) { Debug.Log(" is UnitType (ignored; will set internal unit bank)"); } vp_UnitType unitType = itemType as vp_UnitType; if (debug) { Debug.Log(string.Format(" TrySetUnitCount({0}, {1})", unitType.name, newCount)); } fpInventory.TrySetUnitCount(unitType, newCount); } else { // Item Type (ammo-less weapon such as a mace): if (debug) { Debug.Log(" is ItemType"); } if (newCount < oldCount) // Remove items. { if (debug) { Debug.Log(string.Format(" TryRemoveItems({0}, {1})", itemType.name, oldCount - newCount)); } fpInventory.TryRemoveItems(itemType, oldCount - newCount); } else if (newCount > oldCount) // Add items. { if (debug) { Debug.Log(string.Format(" TryGiveItems({0}, {1})", itemType.name, newCount - oldCount)); } fpInventory.TryGiveItems(itemType, newCount - oldCount); } } } } } }
/// <summary> /// /// </summary> public virtual int GetUnitCount(vp_UnitType unitType) { vp_UnitBankInstance v = GetInternalUnitBank(unitType); if (v == null) return 0; return v.Count; }
/// <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; }
/// <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); }