using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BellGame : MonoBehaviour
{
public List questions;
public int currentQuestionIndex;
public Text questionText;
public Button player1Button;
public Button player2Button;
public bool player1CanAnswer = true;
public bool player2CanAnswer = true;
public float answerTimeLimit = 5f;
public float currentAnswerTime;
public bool isAnswering;
public bool answeredCorrectly;
// Start is called before the first frame update
void Start()
{
currentQuestionIndex = 0;
DisplayQuestion();
}
// Update is called once per frame
void Update()
{
if (isAnswering)
{
currentAnswerTime += Time.deltaTime;
if (currentAnswerTime >= answerTimeLimit)
{
PassTurn();
}
}
}
public void Player1Answer()
{
if (!isAnswering)
{
player1CanAnswer = false;
player2CanAnswer = true;
isAnswering = true;
currentAnswerTime = 0;
CheckAnswer(true);
}
}
public void Player2Answer()
{
if (!isAnswering)
{
player1CanAnswer = true;
player2CanAnswer = false;
isAnswering = true;
currentAnswerTime = 0;
CheckAnswer(false);
}
}
public void PassTurn()
{
player1CanAnswer = true;
player2CanAnswer = true;
isAnswering = false;
answeredCorrectly = false;
currentAnswerTime = 0;
DisplayQuestion();
}
public void CheckAnswer(bool player1Answered)
{
string answer = questions[currentQuestionIndex].answer;
if (player1Answered && player1Button.IsActive())
{
if (player1Button.GetComponentInChildren<Text>().text.ToLower() == answer.ToLower())
{
answeredCorrectly = true;
PlayerScored(true);
}
}
else if (!player1Answered && player2Button.IsActive())
{
if (player2Button.GetComponentInChildren<Text>().text.ToLower() == answer.ToLower())
{
answeredCorrectly = true;
PlayerScored(false);
}
}
if (!answeredCorrectly)
{
PassTurn();
}
}
public void PlayerScored(bool player1Scored)
{
if (player1Scored)
{
Debug.Log("Player 1 scored!");
}
else
{
Debug.Log("Player 2 scored!");
}
currentQuestionIndex++;
if (currentQuestionIndex >= questions.Count)
{
Debug.Log("Game over!");
// TODO: End game and show winner
}
else
{
PassTurn();
}
}
public void DisplayQuestion()
{
questionText.text = questions[currentQuestionIndex].question;
}
}
[System.Serializable]
public class Question
{
public string question;
public string answer;
}