added cost amount to items

added Shop UI
added Shop.cs
 * you now can buy items and you lose the cost when bought
 * shop is scrollable

added comments in Inventory.cs, InventorySlot.cs, InventoryUI.cs
now showing description when hovering over items in shop or inventory
This commit is contained in:
d-hain 2022-06-02 00:08:37 +02:00
parent c9416181fc
commit ce1f6ed389
30 changed files with 4726 additions and 64 deletions

View file

@ -0,0 +1,93 @@
using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ShopSlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
public Image icon;
public Item item;
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;
nameText.text = "";
costText.text = "";
amountText.text = "";
}
/**
* 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;
Debug.Log("Buying Item: " + item.displayName);
Debug.Log("money left: " + _playerController.money);
} else {
Debug.Log("Not enough money to buy item.");
}
_shop.onItemChangedCallback?.Invoke();
_inventory.onItemChangedCallback?.Invoke();
}
}