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_Calls:
|
||||||
- m_Target: {fileID: 8059809276634721085}
|
- m_Target: {fileID: 8059809276634721085}
|
||||||
m_TargetAssemblyTypeName: InventorySlot, Assembly-CSharp
|
m_TargetAssemblyTypeName: InventorySlot, Assembly-CSharp
|
||||||
m_MethodName: UseItem
|
m_MethodName: UseElement
|
||||||
m_Mode: 1
|
m_Mode: 1
|
||||||
m_Arguments:
|
m_Arguments:
|
||||||
m_ObjectArgument: {fileID: 0}
|
m_ObjectArgument: {fileID: 0}
|
||||||
|
|
|
||||||
|
|
@ -610,7 +610,7 @@ MonoBehaviour:
|
||||||
m_Calls:
|
m_Calls:
|
||||||
- m_Target: {fileID: 5121261193055935938}
|
- m_Target: {fileID: 5121261193055935938}
|
||||||
m_TargetAssemblyTypeName: ShopSlot, Assembly-CSharp
|
m_TargetAssemblyTypeName: ShopSlot, Assembly-CSharp
|
||||||
m_MethodName: UseItem
|
m_MethodName: UseElement
|
||||||
m_Mode: 1
|
m_Mode: 1
|
||||||
m_Arguments:
|
m_Arguments:
|
||||||
m_ObjectArgument: {fileID: 0}
|
m_ObjectArgument: {fileID: 0}
|
||||||
|
|
|
||||||
|
|
@ -7621,34 +7621,25 @@ MonoBehaviour:
|
||||||
m_Script: {fileID: 11500000, guid: bc5eb8967b8912c42b93a2086383ddd9, type: 3}
|
m_Script: {fileID: 11500000, guid: bc5eb8967b8912c42b93a2086383ddd9, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
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: 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
|
itemWasBought: 0
|
||||||
--- !u!1001 &1701153146
|
--- !u!1001 &1701153146
|
||||||
PrefabInstance:
|
PrefabInstance:
|
||||||
|
|
@ -8056,9 +8047,12 @@ MonoBehaviour:
|
||||||
m_Script: {fileID: 11500000, guid: a6462fee9117d354abebcd1ff897b920, type: 3}
|
m_Script: {fileID: 11500000, guid: a6462fee9117d354abebcd1ff897b920, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
startItems:
|
startElements:
|
||||||
|
- {fileID: 11400000, guid: ea1a26b19bc34a0ba29bad77253c7266, type: 2}
|
||||||
- {fileID: 11400000, guid: 008a8fdd2c3a95745acafee4087a855d, type: 2}
|
- {fileID: 11400000, guid: 008a8fdd2c3a95745acafee4087a855d, type: 2}
|
||||||
|
- {fileID: 11400000, guid: 430db451ae959f34b8fba8d8b17276fd, type: 2}
|
||||||
- {fileID: 11400000, guid: 6bac44b5c4527b641a3ae772d217ec43, type: 2}
|
- {fileID: 11400000, guid: 6bac44b5c4527b641a3ae772d217ec43, type: 2}
|
||||||
|
- {fileID: 11400000, guid: d651d57ba97a4246a0094409e29fe56a, type: 2}
|
||||||
--- !u!114 &1800469992
|
--- !u!114 &1800469992
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ namespace DefaultNamespace {
|
||||||
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().Tile).Crop;
|
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().Tile).Crop;
|
||||||
if(!crop.Planted) {
|
if(!crop.Planted) {
|
||||||
crop.Plant();
|
crop.Plant();
|
||||||
Inventory.instance.RemoveItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 1);
|
Inventory.instance.RemoveElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateFarmlandSprites(gameObject);
|
UpdateFarmlandSprites(gameObject);
|
||||||
|
|
@ -113,9 +113,9 @@ namespace DefaultNamespace {
|
||||||
private static void HarvestIfPossible(GameObject gameObject) {
|
private static void HarvestIfPossible(GameObject gameObject) {
|
||||||
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().Tile).Crop;
|
Crop crop = ((FarmlandTile)gameObject.GetComponent<TileBehaviour>().Tile).Crop;
|
||||||
if(crop.FullyGrown) {
|
if(crop.FullyGrown) {
|
||||||
Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat Seeds"),
|
Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat Seeds"),
|
||||||
(int)(Random.Range(1, 300)));
|
(int)(Random.Range(1, 300)));
|
||||||
Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Wheat"), 1);
|
Inventory.instance.AddElement(ItemContainer.Instance.GetItemByName("Wheat"), 1);
|
||||||
crop.ResetPlant();
|
crop.ResetPlant();
|
||||||
UpdateFarmlandSprites(gameObject);
|
UpdateFarmlandSprites(gameObject);
|
||||||
}
|
}
|
||||||
|
|
@ -141,7 +141,7 @@ namespace DefaultNamespace {
|
||||||
Debug.Log("aaaaaaaaaaaaaaaaaaaaa");
|
Debug.Log("aaaaaaaaaaaaaaaaaaaaa");
|
||||||
fenceRenderer.color = new Color(1, 1, 1, 1);
|
fenceRenderer.color = new Color(1, 1, 1, 1);
|
||||||
fenceCollider.enabled = true;
|
fenceCollider.enabled = true;
|
||||||
Inventory.instance.RemoveItem(ItemContainer.Instance.GetItemByName("Fence"), 1);
|
Inventory.instance.RemoveElement(ItemContainer.Instance.GetItemByName("Fence"), 1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Debug.LogError("Fence Renderer or Fence Collider is null");
|
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() {
|
public void StartFishing() {
|
||||||
if (!_iv.items.ContainsKey(_ic.GetItemByName("Bait"))) {
|
if (!_iv.elements.ContainsKey(_ic.GetItemByName("Bait"))) {
|
||||||
_messageView.SendMessage("No bait!", 1.0f);
|
_messageView.SendMessage("No bait!", 1.0f);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -106,7 +106,7 @@ public class FishingController : MonoBehaviour {
|
||||||
_messageView.SendMessage("You cannot fish anymore for today!", 1.0f);
|
_messageView.SendMessage("You cannot fish anymore for today!", 1.0f);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_iv.RemoveItem(_ic.GetItemByName("Bait"), 1);
|
_iv.RemoveElement(_ic.GetItemByName("Bait"), 1);
|
||||||
|
|
||||||
Vector3 pos = Input.mousePosition;
|
Vector3 pos = Input.mousePosition;
|
||||||
|
|
||||||
|
|
@ -132,7 +132,7 @@ public class FishingController : MonoBehaviour {
|
||||||
if (_fishing && Catchable) {
|
if (_fishing && Catchable) {
|
||||||
if (_fishingTime <= MaxTime) {
|
if (_fishingTime <= MaxTime) {
|
||||||
_messageView.SendMessage("Caught!", 1.5f);
|
_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++;
|
_fishedThisDay++;
|
||||||
} else {
|
} else {
|
||||||
_messageView.SendMessage("Failed to catch the fish! You were too slow!", 1.5f);
|
_messageView.SendMessage("Failed to catch the fish! You were too slow!", 1.5f);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Inventory : ItemStorage {
|
public class Inventory : ElementStorage<Item> {
|
||||||
#region Singleton
|
#region Singleton
|
||||||
|
|
||||||
public static Inventory instance;
|
public static Inventory instance;
|
||||||
|
|
@ -22,25 +22,25 @@ public class Inventory : ItemStorage {
|
||||||
/**
|
/**
|
||||||
* Adds the specified amount of items to the Inventory
|
* Adds the specified amount of items to the Inventory
|
||||||
*/
|
*/
|
||||||
public override void AddItem(Item item, int amount) {
|
public override void AddElement(Item item, int amount) {
|
||||||
if (items.Count >= _InventorySpace) {
|
if (elements.Count >= _InventorySpace) {
|
||||||
Debug.Log("Not enough inventory space!");
|
Debug.Log("Not enough inventory space!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Sell overflowing Items
|
// Sell overflowing Items
|
||||||
if (items.ContainsKey(item) && items[item] + amount >= _MaxItemStack) {
|
if (elements.ContainsKey(item) && elements[item] + amount >= _MaxItemStack) {
|
||||||
SellItem(item, amount - (_MaxItemStack - items[item]));
|
SellItem(item, amount - (_MaxItemStack - elements[item]));
|
||||||
amount = _MaxItemStack - items[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) {
|
public override void RemoveElement(Item item, int amount) {
|
||||||
base.RemoveItem(item, amount);
|
base.RemoveElement(item, amount);
|
||||||
if (!items.ContainsKey(item) && PlayerController.instance.SelectedItem == item) {
|
if (!elements.ContainsKey(item) && PlayerController.instance.SelectedItem == item) {
|
||||||
PlayerController.instance.DeselectItem();
|
PlayerController.instance.DeselectItem();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -50,7 +50,7 @@ public class Inventory : ItemStorage {
|
||||||
*/
|
*/
|
||||||
public void SellItem(Item item, int amount) {
|
public void SellItem(Item item, int amount) {
|
||||||
PlayerController.instance.ChangeMoney(item.SellPrice);
|
PlayerController.instance.ChangeMoney(item.SellPrice);
|
||||||
Shop.instance.AddItem(item, amount);
|
Shop.instance.AddElement(item, amount);
|
||||||
RemoveItem(item, amount);
|
RemoveElement(item, amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,7 +2,7 @@ using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.EventSystems;
|
using UnityEngine.EventSystems;
|
||||||
|
|
||||||
public class InventorySlot : ItemStorageSlot, IPointerClickHandler {
|
public class InventorySlot : ElementStorageSlot<Item>, IPointerClickHandler {
|
||||||
private Inventory _inventory;
|
private Inventory _inventory;
|
||||||
private PlayerController _playerController;
|
private PlayerController _playerController;
|
||||||
|
|
||||||
|
|
@ -14,10 +14,10 @@ public class InventorySlot : ItemStorageSlot, IPointerClickHandler {
|
||||||
/**
|
/**
|
||||||
* Gets called when the Inventory Slot is clicked
|
* Gets called when the Inventory Slot is clicked
|
||||||
*/
|
*/
|
||||||
public override void UseItem() {
|
public override void UseElement() {
|
||||||
if(Item) {
|
if(Element) {
|
||||||
if(Item.GetType() == typeof(UsableItem)) {
|
if(Element.GetType() == typeof(UsableItem)) {
|
||||||
((UsableItem)Item).Select();
|
((UsableItem)Element).Select();
|
||||||
//Debug.Log("using " + Item.displayName);
|
//Debug.Log("using " + Item.displayName);
|
||||||
} else {
|
} else {
|
||||||
//Debug.Log("Item not usable " + Item.displayName);
|
//Debug.Log("Item not usable " + Item.displayName);
|
||||||
|
|
@ -33,8 +33,8 @@ public class InventorySlot : ItemStorageSlot, IPointerClickHandler {
|
||||||
public void OnPointerClick(PointerEventData eventData) {
|
public void OnPointerClick(PointerEventData eventData) {
|
||||||
// When clicked on with right Mouse Button sell the Item
|
// When clicked on with right Mouse Button sell the Item
|
||||||
if(eventData.button == PointerEventData.InputButton.Right) {
|
if(eventData.button == PointerEventData.InputButton.Right) {
|
||||||
if(Item) {
|
if(Element) {
|
||||||
_inventory.SellItem(Item, 1); //TODO: wie machen mehr als 1 verkaufen?!
|
_inventory.SellItem(Element, 1); //TODO: wie machen mehr als 1 verkaufen?!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ public class InventoryUI : MonoBehaviour {
|
||||||
private void Start() {
|
private void Start() {
|
||||||
// Get Inventory instance and add UpdateUI method to OnItemChanged delegate
|
// Get Inventory instance and add UpdateUI method to OnItemChanged delegate
|
||||||
_inventory = Inventory.instance;
|
_inventory = Inventory.instance;
|
||||||
_inventory.onItemChangedCallback += UpdateUI;
|
_inventory.onElementChangedCallback += UpdateUI;
|
||||||
|
|
||||||
// Add all InventorySlot GameObjects to _slots and turn off the Inventory UI
|
// Add all InventorySlot GameObjects to _slots and turn off the Inventory UI
|
||||||
_slots = itemsParent.GetComponentsInChildren<InventorySlot>();
|
_slots = itemsParent.GetComponentsInChildren<InventorySlot>();
|
||||||
|
|
@ -38,7 +38,7 @@ public class InventoryUI : MonoBehaviour {
|
||||||
inventoryUI.SetActive(!inventoryUI.activeSelf);
|
inventoryUI.SetActive(!inventoryUI.activeSelf);
|
||||||
HoverManager.instance.HideDescription();
|
HoverManager.instance.HideDescription();
|
||||||
foreach(InventorySlot slot in _slots) {
|
foreach(InventorySlot slot in _slots) {
|
||||||
slot.ChangeItemSelectedSprite(false);
|
slot.ChangeElementSelectedSprite(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,10 +48,10 @@ public class InventoryUI : MonoBehaviour {
|
||||||
private void UpdateUI() {
|
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
|
// 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++) {
|
for(int i = 0; i < _slots.Length; i++) {
|
||||||
if(i < _inventory.items.Count) {
|
if(i < _inventory.elements.Count) {
|
||||||
_slots[i].AddItem(_inventory.items.ElementAt(i).Key);
|
_slots[i].AddElement(_inventory.elements.ElementAt(i).Key);
|
||||||
_slots[i].amountText.text = "" + _inventory.items[_inventory.items.ElementAt(i).Key];
|
_slots[i].amountText.text = "" + _inventory.elements[_inventory.elements.ElementAt(i).Key];
|
||||||
if(_inventory.items[_inventory.items.ElementAt(i).Key] == 1) {
|
if(_inventory.elements[_inventory.elements.ElementAt(i).Key] == 1) {
|
||||||
_slots[i].amountText.text = "";
|
_slots[i].amountText.text = "";
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
public UsableItem SelectedItem {
|
||||||
get => _selectedItem;
|
get => _selectedItem;
|
||||||
set {
|
set {
|
||||||
if(_inventory.items.ContainsKey(value)) {
|
if(_inventory.elements.ContainsKey(value)) {
|
||||||
_selectedItem = value;
|
_selectedItem = value;
|
||||||
Cursor.SetCursor(value.defaultSprite.texture, Vector2.zero, CursorMode.Auto);
|
Cursor.SetCursor(value.defaultSprite.texture, Vector2.zero, CursorMode.Auto);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Shop : ItemStorage {
|
public class Shop : ElementStorage<Item> {
|
||||||
#region Singleton
|
#region Singleton
|
||||||
|
|
||||||
public static Shop instance;
|
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
|
* Calls ItemStorage.RemoveItem() and sets 2 Variables to remember the last bought item
|
||||||
*/
|
*/
|
||||||
public override void RemoveItem(Item item, int amount) {
|
public override void RemoveElement(Item item, int amount) {
|
||||||
base.RemoveItem(item, amount);
|
base.RemoveElement(item, amount);
|
||||||
if(itemWasBought){
|
if(itemWasBought){
|
||||||
_lastBoughtItem = item;
|
_lastBoughtItem = item;
|
||||||
_lastBoughtItemAmount = amount;
|
_lastBoughtItemAmount = amount;
|
||||||
|
|
@ -40,8 +40,8 @@ public class Shop : ItemStorage {
|
||||||
|
|
||||||
if(_lastBoughtItem) {
|
if(_lastBoughtItem) {
|
||||||
_playerController.ChangeMoney(_lastBoughtItem.price);
|
_playerController.ChangeMoney(_lastBoughtItem.price);
|
||||||
_inventory.RemoveItem(_lastBoughtItem, _lastBoughtItemAmount);
|
_inventory.RemoveElement(_lastBoughtItem, _lastBoughtItemAmount);
|
||||||
AddItem(_lastBoughtItem, _lastBoughtItemAmount);
|
AddElement(_lastBoughtItem, _lastBoughtItemAmount);
|
||||||
itemWasBought = false;
|
itemWasBought = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class ShopSlot : ItemStorageSlot {
|
public class ShopSlot : ElementStorageSlot<Item> {
|
||||||
public TextMeshProUGUI nameText;
|
public TextMeshProUGUI nameText;
|
||||||
public TextMeshProUGUI costText;
|
public TextMeshProUGUI costText;
|
||||||
|
|
||||||
|
|
@ -29,23 +29,23 @@ public class ShopSlot : ItemStorageSlot {
|
||||||
/**
|
/**
|
||||||
* Gets called when the Shop Slot is clicked
|
* Gets called when the Shop Slot is clicked
|
||||||
*/
|
*/
|
||||||
public override void UseItem() {
|
public override void UseElement() {
|
||||||
if(Item) {
|
if(Element) {
|
||||||
if(_playerController.Money >= Item.price) {
|
if(_playerController.Money >= Element.price) {
|
||||||
if(Item) {
|
if(Element) {
|
||||||
_playerController.ChangeMoney(-Item.price);
|
_playerController.ChangeMoney(-Element.price);
|
||||||
_shop.itemWasBought = true;
|
_shop.itemWasBought = true;
|
||||||
|
|
||||||
Debug.Log("Buying Item: " + Item.displayName);
|
Debug.Log("Buying Item: " + Element.displayName);
|
||||||
}
|
}
|
||||||
_inventory.AddItem(Item, 1);
|
_inventory.AddElement(Element, 1);
|
||||||
_shop.RemoveItem(Item, 1);
|
_shop.RemoveElement(Element, 1);
|
||||||
} else {
|
} else {
|
||||||
Debug.Log("Not enough money to buy item.");
|
Debug.Log("Not enough money to buy item.");
|
||||||
}
|
}
|
||||||
|
|
||||||
_shop.onItemChangedCallback?.Invoke();
|
_shop.onElementChangedCallback?.Invoke();
|
||||||
_inventory.onItemChangedCallback?.Invoke();
|
_inventory.onElementChangedCallback?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ public class ShopUI : MonoBehaviour {
|
||||||
private void Start() {
|
private void Start() {
|
||||||
// Get Shop instance and add UpdateUI method to OnItemChanged delegate
|
// Get Shop instance and add UpdateUI method to OnItemChanged delegate
|
||||||
_shop = Shop.instance;
|
_shop = Shop.instance;
|
||||||
_shop.onItemChangedCallback += UpdateUI;
|
_shop.onElementChangedCallback += UpdateUI;
|
||||||
|
|
||||||
// Add all ShopSlot GameObjects to _slots and turn off the Shop UI
|
// Add all ShopSlot GameObjects to _slots and turn off the Shop UI
|
||||||
_slots = itemsParent.GetComponentsInChildren<ShopSlot>();
|
_slots = itemsParent.GetComponentsInChildren<ShopSlot>();
|
||||||
|
|
@ -46,11 +46,11 @@ public class ShopUI : MonoBehaviour {
|
||||||
private void UpdateUI() {
|
private void UpdateUI() {
|
||||||
// Add all items to the correct slots and clear the ones where no item should be
|
// Add all items to the correct slots and clear the ones where no item should be
|
||||||
for (int i = 0; i < _slots.Length; i++) {
|
for (int i = 0; i < _slots.Length; i++) {
|
||||||
if (i < _shop.items.Count) {
|
if (i < _shop.elements.Count) {
|
||||||
_slots[i].AddItem(_shop.items.ElementAt(i).Key);
|
_slots[i].AddElement(_shop.elements.ElementAt(i).Key);
|
||||||
_slots[i].nameText.text = _slots[i].Item.displayName;
|
_slots[i].nameText.text = _slots[i].Element.displayName;
|
||||||
_slots[i].costText.text = _slots[i].Item.price + " µ";
|
_slots[i].costText.text = _slots[i].Element.price + " µ";
|
||||||
_slots[i].amountText.text = _shop.items[_shop.items.ElementAt(i).Key] + " #";
|
_slots[i].amountText.text = _shop.elements[_shop.elements.ElementAt(i).Key] + " #";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_slots[i].ClearSlot();
|
_slots[i].ClearSlot();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue