From e160867e7efa01104ab0723f549984f51b828ad7 Mon Sep 17 00:00:00 2001 From: d-hain Date: Fri, 17 Jun 2022 14:51:44 +0200 Subject: [PATCH] Generified ElementStorage.cs and ElementStorageSlot.cs --- Assets/Resources/InventorySlot.prefab | 2 +- Assets/Resources/ShopSlot.prefab | 2 +- Assets/Scenes/MainScene.unity | 46 +++++----- Assets/Scripts/ActionInvoker.cs | 8 +- Assets/Scripts/ElementStorage.cs | 46 ++++++++++ ...Storage.cs.meta => ElementStorage.cs.meta} | 0 Assets/Scripts/ElementStorageSlot.cs | 86 +++++++++++++++++++ ...lot.cs.meta => ElementStorageSlot.cs.meta} | 0 Assets/Scripts/FishingController.cs | 6 +- Assets/Scripts/Inventory.cs | 26 +++--- Assets/Scripts/InventorySlot.cs | 14 +-- Assets/Scripts/InventoryUI.cs | 12 +-- Assets/Scripts/ItemStorage.cs | 46 ---------- Assets/Scripts/ItemStorageSlot.cs | 82 ------------------ Assets/Scripts/PlayerController.cs | 2 +- Assets/Scripts/Shop.cs | 10 +-- Assets/Scripts/ShopSlot.cs | 22 ++--- Assets/Scripts/ShopUI.cs | 12 +-- 18 files changed, 210 insertions(+), 212 deletions(-) create mode 100644 Assets/Scripts/ElementStorage.cs rename Assets/Scripts/{ItemStorage.cs.meta => ElementStorage.cs.meta} (100%) create mode 100644 Assets/Scripts/ElementStorageSlot.cs rename Assets/Scripts/{ItemStorageSlot.cs.meta => ElementStorageSlot.cs.meta} (100%) delete mode 100644 Assets/Scripts/ItemStorage.cs delete mode 100644 Assets/Scripts/ItemStorageSlot.cs diff --git a/Assets/Resources/InventorySlot.prefab b/Assets/Resources/InventorySlot.prefab index 72e0ba4..517a66a 100644 --- a/Assets/Resources/InventorySlot.prefab +++ b/Assets/Resources/InventorySlot.prefab @@ -338,7 +338,7 @@ MonoBehaviour: m_Calls: - m_Target: {fileID: 8059809276634721085} m_TargetAssemblyTypeName: InventorySlot, Assembly-CSharp - m_MethodName: UseItem + m_MethodName: UseElement m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} diff --git a/Assets/Resources/ShopSlot.prefab b/Assets/Resources/ShopSlot.prefab index d589e1a..b0d8865 100644 --- a/Assets/Resources/ShopSlot.prefab +++ b/Assets/Resources/ShopSlot.prefab @@ -610,7 +610,7 @@ MonoBehaviour: m_Calls: - m_Target: {fileID: 5121261193055935938} m_TargetAssemblyTypeName: ShopSlot, Assembly-CSharp - m_MethodName: UseItem + m_MethodName: UseElement m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index 360cfa9..7aa9ab2 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -7621,34 +7621,25 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: bc5eb8967b8912c42b93a2086383ddd9, type: 3} m_Name: m_EditorClassIdentifier: - startItems: + startElements: + - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} + - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} + - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} + - {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2} + - {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2} - {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2} - {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2} - {fileID: 11400000, guid: bb9777a7d5804bd6bf25d5510206aaf0, type: 2} - - {fileID: 11400000, guid: 008a8fdd2c3a95745acafee4087a855d, type: 2} - - {fileID: 11400000, guid: d651d57ba97a4246a0094409e29fe56a, type: 2} - - {fileID: 11400000, guid: d651d57ba97a4246a0094409e29fe56a, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: 6bac44b5c4527b641a3ae772d217ec43, type: 2} - - {fileID: 11400000, guid: 430db451ae959f34b8fba8d8b17276fd, type: 2} - - {fileID: 11400000, guid: ea1a26b19bc34a0ba29bad77253c7266, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: a5d525b8ca07172499e8678eb6f73fb2, type: 2} - - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} - - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} - - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} - - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} - - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} - - {fileID: 11400000, guid: d115e153d89653b4f9a74f920d055a93, type: 2} itemWasBought: 0 --- !u!1001 &1701153146 PrefabInstance: @@ -8056,9 +8047,12 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a6462fee9117d354abebcd1ff897b920, type: 3} m_Name: m_EditorClassIdentifier: - startItems: + startElements: + - {fileID: 11400000, guid: ea1a26b19bc34a0ba29bad77253c7266, type: 2} - {fileID: 11400000, guid: 008a8fdd2c3a95745acafee4087a855d, type: 2} + - {fileID: 11400000, guid: 430db451ae959f34b8fba8d8b17276fd, type: 2} - {fileID: 11400000, guid: 6bac44b5c4527b641a3ae772d217ec43, type: 2} + - {fileID: 11400000, guid: d651d57ba97a4246a0094409e29fe56a, type: 2} --- !u!114 &1800469992 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/ActionInvoker.cs b/Assets/Scripts/ActionInvoker.cs index 1ede278..26597e8 100644 --- a/Assets/Scripts/ActionInvoker.cs +++ b/Assets/Scripts/ActionInvoker.cs @@ -70,7 +70,7 @@ namespace DefaultNamespace { Crop crop = ((FarmlandTile)gameObject.GetComponent().Tile).Crop; if(!crop.Planted) { crop.Plant(); - Inventory.instance.RemoveItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 1); + Inventory.instance.RemoveElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 1); } UpdateFarmlandSprites(gameObject); @@ -113,9 +113,9 @@ namespace DefaultNamespace { private static void HarvestIfPossible(GameObject gameObject) { Crop crop = ((FarmlandTile)gameObject.GetComponent().Tile).Crop; if(crop.FullyGrown) { - Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"), + Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"), (int)(Random.Range(1, 300))); - Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat"), 1); + Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat"), 1); crop.ResetPlant(); UpdateFarmlandSprites(gameObject); } @@ -141,7 +141,7 @@ namespace DefaultNamespace { Debug.Log("aaaaaaaaaaaaaaaaaaaaa"); fenceRenderer.color = new Color(1, 1, 1, 1); fenceCollider.enabled = true; - Inventory.instance.RemoveItem(ItemContainer.Instance.GetItemByName("Fence"), 1); + Inventory.instance.RemoveElement(ItemContainer.Instance.GetItemByName("Fence"), 1); } } else { Debug.LogError("Fence Renderer or Fence Collider is null"); diff --git a/Assets/Scripts/ElementStorage.cs b/Assets/Scripts/ElementStorage.cs new file mode 100644 index 0000000..7e4f812 --- /dev/null +++ b/Assets/Scripts/ElementStorage.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using UnityEngine; + +public class ElementStorage : MonoBehaviour { + public Dictionary elements; + public T[] startElements; + + /** + * Methods can be added to this and they will get called every time onItemChangedCallback gets Invoked + */ + public delegate void OnElementChanged(); + public OnElementChanged onElementChangedCallback; + + private void Start() { + elements ??= new Dictionary(); + foreach(T element in startElements) { + AddElement(element, 1); + } + } + + /** + * Adds the specified amount of elements to the Element Storage + */ + public virtual void AddElement(T element, int amount) { + if(!elements.ContainsKey(element)) { + elements.Add(element, amount); + } else { + elements[element] += amount; + } + + onElementChangedCallback?.Invoke(); + } + + /** + * Removes the specified amount of elements in the Element Storage + */ + public virtual void RemoveElement(T element, int amount) { + if(elements[element]-amount <= 0) { + elements.Remove(element); + } else { + elements[element] -= amount; + } + + onElementChangedCallback?.Invoke(); + } +} diff --git a/Assets/Scripts/ItemStorage.cs.meta b/Assets/Scripts/ElementStorage.cs.meta similarity index 100% rename from Assets/Scripts/ItemStorage.cs.meta rename to Assets/Scripts/ElementStorage.cs.meta diff --git a/Assets/Scripts/ElementStorageSlot.cs b/Assets/Scripts/ElementStorageSlot.cs new file mode 100644 index 0000000..7f8e348 --- /dev/null +++ b/Assets/Scripts/ElementStorageSlot.cs @@ -0,0 +1,86 @@ +using System.Collections; +using TMPro; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +public class ElementStorageSlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { + public Image icon; + public TextMeshProUGUI amountText; + + public T Element => _element; + + private T _element; + + #region HoverOverSlot + + public float timeToWait; + + public void OnPointerEnter(PointerEventData eventData) { + StopAllCoroutines(); + StartCoroutine(StartTimer()); + + ChangeElementSelectedSprite(true); + } + + public void OnPointerExit(PointerEventData eventData) { + StopAllCoroutines(); + ChangeElementSelectedSprite(false); + HoverManager.onMouseExit(); + } + + private void ShowMessage() { + if(_element is Item) { //TODO: add animal description showing + Item item = (Item)(object)_element; + HoverManager.onMouseHoverDescription(item.description, Input.mousePosition); + } + } + + public void ChangeElementSelectedSprite(bool on) { + if(_element is Item) { //TODO: add animal sprite change + Item item = (Item)(object)_element; + if(on) { + icon.sprite = item.selectedSprite; + } else { + icon.sprite = item.defaultSprite; + } + } + } + + private IEnumerator StartTimer() { + yield return new WaitForSeconds(timeToWait); + + ShowMessage(); + } + + #endregion + + /** + * Sets the Element of the Element Storage Slot + */ + public void AddElement(T newElement) { + _element = newElement; + + if(_element is Item) { //TODO: add animal sprite change + Item item = (Item)(object)_element; + icon.sprite = item.defaultSprite; + } + + icon.enabled = true; + } + + /** + * Clears the Element Storage Slot + */ + public virtual void ClearSlot() { + _element = default(T); + icon.sprite = null; + icon.enabled = false; + amountText.text = ""; + } + + /** + * Gets called when the Element Storage Slot is clicked + */ + public virtual void UseElement() { } +} \ No newline at end of file diff --git a/Assets/Scripts/ItemStorageSlot.cs.meta b/Assets/Scripts/ElementStorageSlot.cs.meta similarity index 100% rename from Assets/Scripts/ItemStorageSlot.cs.meta rename to Assets/Scripts/ElementStorageSlot.cs.meta diff --git a/Assets/Scripts/FishingController.cs b/Assets/Scripts/FishingController.cs index ae076ed..6a462ce 100644 --- a/Assets/Scripts/FishingController.cs +++ b/Assets/Scripts/FishingController.cs @@ -97,7 +97,7 @@ public class FishingController : MonoBehaviour { } public void StartFishing() { - if (!_iv.items.ContainsKey(_ic.GetItemByName("Bait"))) { + if (!_iv.elements.ContainsKey(_ic.GetItemByName("Bait"))) { _messageView.SendMessage("No bait!", 1.0f); return; } @@ -106,7 +106,7 @@ public class FishingController : MonoBehaviour { _messageView.SendMessage("You cannot fish anymore for today!", 1.0f); return; } - _iv.RemoveItem(_ic.GetItemByName("Bait"), 1); + _iv.RemoveElement(_ic.GetItemByName("Bait"), 1); Vector3 pos = Input.mousePosition; @@ -132,7 +132,7 @@ public class FishingController : MonoBehaviour { if (_fishing && Catchable) { if (_fishingTime <= MaxTime) { _messageView.SendMessage("Caught!", 1.5f); - _iv.AddItem(_ic.GetItemByName("Fish"), Math.Max((int)(1 / (_fishingTime / 2)), 1)); + _iv.AddElement(_ic.GetItemByName("Fish"), Math.Max((int)(1 / (_fishingTime / 2)), 1)); _fishedThisDay++; } else { _messageView.SendMessage("Failed to catch the fish! You were too slow!", 1.5f); diff --git a/Assets/Scripts/Inventory.cs b/Assets/Scripts/Inventory.cs index c2d32de..f60d8fe 100644 --- a/Assets/Scripts/Inventory.cs +++ b/Assets/Scripts/Inventory.cs @@ -1,7 +1,7 @@ using System; using UnityEngine; -public class Inventory : ItemStorage { +public class Inventory : ElementStorage { #region Singleton public static Inventory instance; @@ -22,25 +22,25 @@ public class Inventory : ItemStorage { /** * Adds the specified amount of items to the Inventory */ - public override void AddItem(Item item, int amount) { - if (items.Count >= _InventorySpace) { + public override void AddElement(Item item, int amount) { + if (elements.Count >= _InventorySpace) { Debug.Log("Not enough inventory space!"); return; } // Sell overflowing Items - if (items.ContainsKey(item) && items[item] + amount >= _MaxItemStack) { - SellItem(item, amount - (_MaxItemStack - items[item])); - amount = _MaxItemStack - items[item]; + if (elements.ContainsKey(item) && elements[item] + amount >= _MaxItemStack) { + SellItem(item, amount - (_MaxItemStack - elements[item])); + amount = _MaxItemStack - elements[item]; } - base.AddItem(item, amount); + base.AddElement(item, amount); } /** - * Calls ItemStorage.RemoveItem() and deselects the item if removed + * Calls ItemStorage.RemoveElement() and deselects the item if removed */ - public override void RemoveItem(Item item, int amount) { - base.RemoveItem(item, amount); - if (!items.ContainsKey(item) && PlayerController.instance.SelectedItem == item) { + public override void RemoveElement(Item item, int amount) { + base.RemoveElement(item, amount); + if (!elements.ContainsKey(item) && PlayerController.instance.SelectedItem == item) { PlayerController.instance.DeselectItem(); } } @@ -50,7 +50,7 @@ public class Inventory : ItemStorage { */ public void SellItem(Item item, int amount) { PlayerController.instance.ChangeMoney(item.SellPrice); - Shop.instance.AddItem(item, amount); - RemoveItem(item, amount); + Shop.instance.AddElement(item, amount); + RemoveElement(item, amount); } } \ No newline at end of file diff --git a/Assets/Scripts/InventorySlot.cs b/Assets/Scripts/InventorySlot.cs index 954ef13..666afe4 100644 --- a/Assets/Scripts/InventorySlot.cs +++ b/Assets/Scripts/InventorySlot.cs @@ -2,7 +2,7 @@ using System; using UnityEngine; using UnityEngine.EventSystems; -public class InventorySlot : ItemStorageSlot, IPointerClickHandler { +public class InventorySlot : ElementStorageSlot, IPointerClickHandler { private Inventory _inventory; private PlayerController _playerController; @@ -14,10 +14,10 @@ public class InventorySlot : ItemStorageSlot, IPointerClickHandler { /** * Gets called when the Inventory Slot is clicked */ - public override void UseItem() { - if(Item) { - if(Item.GetType() == typeof(UsableItem)) { - ((UsableItem)Item).Select(); + public override void UseElement() { + if(Element) { + if(Element.GetType() == typeof(UsableItem)) { + ((UsableItem)Element).Select(); //Debug.Log("using " + Item.displayName); } else { //Debug.Log("Item not usable " + Item.displayName); @@ -33,8 +33,8 @@ public class InventorySlot : ItemStorageSlot, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { // When clicked on with right Mouse Button sell the Item if(eventData.button == PointerEventData.InputButton.Right) { - if(Item) { - _inventory.SellItem(Item, 1); //TODO: wie machen mehr als 1 verkaufen?! + if(Element) { + _inventory.SellItem(Element, 1); //TODO: wie machen mehr als 1 verkaufen?! } } } diff --git a/Assets/Scripts/InventoryUI.cs b/Assets/Scripts/InventoryUI.cs index b8da522..9c5536f 100644 --- a/Assets/Scripts/InventoryUI.cs +++ b/Assets/Scripts/InventoryUI.cs @@ -12,7 +12,7 @@ public class InventoryUI : MonoBehaviour { private void Start() { // Get Inventory instance and add UpdateUI method to OnItemChanged delegate _inventory = Inventory.instance; - _inventory.onItemChangedCallback += UpdateUI; + _inventory.onElementChangedCallback += UpdateUI; // Add all InventorySlot GameObjects to _slots and turn off the Inventory UI _slots = itemsParent.GetComponentsInChildren(); @@ -38,7 +38,7 @@ public class InventoryUI : MonoBehaviour { inventoryUI.SetActive(!inventoryUI.activeSelf); HoverManager.instance.HideDescription(); foreach(InventorySlot slot in _slots) { - slot.ChangeItemSelectedSprite(false); + slot.ChangeElementSelectedSprite(false); } } @@ -48,10 +48,10 @@ public class InventoryUI : MonoBehaviour { private void UpdateUI() { // Add all items to the correct slots, clear the ones where no item should be and set the number of how many items are in the slot for(int i = 0; i < _slots.Length; i++) { - if(i < _inventory.items.Count) { - _slots[i].AddItem(_inventory.items.ElementAt(i).Key); - _slots[i].amountText.text = "" + _inventory.items[_inventory.items.ElementAt(i).Key]; - if(_inventory.items[_inventory.items.ElementAt(i).Key] == 1) { + if(i < _inventory.elements.Count) { + _slots[i].AddElement(_inventory.elements.ElementAt(i).Key); + _slots[i].amountText.text = "" + _inventory.elements[_inventory.elements.ElementAt(i).Key]; + if(_inventory.elements[_inventory.elements.ElementAt(i).Key] == 1) { _slots[i].amountText.text = ""; } } else { diff --git a/Assets/Scripts/ItemStorage.cs b/Assets/Scripts/ItemStorage.cs deleted file mode 100644 index bcd616d..0000000 --- a/Assets/Scripts/ItemStorage.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections.Generic; -using UnityEngine; - -public class ItemStorage : MonoBehaviour { - public Dictionary items; - public Item[] startItems; - - /** - * Methods can be added to this and they will get called every time onItemChangedCallback gets Invoked - */ - public delegate void OnItemChanged(); - public OnItemChanged onItemChangedCallback; - - private void Start() { - items ??= new Dictionary(); - foreach(Item item in startItems) { - AddItem(item, 1); - } - } - - /** - * Adds the specified amount of items to the Item Storage - */ - public virtual void AddItem(Item item, int amount) { - if(!items.ContainsKey(item)) { - items.Add(item, amount); - } else { - items[item] += amount; - } - - onItemChangedCallback?.Invoke(); - } - - /** - * Removes the specified amount of items in the Item Storage - */ - public virtual void RemoveItem(Item item, int amount) { - if(items[item]-amount <= 0) { - items.Remove(item); - } else { - items[item] -= amount; - } - - onItemChangedCallback?.Invoke(); - } -} diff --git a/Assets/Scripts/ItemStorageSlot.cs b/Assets/Scripts/ItemStorageSlot.cs deleted file mode 100644 index 1f71e28..0000000 --- a/Assets/Scripts/ItemStorageSlot.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using TMPro; -using UnityEngine; -using UnityEngine.EventSystems; -using UnityEngine.UI; - -public class ItemStorageSlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { - public Image icon; - public TextMeshProUGUI amountText; - - public Item Item => _item; - - private Item _item; - - #region HoverOverItem - - public float timeToWait; - - public void OnPointerEnter(PointerEventData eventData) { - StopAllCoroutines(); - StartCoroutine(StartTimer()); - - ChangeItemSelectedSprite(true); - } - - public void OnPointerExit(PointerEventData eventData) { - StopAllCoroutines(); - ChangeItemSelectedSprite(false); - HoverManager.onMouseExit(); - } - - private void ShowMessage() { - if(_item){ - HoverManager.onMouseHoverDescription(_item.description, Input.mousePosition); - } - } - - public void ChangeItemSelectedSprite(bool on) { - if(_item) { - if(on) { - icon.sprite = _item.selectedSprite; - } else { - icon.sprite = _item.defaultSprite; - } - } - } - - private IEnumerator StartTimer() { - yield return new WaitForSeconds(timeToWait); - - ShowMessage(); - } - - #endregion - - /** - * Sets the Item of the Item Storage Slot - */ - public void AddItem(Item newItem) { - _item = newItem; - - icon.sprite = _item.defaultSprite; - icon.enabled = true; - } - - /** - * Clears the Item Storage Slot - */ - public virtual void ClearSlot() { - _item = null; - icon.sprite = null; - icon.enabled = false; - amountText.text = ""; - } - - /** - * Gets called when the Item Storage Slot is clicked - */ - public virtual void UseItem() { - } -} diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 9fb7387..e88d110 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -33,7 +33,7 @@ public class PlayerController : MonoBehaviour { public UsableItem SelectedItem { get => _selectedItem; set { - if(_inventory.items.ContainsKey(value)) { + if(_inventory.elements.ContainsKey(value)) { _selectedItem = value; Cursor.SetCursor(value.defaultSprite.texture, Vector2.zero, CursorMode.Auto); } else { diff --git a/Assets/Scripts/Shop.cs b/Assets/Scripts/Shop.cs index 24d1b02..6bd6e7e 100644 --- a/Assets/Scripts/Shop.cs +++ b/Assets/Scripts/Shop.cs @@ -1,6 +1,6 @@ using UnityEngine; -public class Shop : ItemStorage { +public class Shop : ElementStorage { #region Singleton public static Shop instance; @@ -25,8 +25,8 @@ public class Shop : ItemStorage { /** * Calls ItemStorage.RemoveItem() and sets 2 Variables to remember the last bought item */ - public override void RemoveItem(Item item, int amount) { - base.RemoveItem(item, amount); + public override void RemoveElement(Item item, int amount) { + base.RemoveElement(item, amount); if(itemWasBought){ _lastBoughtItem = item; _lastBoughtItemAmount = amount; @@ -40,8 +40,8 @@ public class Shop : ItemStorage { if(_lastBoughtItem) { _playerController.ChangeMoney(_lastBoughtItem.price); - _inventory.RemoveItem(_lastBoughtItem, _lastBoughtItemAmount); - AddItem(_lastBoughtItem, _lastBoughtItemAmount); + _inventory.RemoveElement(_lastBoughtItem, _lastBoughtItemAmount); + AddElement(_lastBoughtItem, _lastBoughtItemAmount); itemWasBought = false; } } diff --git a/Assets/Scripts/ShopSlot.cs b/Assets/Scripts/ShopSlot.cs index 99b41da..2167aaf 100644 --- a/Assets/Scripts/ShopSlot.cs +++ b/Assets/Scripts/ShopSlot.cs @@ -1,7 +1,7 @@ using TMPro; using UnityEngine; -public class ShopSlot : ItemStorageSlot { +public class ShopSlot : ElementStorageSlot { public TextMeshProUGUI nameText; public TextMeshProUGUI costText; @@ -29,23 +29,23 @@ public class ShopSlot : ItemStorageSlot { /** * 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); + public override void UseElement() { + if(Element) { + if(_playerController.Money >= Element.price) { + if(Element) { + _playerController.ChangeMoney(-Element.price); _shop.itemWasBought = true; - Debug.Log("Buying Item: " + Item.displayName); + Debug.Log("Buying Item: " + Element.displayName); } - _inventory.AddItem(Item, 1); - _shop.RemoveItem(Item, 1); + _inventory.AddElement(Element, 1); + _shop.RemoveElement(Element, 1); } else { Debug.Log("Not enough money to buy item."); } - _shop.onItemChangedCallback?.Invoke(); - _inventory.onItemChangedCallback?.Invoke(); + _shop.onElementChangedCallback?.Invoke(); + _inventory.onElementChangedCallback?.Invoke(); } } } diff --git a/Assets/Scripts/ShopUI.cs b/Assets/Scripts/ShopUI.cs index bb542bb..6dd5928 100644 --- a/Assets/Scripts/ShopUI.cs +++ b/Assets/Scripts/ShopUI.cs @@ -12,7 +12,7 @@ public class ShopUI : MonoBehaviour { private void Start() { // Get Shop instance and add UpdateUI method to OnItemChanged delegate _shop = Shop.instance; - _shop.onItemChangedCallback += UpdateUI; + _shop.onElementChangedCallback += UpdateUI; // Add all ShopSlot GameObjects to _slots and turn off the Shop UI _slots = itemsParent.GetComponentsInChildren(); @@ -46,11 +46,11 @@ public class ShopUI : MonoBehaviour { private void UpdateUI() { // Add all items to the correct slots and clear the ones where no item should be for (int i = 0; i < _slots.Length; i++) { - 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.price + " µ"; - _slots[i].amountText.text = _shop.items[_shop.items.ElementAt(i).Key] + " #"; + if (i < _shop.elements.Count) { + _slots[i].AddElement(_shop.elements.ElementAt(i).Key); + _slots[i].nameText.text = _slots[i].Element.displayName; + _slots[i].costText.text = _slots[i].Element.price + " µ"; + _slots[i].amountText.text = _shop.elements[_shop.elements.ElementAt(i).Key] + " #"; } else { _slots[i].ClearSlot();