From ee6704abc1493acbf6ad9cfdd75ae264da52d8a9 Mon Sep 17 00:00:00 2001 From: dhain Date: Thu, 2 Jun 2022 15:08:21 +0200 Subject: [PATCH] added ItemStorage.cs, ItemStorageSlot.cs, ItemStorageUI.cs * Inventory, InventorySlot, InventoryUI, Shop, ShopSlot, ShopUI are extending them --- Assets/Scenes/MainScene.unity | 8 ++- Assets/Scripts/Inventory.cs | 43 ++-------------- Assets/Scripts/InventorySlot.cs | 69 +++----------------------- Assets/Scripts/InventoryUI.cs | 1 - Assets/Scripts/ItemStorage.cs | 46 +++++++++++++++++ Assets/Scripts/ItemStorage.cs.meta | 11 ++++ Assets/Scripts/ItemStorageSlot.cs | 68 +++++++++++++++++++++++++ Assets/Scripts/ItemStorageSlot.cs.meta | 11 ++++ Assets/Scripts/ItemStorageUI.cs | 55 ++++++++++++++++++++ Assets/Scripts/ItemStorageUI.cs.meta | 11 ++++ Assets/Scripts/Shop.cs | 44 +--------------- Assets/Scripts/ShopSlot.cs | 63 ++++------------------- Assets/Scripts/ShopUI.cs | 6 +-- 13 files changed, 231 insertions(+), 205 deletions(-) create mode 100644 Assets/Scripts/ItemStorage.cs create mode 100644 Assets/Scripts/ItemStorage.cs.meta create mode 100644 Assets/Scripts/ItemStorageSlot.cs create mode 100644 Assets/Scripts/ItemStorageSlot.cs.meta create mode 100644 Assets/Scripts/ItemStorageUI.cs create mode 100644 Assets/Scripts/ItemStorageUI.cs.meta diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index ee638aa..33531a0 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -4545,7 +4545,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!114 &1291863650 MonoBehaviour: m_ObjectHideFlags: 0 @@ -5946,7 +5946,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: bc5eb8967b8912c42b93a2086383ddd9, type: 3} m_Name: m_EditorClassIdentifier: - tempItems: + startItems: - {fileID: 11400000, guid: 008a8fdd2c3a95745acafee4087a855d, type: 2} - {fileID: 11400000, guid: 430db451ae959f34b8fba8d8b17276fd, type: 2} - {fileID: 11400000, guid: d651d57ba97a4246a0094409e29fe56a, type: 2} @@ -7738,6 +7738,10 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 1985691912} m_Modifications: + - target: {fileID: 5121261193055935939, guid: 2279fa2b47ef2ce40af9052d3a3f438b, type: 3} + propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_MethodName + value: UseItem + objectReference: {fileID: 0} - target: {fileID: 5121261193055935940, guid: 2279fa2b47ef2ce40af9052d3a3f438b, type: 3} propertyPath: m_Pivot.x value: 0.5 diff --git a/Assets/Scripts/Inventory.cs b/Assets/Scripts/Inventory.cs index 4e9250e..f297705 100644 --- a/Assets/Scripts/Inventory.cs +++ b/Assets/Scripts/Inventory.cs @@ -1,7 +1,6 @@ -using System.Collections.Generic; using UnityEngine; -public class Inventory : MonoBehaviour { +public class Inventory : ItemStorage { #region Singleton public static Inventory instance; @@ -16,51 +15,17 @@ public class Inventory : MonoBehaviour { #endregion - public Dictionary items; - public Item[] startItems; public const int InventorySpace = 28; - /** - * 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 Inventory */ - public void AddItem(Item item, int amount) { + public override void AddItem(Item item, int amount) { if(items.Count >= InventorySpace) { Debug.Log("Not enough inventory space!"); return; } - - if(!items.ContainsKey(item)) { - items.Add(item, amount); - } else { - items[item] += amount; - } - - onItemChangedCallback?.Invoke(); - } - - /** - * Removes the specified amount of items in the Inventory - */ - public void RemoveItem(Item item, int amount) { - if(items[item] <= 0) { - items.Remove(item); - } else { - items[item] -= amount; - } - - onItemChangedCallback?.Invoke(); + + base.AddItem(item, amount); } } diff --git a/Assets/Scripts/InventorySlot.cs b/Assets/Scripts/InventorySlot.cs index d9a14ae..6aa5e43 100644 --- a/Assets/Scripts/InventorySlot.cs +++ b/Assets/Scripts/InventorySlot.cs @@ -1,72 +1,15 @@ -using System; -using System.Collections; -using TMPro; using UnityEngine; -using UnityEngine.EventSystems; -using UnityEngine.UI; - -public class InventorySlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { - public Image icon; - public TextMeshProUGUI amountText; - - public Item _item; - - #region DescriptionHover - - public float timeToWait; - - public void OnPointerEnter(PointerEventData eventData) { - StopAllCoroutines(); - StartCoroutine(StartTimer()); - } - - public void OnPointerExit(PointerEventData eventData) { - StopAllCoroutines(); - HoverManager.onMouseExit(); - } - - private void ShowMessage() { - if(_item){ - HoverManager.onMouseHover(_item.description, Input.mousePosition); - } - } - - private IEnumerator StartTimer() { - yield return new WaitForSeconds(timeToWait); - - ShowMessage(); - } - - #endregion - - /** - * Sets the Item of the Inventory Slot - */ - public void AddItem(Item newItem) { - _item = newItem; - - icon.sprite = _item.defaultSprite; - icon.enabled = true; - } - - /** - * Clears the Inventory Slot - */ - public void ClearSlot() { - _item = null; - icon.sprite = null; - icon.enabled = false; - } +public class InventorySlot : ItemStorageSlot { /** * Gets called when the Inventory Slot is clicked */ - public void UseItem() { - if(_item.GetType() == typeof(UsableItem)) { - ((UsableItem) _item).Select(); - Debug.Log("using " + _item.displayName); + public override void UseItem() { + if(Item.GetType() == typeof(UsableItem)) { + ((UsableItem) Item).Select(); + Debug.Log("using " + Item.displayName); } else { - Debug.Log("Item not usable " + _item.displayName); + Debug.Log("Item not usable " + Item.displayName); } } } \ No newline at end of file diff --git a/Assets/Scripts/InventoryUI.cs b/Assets/Scripts/InventoryUI.cs index 94d4a02..ffe6660 100644 --- a/Assets/Scripts/InventoryUI.cs +++ b/Assets/Scripts/InventoryUI.cs @@ -1,4 +1,3 @@ -using System; using System.Linq; using UnityEngine; diff --git a/Assets/Scripts/ItemStorage.cs b/Assets/Scripts/ItemStorage.cs new file mode 100644 index 0000000..3b023ba --- /dev/null +++ b/Assets/Scripts/ItemStorage.cs @@ -0,0 +1,46 @@ +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 void RemoveItem(Item item, int amount) { + if(items[item] <= 0) { + items.Remove(item); + } else { + items[item] -= amount; + } + + onItemChangedCallback?.Invoke(); + } +} diff --git a/Assets/Scripts/ItemStorage.cs.meta b/Assets/Scripts/ItemStorage.cs.meta new file mode 100644 index 0000000..c2c179d --- /dev/null +++ b/Assets/Scripts/ItemStorage.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 21c02dc661faff342aca965c68c2c13a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/ItemStorageSlot.cs b/Assets/Scripts/ItemStorageSlot.cs new file mode 100644 index 0000000..f8c8f04 --- /dev/null +++ b/Assets/Scripts/ItemStorageSlot.cs @@ -0,0 +1,68 @@ +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 DescriptionHover + + public float timeToWait; + + public void OnPointerEnter(PointerEventData eventData) { + StopAllCoroutines(); + StartCoroutine(StartTimer()); + } + + public void OnPointerExit(PointerEventData eventData) { + StopAllCoroutines(); + HoverManager.onMouseExit(); + } + + private void ShowMessage() { + if(_item){ + HoverManager.onMouseHover(_item.description, Input.mousePosition); + } + } + + 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; + } + + /** + * Gets called when the Item Storage Slot is clicked + */ + public virtual void UseItem() { + } +} diff --git a/Assets/Scripts/ItemStorageSlot.cs.meta b/Assets/Scripts/ItemStorageSlot.cs.meta new file mode 100644 index 0000000..c571d3f --- /dev/null +++ b/Assets/Scripts/ItemStorageSlot.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b6f14e8c24660e04e9ceb50a7d8e659d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/ItemStorageUI.cs b/Assets/Scripts/ItemStorageUI.cs new file mode 100644 index 0000000..25677d3 --- /dev/null +++ b/Assets/Scripts/ItemStorageUI.cs @@ -0,0 +1,55 @@ +using System.Linq; +using UnityEngine; + +public class ItemStorageUI : MonoBehaviour { + public Transform itemsParent; + public GameObject inventoryUI; + private Inventory _inventory; + private InventorySlot[] _slots; + + private void Start() { + // Get Inventory instance and add UpdateUI method to OnItemChanged delegate + _inventory = Inventory.instance; + _inventory.onItemChangedCallback += UpdateUI; + + // Add all InventorySlot GameObjects to _slots and turn off the Inventory UI + _slots = itemsParent.GetComponentsInChildren(); + ToggleInventory(); + + // Set the icon to not be a raycast target for the Description Hovering to work + foreach(InventorySlot slot in _slots) { + slot.icon.raycastTarget = false; + } + } + + private void Update() { + // When "Inventory" button is pressed turn on/off Inventory UI + if(Input.GetButtonDown("Inventory")) { + ToggleInventory(); + } + } + + /** + * Turn on/off the Inventory UI + */ + private void ToggleInventory() { + inventoryUI.SetActive(!inventoryUI.activeSelf); + } + + /** + * Is called when something in the Inventory UI should update + */ + 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 < _inventory.items.Count) { + _slots[i].AddItem(_inventory.items.ElementAt(i).Key); + if(_inventory.items[_inventory.items.ElementAt(i).Key] > 1) { + _slots[i].amountText.text = "" + _inventory.items[_inventory.items.ElementAt(i).Key]; + } + } else { + _slots[i].ClearSlot(); + } + } + } +} diff --git a/Assets/Scripts/ItemStorageUI.cs.meta b/Assets/Scripts/ItemStorageUI.cs.meta new file mode 100644 index 0000000..0cf565d --- /dev/null +++ b/Assets/Scripts/ItemStorageUI.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 21b91c5ff9ffa4d45a5e71b08b141657 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Shop.cs b/Assets/Scripts/Shop.cs index 32c2ef1..71c261a 100644 --- a/Assets/Scripts/Shop.cs +++ b/Assets/Scripts/Shop.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using UnityEngine; -public class Shop : MonoBehaviour { +public class Shop : ItemStorage { #region Singleton public static Shop instance; @@ -15,46 +15,4 @@ public class Shop : MonoBehaviour { } #endregion - - public Dictionary items; - public Item[] tempItems; - - /** - * 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 tempItems) { - AddItem(item, 1); - } - } - - /** - * Adds the specified amount of items to the Shop - */ - public void AddItem(Item item, int amount) { - if(!items.ContainsKey(item)) { - items.Add(item, amount); - } else { - items[item] += amount; - } - - onItemChangedCallback?.Invoke(); - } - // TODO: add to buy more than one item - /** - * Removes the specified amount of items in the Shop - */ - public void RemoveItem(Item item, int amount) { - if(items[item] <= 0) { - items.Remove(item); - } else { - items[item] -= amount; - } - - onItemChangedCallback?.Invoke(); - } } \ No newline at end of file diff --git a/Assets/Scripts/ShopSlot.cs b/Assets/Scripts/ShopSlot.cs index 7679618..e62d7a3 100644 --- a/Assets/Scripts/ShopSlot.cs +++ b/Assets/Scripts/ShopSlot.cs @@ -5,68 +5,25 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; -public class ShopSlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { - public Image icon; - public Item item; +public class ShopSlot : ItemStorageSlot { public TextMeshProUGUI nameText; public TextMeshProUGUI costText; - public TextMeshProUGUI amountText; - + private Shop _shop; private Inventory _inventory; private PlayerController _playerController; - - #region DescriptionHover - - public float timeToWait; - - public void OnPointerEnter(PointerEventData eventData) { - StopAllCoroutines(); - StartCoroutine(StartTimer()); - } - - public void OnPointerExit(PointerEventData eventData) { - StopAllCoroutines(); - HoverManager.onMouseExit(); - } - - private void ShowMessage() { - if(item) { - HoverManager.onMouseHover(item.description, Input.mousePosition); - } - } - - private IEnumerator StartTimer() { - yield return new WaitForSeconds(timeToWait); - - ShowMessage(); - } - - #endregion private void Start() { _shop = Shop.instance; _inventory = Inventory.instance; _playerController = PlayerController.instance; } - - /** - * Sets the Item of the Shop Slot - */ - public void AddItem(Item newItem) { - item = newItem; - - icon.sprite = item.defaultSprite; - icon.enabled = true; - } /** * Clears the Shop Slot */ - public void ClearSlot() { - item = null; - icon.sprite = null; - icon.enabled = false; + public override void ClearSlot() { + base.ClearSlot(); nameText.text = ""; costText.text = ""; amountText.text = ""; @@ -75,13 +32,13 @@ public class ShopSlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler /** * Gets called when the Shop Slot is clicked */ - public void UseItem() { - if(_playerController.money >= item.cost) { - _inventory.AddItem(item, 1); - _shop.RemoveItem(item, 1); - _playerController.money -= item.cost; + public override void UseItem() { + if(_playerController.money >= Item.cost) { + _inventory.AddItem(Item, 1); + _shop.RemoveItem(Item, 1); + _playerController.money -= Item.cost; - Debug.Log("Buying Item: " + item.displayName); + Debug.Log("Buying Item: " + Item.displayName); Debug.Log("money left: " + _playerController.money); } else { Debug.Log("Not enough money to buy item."); diff --git a/Assets/Scripts/ShopUI.cs b/Assets/Scripts/ShopUI.cs index 00d7c2f..9e7a37f 100644 --- a/Assets/Scripts/ShopUI.cs +++ b/Assets/Scripts/ShopUI.cs @@ -20,8 +20,6 @@ public class ShopUI : MonoBehaviour { foreach(ShopSlot slot in _slots) { slot.icon.raycastTarget = false; } - - UpdateUI(); } private void Update() { @@ -46,8 +44,8 @@ public class ShopUI : MonoBehaviour { 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.cost + " €"; + _slots[i].nameText.text = _slots[i].Item.displayName; + _slots[i].costText.text = _slots[i].Item.cost + " €"; _slots[i].amountText.text = _shop.items[_shop.items.ElementAt(i).Key] + " #"; } else { _slots[i].ClearSlot();