Skip to content
Snippets Groups Projects
Commit 01979b13 authored by Robin Halseth Sandvik's avatar Robin Halseth Sandvik
Browse files

Minor refactor & code clean up.

- Removed redundant position reset system in ItemPref.
- Created `ResetPosition()` & `Place()` to reduce repetition.
- Added more comments to the code.
- Small reformatting of code for readability.
parent aa32bcf5
No related branches found
No related tags found
1 merge request!53ToolTips, new ability parameter system & misc clean ups.
......@@ -30,84 +30,73 @@ namespace BigSock.UI
// Get the tooltip child-component if it's not set in the prefab.
toolTip ??= transform.Find("ToolTip")?.GetComponent<TextMeshProUGUI>();
}
public void GenerateInv()
{
/*
Generates the GUI elements for the player's inventory.
*/
public void GenerateInv() {
if(player == null) return;
var inventory = player.Inventory;
for (int i = 0; i < inventory.Backpack.Count; ++i)
{
var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridBackPack.transform);
var invScript = invSlot.GetComponent<ItemSlot>();
invScript.inventory = inventory;
invScript.inventoryType = InventoryType.Backpack;
invScript.position = i;
var item = inventory.Backpack[i];
if (item != null)
{
var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform);
var itemScript = itemPref.GetComponent<ItemPref>();
itemScript.item = item;
itemScript.itemSlot = invScript;
invScript.item = itemScript;
itemScript.transform.position = invScript.transform.position;
}
for (int i = 0; i < inventory.Backpack.Count; ++i) {
var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridBackPack.transform);
var invScript = invSlot.GetComponent<ItemSlot>();
invScript.inventory = inventory;
invScript.inventoryType = InventoryType.Backpack;
invScript.position = i;
var item = inventory.Backpack[i];
if (item != null) {
var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform);
var itemScript = itemPref.GetComponent<ItemPref>();
itemScript.item = item;
itemScript.Place(invScript);
}
}
for (int i = 0; i < inventory.Tools.Count; ++i)
{
var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridTools.transform);
var invScript = invSlot.GetComponent<ItemSlot>();
invScript.inventory = inventory;
invScript.inventoryType = InventoryType.Tool;
invScript.position = i;
var item = inventory.Tools[i];
if (item != null)
{
var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform);
var itemScript = itemPref.GetComponent<ItemPref>();
itemScript.item = item;
itemScript.itemSlot = invScript;
invScript.item = itemScript;
itemScript.transform.position = invScript.transform.position;
}
for (int i = 0; i < inventory.Tools.Count; ++i) {
var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridTools.transform);
var invScript = invSlot.GetComponent<ItemSlot>();
invScript.inventory = inventory;
invScript.inventoryType = InventoryType.Tool;
invScript.position = i;
var item = inventory.Tools[i];
if (item != null) {
var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform);
var itemScript = itemPref.GetComponent<ItemPref>();
itemScript.item = item;
itemScript.Place(invScript);
}
}
for (int i = 0; i < inventory.Equipment.Count; ++i)
{
var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridEquipment.transform);
var invScript = invSlot.GetComponent<ItemSlot>();
invScript.inventory = inventory;
invScript.inventoryType = InventoryType.Equipment;
invScript.position = i;
var item = inventory.Equipment[i];
if (item != null)
{
var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform);
var itemScript = itemPref.GetComponent<ItemPref>();
itemScript.item = item;
itemScript.itemSlot = invScript;
invScript.item = itemScript;
itemScript.transform.position = invScript.transform.position;
}
for (int i = 0; i < inventory.Equipment.Count; ++i) {
var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridEquipment.transform);
var invScript = invSlot.GetComponent<ItemSlot>();
invScript.inventory = inventory;
invScript.inventoryType = InventoryType.Equipment;
invScript.position = i;
var item = inventory.Equipment[i];
if (item != null) {
var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform);
var itemScript = itemPref.GetComponent<ItemPref>();
itemScript.item = item;
itemScript.Place(invScript);
}
}
for (int i = 0; i < inventory.Accessories.Count; ++i)
{
var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridAccessory.transform);
var invScript = invSlot.GetComponent<ItemSlot>();
invScript.inventory = inventory;
invScript.inventoryType = InventoryType.Accessory;
invScript.position = i;
var item = inventory.Accessories[i];
if (item != null)
{
var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform);
var itemScript = itemPref.GetComponent<ItemPref>();
itemScript.item = item;
itemScript.itemSlot = invScript;
invScript.item = itemScript;
itemScript.transform.position = invScript.transform.position;
}
for (int i = 0; i < inventory.Accessories.Count; ++i) {
var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridAccessory.transform);
var invScript = invSlot.GetComponent<ItemSlot>();
invScript.inventory = inventory;
invScript.inventoryType = InventoryType.Accessory;
invScript.position = i;
var item = inventory.Accessories[i];
if (item != null) {
var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform);
var itemScript = itemPref.GetComponent<ItemPref>();
itemScript.item = item;
itemScript.Place(invScript);
}
}
}
......@@ -117,8 +106,6 @@ namespace BigSock.UI
(Current version is just a basic test of the concept)
*/
public void SetToolTip(string text) {
//Debug.Log($"[InventoryPanel.SetToolTip()]");
toolTip?.SetText(text);
/*
......
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
......@@ -15,43 +16,65 @@ namespace BigSock.UI
public ItemSlot itemSlot;
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
public Vector2? position;
void Start()
{
void Start() {
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
// Change the sprite if the item has one.
var sprite = item?.Icon;
if(sprite != null)
GetComponent<UnityEngine.UI.Image>().overrideSprite = sprite;
}
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
private void Awake() {
}
/*
Moves the item to be centered on it's ItemSlot
*/
public void ResetPosition() {
if(itemSlot != null) transform.position = itemSlot.transform.position;
}
public void OnBeginDrag(PointerEventData eventData)
{
/*
Place this item in the given slot.
Only does GUI stuff here, inventory code is handled elsewhere.
*/
public void Place(ItemSlot slot) {
// Cannot pass it null.
if(slot == null) throw new ArgumentNullException(nameof(slot));
// Inform our current slot that we've moved.
if(itemSlot != null) itemSlot.item = null;
position = transform.position;
itemSlot = slot;
slot.item = this;
ResetPosition();
}
/*
When the item is getting dragged, make it semi-transparent.
*/
public void OnBeginDrag(PointerEventData eventData) {
canvasGroup.alpha = .6f;
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
/*
Update its position while it's getting dragged.
*/
public void OnDrag(PointerEventData eventData) {
rectTransform.anchoredPosition += eventData.delta;
}
public void OnEndDrag(PointerEventData eventData)
{
if (position != null)
{
transform.position = position.Value;
}
/*
When the item has been let go, make it opaque and center it on its slot.
*/
public void OnEndDrag(PointerEventData eventData) {
ResetPosition();
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
......@@ -61,18 +84,15 @@ namespace BigSock.UI
}
/*
When the user clicks on this box, display the tooltip.
(OnPointerClick only triggers if you press and release on the same object)
*/
public void OnPointerClick(PointerEventData eventData) {
//Debug.Log($"[ItemPref.OnPointerClick()] button: {eventData.button}, dragging: {eventData.dragging}");
if(itemSlot != null && item != null)
itemSlot.DisplayToolTip();
}
}
}
......@@ -9,7 +9,7 @@ using BigSock.Item;
namespace BigSock.UI
{
public class ItemSlot : MonoBehaviour, IDropHandler, IPointerClickHandler
public class ItemSlot : MonoBehaviour, IDropHandler
{
public InventoryType inventoryType;
public int position;
......@@ -34,9 +34,7 @@ namespace BigSock.UI
transform.hasChanged = false;
firstRan = true;
if(item != null)
item.transform.position = transform.position;
item?.ResetPosition();
}
}
......@@ -58,14 +56,7 @@ namespace BigSock.UI
);
// If we successfully moved the item in the inventory: update the gui.
if (didMove) {
itemPref.itemSlot.item = null; // Remove this from the other slot.
itemPref.itemSlot = this; // Make this its new slot.
item = itemPref; // Make it the item in this slot.
// Place the item centered over this slot.
eventData.pointerDrag.transform.position = transform.position;
itemPref.position = null;
}
if (didMove) itemPref.Place(this);
/*
Notes:
......@@ -75,17 +66,6 @@ namespace BigSock.UI
*/
}
/*
When the user clicks on this box, display the tooltip.
(OnPointerClick only triggers if you press and release on the same objectS)
*/
public void OnPointerClick(PointerEventData eventData) {
Debug.Log($"[ItemSlot.OnPointerClick()] button: {eventData.button}, dragging: {eventData.dragging}");
//DisplayToolTip();
}
/*
Display the tooltip for our item onto the screen.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment