diff --git a/MrBigsock/Assets/Code/Services/ItemService.cs b/MrBigsock/Assets/Code/Services/ItemService.cs index 09b98105c1080cf4c5e9257cfa4ef5d4b3a145fd..8a196ad5653e0b6b2b6962dd0bfc2aa975958d02 100644 --- a/MrBigsock/Assets/Code/Services/ItemService.cs +++ b/MrBigsock/Assets/Code/Services/ItemService.cs @@ -38,13 +38,12 @@ namespace BigSock.Service { } public List<IItem> Get3Random() { - List<IItem> tempItemList = _itemList; List<IItem> returnList = new List<IItem>(); - var num = 0; - for(int i = 0; i < 3; i++) { - num = _rnd.Next(tempItemList.Count); - returnList.Add(tempItemList[num]); - tempItemList.Remove(tempItemList[num]); + while (returnList.Count < 3) { + IItem random = _itemList[_rnd.Next(_itemList.Count)]; + if(!returnList.Contains(random)) { + returnList.Add(random); + } } return returnList; } diff --git a/MrBigsock/Assets/Code/UI/ChestDisplay.cs b/MrBigsock/Assets/Code/UI/ChestDisplay.cs index 5fde0a2660f68a99f328aa2a1abf051da55f1ff1..d7a0ce6b859c6f0ed108e7ab59e9ab27a23f0fd9 100644 --- a/MrBigsock/Assets/Code/UI/ChestDisplay.cs +++ b/MrBigsock/Assets/Code/UI/ChestDisplay.cs @@ -13,62 +13,53 @@ using UnityEditor; namespace BigSock.UI { public class ChestDisplay : MonoBehaviour { - public Button item1Button, item2Button, item3Button, closeButton; - - public IItem item1, item2, item3; + public Button closeButton; public List<IItem> items; - + public List<Button> buttons; public GameObject other; - public GameObject chest; PlayerController player; - // Update is called once per frame - void Start() { var itemButtonLocation = transform.Find("ItemBox").Find("ItemBoxBackground"); + buttons = new List<Button>(); - item1Button = itemButtonLocation.transform.Find("ButtonItem1").GetComponent<Button>(); - item1Button.onClick.AddListener(ItemPicked1); - - item2Button = itemButtonLocation.transform.Find("ButtonItem2").GetComponent<Button>(); - item2Button.onClick.AddListener(ItemPicked2); - - item3Button = itemButtonLocation.transform.Find("ButtonItem3").GetComponent<Button>(); - item3Button.onClick.AddListener(ItemPicked3); + // Gets button gameobjects and adds onClick listener + for (int i = 0; i < 3; i++) { + var button = itemButtonLocation.transform.Find("ButtonItem"+(i+1).ToString()).GetComponent<Button>(); + button.onClick.AddListener(delegate {ItemPicked(i+1); }); + buttons.Add(button); + } + // The X button :) closeButton = transform.Find("CloseButton").GetComponent<Button>(); closeButton.onClick.AddListener(manuallyCloseWindow); + // list of random items items = chest.transform.GetComponent<Chest>().GetChestItems(); - item1 = items[0]; - item2 = items[1]; - item3 = items[2]; - item1Button.gameObject.transform.Find("Item").GetComponent<Image>().sprite = ItemService.SINGLETON.Get(item1.Id).Icon; - item1Button.gameObject.transform.Find("ItemName").GetComponent<TMPro.TMP_Text>().text = ItemService.SINGLETON.Get(item1.Id).Name; - - item2Button.gameObject.transform.Find("Item").GetComponent<Image>().sprite = ItemService.SINGLETON.Get(item2.Id).Icon; - item2Button.gameObject.transform.Find("ItemName").GetComponent<TMPro.TMP_Text>().text = ItemService.SINGLETON.Get(item2.Id).Name; - - item3Button.gameObject.transform.Find("Item").GetComponent<Image>().sprite = ItemService.SINGLETON.Get(item3.Id).Icon; - item3Button.gameObject.transform.Find("ItemName").GetComponent<TMPro.TMP_Text>().text = ItemService.SINGLETON.Get(item3.Id).Name; + // Sets Item icon and name + for (int i = 0; i < buttons.Count; i++) { + buttons[i].gameObject.transform.Find("Item").GetComponent<Image>().sprite = ItemService.SINGLETON.Get(items[i].Id).Icon; + buttons[i].gameObject.transform.Find("ItemName").GetComponent<TMPro.TMP_Text>().text = ItemService.SINGLETON.Get(items[i].Id).Name; + } this.other = GameObject.FindWithTag("Player"); player = other.GetComponent<PlayerController>(); if (player.Inventory.AddItem(null) == -1) { - item1Button.interactable = false; - item2Button.interactable = false; - item3Button.interactable = false; + for (int i = 0; i < buttons.Count; i++) { + buttons[i].interactable = false; + } transform.Find("Banner").Find("BannerBackground").Find("BannerExtraText").GetComponent<TMPro.TMP_Text>().text = "Inventory is full, free up some space to select an item"; } - } + } + public void ShowItemDescription(GameObject button) { string[] buttonNumber = button.name.Split("ButtonItem"); - button.transform.Find("ItemDescription").GetComponent<TMPro.TMP_Text>().text = items[int.Parse(buttonNumber[1])-1].Description; - button.transform.Find("Item").GetComponent<Image>().color = Color.grey; + button.transform.Find("ItemDescription").GetComponent<TMPro.TMP_Text>().text = items[int.Parse(buttonNumber[1])-1].GetToolTip(); + button.transform.Find("Item").GetComponent<Image>().color = Color.clear; } public void HideItemDescription(GameObject button) { @@ -76,10 +67,6 @@ namespace BigSock.UI { button.transform.Find("Item").GetComponent<Image>().color = Color.white; } - private void OnPointerEnter() { - - } - private void Update() { if (Input.GetKeyDown(KeyCode.Escape)){ //|| Input.GetKeyDown(KeyCode.Mouse1)) { manuallyCloseWindow(); @@ -91,30 +78,15 @@ namespace BigSock.UI { Destroy(this.gameObject); } + // Gets the chest element the display belongs to public void setChestObject(GameObject chestInstance) { chest = chestInstance; } - void showItemDescription() { - - } - - void ItemPicked1() { - ItemPicked(1); - } - - void ItemPicked2() { - ItemPicked(2); - } - - void ItemPicked3() { - ItemPicked(3); - } - void ItemPicked(int number) { - if (number == 1) {player.TryPickUpItem(ItemService.SINGLETON.Get(item1.Id));} - else if (number == 2) {player.TryPickUpItem(ItemService.SINGLETON.Get(item2.Id));} - else if (number == 3) {player.TryPickUpItem(ItemService.SINGLETON.Get(item3.Id));} + if (number == 1) {player.TryPickUpItem(ItemService.SINGLETON.Get(items[number-1].Id));} + else if (number == 2) {player.TryPickUpItem(ItemService.SINGLETON.Get(items[number-1].Id));} + else if (number == 3) {player.TryPickUpItem(ItemService.SINGLETON.Get(items[number-1].Id));} Destroy(this.gameObject); } } diff --git a/MrBigsock/Assets/Prefabs/UI/ChestContents.prefab b/MrBigsock/Assets/Prefabs/UI/ChestContents.prefab index e924aac9d60b2d20c3a8bf9e8d54e10deb9ee305..1fcbe3d96e596d4ed7d868c2724a3c52970504a6 100644 --- a/MrBigsock/Assets/Prefabs/UI/ChestContents.prefab +++ b/MrBigsock/Assets/Prefabs/UI/ChestContents.prefab @@ -370,8 +370,8 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: 0, y: -25} - m_SizeDelta: {x: -40, y: -90} + m_AnchoredPosition: {x: 0, y: -27.5} + m_SizeDelta: {x: -40, y: -95} m_Pivot: {x: 0.5, y: 0.5} --- !u!222 &8214718043214627680 CanvasRenderer: @@ -428,8 +428,8 @@ MonoBehaviour: m_faceColor: serializedVersion: 2 rgba: 4294967295 - m_fontSize: 24 - m_fontSizeBase: 24 + m_fontSize: 16 + m_fontSizeBase: 16 m_fontWeight: 400 m_enableAutoSizing: 0 m_fontSizeMin: 18 @@ -1426,8 +1426,8 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: 0, y: -25} - m_SizeDelta: {x: -40, y: -90} + m_AnchoredPosition: {x: 0, y: -27.5} + m_SizeDelta: {x: -40, y: -95} m_Pivot: {x: 0.5, y: 0.5} --- !u!222 &540216411197347177 CanvasRenderer: @@ -1484,8 +1484,8 @@ MonoBehaviour: m_faceColor: serializedVersion: 2 rgba: 4294967295 - m_fontSize: 24 - m_fontSizeBase: 24 + m_fontSize: 16 + m_fontSizeBase: 16 m_fontWeight: 400 m_enableAutoSizing: 0 m_fontSizeMin: 18 @@ -1637,8 +1637,8 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: 0, y: -25} - m_SizeDelta: {x: -40, y: -90} + m_AnchoredPosition: {x: 0, y: -27.5} + m_SizeDelta: {x: -40, y: -95} m_Pivot: {x: 0.5, y: 0.5} --- !u!222 &2545237751018405972 CanvasRenderer: @@ -1695,8 +1695,8 @@ MonoBehaviour: m_faceColor: serializedVersion: 2 rgba: 4294967295 - m_fontSize: 24 - m_fontSizeBase: 24 + m_fontSize: 16 + m_fontSizeBase: 16 m_fontWeight: 400 m_enableAutoSizing: 0 m_fontSizeMin: 18