Files
2025TapTapGameJam/Assets/Script/Gameplay/Global/BUGManager.cs

85 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using Core;
using UnityEngine;
using UnityEngine.Events;
namespace Script.Gameplay.Global
{
public struct StackOverflowBUGLog
{
public Transform TargetTransform;
public int BeTriggerCycle; // 触发时的游戏循环次数
public StackOverflowBUGLog(Transform targetTransform, int beTriggerCycle)
{
TargetTransform = targetTransform;
BeTriggerCycle = beTriggerCycle;
}
}
public class BUGManager : MonoSingleton<BUGManager>
{
[SerializeField] private GameObject bugCubePrefab;
[Header("天核区域检测设置")] [SerializeField] private Vector3 AreaCenter = Vector3.zero;
[SerializeField] private Vector3 AreaSize = new Vector3(50, 50, 50);
public UnityEvent OnBUGHappenedInArea;
private List<StackOverflowBUGLog> stackOverflowBugLogs = new List<StackOverflowBUGLog>();
private List<GameObject> bugCubes = new List<GameObject>();
private void Start()
{
OnBUGHappenedInArea.AddListener(() =>
{
Debug.LogWarning("天核区域内检测到BUG立方体");
});
GameManager.Instance.OnGameStart += GenerateBUGCubes;
}
public void LogStackOverflowBUG(Transform targetTransform)
{
int beTriggerCycle = GameDataManager.Instance.TotalLoopCount;
stackOverflowBugLogs.Add(new StackOverflowBUGLog(targetTransform, beTriggerCycle));
StartCoroutine(GameManager.Instance.ReStartGame());
}
public void GenerateBUGCubes()
{
bugCubes.Clear();
int currentCycle = GameDataManager.Instance.TotalLoopCount;
// 记录的BUG位置生成BUG立方体
foreach (var log in stackOverflowBugLogs)
{
if (bugCubePrefab != null)
{
// 仅在下一次循环生成BUG立方体
if (log.BeTriggerCycle + 1 != currentCycle) continue;
GameObject go = Instantiate(bugCubePrefab, log.TargetTransform.position, Quaternion.identity);
bugCubes.Add(go);
}
}
// 检测天核区域内是否有BUG立方体
Collider[] colliders = Physics.OverlapBox(AreaCenter, AreaSize * 0.5f);
foreach (var collider in colliders)
{
if (bugCubes.Contains(collider.gameObject))
{
OnBUGHappenedInArea?.Invoke();
break;
}
}
}
// 可视化天核区域
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 0, 0, 0.3f);
Gizmos.DrawCube(AreaCenter, AreaSize);
}
}
}