Browse Source

voting

combinedRaceAndNameDropper
Josh 2 years ago
parent
commit
5fd7ae9f20
  1. 12
      namedropper/Assets/Scripts/Player.cs
  2. 16
      namedropper/Assets/Scripts/TopicBox.cs
  3. 61
      namedropper/Assets/Scripts/TopicSelect.cs

12
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");
}
}
}

16
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<TopicSelect>().CheckForAllVotes();
if (_votes > 0)
transform.Find("Text").GetComponent<Text>().text = Name + " (" + Votes + ")";
}
}
// Start is called before the first frame update
void Start()
{

61
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<TopicBox> _picks = new List<TopicBox>();
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");
}
}

Loading…
Cancel
Save