// GameManager.cs - главный скрипт игры, менеджер using UnityEngine; using…
- User
// GameManager.cs - главный скрипт игры, менеджер using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic;
// Объявление структур вне классов public struct Question { public string text; public Choice[] choices; public int populationType; // 1 - до 100, 2 - до 500, 3 - до 1500, 4 - до 5000, 5 - до 10000
public Question(string text, Choice[] choices, int populationType) { this.text = text; this.choices = choices; this.populationType = populationType; }}
public struct Choice { public string text; public int populationChange; public int happinessChange; public string resultText;
public Choice(string text, int populationChange, int happinessChange, string resultText) { this.text = text; this.populationChange = populationChange; this.happinessChange = happinessChange; this.resultText = resultText; }}
public class GameManager : MonoBehaviour { public static GameManager Instance { get; private set; } public Question currentQuestion;
public Text questionText; public Text populationText; public Text happinessText; public Button button1; public Button button2; public Button button3; public Button button4; public GameObject gameOverPanel; private bool isChoiceLocked = false; public QuestsLogic questsLogic; // Ссылка на скрипт с логикой квестов // Панель с инфой о том, на что повлиял выбор public Text resultText; public GameObject resultPanel; public Button understandButton; //Показатели поселения public int population = 20; private int happiness = 50; void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); } } void Start() { questsLogic.Initialize(); DisplayQuestion(); UpdatePopulationText(); UpdateHappinessText(); gameOverPanel.SetActive(false); resultPanel.SetActive(false); } // Вызывается из QuestsLogic, когда нужно отобразить вопрос public void DisplayQuestion() { currentQuestion = questsLogic.GetCurrentQuestion(); if (currentQuestion.text != null) { questionText.text = currentQuestion.text; button1.GetComponentInChildren<Text>().text = currentQuestion.choices[0].text; button1.onClick.RemoveAllListeners(); button1.onClick.AddListener(() => MakeChoice(0)); button2.GetComponentInChildren<Text>().text = currentQuestion.choices[1].text; button2.onClick.RemoveAllListeners(); button2.onClick.AddListener(() => MakeChoice(1)); button3.GetComponentInChildren<Text>().text = currentQuestion.choices[2].text; button3.onClick.RemoveAllListeners(); button3.onClick.AddListener(() => MakeChoice(2)); button4.GetComponentInChildren<Text>().text = currentQuestion.choices[3].text; button4.onClick.RemoveAllListeners(); button4.onClick.AddListener(() => MakeChoice(3)); } else { GameOver("Вы прошли все испытания и привели свое поселение к процветанию!"); } } public AudioSource audioSource; public AudioClip fxMinusPeople; public AudioClip fxMinusHappy; public AudioClip fxPlusHappy; public AudioClip fxPlusPeople; public AudioClip fxGameOver; public void MakeChoice(int index) { if (isChoiceLocked) return; Choice chosenChoice = questsLogic.MakeChoice(index, currentQuestion); population += chosenChoice.populationChange; happiness += chosenChoice.happinessChange; resultText.text = chosenChoice.resultText; PlayChoiceSound(chosenChoice); isChoiceLocked = true; // Блокируем кнопки после выбора StartCoroutine(ShowResultAndContinue(2f)); } private void PlayChoiceSound(Choice choice) { if (choice.populationChange < 0 && Mathf.Abs(choice.populationChange) > Mathf.Abs(choice.happinessChange)) { audioSource.PlayOneShot(fxMinusPeople); } else if (choice.happinessChange < 0 && Mathf.Abs(choice.happinessChange) > Mathf.Abs(choice.populationChange)) { audioSource.PlayOneShot(fxMinusHappy); } else if (choice.happinessChange > 0 && choice.happinessChange > choice.populationChange) { audioSource.PlayOneShot(fxPlusHappy); } else if (choice.populationChange > 0 && choice.populationChange > choice.happinessChange) { audioSource.PlayOneShot(fxPlusPeople); } } private IEnumerator ShowResultAndContinue(float delay) { // Открываем панель результата resultPanel.SetActive(true); understandButton.onClick.RemoveAllListeners(); // Ждём нажатия кнопки “Понял” bool understood = false; understandButton.onClick.AddListener(() => understood = true); // Пока кнопку не нажали, ждём yield return new WaitUntil(() => understood); // Очищаем результат resultText.text = ""; UpdatePopulationText(); UpdateHappinessText(); if (happiness <= 0) { GameOver("Ваше поселение погрузилось в хаос и отчаяние!"); } else if (population <= 1) { GameOver("Ваше поселение погибло!"); } else { resultPanel.SetActive(false); isChoiceLocked = false; DisplayQuestion(); } } private void UpdatePopulationText() { populationText.text = "Население: " + population; } private void UpdateHappinessText() { happinessText.text = "Счастье: " + happiness; } private void GameOver(string message) { gameOverPanel.SetActive(true); gameOverPanel.transform.Find("Text").GetComponent<Text>().text = message; audioSource.PlayOneShot(fxGameOver); } - Assistant