using UnityEngine; using TMPro; using System.Collections.Generic; using Player.Wallet; public class MathQuizManager : MonoBehaviour { [Header("UI References")] public TextMeshProUGUI questionText; // 显示题目的文本 //public TextMeshProUGUI coinText; // 显示金币的文本 public GameObject answerPrefab; // 答案选项预制体 public GameObject questionWindow; // 在Inspector拖入QuestionWindow [Header("Game Settings")] public float wrongAnswerPenalty = 0.1f; // 错误答案的攻击力减弱比例 public int correctAnswerReward = 10; // 正确答案的金币奖励 public float answerLifeTime = 8f; // 答案选项存在时间 public float floatSpeed = 0.5f; // 答案选项上浮速度 public int maxAnswersOnScreen = 3; // 屏幕上最多显示的答案数量 public float answerSpawnDelay = 1.5f; // 答案生成间隔时间 public float correctAnswerBoost = 0.2f; // 正确答案的攻击力加强比例 public float boostDuration = 5f; // 攻击力加强持续时间 // 当前题目的正确答案 private int currentAnswer; // 受到惩罚的战士列表 private List penalizedWarriors = new List(); // 当前活动的答案选项列表 private List activeAnswers = new List(); // 上次生成答案的时间 private float lastSpawnTime; // 钱包引用 private Wallet _wallet; /// /// 初始化,获取钱包引用并生成第一道题目 /// void Start() { _wallet = ComponentsManager.PlayerWallet; GenerateNewProblem(); // UpdateCoinDisplay(); lastSpawnTime = Time.time; } /// /// 生成新的数学题目(加法或减法) /// public void GenerateNewProblem() { int num1, num2; int operationType = Random.Range(0, 2); // 0:加法, 1:减法 switch (operationType) { case 0: // 加法 num1 = Random.Range(1, 11); // 1-10 num2 = Random.Range(1, 11); // 1-10 currentAnswer = num1 + num2; questionText.text = $"{num1} + {num2} = ?"; break; case 1: // 减法 num1 = Random.Range(1, 11); // 1-10 num2 = Random.Range(1, num1 + 1); // 确保结果为正数 currentAnswer = num1 - num2; questionText.text = $"{num1} - {num2} = ?"; break; } } /// /// 在指定位置生成一个答案选项 /// /// 生成位置 public void SpawnAnswerOption(Vector3 position) { // 检查生成间隔 if (Time.time - lastSpawnTime < answerSpawnDelay) { return; } if (answerPrefab == null) { Debug.LogError("MathQuizManager: Answer prefab is not assigned!"); return; } // 检查是否达到最大答案数量 if (activeAnswers.Count >= maxAnswersOnScreen) { // 移除最早的答案 AnswerOption oldestAnswer = activeAnswers[0]; activeAnswers.RemoveAt(0); if (oldestAnswer != null) { Destroy(oldestAnswer.gameObject); } } // 确保位置有效 if (position == Vector3.zero) { Debug.LogWarning("MathQuizManager: Invalid spawn position!"); return; } // 检查是否已经有正确答案 bool hasCorrectAnswer = false; foreach (AnswerOption answer in activeAnswers) { if (answer != null && answer.GetAnswerValue() == currentAnswer) { hasCorrectAnswer = true; break; } } // 实例化答案选项 GameObject answerObj = Instantiate(answerPrefab, position, Quaternion.identity); AnswerOption answerOption = answerObj.GetComponent(); if (answerOption != null) { // 如果没有正确答案,这次必须生成正确答案 // 如果有正确答案,随机决定是否生成正确答案 bool isCorrect = !hasCorrectAnswer || Random.value > 0.7f; int answerValue = isCorrect ? currentAnswer : GenerateWrongAnswer(); answerOption.Initialize(this, answerValue, isCorrect); activeAnswers.Add(answerOption); lastSpawnTime = Time.time; // 更新最后生成时间 } else { Debug.LogError("MathQuizManager: AnswerOption component not found on prefab!"); Destroy(answerObj); } } /// /// 生成一个错误答案,确保不与正确答案和现有答案重复 /// /// 错误答案 private int GenerateWrongAnswer() { int wrongAnswer; do { // 生成1-10之间的错误答案 wrongAnswer = Random.Range(1, 11); } while (wrongAnswer == currentAnswer || IsAnswerAlreadyExists(wrongAnswer)); return wrongAnswer; } /// /// 检查某个答案是否已存在于当前答案列表 /// /// 要检查的答案 /// 是否已存在 private bool IsAnswerAlreadyExists(int answer) { foreach (AnswerOption option in activeAnswers) { if (option != null && option.GetAnswerValue() == answer) { return true; } } return false; } /// /// 检查玩家选择的答案是否正确,处理奖励或惩罚 /// /// 玩家选择的答案选项 /// 是否正确 public bool CheckAnswer(AnswerOption answerOption) { bool isCorrect = answerOption.GetAnswerValue() == currentAnswer; if (isCorrect) { // 答对题目,奖励金币、重置惩罚、生成新题目 int previousMoney = ComponentsManager.PlayerWallet.GetMoney; ComponentsManager.PlayerWallet.AddMoney(correctAnswerReward); if (GameDataManager.Instance != null) { GameDataManager.Instance.RefreshCoins(); } Debug.Log($"答题奖励金币: +{correctAnswerReward}"); Debug.Log($"当前总金币: {ComponentsManager.PlayerWallet.GetMoney} (之前: {previousMoney})"); ComponentsManager.GameUI.CoiAudioPlay(); // 播放金币音效 GenerateNewProblem(); ResetAllWarriorsPenalties(); ClearAllAnswers(); } else { // 答错题目,对所有我方战士施加攻击力减弱效果 ApplyPenaltyToAllWarriors(); } return isCorrect; } /// /// 清除所有答案选项 /// private void ClearAllAnswers() { foreach (AnswerOption answer in activeAnswers) { if (answer != null) { // w.ApplyDamagePenalty(wrongPenalty); //penalized.Add(w); Destroy(answer.gameObject); } }} /// /// 从活动答案列表中移除指定答案 /// /// 要移除的答案 public void RemoveAnswer(AnswerOption answer) { //foreach (var w in penalized) if (w) w.ResetDamagePenalty(); //penalized.Clear(); activeAnswers.Remove(answer); } // 添加金币(已弃用,见CheckAnswer) /* private void AddCoins(int amount) { _wallet.AddMoney(amount); UpdateCoinDisplay(); } // 更新金币显示 private void UpdateCoinDisplay() { if (coinText != null && _wallet != null) { coinText.text = $" {_wallet.GetMoney}"; } } */ /// /// 对所有我方战士施加攻击力减弱效果(答错题时) /// private void ApplyPenaltyToAllWarriors() { Warrior[] warriors = FindObjectsOfType(); foreach (Warrior warrior in warriors) { if (!warrior.IsEnemy && !penalizedWarriors.Contains(warrior)) { warrior.ApplyDamagePenalty(wrongAnswerPenalty); penalizedWarriors.Add(warrior); } } } /// /// 重置所有战士的攻击力(答对题时) /// private void ResetAllWarriorsPenalties() { foreach (Warrior warrior in penalizedWarriors) { //Destroy(obj); if (warrior != null) { warrior.ResetDamagePenalty(); } } penalizedWarriors.Clear(); } public void ShowAnswerWindow() { if (answerPrefab != null) // Assuming answerPrefab is the window { answerPrefab.SetActive(true); } } public void HideAnswerWindow() { if (answerPrefab != null) // Assuming answerPrefab is the window { answerPrefab.SetActive(false); } } public void ShowQuestionWindow() { if (questionWindow != null) questionWindow.SetActive(true); } public void HideQuestionWindow() { Debug.Log("HideQuestionWindow called"); if (questionWindow != null) { Debug.Log("SetActive(false) on questionWindow: " + questionWindow.name); questionWindow.SetActive(false); } else { Debug.LogWarning("questionWindow is null!"); } } void OnDisable() { CancelInvoke(); // 取消所有Invoke,防止场景重载后自动触发 StopAllCoroutines(); // 保险起见,停止所有协程 } }