using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.UI; public class QuestionHandler : MonoBehaviour { public GameObject managers; public Button submitButton; public TextMeshProUGUI explainText; public PromptList promptList; private GameManager gameManager; private List questionsToAnswer = new List(); private int activeAnsweredQuestionIndex = 0; private const int FirstQuestionIndex = 0; private const int SecondQuestionIndex = 1; /// /// Called when the script is enabled. /// Initializes the GameManager and questionsToAnswer list, and updates the UI. /// void OnEnable() { activeAnsweredQuestionIndex = FirstQuestionIndex; submitButton.interactable = true; submitButton.GetComponent().text = "Submit"; gameManager = managers.GetComponent(); questionsToAnswer = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer); Redraw(activeAnsweredQuestionIndex); } /// /// Redraws the UI with the information from the current question. /// /// The index of the current question to display. void Redraw(int currentQuestion) { Prompt prompt = promptList.prompts.Find(x => x.id == questionsToAnswer[currentQuestion].promptId); explainText.SetText(gameManager.myRoom.promptsLanguage == "en" ? prompt.en : prompt.fr); CameraManager cameraManager = gameObject.GetComponent(); cameraManager.WebcamResume(); } /// /// Gets the index of the proposition owned by a player for the current question. /// /// The player's ID for whom the proposition index is retrieved. /// The index of the proposition for the player. int GetPropositionIndex(string _playerId) { return questionsToAnswer[activeAnsweredQuestionIndex].propositions.First(x => x.Value.owner.id == _playerId).Key; } /// /// Handles the click event of the submit button. /// Uploads a photo and manages the UI accordingly. /// public void OnSubmitButton() { submitButton.interactable = false; submitButton.GetComponent().text = "Uploading..."; StorageManager storageManager = managers.GetComponent(); if (storageManager != null) { storageManager.UploadPhoto( gameManager.myRoom.code, gameManager.currentPlayer.id, questionsToAnswer[activeAnsweredQuestionIndex].index, GetPropositionIndex(gameManager.currentPlayer.id), succeed => { if (!succeed) { Debug.LogError("Photo upload failed. Please do something.", this); return; } if (activeAnsweredQuestionIndex == SecondQuestionIndex) { gameManager.WaitForPlayers(); } else { submitButton.interactable = true; submitButton.GetComponent().text = "Submit"; activeAnsweredQuestionIndex = SecondQuestionIndex; Redraw(activeAnsweredQuestionIndex); } }); } } }