This commit is contained in:
Janis
2023-03-01 22:28:06 +01:00
parent 36337c8a72
commit ae0b21f9e9
19 changed files with 600 additions and 455 deletions

View File

@@ -1,33 +1,33 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
// using UnityEngine;
// using UnityEngine.EventSystems;
// using UnityEngine.UI;
public class DraggableSlotContentUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public int slotIndex; // slotIndex is set by SlotUI
[HideInInspector] public Transform parentAfterDrag;
public Image image;
public void OnBeginDrag(PointerEventData eventData)
{
parentAfterDrag = transform.parent;
slotIndex = parentAfterDrag.GetComponent<SlotUI>().slotIndex; // Set slotIndex again to be safe, but should be already set
transform.SetParent(transform.root);
transform.SetAsLastSibling();
image.raycastTarget = false;
}
// public class DraggableSlotContentUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
// {
// public int slotIndex; // slotIndex is set by SlotUI
// [HideInInspector] public Transform parentAfterDrag;
// public Image image;
// public void OnBeginDrag(PointerEventData eventData)
// {
// parentAfterDrag = transform.parent;
// slotIndex = parentAfterDrag.GetComponent<SlotUI>().slotIndex; // Set slotIndex again to be safe, but should be already set
// transform.SetParent(transform.root);
// transform.SetAsLastSibling();
// image.raycastTarget = false;
// }
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragging");
transform.position = Input.mousePosition;
// public void OnDrag(PointerEventData eventData)
// {
// Debug.Log("Dragging");
// transform.position = Input.mousePosition;
}
// }
public void OnEndDrag(PointerEventData eventData)
{
transform.SetParent(parentAfterDrag);
image.raycastTarget = true;
// invoke
Inventory.OnPlayerInventoryChanged?.Invoke();
}
}
// public void OnEndDrag(PointerEventData eventData)
// {
// transform.SetParent(parentAfterDrag);
// image.raycastTarget = true;
// // invoke
// Inventory.OnPlayerInventoryChanged?.Invoke();
// }
// }

View File

@@ -1,56 +1,56 @@
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class HotbarSlotUI : MonoBehaviour
{
public int slotIndex;
// 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 PlayerController playerController;
private InventoryItem invItem;
// private InventoryItem invItem;
[SerializeField] private Image border;
[SerializeField] private Image image;
[SerializeField] private TextMeshProUGUI quantityText;
// [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 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);
}
}
// private void CheckSelectedSlot()
// {
// if (playerController.activeHotbarSlot == slotIndex)
// {
// border.gameObject.SetActive(true);
// }
// else
// {
// border.gameObject.SetActive(false);
// }
// }
public void SetInventoryItem(InventoryItem newInvItem)
{
invItem = newInvItem;
UpdateSlot();
}
// 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;
}
}
}
// 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

@@ -1,52 +1,52 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
// 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;
// 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();
}
// 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()
{
}
// 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 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);
}
}
// private void UpdateSlots()
// {
// for (int i = 0; i < slots.Count; i++)
// {
// slots[i].SetInventoryItem(playerInventory.items[i] ?? null);
// }
// }
}
// }

View File

@@ -1,53 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// using System.Collections;
// using System.Collections.Generic;
// using UnityEngine;
public class InventoryUI : GuiPanel
{
[SerializeField] private GameObject slotPrefab;
[SerializeField] private GameObject slotListObj;
[SerializeField] private List<SlotUI> slotUIList;
// public class InventoryUI : GuiPanel
// {
// [SerializeField] private GameObject slotPrefab;
// [SerializeField] private GameObject slotListObj;
// [SerializeField] private List<SlotUI> slotUIList;
private Inventory playerInventory;
// private Inventory playerInventory;
private void Start()
{
Inventory.OnPlayerInventoryChanged += UpdateSlots;
}
public override void OnOpen()
{
if (slotUIList.Count == 0)
{
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
CreateSlots();
}
UpdateSlots();
GuiManager.Instance.ClosePanel("Hotbar");
}
// private void Start()
// {
// Inventory.OnPlayerInventoryChanged += UpdateSlots;
// }
// public override void OnOpen()
// {
// if (slotUIList.Count == 0)
// {
// playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
// CreateSlots();
// }
// UpdateSlots();
// GuiManager.Instance.ClosePanel("Hotbar");
// }
public override void OnClose()
{
GuiManager.Instance.OpenPanel("Hotbar");
}
private void CreateSlots()
{
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
// public override void OnClose()
// {
// GuiManager.Instance.OpenPanel("Hotbar");
// }
// private void CreateSlots()
// {
// playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
for (int i = 0; i < playerInventory.maxSlots; i++)
{
SlotUI newSlot = Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>();
newSlot.slotIndex = i;
slotUIList.Add(newSlot);
}
}
private void UpdateSlots()
{
int i = 0;
foreach (SlotUI slotUi in slotUIList)
{
slotUi.SetInventoryItem(playerInventory.items[i]);
i++;
}
}
// for (int i = 0; i < playerInventory.maxSlots; i++)
// {
// SlotUI newSlot = Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>();
// newSlot.slotIndex = i;
// slotUIList.Add(newSlot);
// }
// }
// private void UpdateSlots()
// {
// int i = 0;
// foreach (SlotUI slotUi in slotUIList)
// {
// slotUi.SetInventoryItem(playerInventory.items[i]);
// i++;
// }
// }
}
// }

View File

@@ -1,60 +1,60 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class SlotUI : MonoBehaviour, IDropHandler
{
public int slotIndex;
[SerializeField] private GameObject contentObject;
[SerializeField] private InventoryItem inventoryItem;
[SerializeField] private Image itemSpriteImage;
[SerializeField] private TMPro.TextMeshProUGUI quantityText;
// using System.Collections;
// using System.Collections.Generic;
// using UnityEngine;
// using UnityEngine.UI;
// using UnityEngine.EventSystems;
// public class SlotUI : MonoBehaviour, IDropHandler
// {
// public int slotIndex;
// [SerializeField] private GameObject contentObject;
// [SerializeField] private InventoryItem inventoryItem;
// [SerializeField] private Image itemSpriteImage;
// [SerializeField] private TMPro.TextMeshProUGUI quantityText;
private Inventory playerInventory;
private void Start()
{
UpdateSlot();
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
}
public void SetInventoryItem(InventoryItem newInventoryItem)
{
inventoryItem = newInventoryItem;
UpdateSlot();
}
// private Inventory playerInventory;
// private void Start()
// {
// UpdateSlot();
// playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
// }
// public void SetInventoryItem(InventoryItem newInventoryItem)
// {
// inventoryItem = newInventoryItem;
// UpdateSlot();
// }
private void UpdateSlot()
{
if (inventoryItem != null)
{
itemSpriteImage.sprite = inventoryItem?.item?.sprite ?? null;
if (itemSpriteImage.sprite == null)
{
itemSpriteImage.color = new Color(0, 0, 0, 0);
}
else
{
itemSpriteImage.color = new Color(255, 255, 255, 1);
}
// private void UpdateSlot()
// {
// if (inventoryItem != null)
// {
// itemSpriteImage.sprite = inventoryItem?.item?.sprite ?? null;
// if (itemSpriteImage.sprite == null)
// {
// itemSpriteImage.color = new Color(0, 0, 0, 0);
// }
// else
// {
// itemSpriteImage.color = new Color(255, 255, 255, 1);
// }
quantityText.SetText(inventoryItem.count != 0 ? inventoryItem.count.ToString() : "");
}
else
{
itemSpriteImage.sprite = null;
quantityText.SetText("");
itemSpriteImage.color = new Color(0, 0, 0, 0);
}
// quantityText.SetText(inventoryItem.count != 0 ? inventoryItem.count.ToString() : "");
// }
// else
// {
// itemSpriteImage.sprite = null;
// quantityText.SetText("");
// itemSpriteImage.color = new Color(0, 0, 0, 0);
// }
}
// }
public void OnDrop(PointerEventData eventData)
{
Debug.Log("DROP");
DraggableSlotContentUI dropped = eventData.pointerDrag.GetComponent<DraggableSlotContentUI>();
playerInventory.SwapItems(dropped.slotIndex, slotIndex);
}
// public void OnDrop(PointerEventData eventData)
// {
// Debug.Log("DROP");
// DraggableSlotContentUI dropped = eventData.pointerDrag.GetComponent<DraggableSlotContentUI>();
// playerInventory.SwapItems(dropped.slotIndex, slotIndex);
// }
}
// }