MathQuizManager.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using UnityEngine;
  2. using TMPro;
  3. using System.Collections.Generic;
  4. using Player.Wallet;
  5. public class MathQuizManager : MonoBehaviour
  6. {
  7. [Header("UI References")]
  8. public TextMeshProUGUI questionText; // 显示题目的文本
  9. //public TextMeshProUGUI coinText; // 显示金币的文本
  10. public GameObject answerPrefab; // 答案选项预制体
  11. public GameObject questionWindow; // 在Inspector拖入QuestionWindow
  12. [Header("Game Settings")]
  13. public float wrongAnswerPenalty = 0.1f; // 错误答案的攻击力减弱比例
  14. public int correctAnswerReward = 10; // 正确答案的金币奖励
  15. public float answerLifeTime = 8f; // 答案选项存在时间
  16. public float floatSpeed = 0.5f; // 答案选项上浮速度
  17. public int maxAnswersOnScreen = 3; // 屏幕上最多显示的答案数量
  18. public float answerSpawnDelay = 1.5f; // 答案生成间隔时间
  19. public float correctAnswerBoost = 0.2f; // 正确答案的攻击力加强比例
  20. public float boostDuration = 5f; // 攻击力加强持续时间
  21. // 当前题目的正确答案
  22. private int currentAnswer;
  23. // 受到惩罚的战士列表
  24. private List<Warrior> penalizedWarriors = new List<Warrior>();
  25. // 当前活动的答案选项列表
  26. private List<AnswerOption> activeAnswers = new List<AnswerOption>();
  27. // 上次生成答案的时间
  28. private float lastSpawnTime;
  29. // 钱包引用
  30. private Wallet _wallet;
  31. /// <summary>
  32. /// 初始化,获取钱包引用并生成第一道题目
  33. /// </summary>
  34. void Start()
  35. { _wallet = ComponentsManager.PlayerWallet;
  36. GenerateNewProblem();
  37. // UpdateCoinDisplay();
  38. lastSpawnTime = Time.time;
  39. }
  40. /// <summary>
  41. /// 生成新的数学题目(加法或减法)
  42. /// </summary>
  43. public void GenerateNewProblem()
  44. {
  45. int num1, num2;
  46. int operationType = Random.Range(0, 2); // 0:加法, 1:减法
  47. switch (operationType)
  48. {
  49. case 0: // 加法
  50. num1 = Random.Range(1, 11); // 1-10
  51. num2 = Random.Range(1, 11); // 1-10
  52. currentAnswer = num1 + num2;
  53. questionText.text = $"{num1} + {num2} = ?";
  54. break;
  55. case 1: // 减法
  56. num1 = Random.Range(1, 11); // 1-10
  57. num2 = Random.Range(1, num1 + 1); // 确保结果为正数
  58. currentAnswer = num1 - num2;
  59. questionText.text = $"{num1} - {num2} = ?";
  60. break;
  61. }
  62. }
  63. /// <summary>
  64. /// 在指定位置生成一个答案选项
  65. /// </summary>
  66. /// <param name="position">生成位置</param>
  67. public void SpawnAnswerOption(Vector3 position)
  68. {
  69. // 检查生成间隔
  70. if (Time.time - lastSpawnTime < answerSpawnDelay)
  71. {
  72. return;
  73. }
  74. if (answerPrefab == null)
  75. {
  76. Debug.LogError("MathQuizManager: Answer prefab is not assigned!");
  77. return;
  78. }
  79. // 检查是否达到最大答案数量
  80. if (activeAnswers.Count >= maxAnswersOnScreen)
  81. {
  82. // 移除最早的答案
  83. AnswerOption oldestAnswer = activeAnswers[0];
  84. activeAnswers.RemoveAt(0);
  85. if (oldestAnswer != null)
  86. {
  87. Destroy(oldestAnswer.gameObject);
  88. }
  89. }
  90. // 确保位置有效
  91. if (position == Vector3.zero)
  92. {
  93. Debug.LogWarning("MathQuizManager: Invalid spawn position!");
  94. return;
  95. }
  96. // 检查是否已经有正确答案
  97. bool hasCorrectAnswer = false;
  98. foreach (AnswerOption answer in activeAnswers)
  99. {
  100. if (answer != null && answer.GetAnswerValue() == currentAnswer)
  101. {
  102. hasCorrectAnswer = true;
  103. break;
  104. }
  105. }
  106. // 实例化答案选项
  107. GameObject answerObj = Instantiate(answerPrefab, position, Quaternion.identity);
  108. AnswerOption answerOption = answerObj.GetComponent<AnswerOption>();
  109. if (answerOption != null)
  110. {
  111. // 如果没有正确答案,这次必须生成正确答案
  112. // 如果有正确答案,随机决定是否生成正确答案
  113. bool isCorrect = !hasCorrectAnswer || Random.value > 0.7f;
  114. int answerValue = isCorrect ? currentAnswer : GenerateWrongAnswer();
  115. answerOption.Initialize(this, answerValue, isCorrect);
  116. activeAnswers.Add(answerOption);
  117. lastSpawnTime = Time.time; // 更新最后生成时间
  118. }
  119. else
  120. {
  121. Debug.LogError("MathQuizManager: AnswerOption component not found on prefab!");
  122. Destroy(answerObj);
  123. }
  124. }
  125. /// <summary>
  126. /// 生成一个错误答案,确保不与正确答案和现有答案重复
  127. /// </summary>
  128. /// <returns>错误答案</returns>
  129. private int GenerateWrongAnswer()
  130. {
  131. int wrongAnswer;
  132. do
  133. {
  134. // 生成1-10之间的错误答案
  135. wrongAnswer = Random.Range(1, 11);
  136. } while (wrongAnswer == currentAnswer || IsAnswerAlreadyExists(wrongAnswer));
  137. return wrongAnswer;
  138. }
  139. /// <summary>
  140. /// 检查某个答案是否已存在于当前答案列表
  141. /// </summary>
  142. /// <param name="answer">要检查的答案</param>
  143. /// <returns>是否已存在</returns>
  144. private bool IsAnswerAlreadyExists(int answer)
  145. {
  146. foreach (AnswerOption option in activeAnswers)
  147. {
  148. if (option != null && option.GetAnswerValue() == answer)
  149. {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. /// <summary>
  156. /// 检查玩家选择的答案是否正确,处理奖励或惩罚
  157. /// </summary>
  158. /// <param name="answerOption">玩家选择的答案选项</param>
  159. /// <returns>是否正确</returns>
  160. public bool CheckAnswer(AnswerOption answerOption)
  161. {
  162. bool isCorrect = answerOption.GetAnswerValue() == currentAnswer;
  163. if (isCorrect)
  164. {
  165. // 答对题目,奖励金币、重置惩罚、生成新题目
  166. int previousMoney = ComponentsManager.PlayerWallet.GetMoney;
  167. ComponentsManager.PlayerWallet.AddMoney(correctAnswerReward);
  168. if (GameDataManager.Instance != null)
  169. {
  170. GameDataManager.Instance.RefreshCoins();
  171. }
  172. Debug.Log($"答题奖励金币: +{correctAnswerReward}");
  173. Debug.Log($"当前总金币: {ComponentsManager.PlayerWallet.GetMoney} (之前: {previousMoney})");
  174. ComponentsManager.GameUI.CoiAudioPlay(); // 播放金币音效
  175. GenerateNewProblem();
  176. ResetAllWarriorsPenalties();
  177. ClearAllAnswers();
  178. }
  179. else
  180. {
  181. // 答错题目,对所有我方战士施加攻击力减弱效果
  182. ApplyPenaltyToAllWarriors();
  183. }
  184. return isCorrect;
  185. }
  186. /// <summary>
  187. /// 清除所有答案选项
  188. /// </summary>
  189. private void ClearAllAnswers()
  190. {
  191. foreach (AnswerOption answer in activeAnswers)
  192. {
  193. if (answer != null)
  194. {
  195. // w.ApplyDamagePenalty(wrongPenalty);
  196. //penalized.Add(w);
  197. Destroy(answer.gameObject);
  198. }
  199. }}
  200. /// <summary>
  201. /// 从活动答案列表中移除指定答案
  202. /// </summary>
  203. /// <param name="answer">要移除的答案</param>
  204. public void RemoveAnswer(AnswerOption answer)
  205. {
  206. //foreach (var w in penalized) if (w) w.ResetDamagePenalty();
  207. //penalized.Clear();
  208. activeAnswers.Remove(answer);
  209. }
  210. // 添加金币(已弃用,见CheckAnswer)
  211. /* private void AddCoins(int amount)
  212. {
  213. _wallet.AddMoney(amount);
  214. UpdateCoinDisplay();
  215. }
  216. // 更新金币显示
  217. private void UpdateCoinDisplay()
  218. {
  219. if (coinText != null && _wallet != null)
  220. {
  221. coinText.text = $" {_wallet.GetMoney}";
  222. }
  223. }
  224. */
  225. /// <summary>
  226. /// 对所有我方战士施加攻击力减弱效果(答错题时)
  227. /// </summary>
  228. private void ApplyPenaltyToAllWarriors()
  229. {
  230. Warrior[] warriors = FindObjectsOfType<Warrior>();
  231. foreach (Warrior warrior in warriors)
  232. {
  233. if (!warrior.IsEnemy && !penalizedWarriors.Contains(warrior))
  234. {
  235. warrior.ApplyDamagePenalty(wrongAnswerPenalty);
  236. penalizedWarriors.Add(warrior);
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// 重置所有战士的攻击力(答对题时)
  242. /// </summary>
  243. private void ResetAllWarriorsPenalties()
  244. {
  245. foreach (Warrior warrior in penalizedWarriors)
  246. {
  247. //Destroy(obj);
  248. if (warrior != null)
  249. {
  250. warrior.ResetDamagePenalty();
  251. }
  252. }
  253. penalizedWarriors.Clear();
  254. }
  255. public void ShowAnswerWindow()
  256. {
  257. if (answerPrefab != null) // Assuming answerPrefab is the window
  258. {
  259. answerPrefab.SetActive(true);
  260. }
  261. }
  262. public void HideAnswerWindow()
  263. {
  264. if (answerPrefab != null) // Assuming answerPrefab is the window
  265. {
  266. answerPrefab.SetActive(false);
  267. }
  268. }
  269. public void ShowQuestionWindow()
  270. {
  271. if (questionWindow != null)
  272. questionWindow.SetActive(true);
  273. }
  274. public void HideQuestionWindow()
  275. {
  276. Debug.Log("HideQuestionWindow called");
  277. if (questionWindow != null)
  278. {
  279. Debug.Log("SetActive(false) on questionWindow: " + questionWindow.name);
  280. questionWindow.SetActive(false);
  281. }
  282. else
  283. {
  284. Debug.LogWarning("questionWindow is null!");
  285. }
  286. }
  287. void OnDisable()
  288. {
  289. CancelInvoke(); // 取消所有Invoke,防止场景重载后自动触发
  290. StopAllCoroutines(); // 保险起见,停止所有协程
  291. }
  292. }