示例#1
0
    /// <summary>
    /// returns true if the inventory contains a weapon by the index
    /// fed as an argument to the 'SetWeapon' activity. false if not.
    /// this is used to regulate which weapons the player currently
    /// has access to.
    /// </summary>
    protected virtual bool CanStart_SetWeapon()
    {
        int index = (int)Player.SetWeapon.Argument;

        if (index == 0)
        {
            return(true);
        }

        if ((index < 1) || index > (WeaponHandler.Weapons.Count))
        {
            return(false);
        }

        vp_ItemIdentifier weaponIdentifier = GetWeaponIdentifier(WeaponHandler.Weapons[index - 1]);

        if (weaponIdentifier == null)
        {
            return(MissingIdentifierError(index));
        }

        bool haveItem = HaveItem(weaponIdentifier.Type, weaponIdentifier.ID);

        // see if weapon is thrown
        if (haveItem && (vp_Weapon.Type)WeaponHandler.Weapons[index - 1].AnimationType == vp_Weapon.Type.Thrown)
        {
            if (GetAmmoInWeapon(WeaponHandler.Weapons[index - 1]) < 1)
            {
                vp_UnitBankType uType = weaponIdentifier.Type as vp_UnitBankType;
                if (uType == null)
                {
                    Debug.LogError("Error (" + this + ") Tried to wield thrown weapon " + WeaponHandler.Weapons[index - 1] + " but its item identifier does not point to a UnitBank.");
                    return(false);
                }
                else
                {
                    if (!TryReload(uType, weaponIdentifier.ID))                         // NOTE: ID might not work for identification here because of multiplayer add-on pickup logic
                    {
                        //Debug.Log("uType: " + uType + ", weapon.ID: " + weapon.ID);
                        //Debug.Log("failed because: no thrower wielded and no extra ammo");
                        return(false);
                    }
                }
                //Debug.Log("success because: no thrower wielded but we have extra ammo");
            }
            //else
            //Debug.Log("success because: thrower wielded");
        }

        return(haveItem);
    }
    /// <summary>
    /// tries to remove a number of unitbanks by type and amount,
    /// regardless of their IDs
    /// </summary>
    public virtual bool TryRemoveUnitBanks(vp_UnitBankType type, int amount)
    {
        bool result = false;

        while (amount > 0)
        {
            if (TryRemoveUnitBank(type, UNIDENTIFIED))
            {
                result = true;
            }
            amount--;
        }
        return(result);
    }
    /// <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>
    ///
    /// </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();
    }
示例#5
0
    /// <summary>
    /// returns the first known vp_UnitBankInstance of a certain UnitBankType,
    /// provided that the player rig has a vp_Weapon with the same UnitBankType
    /// that has 'AnimationType:Thrown'.
    /// </summary>
    public virtual vp_UnitBankInstance GetThrowingWeaponUnitBankInstance(vp_UnitBankType unitBankType)
    {
        if (WeaponHandler == null)
        {
            return(null);
        }

        if (!m_HaveThrowingWeaponInfo)
        {
            StoreThrowingWeaponInfo();
        }

        vp_UnitBankInstance unitBankInstance = null;

        m_ThrowingWeaponUnitBankInstances.TryGetValue(unitBankType, out unitBankInstance);

        return(unitBankInstance);
    }
        protected virtual void SetAmmo(vp_UnitBankType bankType)
        {
            ufpsInventory.TryRemoveUnits(bankType.Unit, 9999); // Remove all, then figure out how many we have
            uint bulletCount = 0;

            foreach (var item in itemCollection.items)
            {
                var i = item.item as UnitTypeUFPSInventoryItem;
                if (i != null && i.unitType == bankType.Unit)
                {
                    // It's ammo!
                    bulletCount += i.currentStackSize;
                    item.Repaint();
                    break; // Currently only supporting 1 stack of same ammo type
                }
            }

            ufpsInventory.TryGiveUnits(bankType.Unit, (int)bulletCount);
            //eventHandler.AddItem.Try(new object[] { bankType.Unit, (int)bulletCount });
        }
示例#7
0
    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);
    }
示例#9
0
 public vp_UnitBankInstance(vp_UnitBankType unitBankType, int id, vp_Inventory inventory)
     : base(unitBankType, id)
 {
     UnitType    = unitBankType.Unit;
     m_Inventory = inventory;
 }
示例#10
0
 /// <summary>
 /// returns true if the player rig has a vp_Weapon with the same UnitBankType
 /// and 'AnimationType:Thrown'.
 /// </summary>
 public virtual bool IsThrowingUnitBank(vp_UnitBankType unitBankType)
 {
     return(GetThrowingWeaponUnitBankInstance(unitBankType) != null);
 }
示例#11
0
	/// <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;

	}
示例#12
0
	/// <summary>
	/// tries to remove a unitbank by type and ID
	/// </summary>
	public virtual bool TryRemoveUnitBank(vp_UnitBankType type, int id)
	{

		return TryRemoveUnitBank(GetItem(type, id) as vp_UnitBankInstance);

	}
示例#13
0
	/// <summary>
	/// tries to remove a number of unitbanks by type and amount,
	/// regardless of their IDs
	/// </summary>
	public virtual bool TryRemoveUnitBanks(vp_UnitBankType type, int amount)
	{

		bool result = false;
		while (amount > 0)
		{
			if (TryRemoveUnitBank(type, UNIDENTIFIED))
				result = true;
			amount--;
		}
		return result;

	}
示例#14
0
	/// <summary>
	/// tries to add an item unit bank (such as a firearm) and - upon success -
	/// tries to fill it with 'unitsLoaded' units. NOTES: 1) unitbanks can only
	/// be added to the inventory one at a time.
	/// 2) 'unitsLoaded' will not be altered to fit in the inventory. that is:
	/// a weapon loaded with more bullets than the player can carry will be
	/// impossible to pick up.
	/// </summary>
	public virtual bool TryGiveUnitBank(vp_UnitBankType unitBankType, int unitsLoaded, int id)
	{

		if (unitBankType == null)
		{
			Debug.LogError("Error (" + vp_Utility.GetErrorLocation() + ") 'unitBankType' was null.");
			return false;
		}

		if (CapsEnabled)
		{

			// enforce item cap for this type of unitbank
			int capacity = GetItemCap(unitBankType);
			if ((capacity != UNLIMITED) && (GetItemCount(unitBankType) >= capacity))
				return false;

			// enforce unit capacity of the unitbank type
			if (unitBankType.Capacity != UNLIMITED)
				unitsLoaded = Mathf.Min(unitsLoaded, unitBankType.Capacity);

		}

		if (SpaceEnabled)
		{

			// enforce space (mass / volume) limitations of the inventory
			switch (SpaceMode)
			{
				case Mode.Weight:
					if (unitBankType.Unit == null)
					{
						Debug.LogError("Error (vp_Inventory) UnitBank item type " + unitBankType + " can't be added because its unit type has not been set.");
						return false;
					}
					if (((UsedSpace + unitBankType.Space) + (unitBankType.Unit.Space * unitsLoaded)) > TotalSpace)
						return false;
					break;
				case Mode.Volume:
					if ((UsedSpace + unitBankType.Space) > TotalSpace)
						return false;
					break;
			}

		}

		DoAddUnitBank(unitBankType, id, unitsLoaded);
		
		return true;

	}
示例#15
0
	/// <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();
	}
示例#16
0
 /// <summary>
 /// tries to remove a unitbank by type and ID
 /// </summary>
 public virtual bool TryRemoveUnitBank(vp_UnitBankType type, int id)
 {
     return(TryRemoveUnitBank(GetItem(type, id) as vp_UnitBankInstance));
 }
示例#17
0
 /// <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);
                     }
                 }
             }
         }
     }
 }
示例#18
0
	/// <summary>
	/// 
	/// </summary>
	protected override void DoAddUnitBank(vp_UnitBankType unitBankType, int id, int unitsLoaded)
	{

		bool hadItBefore = HaveItem(unitBankType, id);
		base.DoAddUnitBank(unitBankType, id, unitsLoaded);
		if (!hadItBefore)
			TryWield(GetItem(unitBankType, id));
	}
	/// <summary>
	/// 
	/// </summary>
	protected override void DoAddUnitBank(vp_UnitBankType unitBankType, int id, int unitsLoaded)
	{

		bool alreadyHaveIt = vp_Gameplay.isMultiplayer ?
			HaveItem(unitBankType)			// NOTE: id not supported in UFPS multiplayer add-on
			: HaveItem(unitBankType, id);	// singleplayer

		base.DoAddUnitBank(unitBankType, id, unitsLoaded);

		TryWieldNewItem(unitBankType, alreadyHaveIt);

	}
    /// <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);
    }
	public vp_UnitBankInstance(vp_UnitBankType unitBankType, int id, vp_Inventory inventory)
		: base(unitBankType, id)
	{
		UnitType = unitBankType.Unit;
		m_Inventory = inventory;
	}
        protected virtual void SetAmmo(vp_UnitBankType bankType)
        {
            ufpsInventory.TryRemoveUnits(bankType.Unit, 9999); // Remove all, then figure out how many we have
            uint bulletCount = 0;
            foreach (var item in itemCollection.items)
            {
                var i = item.item as UnitTypeUFPSInventoryItem;
                if (i != null && i.unitType == bankType.Unit)
                {
                    // It's ammo!
                    bulletCount += i.currentStackSize;
                    item.Repaint();
                    break; // Currently only supporting 1 stack of same ammo type
                }
            }

            ufpsInventory.TryGiveUnits(bankType.Unit, (int)bulletCount);
            //eventHandler.AddItem.Try(new object[] { bankType.Unit, (int)bulletCount });
        }