// 单轮游戏中管理游戏流程的部分 using System; using System.Collections; using System.Threading; using Cysharp.Threading.Tasks; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Playables; public class GameProcedureManager: Singleton { private int currentDifficulty; public Action onDifficultyChange; public int CurrentDifficulty { get => currentDifficulty; set { currentDifficulty = value; onDifficultyChange?.Invoke(currentDifficulty); if (currentDifficulty >= 9000) { GameOverManager.Instance.LoseGame(); } } } public int DayCount = 0; public float CurrentTime; public float MaxDayTime = 10; public float MaxNightTime = 60; [Header("在夜晚难度每多少秒涨1")] public int DifficultyIncreaseSpeedAtNight = 20; #region ResourceCode // 木头 public Action onWoodCountChange; private int woodCount; public int WoodCount { get { return woodCount; } set { if (value < 0) return; onWoodCountChange?.Invoke(value); woodCount = value; } } // 石头 public Action onStoneCountChange; private int stoneCount; public int StoneCount { get { return stoneCount; } set { if (value < 0) return; onStoneCountChange?.Invoke(value); stoneCount = value; } } //信仰 public Action onFaithCountChange; private int faithCount; public int FaithCount { get { return faithCount; } set { if (value < 0) return; onFaithCountChange?.Invoke(value); faithCount = value; } } //最大团子数 public int MaxBlobCount; public int WrokingBlobCount; public int LimitedBlobCount; #endregion public Action OnDay; public Action OnNight; public bool isDay = true; public CancellationTokenSource UpdateDifficultyToken = new(); public CancellationTokenSource ForceDayToken = new(); public CancellationTokenSource ForceNightToken = new(); private void Start() { StartGameProcedure(ForceDayToken.Token,!isDay).Forget(); OnDay += () => { UpdateDifficultyToken.Cancel(); }; OnNight += () => { UpdateDifficultyToken = new(); UpdateDifficulty(UpdateDifficultyToken.Token); }; } public async UniTaskVoid UpdateDifficulty(CancellationToken token) { while (true) { print("update difficulty"); await UniTask.WaitForSeconds(DifficultyIncreaseSpeedAtNight, cancellationToken: token); CurrentDifficulty += 1; } } public void ForceDay() { ForceDayToken.Cancel(); ForceDayToken = new(); DayCount += 1; StartGameProcedure(ForceDayToken.Token, false).Forget(); } public async UniTaskVoid StartGameProcedure(CancellationToken token,bool isNight = false) { print($"is night {isNight}"); if (isNight) { isDay = false; print("夜晚开始"); await UniTask.WaitForSeconds(MaxNightTime, cancellationToken:token); DayCount += 1; } while (true) { await UniTask.NextFrame(); //开始游戏或黑夜结束,进入白天 OnDay?.Invoke(); isDay = true; print("白天开始"); await UniTask.WaitForSeconds(MaxDayTime, cancellationToken:token); //白天结束,进入黑夜 OnNight?.Invoke(); isDay = false; print("夜晚开始"); await UniTask.WaitForSeconds(MaxNightTime, cancellationToken:token); DayCount += 1; } } public PlayableDirector director; public GameObject dd; public void PlayTimelineAndInvoke() { dd.SetActive(true); if (director == null) { Debug.LogWarning("PlayableDirector 未设置!"); return; } //director.gameObject.SetActive(true); // 开始播放Timeline director.Play(); // 开始一个协程,等待播放结束 StartCoroutine(WaitForTimelineToEnd()); } private IEnumerator WaitForTimelineToEnd() { // 等待Timeline播放结束 yield return new WaitForSeconds(23f); Debug.Log("Director OVer"); dd.SetActive(false); // Timeline结束后等待3秒 // 调用函数B ForceDay(); } }