Foerming/Assets/Scripts/ShopSlot.cs
d-hain ba9be3d96b you can now sell Items with Right Mouse Click on an InventorySlot for the Item.SellPrice
and it gets put into the Shop for full price

renamed Item.cost to Item.price
2022-06-08 22:52:28 +02:00

51 lines
1.4 KiB
C#

using TMPro;
using UnityEngine;
public class ShopSlot : ItemStorageSlot {
public TextMeshProUGUI nameText;
public TextMeshProUGUI costText;
private Shop _shop;
private Inventory _inventory;
private PlayerController _playerController;
private void Start() {
_shop = Shop.instance;
_inventory = Inventory.instance;
_playerController = PlayerController.instance;
}
/**
* Clears the Shop Slot
*/
public override void ClearSlot() {
nameText.text = "";
costText.text = "";
amountText.text = "";
// _shop.RemoveItem(Item, 1);
base.ClearSlot();
}
/**
* Gets called when the Shop Slot is clicked
*/
public override void UseItem() {
if(Item) {
if(_playerController.Money >= Item.price) {
if(Item) {
_playerController.ChangeMoney(-Item.price);
_shop.itemWasBought = true;
Debug.Log("Buying Item: " + Item.displayName);
}
_inventory.AddItem(Item, 1);
_shop.RemoveItem(Item, 1);
} else {
Debug.Log("Not enough money to buy item.");
}
_shop.onItemChangedCallback?.Invoke();
_inventory.onItemChangedCallback?.Invoke();
}
}
}