Commit 6582f6c6 authored by 杨泽宇's avatar 杨泽宇

更新迷宫第二层:播放视频、回答数学问题

parent 8d9b103b
This diff is collapsed.
...@@ -5,7 +5,7 @@ using UnityEngine.Events; ...@@ -5,7 +5,7 @@ using UnityEngine.Events;
using UnityEngine.UI; using UnityEngine.UI;
using TMPro; using TMPro;
public class GameSystem : MonoBehaviour public class GameSystem : SingletonBase<GameSystem>
{ {
[SerializeField] private float totalTime = 60f; // 倒计时总时间 [SerializeField] private float totalTime = 60f; // 倒计时总时间
[Tooltip("时间文本")] [Tooltip("时间文本")]
...@@ -60,6 +60,9 @@ public class GameSystem : MonoBehaviour ...@@ -60,6 +60,9 @@ public class GameSystem : MonoBehaviour
} }
} }
/// <summary>
/// 倒计时结束
/// </summary>
public void EndGame() public void EndGame()
{ {
if (currentTime <= 0) if (currentTime <= 0)
......
...@@ -14,9 +14,20 @@ public class MathGame : MonoBehaviour ...@@ -14,9 +14,20 @@ public class MathGame : MonoBehaviour
[Tooltip("倒计时时长")] [Tooltip("倒计时时长")]
public float countdownDuration = 10f; public float countdownDuration = 10f;
[Tooltip("错误机会数量")] [Tooltip("错误机会数量")]
public int chances = 3; // 初始错误机会数量 public int chances = 3;
[Tooltip("错误机会文本")] [Tooltip("错误机会文本")]
public TMP_Text chancesText; public TMP_Text chancesText;
[Tooltip("重新开始提示UI")]
public GameObject restartUI;
[Tooltip("重新生成问题的按钮")]
public Button regenerateButton;
[Tooltip("游戏失败提示UI")]
public GameObject failedUI;
[Tooltip("重新生成问题的按钮")]
public Button failedButton;
[Tooltip("游戏成功提示UI")]
public GameObject SuccessUI;
private int num1; private int num1;
private int num2; private int num2;
...@@ -24,11 +35,21 @@ public class MathGame : MonoBehaviour ...@@ -24,11 +35,21 @@ public class MathGame : MonoBehaviour
private int correctAnswer; private int correctAnswer;
private bool isAnswered = false; private bool isAnswered = false;
private Coroutine countdownCoroutine; private Coroutine countdownCoroutine;
private int currentChance;
void Start() void Start()
{ {
currentChance = chances;
regenerateButton.onClick.AddListener(RegenerateQuestion);
GenerateQuestion(); GenerateQuestion();
countdownCoroutine = StartCoroutine(StartCountdown()); countdownCoroutine = StartCoroutine(StartCountdown());
failedButton.onClick.AddListener(GameSystem.Instance.EndGame);
}
private void OnDestroy()
{
regenerateButton.onClick.RemoveAllListeners();
failedButton.onClick.RemoveAllListeners();
} }
/// <summary> /// <summary>
...@@ -47,25 +68,23 @@ public class MathGame : MonoBehaviour ...@@ -47,25 +68,23 @@ public class MathGame : MonoBehaviour
if (!isAnswered) if (!isAnswered)
{ {
Debug.Log("时间到!"); if(currentChance <= 0)
// 处理时间到的情况,例如重新生成问题 {
GenerateQuestion(); failedUI.SetActive(true);
ReduceChances(); // 减少错误机会 }
else
{
restartUI.SetActive(true);
ReduceChances(); // 减少错误机会
}
} }
} }
/// <summary> /// <summary>
/// 生成问题 /// 生成问题
/// </summary> /// </summary>
private void GenerateQuestion() public void GenerateQuestion()
{ {
// 检查错误机会是否大于零
if (chances <= 0)
{
Debug.Log("没有机会了!");
return;
}
// 生成两个随机数(1-9) // 生成两个随机数(1-9)
num1 = Random.Range(1, 10); num1 = Random.Range(1, 10);
num2 = Random.Range(1, 10); num2 = Random.Range(1, 10);
...@@ -112,17 +131,31 @@ public class MathGame : MonoBehaviour ...@@ -112,17 +131,31 @@ public class MathGame : MonoBehaviour
{ {
if (answerText.text == correctAnswer.ToString()) if (answerText.text == correctAnswer.ToString())
{ {
Debug.Log("正确");
isAnswered = true; isAnswered = true;
if (countdownCoroutine != null) if (countdownCoroutine != null)
{ {
StopCoroutine(countdownCoroutine); // 停止倒计时 StopCoroutine(countdownCoroutine); // 停止倒计时
} }
} }
else else
{ {
Debug.Log("错误"); if (countdownCoroutine != null)
ReduceChances(); // 减少错误机会 {
StopCoroutine(countdownCoroutine); // 停止倒计时
}
if (!isAnswered)
{
if (currentChance <= 0)
{
failedUI.SetActive(true);
}
else
{
restartUI.SetActive(true);
ReduceChances(); // 减少错误机会
}
}
} }
} }
...@@ -131,7 +164,7 @@ public class MathGame : MonoBehaviour ...@@ -131,7 +164,7 @@ public class MathGame : MonoBehaviour
/// </summary> /// </summary>
private void ReduceChances() private void ReduceChances()
{ {
chances--; currentChance--;
UpdateChancesText(); UpdateChancesText();
} }
...@@ -140,7 +173,7 @@ public class MathGame : MonoBehaviour ...@@ -140,7 +173,7 @@ public class MathGame : MonoBehaviour
/// </summary> /// </summary>
private void UpdateChancesText() private void UpdateChancesText()
{ {
chancesText.text = "Chances: " + chances; chancesText.text = "剩余机会: " + currentChance;
} }
/// <summary> /// <summary>
...@@ -159,4 +192,13 @@ public class MathGame : MonoBehaviour ...@@ -159,4 +192,13 @@ public class MathGame : MonoBehaviour
{ {
answerText.text = ""; answerText.text = "";
} }
/// <summary>
/// 重新生成问题
/// </summary>
void RegenerateQuestion()
{
restartUI.SetActive(false);
GenerateQuestion(); // 重新生成问题
}
} }
using UnityEngine;
/// <summary>
/// 单例基类
/// </summary>
public abstract class SingletonBase<T> : MonoBehaviour
where T : Component
{
private static T _instance;
public static T Instance { get => _instance; }
protected virtual void Awake()
{
if (_instance == null)
_instance = this as T;
else
Destroy(this);
}
protected virtual void OnDestroy()
{
if (_instance == this)
_instance = null;
}
}
fileFormatVersion: 2
guid: 41d39d6504479094ab5bf66c04d989e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment