This commit is contained in:
Janis M
2022-02-22 10:46:31 +01:00
parent 5dfbaf3f30
commit 94654c714a
4 changed files with 50 additions and 26 deletions

View File

@@ -24,9 +24,9 @@ public class Inventory : MonoBehaviour
public Item testItemType; public Item testItemType;
public static Action OnInventoryChanged; public static Action OnPlayerInventoryChanged;
public static Action OnItemAdded; public static Action OnPlayerItemAdded;
public static Action OnItemRemoved; public static Action OnPlayerItemRemoved;
[Header("Inventory")] [Header("Inventory")]
[SerializeField] [SerializeField]
@@ -35,16 +35,22 @@ public class Inventory : MonoBehaviour
[SerializeField] [SerializeField]
int maxSpaceOfSlots = 10; int maxSpaceOfSlots = 10;
[SerializeField]
bool isPlayerInventory = true;
[SerializeField] [SerializeField]
List<Slot> inventory = new List<Slot>(); List<Slot> inventory = new List<Slot>();
public List<Slot> GetInventory() => inventory;
public void AddSlots(int _numberOfSlots) public void AddSlots(int _numberOfSlots)
{ {
for (int i = 0; i < _numberOfSlots; i++) for (int i = 0; i < _numberOfSlots; i++)
{ {
inventory.Add(new Slot()); inventory.Add(new Slot());
} }
OnInventoryChanged?.Invoke(); if(isPlayerInventory)
OnPlayerInventoryChanged?.Invoke();
} }
Slot GetEmptySlot() Slot GetEmptySlot()
{ {
@@ -143,8 +149,13 @@ public class Inventory : MonoBehaviour
rest = count - slot.GetCount(); rest = count - slot.GetCount();
slot.Clear(); slot.Clear();
} }
OnInventoryChanged?.Invoke();
OnItemRemoved?.Invoke(); if (isPlayerInventory)
{
OnPlayerInventoryChanged?.Invoke();
OnPlayerItemRemoved?.Invoke();
}
return rest; return rest;
} }
else else
@@ -204,8 +215,12 @@ public class Inventory : MonoBehaviour
else else
return -1; return -1;
OnInventoryChanged?.Invoke(); if (isPlayerInventory)
OnItemAdded?.Invoke(); {
OnPlayerInventoryChanged?.Invoke();
OnPlayerItemAdded?.Invoke();
}
return rest; return rest;
} }

View File

@@ -13,6 +13,7 @@ public class Slot
int count = 0; int count = 0;
public Item GetItem() => item; public Item GetItem() => item;
public void SetItem(Item _item) => item = _item;
public int GetCount() => count; public int GetCount() => count;
@@ -38,7 +39,14 @@ public class Slot
item = null; item = null;
count = 0; count = 0;
} }
public Slot Copy()
{
Slot slot = new Slot();
slot.SetCount(count);
slot.SetItem(item);
return slot;
}
public void Set(Item _item, int _count = 1) public void Set(Item _item, int _count = 1)
{ {
item = _item; item = _item;

View File

@@ -6,14 +6,15 @@ using UnityEngine.UI;
public class UI_Inventory : MonoBehaviour public class UI_Inventory : MonoBehaviour
{ {
/* Inventory playerInventory; [SerializeField]
Inventory inventory;
public Transform inventoryContainer; public Transform inventoryContainer;
public Transform itemSlotTemplate; public Transform itemSlotTemplate;
private RectTransform[] UIItemSlots; private RectTransform[] UIItemSlots;
private Button[] buttons; private Button[] buttons;
private Slot pickedItem = new Slot(); private Slot pickedItem = new Slot();
private RectTransform pickedItemSlot; private RectTransform pickedItemSlot;
private void Awake() private void Start()
{ {
//itemSlotTemplate = inventoryContainer.Find("containerTemplate"); //itemSlotTemplate = inventoryContainer.Find("containerTemplate");
pickedItemSlot = Instantiate(itemSlotTemplate, this.transform).GetComponent<RectTransform>(); pickedItemSlot = Instantiate(itemSlotTemplate, this.transform).GetComponent<RectTransform>();
@@ -21,8 +22,8 @@ public class UI_Inventory : MonoBehaviour
void pickItem(int index) void pickItem(int index)
{ {
pickedItem = playerInventory.getInventory[index].copy(); pickedItem = inventory.GetInventory()[index].Copy();
playerInventory.getInventory[index].clear(); inventory.GetInventory()[index].Clear();
UIItemSlots[index].GetChild(0).Find("Icon").gameObject.SetActive(false); UIItemSlots[index].GetChild(0).Find("Icon").gameObject.SetActive(false);
} }
void placeItem() void placeItem()
@@ -31,18 +32,18 @@ public class UI_Inventory : MonoBehaviour
} }
public void buttonEvent(int index) public void buttonEvent(int index)
{ {
if (pickedItem.ItemType == null && playerInventory.getInventory[index].ItemType != null) if (pickedItem.GetItem() == null && inventory.GetInventory()[index].GetItem() != null)
{ {
pickItem(index); pickItem(index);
} }
else else
{ {
if (pickedItem.ItemType != null) if (pickedItem.GetItem() != null)
{ {
int rest = playerInventory.addItemAt(index, pickedItem.ItemType, pickedItem.Count); int rest = inventory.Add(pickedItem.GetItem(), pickedItem.GetCount(), index);
if (rest == 0) if (rest == 0)
{ {
pickedItem.clear(); pickedItem.Clear();
} }
else else
{ {
@@ -57,7 +58,6 @@ public class UI_Inventory : MonoBehaviour
} }
public void setInventory(Inventory inventory) public void setInventory(Inventory inventory)
{ {
playerInventory = inventory;
inventory.onItemChangedCallback += updateInventory; inventory.onItemChangedCallback += updateInventory;
updateInventory(); updateInventory();
} }
@@ -98,10 +98,10 @@ public class UI_Inventory : MonoBehaviour
} }
} }
UIItemSlots = new RectTransform[playerInventory.getInventory.Count]; UIItemSlots = new RectTransform[inventory.Count];
buttons = new Button[playerInventory.getInventory.Count]; buttons = new Button[inventory.Count];
//inventoryContainer.GetComponent<RectTransform>(). //inventoryContainer.GetComponent<RectTransform>().
foreach (Slot slot in playerInventory.getInventory) foreach (Slot slot in inventory)
{ {
RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, inventoryContainer).GetComponent<RectTransform>(); RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, inventoryContainer).GetComponent<RectTransform>();
buttons[index] = itemSlotRectTransform.Find("Button").GetComponent<Button>(); buttons[index] = itemSlotRectTransform.Find("Button").GetComponent<Button>();
@@ -111,16 +111,16 @@ public class UI_Inventory : MonoBehaviour
itemSlotRectTransform.gameObject.SetActive(true); itemSlotRectTransform.gameObject.SetActive(true);
itemSlotRectTransform.gameObject.name = "ItemSlot" + index; itemSlotRectTransform.gameObject.name = "ItemSlot" + index;
if (slot.ItemType != null) if (slot.GetItem() != null)
{ {
Transform item = itemSlotRectTransform.GetChild(0).Find("Icon"); Transform item = itemSlotRectTransform.GetChild(0).Find("Icon");
Transform itemCount = itemSlotRectTransform.GetChild(0).Find("Count"); Transform itemCount = itemSlotRectTransform.GetChild(0).Find("Count");
item.gameObject.SetActive(true); item.gameObject.SetActive(true);
item.GetComponent<Image>().sprite = slot.ItemType.sprite; item.GetComponent<Image>().sprite = slot.GetItem().sprite;
if (slot.Count > 1) if (slot.GetCount() > 1)
{ {
itemCount.gameObject.SetActive(true); itemCount.gameObject.SetActive(true);
itemCount.GetComponent<TextMeshProUGUI>().text = slot.Count.ToString(); itemCount.GetComponent<TextMeshProUGUI>().text = slot.GetCount().ToString();
} }
} }
x++; x++;
@@ -132,5 +132,5 @@ public class UI_Inventory : MonoBehaviour
UIItemSlots[index] = itemSlotRectTransform; UIItemSlots[index] = itemSlotRectTransform;
index++; index++;
} }
}*/ }
} }

View File

@@ -12,10 +12,11 @@ public class TimeUI : MonoBehaviour
private void Start() private void Start()
{ {
timeManager = GameObject.Find("GameManager").GetComponent<TimeManager>(); timeManager = GameObject.Find("GameManager").GetComponent<TimeManager>();
TimeManager.OnTimeInterval += OnInterval;
} }
private void Update() void OnInterval()
{ {
DateTime dateTime = timeManager.GetDateTime(); DateTime dateTime = timeManager.GetDateTime();
if (dateTimeText != null) if (dateTimeText != null)