public void UpdateHotbarSelected(Hotbar inventory) { inventory.GetComponentsInChildren <Image>()[previousIndex * 2].color = COLOUR_HOTBAR_DEFAULT; inventory.GetComponentsInChildren <Image>()[hotbarNum * 2].color = COLOUR_HOTBAR_SELECTED; previousIndex = hotbarNum; //Set the held item parent.SetHeldItem(GetHotbarItem(), false); }
public void UpdateHotbarImage(int index, bool itemDropped) { int hotBarIndex = hotbarIndexes[index]; Hotbar hotbar = Object.FindObjectOfType <Hotbar>(); if (hotBarIndex != -1) { Item itemInSlot = inventory[hotBarIndex]; hotbar.GetComponentsInChildren <Image>()[index * 2 + 1].sprite = Resources.Load <Sprite>($"Icons/{itemInSlot.iconName}"); } else { hotbar.GetComponentsInChildren <Image>()[index * 2 + 1].sprite = Resources.Load <Sprite>($"Icons/icon_blank"); } //If the item was inserted into the slot we are holding, pull it out. if (index == hotbarNum) { parent.SetHeldItem(GetHotbarItem(), itemDropped); } }
/// <summary> /// Converts a mouse position into index of the hotbar. /// Returns -1 if the mouse isn't over any hotbar buttons. /// I am going to be lazy with this one and use an inefficient loop rather than maths. /// O(N) instead of O(1) but literally who cares. (I do otherwise I wouldn't be writting this.) /// TODO: This method is awful and needs revising but I will probably never end up doing it :^). /// 17/09/2021: seriously now just use a div function. /// </summary> /// <param name="cursorX"></param> /// <param name="cursorY"></param> /// <returns></returns> public static int CursorPositionToHotbarIndex(Hotbar hotbar, float cursorX, float cursorY) { //aww why :( Image[] cachedImages = hotbar.GetComponentsInChildren <Image>(); for (int i = 0; i < InventorySystem.HOTBAR_SIZE; i++) { Image hotbarCurrent = cachedImages[i * 2]; RectTransform hotbarRectTransform = hotbarCurrent.transform as RectTransform; //Even worse Vector2 bottomLeft = new Vector2(hotbarRectTransform.position.x, hotbarRectTransform.position.y) - new Vector2(50, 50); Vector2 topRight = new Vector2(hotbarRectTransform.position.x, hotbarRectTransform.position.y) + new Vector2(50, 50); if (bottomLeft.x > cursorX || cursorX > topRight.x) { continue; } if (bottomLeft.y > cursorY || cursorY > topRight.y) { continue; } return(i); } return(-1); }