Browse Source

voting

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

8
namedropper/Assets/Scripts/Player.cs

@ -207,11 +207,19 @@ public class Player : MonoBehaviour
TopicBox topicBox = collision.gameObject.GetComponent<TopicBox>(); TopicBox topicBox = collision.gameObject.GetComponent<TopicBox>();
if (topicBox != null) if (topicBox != null)
{
if (TopicSelect.VOTE_ON_TOPIC)
{
GameObject.Destroy(this.gameObject);
topicBox.Votes++;
}
else
{ {
GameDataManager.Instance.CurrentTopic = topicBox.TopicData; GameDataManager.Instance.CurrentTopic = topicBox.TopicData;
SceneManager.LoadScene("Game"); SceneManager.LoadScene("Game");
} }
} }
}
// Update is called once per frame // Update is called once per frame
void Update() void Update()

16
namedropper/Assets/Scripts/TopicBox.cs

@ -7,6 +7,22 @@ public class TopicBox : MonoBehaviour
{ {
public string Name; public string Name;
public TopicData TopicData; 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 // Start is called before the first frame update
void Start() void Start()
{ {

61
namedropper/Assets/Scripts/TopicSelect.cs

@ -17,7 +17,11 @@ public class TopicSelect : MonoBehaviour
[SerializeField] GameObject _player3; [SerializeField] GameObject _player3;
[SerializeField] GameObject _player4; [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 // Start is called before the first frame update
void Awake() void Awake()
{ {
@ -70,6 +74,10 @@ public class TopicSelect : MonoBehaviour
_subTitle.text = worstPlayerName + "\nmay pick the topic"; _subTitle.text = worstPlayerName + "\nmay pick the topic";
} }
else if (VOTE_ON_TOPIC)
{
_subTitle.text = "Cast your votes!";
}
else else
{ {
_subTitle.text = "Pick a topic!"; _subTitle.text = "Pick a topic!";
@ -86,5 +94,56 @@ public class TopicSelect : MonoBehaviour
// Update is called once per frame // Update is called once per frame
void Update() 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