diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index ad3c98e..0f079be 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -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 diff --git a/Assets/Scripts/InventorySlot.cs b/Assets/Scripts/InventorySlot.cs index d36375d..d466efb 100644 --- a/Assets/Scripts/InventorySlot.cs +++ b/Assets/Scripts/InventorySlot.cs @@ -1,11 +1,23 @@ +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 */ public override void UseItem() { - if(Item){ + if(Item) { if(Item.GetType() == typeof(UsableItem)) { ((UsableItem)Item).Select(); Debug.Log("using " + Item.displayName); @@ -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(); + } + } } \ No newline at end of file diff --git a/Assets/Scripts/Item.cs b/Assets/Scripts/Item.cs index ecc0a19..9b08db5 100644 --- a/Assets/Scripts/Item.cs +++ b/Assets/Scripts/Item.cs @@ -8,8 +8,8 @@ public class Item : ScriptableObject, IComparable { 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; diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 5a96a8e..8d92a72 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -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(); } diff --git a/Assets/Scripts/Shop.cs b/Assets/Scripts/Shop.cs index 8ccf7c6..24d1b02 100644 --- a/Assets/Scripts/Shop.cs +++ b/Assets/Scripts/Shop.cs @@ -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; diff --git a/Assets/Scripts/ShopSlot.cs b/Assets/Scripts/ShopSlot.cs index ced7a06..99b41da 100644 --- a/Assets/Scripts/ShopSlot.cs +++ b/Assets/Scripts/ShopSlot.cs @@ -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); diff --git a/Assets/Scripts/ShopUI.cs b/Assets/Scripts/ShopUI.cs index 9b4fc66..7fa5f9f 100644 --- a/Assets/Scripts/ShopUI.cs +++ b/Assets/Scripts/ShopUI.cs @@ -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 {