using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; public class AnswerSelectionController : MonoBehaviour { public static AnswerSelectionController Instance; [Header("UI")] public GameObject answerSelectionPanel; public TextMeshProUGUI descriptionText; public Toggle[] toggles; public Button checkButton; [Header("Кольори")] public Color correctColor = new Color(0.3f, 1f, 0.3f); public Color wrongColor = new Color(1f, 0.3f, 0.3f); public Color missedColor = new Color(1f, 0.8f, 0f); public Color defaultColor = new Color(0.725f, 0.522f, 0.949f); public class AnswerQuestion { public string questionText; public string[] answers; public int[] correctIndices; } private AnswerQuestion[] questions; private int currentQuestionIndex = 0; private int[] shuffledIndices; void Awake() { Instance = this; if (answerSelectionPanel != null) answerSelectionPanel.SetActive(false); } public void ShowQuestions(AnswerQuestion[] questionList) { questions = questionList; currentQuestionIndex = 0; answerSelectionPanel.SetActive(true); ShowCurrentQuestion(); } void ShowCurrentQuestion() { if (questions == null || currentQuestionIndex >= questions.Length) return; var q = questions[currentQuestionIndex]; if (descriptionText != null) descriptionText.text = q.questionText; shuffledIndices = ShuffleIndices(q.answers.Length); for (int i = 0; i < toggles.Length; i++) { if (i < q.answers.Length) { toggles[i].gameObject.SetActive(true); toggles[i].isOn = false; toggles[i].interactable = true; var label = toggles[i].GetComponentInChildren(); if (label != null) label.text = q.answers[shuffledIndices[i]]; var bg = toggles[i].transform.Find("Background")?.GetComponent(); if (bg != null) bg.color = defaultColor; } else { toggles[i].gameObject.SetActive(false); } } checkButton.interactable = true; var btnImg = checkButton.GetComponent(); if (btnImg != null) btnImg.color = defaultColor; } int[] ShuffleIndices(int count) { int[] indices = new int[count]; for (int i = 0; i < count; i++) indices[i] = i; for (int i = count - 1; i > 0; i--) { int j = Random.Range(0, i + 1); int tmp = indices[i]; indices[i] = indices[j]; indices[j] = tmp; } return indices; } public void OnCheckPressed() { if (questions == null || currentQuestionIndex >= questions.Length) return; var q = questions[currentQuestionIndex]; checkButton.interactable = false; for (int i = 0; i < toggles.Length; i++) { if (!toggles[i].gameObject.activeSelf) continue; toggles[i].interactable = false; int originalIndex = shuffledIndices[i]; bool isCorrect = System.Array.IndexOf(q.correctIndices, originalIndex) >= 0; bool isSelected = toggles[i].isOn; var bg = toggles[i].transform.Find("Background")?.GetComponent(); if (bg == null) continue; if (isCorrect && isSelected) bg.color = correctColor; else if (isCorrect && !isSelected) bg.color = missedColor; else if (!isCorrect && isSelected) bg.color = wrongColor; else bg.color = defaultColor; } StartCoroutine(NextAfterDelay()); } IEnumerator NextAfterDelay() { yield return new WaitForSeconds(2f); currentQuestionIndex++; if (currentQuestionIndex < questions.Length) ShowCurrentQuestion(); else { answerSelectionPanel.SetActive(false); GameManager.Instance.OnQuestionComplete(); } } }