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
This commit is contained in:
d-hain 2022-06-08 22:52:28 +02:00
parent 64b25a2029
commit ba9be3d96b
7 changed files with 50 additions and 13 deletions

View file

@ -6456,7 +6456,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
itemsParent: {fileID: 610140154}
shopUI: {fileID: 1671356616}
inventoryUI: {fileID: 971652020}
inventoryUI: {fileID: 1609015285}
--- !u!224 &1551890462 stripped
RectTransform:
m_CorrespondingSourceObject: {fileID: 3510585822996971025, guid: 44dae5fbdb6f7df4f93a10807f66956f, type: 3}
@ -6651,7 +6651,9 @@ MonoBehaviour:
- {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2}
- {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2}
- {fileID: 11400000, guid: 008a8fdd2c3a95745acafee4087a855d, type: 2}
itemWasSold: 0
- {fileID: 11400000, guid: d651d57ba97a4246a0094409e29fe56a, type: 2}
- {fileID: 11400000, guid: d651d57ba97a4246a0094409e29fe56a, type: 2}
itemWasBought: 0
--- !u!1001 &1701153146
PrefabInstance:
m_ObjectHideFlags: 0

View file

@ -1,6 +1,18 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;
public class InventorySlot : ItemStorageSlot, IPointerClickHandler {
private Inventory _inventory;
private Shop _shop;
private PlayerController _playerController;
private void Start() {
_inventory = Inventory.instance;
_shop = Shop.instance;
_playerController = PlayerController.instance;
}
public class InventorySlot : ItemStorageSlot {
/**
* Gets called when the Inventory Slot is clicked
*/
@ -14,4 +26,25 @@ public class InventorySlot : ItemStorageSlot {
}
}
}
/**
* Sells the Item for the Item Sell Price and puts it in the Shop if the selling was a mistake
*/
private void SellItem() {
if(Item){
_playerController.ChangeMoney(Item.SellPrice);
_shop.AddItem(Item, 1);
_inventory.RemoveItem(Item, 1); // TODO: somehow sell more than 1 Item
}
}
/**
* Gets called when the Inventory Slot gets clicked on
*/
public void OnPointerClick(PointerEventData eventData) {
// When clicked on with right Mouse Button sell the Item
if(eventData.button == PointerEventData.InputButton.Right) {
SellItem();
}
}
}

View file

@ -8,8 +8,8 @@ public class Item : ScriptableObject, IComparable<Item> {
public int id; //TODO: create an actual ID System that makes snens
public Sprite selectedSprite;
public Sprite defaultSprite;
public int cost;
public int SellPrice => Convert.ToInt32(cost * 0.8);
public int price;
public int SellPrice => Convert.ToInt32(price * 0.8);
public Item(string displayName, string description, int id) {
this.displayName = displayName;

View file

@ -25,7 +25,7 @@ public class PlayerController : MonoBehaviour {
private int _money;
private UsableItem _selectedItem;
public int startMoney = 100;
public int startMoney;
public TextMeshProUGUI moneyTextMeshProUGUI;
public int Money => _money;
@ -35,8 +35,9 @@ public class PlayerController : MonoBehaviour {
// Start is called before the first frame update
private void Start() {
_money = startMoney;
_inventory = Inventory.instance;
_money = startMoney;
UpdateMoneyUI();
onMoneyChangedCallback += UpdateMoneyUI;
}
@ -56,6 +57,7 @@ public class PlayerController : MonoBehaviour {
public void ChangeMoney(int amount) {
_money += amount;
onMoneyChangedCallback?.Invoke();
}

View file

@ -39,7 +39,7 @@ public class Shop : ItemStorage {
_playerController = PlayerController.instance;
if(_lastBoughtItem) {
_playerController.ChangeMoney(_lastBoughtItem.cost);
_playerController.ChangeMoney(_lastBoughtItem.price);
_inventory.RemoveItem(_lastBoughtItem, _lastBoughtItemAmount);
AddItem(_lastBoughtItem, _lastBoughtItemAmount);
itemWasBought = false;

View file

@ -31,9 +31,9 @@ public class ShopSlot : ItemStorageSlot {
*/
public override void UseItem() {
if(Item) {
if(_playerController.Money >= Item.cost) {
if(_playerController.Money >= Item.price) {
if(Item) {
_playerController.ChangeMoney(-Item.cost);
_playerController.ChangeMoney(-Item.price);
_shop.itemWasBought = true;
Debug.Log("Buying Item: " + Item.displayName);

View file

@ -35,7 +35,7 @@ public class ShopUI : MonoBehaviour {
* Turn on/off the Shop UI
*/
private void ToggleShop() {
// TODO: geht ned oda so: inventoryUI.gameObject.SetActive(!shopUI.activeSelf);
inventoryUI.gameObject.SetActive(!shopUI.activeSelf);
shopUI.SetActive(!shopUI.activeSelf);
}
@ -48,7 +48,7 @@ public class ShopUI : MonoBehaviour {
if (i < _shop.items.Count) {
_slots[i].AddItem(_shop.items.ElementAt(i).Key);
_slots[i].nameText.text = _slots[i].Item.displayName;
_slots[i].costText.text = _slots[i].Item.cost + " µ";
_slots[i].costText.text = _slots[i].Item.price + " µ";
_slots[i].amountText.text = _shop.items[_shop.items.ElementAt(i).Key] + " #";
}
else {