示例#1
0
        public void Broadcast(BroadcastEventType e,
                              AddItemEventArgs aea             = null,
                              RemoveItemEventArgs rea          = null,
                              SwapItemsEventArgs sea           = null,
                              SwapItemsTrhuInvEventArgs siea   = null,
                              UseItemEventArgs uea             = null,
                              DropItemEventArgs dea            = null,
                              InitializeInventoryEventArgs iea = null)
        {
            //Debug.Log($"Broadcasting event {e}");
            switch (e)
            {
            case BroadcastEventType.AddItem:
                OnAddItem?.Invoke(this, aea);
                OnChange?.Invoke(this, aea);
                break;

            case BroadcastEventType.RemoveItem:
                OnRemoveItem?.Invoke(this, rea);
                OnChange?.Invoke(this, rea);
                break;

            case BroadcastEventType.SwapItem:
                OnSwapItem?.Invoke(this, sea);
                OnChange?.Invoke(this, sea);
                break;

            case BroadcastEventType.SwapTrhuInventory:
                OnSwapTrhuInventory?.Invoke(this, siea);
                OnChange?.Invoke(this, siea);
                break;

            case BroadcastEventType.UseItem:
                OnUseItem?.Invoke(this, uea);
                OnChange?.Invoke(this, uea);
                break;

            case BroadcastEventType.DropItem:
                OnDropItem?.Invoke(this, dea);
                OnChange?.Invoke(this, dea);
                break;

            case BroadcastEventType.PickUpItem:
                OnPickUpItem?.Invoke(this, aea);
                OnChange?.Invoke(this, aea);
                break;

            case BroadcastEventType.InitializeInventory:
                OnInitializeInventory?.Invoke(this, iea);
                OnChange?.Invoke(this, iea);
                break;

            default:
                break;
            }
            if (autoSaveOnChange)
            {
                InventoryController.SaveInventoryData();
            }
        }
        /// <summary>
        /// This function must be called when a inventory is being created. It fills the inventory if null Slots if the list of slots is null or have less elments than inv.SlotAmount, give an id to the inventory and add it to the list of inventories in the InventoryController. This function dont need to be called if you are using an loading system;
        /// </summary>
        /// <returns>The initiaized inventory</returns>
        public Inventory Initialize(bool loadSeeds = true, BroadcastEventType e = BroadcastEventType.InitializeInventory)
        {
            if (this == null)
            {
                Debug.LogError("Null inventory provided for Initialize");
                throw new ArgumentNullException("inv", "Null inventory provided");
            }
            //Debug.Log("Init");
            if (hasInitialized)
            {
                return(this);
            }

            if (seeds == null)
            {
                seeds = new Seed[] { }
            }
            ;
            if (seeds.Length >= 1 && loadSeeds)
            {
                LoadSeed(0);
            }
            else
            {
                if (slots == null)
                {
                    slots = new List <Slot>();
                }
                if (slots.Count != SlotAmount)
                {
                    for (int i = 0; i < SlotAmount; i++)
                    {
                        if (i < slots.Count)
                        {
                            continue;
                        }
                        else
                        {
                            slots.Add(Slot.nullSlot);
                        }
                    }
                }
            }

            hasInitialized = true;
            if (!InventoryController.inventories.Contains(this))
            {
                InventoryController.inventories.Add(this);
            }
            InventoryHandler.InitializeInventoryEventArgs iea = new InventoryHandler.InitializeInventoryEventArgs(this);
            if (InventoryHandler.current != null)
            {
                InventoryHandler.current.Broadcast(e, iea: iea);
            }
            return(this);
        }
        public void BroadcastUIEvent(BroadcastEventType e,
                                     OnToggleInventoryEventArgs oti = null,
                                     OnDragItemEventArgs odi        = null,
                                     OnDropItemUIEventArgs drop     = null)
        {
            switch (e)
            {
            case BroadcastEventType.UIToggled:
                OnToggleInventory?.Invoke(this, oti);
                break;

            case BroadcastEventType.ItemDragged:
                OnDragItem?.Invoke(this, odi);
                break;

            case BroadcastEventType.DropItem:
                OnDropItemUI?.Invoke(this, drop);
                break;

            default:
                break;
            }
        }