This commit is contained in:
Janis
2023-02-22 18:43:55 +01:00
parent 50916358f2
commit 5edcd3625b
17 changed files with 3234 additions and 18 deletions

View File

@@ -1,21 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class PlayerController : MonoBehaviour
{
public static Action OnActiveSlotChanged;
public Item testItem1;
public Item testItem2;
[SerializeField] private int hotbarSlotCount = 9;
[SerializeField] private int activeHotbarSlot = 0;
public int hotbarSlotCount = 9;
public int activeHotbarSlot = 0;
[SerializeField] private Inventory playerInventory;
// Start is called before the first frame update
void Start()
{
playerInventory = GetComponent<Inventory>();
GuiManager.Instance.OpenPanel("Hotbar");
}
// Update is called once per frame
@@ -33,6 +36,7 @@ public class PlayerController : MonoBehaviour
{
activeHotbarSlot = 0;
}
OnActiveSlotChanged?.Invoke();
}
if (Input.GetKeyDown(KeyCode.Tab))

View File

@@ -28,14 +28,13 @@ public class GuiManager : MonoBehaviour
private void Awake()
{
if (instance == null)
if (instance != null && instance != this)
{
instance = this;
}
else
{
Destroy(gameObject);
Destroy(this.gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
}
@@ -65,6 +64,10 @@ public class GuiManager : MonoBehaviour
{
OpenPanel(panel);
}
else
{
Debug.Log("Panel not found: " + name);
}
}
public void OpenPanel(GuiPanel panel)
@@ -73,7 +76,7 @@ public class GuiManager : MonoBehaviour
if (!panel.gameObject.activeSelf)
{
panel.gameObject.SetActive(true);
panel.OnClose();
panel.OnOpen();
Debug.Log("Open Panel");
return;
}

View File

@@ -0,0 +1,56 @@
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class HotbarSlotUI : MonoBehaviour
{
public int slotIndex;
private PlayerController playerController;
private InventoryItem invItem;
[SerializeField] private Image border;
[SerializeField] private Image image;
[SerializeField] private TextMeshProUGUI quantityText;
private void Start()
{
playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
PlayerController.OnActiveSlotChanged += CheckSelectedSlot;
CheckSelectedSlot();
}
private void CheckSelectedSlot()
{
if (playerController.activeHotbarSlot == slotIndex)
{
border.gameObject.SetActive(true);
}
else
{
border.gameObject.SetActive(false);
}
}
public void SetInventoryItem(InventoryItem newInvItem)
{
invItem = newInvItem;
UpdateSlot();
}
private void UpdateSlot()
{
image.sprite = invItem?.item?.sprite ?? null;
quantityText.SetText(invItem?.count.ToString() ?? "");
if (image.sprite == null)
{
image.color = new Color(0, 0, 0, 0);
}
else
{
image.color = Color.white;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 12c5ff2bade265a499f7b2d983c6508c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,52 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HotbarUI : GuiPanel
{
[SerializeField] private GameObject hotbarSlotPrefab;
private List<HotbarSlotUI> slots = new List<HotbarSlotUI>();
private Inventory playerInventory;
private PlayerController playerController;
private int hotbarSlotCount = 9;
private void Start()
{
Inventory.OnPlayerInventoryChanged += UpdateSlots;
}
public override void OnOpen()
{
if (slots.Count == 0)
{
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
hotbarSlotCount = playerController.hotbarSlotCount;
CreateSlots();
}
UpdateSlots();
}
public override void OnClose()
{
}
private void CreateSlots()
{
for (int i = 0; i < hotbarSlotCount; i++)
{
HotbarSlotUI newSlot = Instantiate(hotbarSlotPrefab, Vector3.zero, Quaternion.identity, transform).GetComponent<HotbarSlotUI>();
newSlot.slotIndex = i;
slots.Add(newSlot);
}
}
private void UpdateSlots()
{
for (int i = 0; i < slots.Count; i++)
{
slots[i].SetInventoryItem(playerInventory.items[i] ?? null);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: daff4f23cf5534f478f13df54b51124a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -12,12 +12,15 @@ public class InventoryUI : GuiPanel
private void Start()
{
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
Inventory.OnPlayerInventoryChanged += UpdateSlots;
CreateSlots();
}
public override void OnOpen()
{
if (slotUIList.Count == 0)
{
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
CreateSlots();
}
UpdateSlots();
}