From 5fd7ae9f209597e696b350b0bde0d27b33626f0d Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 6 Mar 2023 13:56:57 -0500 Subject: [PATCH] voting --- namedropper/Assets/Scripts/Player.cs | 12 ++++- namedropper/Assets/Scripts/TopicBox.cs | 16 ++++++ namedropper/Assets/Scripts/TopicSelect.cs | 61 ++++++++++++++++++++++- 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/namedropper/Assets/Scripts/Player.cs b/namedropper/Assets/Scripts/Player.cs index d02590b..37a38b2 100644 --- a/namedropper/Assets/Scripts/Player.cs +++ b/namedropper/Assets/Scripts/Player.cs @@ -208,8 +208,16 @@ public class Player : MonoBehaviour if (topicBox != null) { - GameDataManager.Instance.CurrentTopic = topicBox.TopicData; - SceneManager.LoadScene("Game"); + if (TopicSelect.VOTE_ON_TOPIC) + { + GameObject.Destroy(this.gameObject); + topicBox.Votes++; + } + else + { + GameDataManager.Instance.CurrentTopic = topicBox.TopicData; + SceneManager.LoadScene("Game"); + } } } diff --git a/namedropper/Assets/Scripts/TopicBox.cs b/namedropper/Assets/Scripts/TopicBox.cs index e3d4746..d2b28f8 100644 --- a/namedropper/Assets/Scripts/TopicBox.cs +++ b/namedropper/Assets/Scripts/TopicBox.cs @@ -7,6 +7,22 @@ public class TopicBox : MonoBehaviour { public string Name; public TopicData TopicData; + + int _votes = 0; + + public int Votes + { + get => _votes; + set + { + _votes = value; + + GameObject.FindObjectOfType().CheckForAllVotes(); + if (_votes > 0) + transform.Find("Text").GetComponent().text = Name + " (" + Votes + ")"; + } + } + // Start is called before the first frame update void Start() { diff --git a/namedropper/Assets/Scripts/TopicSelect.cs b/namedropper/Assets/Scripts/TopicSelect.cs index a9ea10f..26596be 100644 --- a/namedropper/Assets/Scripts/TopicSelect.cs +++ b/namedropper/Assets/Scripts/TopicSelect.cs @@ -17,7 +17,11 @@ public class TopicSelect : MonoBehaviour [SerializeField] GameObject _player3; [SerializeField] GameObject _player4; - const bool WORST_PLAYER_PICKS = true; + public const bool WORST_PLAYER_PICKS = false; + public const bool VOTE_ON_TOPIC = true; + + + // Start is called before the first frame update void Awake() { @@ -70,6 +74,10 @@ public class TopicSelect : MonoBehaviour _subTitle.text = worstPlayerName + "\nmay pick the topic"; } + else if (VOTE_ON_TOPIC) + { + _subTitle.text = "Cast your votes!"; + } else { _subTitle.text = "Pick a topic!"; @@ -86,5 +94,56 @@ public class TopicSelect : MonoBehaviour // Update is called once per frame void Update() { + + } + + List _picks = new List(); + + + public void CheckForAllVotes() + { + if (_topic1Box.Votes + _topic2Box.Votes + _topic3Box.Votes >= 4) + { + + //all votes cast + if (_topic1Box.Votes >= 2) + { + _picks.Add(_topic1Box); + } + else + { + GameObject.Destroy(_topic1Box.gameObject); + } + + if (_topic2Box.Votes >= 2) + { + _picks.Add(_topic2Box); + } + else + { + GameObject.Destroy(_topic2Box.gameObject); + } + + if (_topic3Box.Votes >= 2) + { + _picks.Add(_topic3Box); + } + else + { + GameObject.Destroy(_topic3Box.gameObject); + } + + if (_picks.Count > 1) + _subTitle.text = "Breaking tie randomly!"; + + Invoke("Results", 2f); + } + } + + void Results() + { + TopicBox topicBox = _picks[Random.Range(0, _picks.Count)]; + GameDataManager.Instance.CurrentTopic = topicBox.TopicData; + SceneManager.LoadScene("Game"); } }