The Merchant script provides functionality for creating a merchant system where players can buy and sell items using grids. This system includes a merchant grid for items available for purchase and a sell grid where players can place items they wish to sell.
The script interacts with ItemGrid2D, InventorySo, and InventorySupplierSo to manage item placement and transactions. Below are the key components:
using Inventory.Scripts.Core.Grids;using Inventory.Scripts.Core.ScriptableObjects;using Inventory.Scripts.Core.ScriptableObjects.Items;using UnityEngine;namespace Inventory.Samples{ public class Merchant : MonoBehaviour { [SerializeField] private ItemGrid2D merchantGrid; // Where will contain the items that the merchant is selling. [SerializeField] private ItemGrid2D sellGrid; // Grid which player will put items to sell to the merchant // Merchant Items [SerializeField] private ItemDataSo[] merchantItems; // Handlers [SerializeField] private InventorySo playerInventory; [SerializeField] private InventorySupplierSo inventorySupplierSo; private void OnEnable() { // Here you can insert the items on the merchantGrid, will be the merchant items. var merchantGridItems = merchantGrid.Grid; foreach (var itemDataSo in merchantItems) { // Maybe you want to get the response in order to do something with it... Up to you inventorySupplierSo.PlaceItem( itemDataSo, merchantGridItems ); } } // Trigger this once player clicks in a "Sell Button" public void SellItems() { var sellerGrid = sellGrid.Grid; var allItemsFromGrid = sellerGrid.GetAllItemsFromGrid(); foreach (var itemSoldByPlayer in allItemsFromGrid) { itemSoldByPlayer.RemoveItselfFromLocation(); // Here you can give player money or something... // If money is an item you can do something like this: // the 'itemDataSoFromMoney' can be a serializable field from type ItemDataSo... // inventorySupplierSo.FindPlaceForItemInGrids( // itemDataSoFromMoney, // playerInventory.GetGrids() // ); } } // This an example on how to rollback the items to the player inventory. private void OnDisable() { RollbackSellingItemsToPlayerInventory(); } // Method to return items to player inventory in case, player closes the UI or for some other reason... private void RollbackSellingItemsToPlayerInventory() { var sellerGrid = sellGrid.Grid; var allItemsFromGrid = sellerGrid.GetAllItemsFromGrid(); foreach (var itemTable in allItemsFromGrid) { inventorySupplierSo.PlaceItemInPlayerInventory(itemTable); } } }}
The Merchant System provides a structured way to implement item transactions in an inventory grid. It ensures seamless item management while allowing for easy modifications to support different game mechanics.