123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- 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<Warrior> penalizedWarriors = new List<Warrior>();
- // 当前活动的答案选项列表
- private List<AnswerOption> activeAnswers = new List<AnswerOption>();
- // 上次生成答案的时间
- private float lastSpawnTime;
- // 钱包引用
- private Wallet _wallet;
- /// <summary>
- /// 初始化,获取钱包引用并生成第一道题目
- /// </summary>
- void Start()
- { _wallet = ComponentsManager.PlayerWallet;
- GenerateNewProblem();
- // UpdateCoinDisplay();
- lastSpawnTime = Time.time;
- }
- /// <summary>
- /// 生成新的数学题目(加法或减法)
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 在指定位置生成一个答案选项
- /// </summary>
- /// <param name="position">生成位置</param>
- 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<AnswerOption>();
-
- 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);
- }
- }
- /// <summary>
- /// 生成一个错误答案,确保不与正确答案和现有答案重复
- /// </summary>
- /// <returns>错误答案</returns>
- private int GenerateWrongAnswer()
- {
- int wrongAnswer;
- do
- {
- // 生成1-10之间的错误答案
- wrongAnswer = Random.Range(1, 11);
- } while (wrongAnswer == currentAnswer || IsAnswerAlreadyExists(wrongAnswer));
- return wrongAnswer;
- }
- /// <summary>
- /// 检查某个答案是否已存在于当前答案列表
- /// </summary>
- /// <param name="answer">要检查的答案</param>
- /// <returns>是否已存在</returns>
- private bool IsAnswerAlreadyExists(int answer)
- {
- foreach (AnswerOption option in activeAnswers)
- {
- if (option != null && option.GetAnswerValue() == answer)
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 检查玩家选择的答案是否正确,处理奖励或惩罚
- /// </summary>
- /// <param name="answerOption">玩家选择的答案选项</param>
- /// <returns>是否正确</returns>
- 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;
- }
- /// <summary>
- /// 清除所有答案选项
- /// </summary>
- private void ClearAllAnswers()
- {
- foreach (AnswerOption answer in activeAnswers)
- {
- if (answer != null)
- {
- // w.ApplyDamagePenalty(wrongPenalty);
- //penalized.Add(w);
- Destroy(answer.gameObject);
- }
- }}
- /// <summary>
- /// 从活动答案列表中移除指定答案
- /// </summary>
- /// <param name="answer">要移除的答案</param>
- 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}";
- }
- }
- */
- /// <summary>
- /// 对所有我方战士施加攻击力减弱效果(答错题时)
- /// </summary>
- private void ApplyPenaltyToAllWarriors()
- {
- Warrior[] warriors = FindObjectsOfType<Warrior>();
- foreach (Warrior warrior in warriors)
- {
-
- if (!warrior.IsEnemy && !penalizedWarriors.Contains(warrior))
- {
- warrior.ApplyDamagePenalty(wrongAnswerPenalty);
- penalizedWarriors.Add(warrior);
- }
- }
- }
- /// <summary>
- /// 重置所有战士的攻击力(答对题时)
- /// </summary>
- 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(); // 保险起见,停止所有协程
- }
- }
|