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

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

parent 8d9b103b
This diff is collapsed.
......@@ -5,7 +5,7 @@ using UnityEngine.Events;
using UnityEngine.UI;
using TMPro;
public class GameSystem : MonoBehaviour
public class GameSystem : SingletonBase<GameSystem>
{
[SerializeField] private float totalTime = 60f; // 倒计时总时间
[Tooltip("时间文本")]
......@@ -60,6 +60,9 @@ public class GameSystem : MonoBehaviour
}
}
/// <summary>
/// 倒计时结束
/// </summary>
public void EndGame()
{
if (currentTime <= 0)
......
......@@ -14,9 +14,20 @@ public class MathGame : MonoBehaviour
[Tooltip("倒计时时长")]
public float countdownDuration = 10f;
[Tooltip("错误机会数量")]
public int chances = 3; // 初始错误机会数量
public int chances = 3;
[Tooltip("错误机会文本")]
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 num2;
......@@ -24,11 +35,21 @@ public class MathGame : MonoBehaviour
private int correctAnswer;
private bool isAnswered = false;
private Coroutine countdownCoroutine;
private int currentChance;
void Start()
{
currentChance = chances;
regenerateButton.onClick.AddListener(RegenerateQuestion);
GenerateQuestion();
countdownCoroutine = StartCoroutine(StartCountdown());
failedButton.onClick.AddListener(GameSystem.Instance.EndGame);
}
private void OnDestroy()
{
regenerateButton.onClick.RemoveAllListeners();
failedButton.onClick.RemoveAllListeners();
}
/// <summary>
......@@ -47,25 +68,23 @@ public class MathGame : MonoBehaviour
if (!isAnswered)
{
Debug.Log("时间到!");
// 处理时间到的情况,例如重新生成问题
GenerateQuestion();
if(currentChance <= 0)
{
failedUI.SetActive(true);
}
else
{
restartUI.SetActive(true);
ReduceChances(); // 减少错误机会
}
}
}
/// <summary>
/// 生成问题
/// </summary>
private void GenerateQuestion()
public void GenerateQuestion()
{
// 检查错误机会是否大于零
if (chances <= 0)
{
Debug.Log("没有机会了!");
return;
}
// 生成两个随机数(1-9)
num1 = Random.Range(1, 10);
num2 = Random.Range(1, 10);
......@@ -112,26 +131,40 @@ public class MathGame : MonoBehaviour
{
if (answerText.text == correctAnswer.ToString())
{
Debug.Log("正确");
isAnswered = true;
if (countdownCoroutine != null)
{
StopCoroutine(countdownCoroutine); // 停止倒计时
}
}
else
{
Debug.Log("错误");
if (countdownCoroutine != null)
{
StopCoroutine(countdownCoroutine); // 停止倒计时
}
if (!isAnswered)
{
if (currentChance <= 0)
{
failedUI.SetActive(true);
}
else
{
restartUI.SetActive(true);
ReduceChances(); // 减少错误机会
}
}
}
}
/// <summary>
/// 减少错误机会数量并更新显示
/// </summary>
private void ReduceChances()
{
chances--;
currentChance--;
UpdateChancesText();
}
......@@ -140,7 +173,7 @@ public class MathGame : MonoBehaviour
/// </summary>
private void UpdateChancesText()
{
chancesText.text = "Chances: " + chances;
chancesText.text = "剩余机会: " + currentChance;
}
/// <summary>
......@@ -159,4 +192,13 @@ public class MathGame : MonoBehaviour
{
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