/// <summary> /// Frankensteined version of the priority update method to ensure tagging is able when the server says so /// </summary> public void OnTaggablePlayers() { // In case there were no collidings before. m_MainPanel.gameObject.SetActive(true); m_Switch.gameObject.SetActive(false); if (m_AllGameObjectsInRadius.Count + 1 > 1) { m_ListPanel.gameObject.SetActive(true); m_Switch.gameObject.SetActive(true); } int lengthEnum = Enum.GetValues(typeof(Priority)).Cast <Priority>().Count(); int[] priorityPresence = new int[lengthEnum]; for (int i = 0; i < priorityPresence.Length; i++) { priorityPresence[i] = 0; } // Set temp priority to lowest int tempPriority = 0; int priorityNbr = 0; //loop through all gos in radius string tag; Priority tempP; for (int i = 0; i < m_AllGameObjectsInRadius.Count; i++) { tag = m_AllGameObjectsInRadius[i].tag; if (tag == "Treasure") //todo make switch case { /*Square */ Debug.Log("tag == Treasure"); if (m_AllGameObjectsInRadius[i].GetComponent <Square>().Team == m_Person.Team) { tempP = Priority.Treasure; } else { tempP = Priority.TreasureEnemy; } } else if (tag == "Square") { tempP = Priority.EnemyDistrict; } else if (tag == "Bank") { tempP = Priority.Bank; GameObject bank = m_AllGameObjectsInRadius[i]; Bank bankScript = bank.GetComponent <Bank>(); if (bankScript != null) { CurrentGame.Instance.nearBank = bankScript.BankId; } } else if (tag.Equals("TradingPost", StringComparison.InvariantCultureIgnoreCase)) { GameObject tp = m_AllGameObjectsInRadius[i]; TradingPost tpScript = tp.GetComponent <TradingPost>(); if (tpScript != null) { CurrentGame.Instance.nearTP = tpScript.TPId; } tempP = Priority.TradingPost; } else { tempP = (Priority)Enum.Parse(typeof(Priority), tag); } if (m_AllGameObjectsInRadius[i].GetComponentInParent <CapturableDistrict>() == null) { priorityNbr = (int)tempP; priorityPresence[priorityNbr] = 1; } if (priorityNbr > tempPriority) { if (m_AllGameObjectsInRadius[i].GetComponentInParent <CapturableDistrict>() == null) { tempPriority = priorityNbr; } } } //do one mock loop for the taggable players as we know they are presentstring tag = m_AllGameObjectsInRadius[i].tag; tempP = Priority.Enemy; priorityNbr = (int)tempP; priorityPresence[priorityNbr] = 1; if (priorityNbr > tempPriority) { tempPriority = priorityNbr; } // if there is a new highestpriority, do next lines m_HighestPriority = tempPriority; Button mainButton = null; RectTransform tempPanel = null; //Make room for new mainbutton if (m_MainPanel.childCount > 0) { for (int i = 0; i < m_MainPanel.childCount; i++) { Destroy(m_MainPanel.GetChild(i).gameObject); } } if (m_HighestPriority != 0) { mainButton = Instantiate(m_Buttons[m_HighestPriority], transform.position, transform.rotation, m_MainPanel); mainButton.transform.Find("Text").GetComponent <Text>().text = m_ButtonNames[m_HighestPriority]; } if (((Priority)m_HighestPriority).ToString() == "Enemy") { if (!robStatus.RecentlyGotRobbed) { mainButton.GetComponent <Button>().onClick.AddListener(() => m_Person.Rob()); } } else { //names of the panels need to be the same as the priorities & layernames for (int j = 0; j < m_Panels.Length; j++) { if (m_Panels[j].name == ((Priority)m_HighestPriority).ToString()) { if ("Enemy" != ((Priority)m_HighestPriority).ToString()) { tempPanel = m_Panels[j]; mainButton.GetComponent <Button>().onClick.AddListener(() => buttonClicked(tempPanel)); } } } } // For all priorities, check if more buttons are needed in listpanel for (int i = priorityPresence.Length - 1; i >= 0; i--) { //we dont want mainbutton in the listpanel if (i != m_HighestPriority) { //Spawn specific priority button if (m_CurrentButtons[i] == 0 && priorityPresence[i] == 1) { Button tempB = (Button)Instantiate(m_Buttons[i], transform.position, transform.rotation, m_ListPanel); tempB.transform.Find("Text").GetComponent <Text>().text = m_ButtonNames[i]; tempPanel = null; for (int j = 0; j < m_Panels.Length; j++) { if (m_Panels[j].name == ((Priority)i).ToString()) { tempPanel = m_Panels[j]; } } if (tempPanel != null) { tempB.GetComponent <Button>().onClick.AddListener(() => buttonClicked(tempPanel)); } // 1 indicates that the button of the specific priority is present in the listpanel m_CurrentButtons[i] = 1; m_NumberOfButtonsInlistPanel++; } if ((m_CurrentButtons[i] == 1 && priorityPresence[i] == 0)) { TryToDestroyIndexOfListPanel(i); } } else { TryToDestroyIndexOfListPanel(i); } } }
/// <summary> /// Handles all logic for adding and removing buttons from the action button list. TODO: Split up in more managable functions /// </summary> public void PriorityUpdate(List <GameObject> allGameObjectsInRadius) { if (allGameObjectsInRadius.Count > 0) { // In case there were no collidings before. m_MainPanel.gameObject.SetActive(true); m_Switch.gameObject.SetActive(false); if (allGameObjectsInRadius.Count > 1) //todo change check so that button holder does not display when no buttons { m_ListPanel.gameObject.SetActive(true); m_Switch.gameObject.SetActive(true); } int lengthEnum = Enum.GetValues(typeof(Priority)).Cast <Priority>().Count(); int[] priorityPresence = new int[lengthEnum]; for (int i = 0; i < priorityPresence.Length; i++) { priorityPresence[i] = 0; } // Set temp priority to lowest int tempPriority = 0; int priorityNbr = 0; //Check with the tag of the gameObject, which priority it has in the enum, // If the priority is higher then the current, update the priority Priority tempP; for (int i = 0; i < allGameObjectsInRadius.Count; i++) { string tag = allGameObjectsInRadius[i].tag; #if (UNITY_EDITOR) Debug.Log("priority update: " + tag + " And name of object: " + allGameObjectsInRadius[i].name); #endif if (tag == "Treasure") //todo make switch case { /*Square */ Debug.Log("tag == Treasure"); if (allGameObjectsInRadius[i].GetComponent <Square>().Team.teamName == m_Person.Team.teamName) { tempP = Priority.Treasure; } else { tempP = Priority.TreasureEnemy; } } else if (tag == "Square") { tempP = Priority.EnemyDistrict; } else if (tag == "Bank") { tempP = Priority.Bank; GameObject bank = allGameObjectsInRadius[i]; Bank bankScript = bank.GetComponent <Bank>(); if (bankScript != null) { CurrentGame.Instance.nearBank = bankScript.BankId; } } else if (tag.Equals("TradingPost", StringComparison.InvariantCultureIgnoreCase)) { GameObject tp = allGameObjectsInRadius[i]; TradingPost tpScript = tp.GetComponent <TradingPost>(); if (tpScript != null) { CurrentGame.Instance.nearTP = tpScript.TPId; } tempP = Priority.TradingPost; } else { if ((Priority)Enum.Parse(typeof(Priority), tag) != Priority.Enemy) //exclude enemies from this check { tempP = (Priority)Enum.Parse(typeof(Priority), tag); } else { continue; //if is enemy skip loop iteration } } if (allGameObjectsInRadius[i].GetComponentInParent <CapturableDistrict>() == null) { priorityNbr = (int)tempP; priorityPresence[priorityNbr] = 1; } if (priorityNbr > tempPriority) { if (allGameObjectsInRadius[i].GetComponentInParent <CapturableDistrict>() == null) { tempPriority = priorityNbr; } } } //just in case check for players //this should be a shorter version of one loop from the above loop if (CurrentGame.Instance.TagablePlayers.Count > 0) { tempP = Priority.Enemy; priorityNbr = (int)tempP; priorityPresence[priorityNbr] = 1; if (priorityNbr > tempPriority) { tempPriority = priorityNbr; } } // if there is a new highestpriority, do next lines m_HighestPriority = tempPriority; Button mainButton = null; RectTransform tempPanel = null; //Make room for new mainbutton if (m_MainPanel.childCount > 0) { for (int i = 0; i < m_MainPanel.childCount; i++) { Destroy(m_MainPanel.GetChild(i).gameObject); } } if (m_HighestPriority != 0) { mainButton = Instantiate(m_Buttons[m_HighestPriority], transform.position, transform.rotation, m_MainPanel); mainButton.transform.Find("Text").GetComponent <Text>().text = m_ButtonNames[m_HighestPriority]; } if (((Priority)m_HighestPriority).ToString() == "Enemy") { if (!robStatus.RecentlyGotRobbed) { mainButton.GetComponent <Button>().onClick.AddListener(() => m_Person.Rob()); } } else { //names of the panels need to be the same as the priorities & layernames for (int j = 0; j < m_Panels.Length; j++) { if (m_Panels[j].name == ((Priority)m_HighestPriority).ToString()) { if ("Enemy" != ((Priority)m_HighestPriority).ToString()) { tempPanel = m_Panels[j]; mainButton.GetComponent <Button>().onClick.AddListener(() => buttonClicked(tempPanel)); } } } } // For all priorities, check if more buttons are needed in listpanel for (int i = priorityPresence.Length - 1; i >= 0; i--) { //we dont want mainbutton in the listpanel if (i != m_HighestPriority) { //Spawn specific priority button if (m_CurrentButtons[i] == 0 && priorityPresence[i] == 1) { Button tempB = (Button)Instantiate(m_Buttons[i], transform.position, transform.rotation, m_ListPanel); tempB.transform.Find("Text").GetComponent <Text>().text = m_ButtonNames[i]; tempPanel = null; for (int j = 0; j < m_Panels.Length; j++) { if (m_Panels[j].name == ((Priority)i).ToString()) { tempPanel = m_Panels[j]; } } if (tempPanel != null) { tempB.GetComponent <Button>().onClick.AddListener(() => buttonClicked(tempPanel)); } // 1 indicates that the button of the specific priority is present in the listpanel m_CurrentButtons[i] = 1; m_NumberOfButtonsInlistPanel++; } if ((m_CurrentButtons[i] == 1 && priorityPresence[i] == 0)) { TryToDestroyIndexOfListPanel(i); } } else { TryToDestroyIndexOfListPanel(i); } } } else { m_MainPanel.gameObject.SetActive(false); m_ListPanel.gameObject.SetActive(false); m_Switch.gameObject.SetActive(false); } }