Exemple #1
0
        /// <summary>
        /// Swap <see cref="UIItem"/>s from one <see cref="UIInventory"/> to the other.
        /// </summary>
        private void SwapUIItems(UIItem uiItem, bool fromPlayer, EventArg3 <Item, Trader, Trader> tradeEvent)
        {
            // Check if the trade was successful:
            // (I know this is a very ugly way of doing it, but I don't want to introduce new EventArg(s) with boolean as their return type...)
            // If we are transfering the item FROM the player, then the player shouldn't have the item in the inventory if the trade was successful and vice versa for the merchant.
            if ((fromPlayer && !PlayerController.Player.Inventory.Items.Contains(uiItem.Item)) || (!fromPlayer && PlayerController.Player.Inventory.Items.Contains(uiItem.Item)))
            {
                //Remove the old UIItem:
                if (fromPlayer)
                {
                    uiPlayerInventory.ItemGrid.RemoveUIElement(uiItem);
                }
                else
                {
                    uiMerchantInventory.ItemGrid.RemoveUIElement(uiItem);
                }

                //Assign a new instance to the reference that looks the same etc.:
                uiItem = new UIItem(new Transform(uiItem.Transform.Size), uiItem.Sprite, screen, uiItem.Item);

                //Swap the Trader references in the EventArg:
                tradeEvent.SetArguments(
                    tradeEvent.Arg1,  //Keep the item reference
                    tradeEvent.Arg3,  //Switch reference
                    tradeEvent.Arg2); //Switch reference

                //Create a new event list where the new swapped trade event and an entirely new EventArg3 can be created.
                EventArgList onButtonClickEvents = new EventArgList(
                    tradeEvent,
                    new EventArg3 <UIItem, bool, EventArg3 <Item, Trader, Trader> >(SwapUIItems, uiItem, !fromPlayer /* We want to trigger the opposite reaction */, tradeEvent)
                    );

                //Create the purchase/sell button whether or not it came from the player's inventory.
                if (fromPlayer)
                {
                    uiItem.CreatePurchaseButton(onButtonClickEvents);
                }
                else if (uiItem.Item.CanSell)
                {
                    uiItem.CreateSellButton(onButtonClickEvents);
                }

                //Add the new UIItem to the right grid:
                if (fromPlayer)
                {
                    uiMerchantInventory.ItemGrid.AddUIElement(uiItem);
                }
                else
                {
                    uiPlayerInventory.ItemGrid.AddUIElement(uiItem);
                }
            }
        }