Generified ElementStorage.cs and ElementStorageSlot.cs
This commit is contained in:
parent
df3fad8c51
commit
e160867e7e
18 changed files with 210 additions and 212 deletions
|
|
@ -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}
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ namespace DefaultNamespace {
|
|||
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().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<TileBehaviour>().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");
|
||||
|
|
|
|||
46
Assets/Scripts/ElementStorage.cs
Normal file
46
Assets/Scripts/ElementStorage.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ElementStorage<T> : MonoBehaviour {
|
||||
public Dictionary<T, int> 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<T, int>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
86
Assets/Scripts/ElementStorageSlot.cs
Normal file
86
Assets/Scripts/ElementStorageSlot.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
using System.Collections;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ElementStorageSlot<T> : 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() { }
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class Inventory : ItemStorage {
|
||||
public class Inventory : ElementStorage<Item> {
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ using System;
|
|||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class InventorySlot : ItemStorageSlot, IPointerClickHandler {
|
||||
public class InventorySlot : ElementStorageSlot<Item>, 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?!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<InventorySlot>();
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
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 virtual void RemoveItem(Item item, int amount) {
|
||||
if(items[item]-amount <= 0) {
|
||||
items.Remove(item);
|
||||
} else {
|
||||
items[item] -= amount;
|
||||
}
|
||||
|
||||
onItemChangedCallback?.Invoke();
|
||||
}
|
||||
}
|
||||
|
|
@ -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() {
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class Shop : ItemStorage {
|
||||
public class Shop : ElementStorage<Item> {
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class ShopSlot : ItemStorageSlot {
|
||||
public class ShopSlot : ElementStorageSlot<Item> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<ShopSlot>();
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue