102 lines
3.2 KiB
C#
102 lines
3.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using Core;
|
||
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
|
||
namespace Script.Gameplay.Global
|
||
{
|
||
public struct TransformSnapshot
|
||
{
|
||
public Vector3 Position;
|
||
public Quaternion Rotation;
|
||
public Vector3 Scale;
|
||
|
||
public TransformSnapshot(Transform t)
|
||
{
|
||
Position = t.position;
|
||
Rotation = t.rotation;
|
||
Scale = t.localScale;
|
||
}
|
||
}
|
||
|
||
public struct StackOverflowBUGLog
|
||
{
|
||
public TransformSnapshot TargetSnapshot;
|
||
public int BeTriggerCycle; // 触发时的游戏循环次数
|
||
|
||
public StackOverflowBUGLog(Transform targetTransform, int beTriggerCycle)
|
||
{
|
||
TargetSnapshot = new TransformSnapshot(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.Log("天核区域内检测到BUG立方体!"); });
|
||
|
||
GameManager.Instance.OnGameStart += GenerateBugCubes;
|
||
}
|
||
|
||
|
||
public void LogStackOverflowBug(Transform targetTransform)
|
||
{
|
||
int beTriggerCycle = GameDataManager.Instance.TotalLoopCount;
|
||
stackOverflowBugLogs.Add(new StackOverflowBUGLog(targetTransform, beTriggerCycle));
|
||
}
|
||
|
||
public void GenerateBugCubes()
|
||
{
|
||
foreach (var bugCube in bugCubes)
|
||
{
|
||
Destroy(bugCube);
|
||
}
|
||
bugCubes.Clear();
|
||
int currentCycle = GameDataManager.Instance.TotalLoopCount;
|
||
|
||
// 记录的BUG位置生成BUG立方体
|
||
foreach (var log in stackOverflowBugLogs)
|
||
{
|
||
if (bugCubePrefab != null)
|
||
{
|
||
// 仅在下一、二次循环生成BUG立方体
|
||
if (currentCycle - log.BeTriggerCycle >= 0 && currentCycle - log.BeTriggerCycle < 2)
|
||
{
|
||
GameObject go = Instantiate(bugCubePrefab, log.TargetSnapshot.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);
|
||
}
|
||
}
|
||
} |