added ItemStorage.cs, ItemStorageSlot.cs, ItemStorageUI.cs
* Inventory, InventorySlot, InventoryUI, Shop, ShopSlot, ShopUI are extending them
This commit is contained in:
parent
d28a1947eb
commit
ee6704abc1
13 changed files with 231 additions and 205 deletions
|
|
@ -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<Item, int> 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<Item, int>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
|
|
|
|||
46
Assets/Scripts/ItemStorage.cs
Normal file
46
Assets/Scripts/ItemStorage.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ItemStorage : MonoBehaviour {
|
||||
public Dictionary<Item, int> 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<Item, int>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ItemStorage.cs.meta
Normal file
11
Assets/Scripts/ItemStorage.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 21c02dc661faff342aca965c68c2c13a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
68
Assets/Scripts/ItemStorageSlot.cs
Normal file
68
Assets/Scripts/ItemStorageSlot.cs
Normal file
|
|
@ -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() {
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ItemStorageSlot.cs.meta
Normal file
11
Assets/Scripts/ItemStorageSlot.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b6f14e8c24660e04e9ceb50a7d8e659d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
55
Assets/Scripts/ItemStorageUI.cs
Normal file
55
Assets/Scripts/ItemStorageUI.cs
Normal file
|
|
@ -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<InventorySlot>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ItemStorageUI.cs.meta
Normal file
11
Assets/Scripts/ItemStorageUI.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 21b91c5ff9ffa4d45a5e71b08b141657
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -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<Item, int> 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<Item, int>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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.");
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue