Files
AppleHillsProduction/Assets/Scripts/Minigames/BirdPooper/BirdPooperDebugDisplay.cs
2025-12-18 14:09:05 +01:00

138 lines
5.5 KiB
C#

using UnityEngine;
using TMPro;
namespace Minigames.BirdPooper
{
/// <summary>
/// On-screen debug display for Bird Pooper minigame diagnostics.
/// Shows critical values that might differ between PC and mobile platforms.
/// </summary>
public class BirdPooperDebugDisplay : MonoBehaviour
{
[Header("References")]
[SerializeField] private BirdPlayerController birdController;
[SerializeField] private TextMeshProUGUI debugText;
[Header("Settings")]
[SerializeField] private bool showDebugDisplay = true;
[SerializeField] private float updateInterval = 0.1f; // Update display every 0.1 seconds
private float _nextUpdateTime;
private int _frameCount;
private float _minDeltaTime = float.MaxValue;
private float _maxDeltaTime = 0f;
private float _avgDeltaTime;
private float _deltaTimeSum;
private void Start()
{
if (debugText == null)
{
Debug.LogError("[BirdPooperDebugDisplay] Debug text reference not assigned!");
enabled = false;
return;
}
if (birdController == null)
{
birdController = FindFirstObjectByType<BirdPlayerController>();
if (birdController == null)
{
Debug.LogError("[BirdPooperDebugDisplay] Could not find BirdPlayerController!");
enabled = false;
return;
}
}
debugText.gameObject.SetActive(showDebugDisplay);
_nextUpdateTime = Time.time + updateInterval;
}
private void Update()
{
if (!showDebugDisplay || debugText == null)
return;
// Track deltaTime statistics
_frameCount++;
_deltaTimeSum += Time.deltaTime;
if (Time.deltaTime < _minDeltaTime) _minDeltaTime = Time.deltaTime;
if (Time.deltaTime > _maxDeltaTime) _maxDeltaTime = Time.deltaTime;
_avgDeltaTime = _deltaTimeSum / _frameCount;
// Update display at specified interval
if (Time.time >= _nextUpdateTime)
{
UpdateDebugDisplay();
_nextUpdateTime = Time.time + updateInterval;
}
}
private void UpdateDebugDisplay()
{
if (debugText == null) return;
// Get settings from the bird controller via reflection (hacky but works for debug)
var settingsField = typeof(BirdPlayerController).GetField("_settings",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var settings = settingsField?.GetValue(birdController) as Core.Settings.IBirdPooperSettings;
var velocityField = typeof(BirdPlayerController).GetField("_verticalVelocity",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
float velocity = velocityField != null ? (float)velocityField.GetValue(birdController) : 0f;
var rbField = typeof(BirdPlayerController).GetField("_rb",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var rb = rbField?.GetValue(birdController) as Rigidbody2D;
// Calculate actual FPS
float fps = _avgDeltaTime > 0 ? 1f / _avgDeltaTime : 0f;
// Build debug string
string debugInfo = "<b>BIRD POOPER DEBUG</b>\n";
debugInfo += "───────────\n";
debugInfo += $"<b>PLATFORM:</b> {Application.platform}\n";
debugInfo += $"<b>TIME SCALE:</b> {Time.timeScale:F2}\n";
debugInfo += $"<b>TARGET FPS:</b> {Application.targetFrameRate}\n";
debugInfo += $"<b>ACTUAL FPS:</b> {fps:F1}\n";
debugInfo += "───────────\n";
debugInfo += $"<b>ΔTime:</b> {Time.deltaTime:F4}s\n";
debugInfo += $"<b>ΔTime Min:</b> {_minDeltaTime:F4}s\n";
debugInfo += $"<b>ΔTime Max:</b> {_maxDeltaTime:F4}s\n";
debugInfo += $"<b>ΔTime Avg:</b> {_avgDeltaTime:F4}s\n";
debugInfo += $"<color=yellow><b>Fixed ΔT:</b> {Time.fixedDeltaTime:F4}s</color>\n";
debugInfo += "───────────\n";
if (settings != null)
{
debugInfo += $"<b>Gravity:</b> {settings.Gravity}\n";
debugInfo += $"<b>Flap Force:</b> {settings.FlapForce}\n";
debugInfo += $"<b>Max Fall:</b> {settings.MaxFallSpeed}\n";
debugInfo += "───────────\n";
}
debugInfo += $"<b>Velocity:</b> {velocity:F2}\n";
if (rb != null)
{
debugInfo += $"<b>Position Y:</b> {rb.position.y:F2}\n";
}
debugInfo += $"<b>Dead:</b> {birdController.IsDead}\n";
debugText.text = debugInfo;
}
/// <summary>
/// Toggle debug display visibility - can be called from UI button
/// </summary>
public void ToggleDisplay()
{
showDebugDisplay = !showDebugDisplay;
if (debugText != null)
{
debugText.gameObject.SetActive(showDebugDisplay);
}
}
}
}