Exemple #1
0
    /// <summary>
    /// Removes a certain amount of the item in the inventory slot
    /// </summary>
    /// <param name="amount"></param>
    /// <returns> This returns the item with the amount that was removed, whatever was left  stays  inn the inventory slot</returns>
    public override Item RemoveItem(int amount = 1)
    {
        Item temp = Instantiate(HeldItem, HeldItem.transform);

        temp.transform.parent = null;
        SlotWeight           -= HeldItem.itemWeight * amount;
        ParentInventory.ReduceWeight(HeldItem.itemWeight * amount);
        if (amount > HeldItem.stackCount)
        {
            amount = HeldItem.stackCount;
        }
        if (amount == HeldItem.stackCount)
        {
            Destroy(HeldItem.gameObject);
            HeldItem = null;
        }
        else
        {
            if (temp.stackCount - amount > 0)
            {
                temp.RemoveFromStack(temp.stackCount - amount);
            }

            HeldItem.RemoveFromStack(amount);
        }
        SetHeldItem();
        UpdateItemDisplay();
        return(temp);
    }
Exemple #2
0
        public void WriteSpawnData(IWriteMessage msg, UInt16 entityID, UInt16 originalInventoryID, byte originalItemContainerIndex)
        {
            if (GameMain.Server == null)
            {
                return;
            }

            msg.Write(Prefab.OriginalName);
            msg.Write(Prefab.Identifier);
            msg.Write(Description != prefab.Description);
            if (Description != prefab.Description)
            {
                msg.Write(Description);
            }

            msg.Write(entityID);

            if (ParentInventory == null || ParentInventory.Owner == null || originalInventoryID == 0)
            {
                msg.Write((ushort)0);

                msg.Write(Position.X);
                msg.Write(Position.Y);
                msg.Write(Submarine != null ? Submarine.ID : (ushort)0);
            }
            else
            {
                msg.Write(originalInventoryID);
                msg.Write(originalItemContainerIndex);

                int slotIndex = ParentInventory.FindIndex(this);
                msg.Write(slotIndex < 0 ? (byte)255 : (byte)slotIndex);
            }

            msg.Write(body == null ? (byte)0 : (byte)body.BodyType);
            msg.Write(SpawnedInOutpost);

            byte teamID = 0;

            foreach (WifiComponent wifiComponent in GetComponents <WifiComponent>())
            {
                teamID = (byte)wifiComponent.TeamID;
                break;
            }

            msg.Write(teamID);
            bool tagsChanged = tags.Count != prefab.Tags.Count || !tags.All(t => prefab.Tags.Contains(t));

            msg.Write(tagsChanged);
            if (tagsChanged)
            {
                msg.Write(Tags);
            }
        }
Exemple #3
0
    /// <summary>
    /// Adds the item to the inventory slot
    /// </summary>
    /// <param name="item"></param>
    /// <returns> This Returns the left over amount of items that couldn't be picked up cause the slot was either full or it went over the characters strength limit </returns>
    public override Item AddItem(Item item)
    {
        Item temp = item;

        if (HeldItem == null)
        {
            if (ParentInventory != null && item != null)
            {
                if (item.stackCount * item.itemWeight <= ParentInventory.GetAddableWeight)
                {
                    HeldItem = item;
                    temp     = Instantiate(HeldItem, HeldItem.transform.position + new Vector3(0, 0.2f, 0), HeldItem.transform.rotation);
                    temp.transform.parent = null;
                    Destroy(temp.gameObject);
                    SlotWeight = item.stackCount * item.itemWeight;
                    ParentInventory.AddWeight(SlotWeight);
                }
                else
                {
                    int numberOfAddableItems = Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight);
                    if (numberOfAddableItems > 0)
                    {
                        HeldItem = item;
                        temp     = Instantiate(HeldItem, HeldItem.transform);
                        temp.transform.parent = null;

                        HeldItem.RemoveFromStack(HeldItem.stackCount - numberOfAddableItems);
                        temp.RemoveFromStack(numberOfAddableItems);

                        SlotWeight = numberOfAddableItems * item.itemWeight;
                        ParentInventory.AddWeight(SlotWeight);
                    }
                }
            }
        }
        else
        {
            if (ParentInventory != null && item != null)
            {
                if (item.itemID == HeldItem.itemID)
                {
                    int ItemAmount = HeldItem.stackCount + item.stackCount;
                    if (ItemAmount > HeldItem.maxStackCount)
                    {
                        int AmountToFill = HeldItem.maxStackCount - HeldItem.stackCount <= item.stackCount ? HeldItem.maxStackCount - HeldItem.stackCount
                            : item.stackCount;
                        if (AmountToFill > 0)
                        {
                            if (AmountToFill * item.itemWeight <= ParentInventory.GetAddableWeight)
                            {
                                HeldItem.AddToStack(AmountToFill);
                                temp.RemoveFromStack(AmountToFill);
                                SlotWeight += AmountToFill * item.itemWeight;
                                ParentInventory.AddWeight(AmountToFill * item.itemWeight);
                            }
                            else
                            {
                                int numberOfAddableItems = Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight) <= item.stackCount ? Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight)
                                    : item.stackCount;
                                if (numberOfAddableItems > 0)
                                {
                                    HeldItem.AddToStack(numberOfAddableItems);
                                    temp.RemoveFromStack(numberOfAddableItems);
                                    if (temp.stackCount <= 0)
                                    {
                                        Destroy(temp.gameObject);
                                    }
                                    SlotWeight += numberOfAddableItems * item.itemWeight;
                                    ParentInventory.AddWeight(numberOfAddableItems * item.itemWeight);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (item.stackCount * item.itemWeight <= ParentInventory.GetAddableWeight)
                        {
                            HeldItem.AddToStack(temp.stackCount);
                            temp = Instantiate(HeldItem, HeldItem.transform);
                            temp.transform.parent = null;
                            temp.RemoveFromStack(temp.stackCount);
                            if (temp.stackCount <= 0)
                            {
                                Destroy(temp.gameObject);
                            }
                            SlotWeight += item.stackCount * item.itemWeight;
                            ParentInventory.AddWeight(item.stackCount * item.itemWeight);
                        }
                        else
                        {
                            int numberOfAddableItems = Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight) <= item.stackCount ? Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight)
                                : item.stackCount;
                            if (numberOfAddableItems > 0)
                            {
                                HeldItem.AddToStack(numberOfAddableItems);
                                temp.RemoveFromStack(numberOfAddableItems);
                                SlotWeight += numberOfAddableItems * item.itemWeight;
                                ParentInventory.AddWeight(numberOfAddableItems * item.itemWeight);
                            }
                        }
                    }
                }
            }
        }
        if (HeldItem != null)
        {
            HeldItem.DeactivateItem();
        }
        SetHeldItem();
        UpdateItemDisplay();
        if (temp != null)
        {
            temp.ReleaseItem();
        }
        return(temp);
    }
Exemple #4
0
 public void Delete()
 {
     Button?.gameObject.SetActive(false);
     ParentInventory.Remove(this);
 }
Exemple #5
0
        public void WriteSpawnData(NetBuffer msg)
        {
            if (GameMain.Server == null)
            {
                return;
            }

            msg.Write(Prefab.OriginalName);
            msg.Write(Prefab.Identifier);
            msg.Write(Description != prefab.Description);
            if (Description != prefab.Description)
            {
                msg.Write(Description);
            }

            msg.Write(ID);

            if (ParentInventory == null || ParentInventory.Owner == null)
            {
                msg.Write((ushort)0);

                msg.Write(Position.X);
                msg.Write(Position.Y);
                msg.Write(Submarine != null ? Submarine.ID : (ushort)0);
            }
            else
            {
                msg.Write(ParentInventory.Owner.ID);

                //find the index of the ItemContainer this item is inside to get the item to
                //spawn in the correct inventory in multi-inventory items like fabricators
                byte containerIndex = 0;
                if (Container != null)
                {
                    for (int i = 0; i < Container.components.Count; i++)
                    {
                        if (Container.components[i] is ItemContainer container &&
                            container.Inventory == ParentInventory)
                        {
                            containerIndex = (byte)i;
                            break;
                        }
                    }
                }
                msg.Write(containerIndex);

                int slotIndex = ParentInventory.FindIndex(this);
                msg.Write(slotIndex < 0 ? (byte)255 : (byte)slotIndex);
            }

            byte teamID = 0;

            foreach (WifiComponent wifiComponent in GetComponents <WifiComponent>())
            {
                teamID = (byte)wifiComponent.TeamID;
                break;
            }

            msg.Write(teamID);
            bool tagsChanged = tags.Count != prefab.Tags.Count || !tags.All(t => prefab.Tags.Contains(t));

            msg.Write(tagsChanged);
            if (tagsChanged)
            {
                msg.Write(Tags);
            }
        }
Exemple #6
0
        public void WriteSpawnData(IWriteMessage msg, UInt16 entityID, UInt16 originalInventoryID, byte originalItemContainerIndex)
        {
            if (GameMain.Server == null)
            {
                return;
            }

            msg.Write(Prefab.OriginalName);
            msg.Write(Prefab.Identifier);
            msg.Write(Description != prefab.Description);
            if (Description != prefab.Description)
            {
                msg.Write(Description);
            }

            msg.Write(entityID);

            if (ParentInventory == null || ParentInventory.Owner == null || originalInventoryID == 0)
            {
                msg.Write((ushort)0);

                msg.Write(Position.X);
                msg.Write(Position.Y);
                msg.Write(Submarine != null ? Submarine.ID : (ushort)0);
            }
            else
            {
                msg.Write(originalInventoryID);
                msg.Write(originalItemContainerIndex);

                int slotIndex = ParentInventory.FindIndex(this);
                msg.Write(slotIndex < 0 ? (byte)255 : (byte)slotIndex);
            }

            msg.Write(body == null ? (byte)0 : (byte)body.BodyType);
            msg.Write(SpawnedInOutpost);
            msg.Write(AllowStealing);
            msg.WriteRangedInteger(Quality, 0, Items.Components.Quality.MaxQuality);

            byte teamID = 0;

            foreach (WifiComponent wifiComponent in GetComponents <WifiComponent>())
            {
                teamID = (byte)wifiComponent.TeamID;
                break;
            }
            if (teamID == 0)
            {
                foreach (IdCard idCard in GetComponents <IdCard>())
                {
                    teamID = (byte)idCard.TeamID;
                    break;
                }
            }

            msg.Write(teamID);
            bool tagsChanged = tags.Count != prefab.Tags.Count || !tags.All(t => prefab.Tags.Contains(t));

            msg.Write(tagsChanged);
            if (tagsChanged)
            {
                string[] splitTags = Tags.Split(',');
                msg.Write(string.Join(',', splitTags.Where(t => !prefab.Tags.Contains(t))));
                msg.Write(string.Join(',', prefab.Tags.Where(t => !splitTags.Contains(t))));
            }
            var nameTag = GetComponent <NameTag>();

            msg.Write(nameTag != null);
            if (nameTag != null)
            {
                msg.Write(nameTag.WrittenName ?? "");
            }
        }