PuckDebugVisualizer.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. #endif
  5. /// <summary>
  6. /// Helper script to visualize puck-related debug information in the scene
  7. /// Attach this to any GameObject to see possession ranges, puck trajectory, etc.
  8. /// </summary>
  9. public class PuckDebugVisualizer : MonoBehaviour
  10. {
  11. [Header("Visualization Settings")]
  12. [SerializeField] private bool showPossessionRanges = true;
  13. [SerializeField] private bool showPuckTrajectory = true;
  14. [SerializeField] private bool showPlayerLabels = true;
  15. [SerializeField] private float trajectoryTime = 2.0f;
  16. [Header("Colors")]
  17. [SerializeField] private Color possessionRangeColor = new Color(1f, 1f, 0f, 0.3f);
  18. [SerializeField] private Color hasPuckColor = new Color(0f, 1f, 0f, 0.5f);
  19. [SerializeField] private Color noPuckColor = new Color(1f, 0f, 0f, 0.3f);
  20. private PuckController puck;
  21. private PlayerController[] allPlayers;
  22. void Start()
  23. {
  24. puck = FindObjectOfType<PuckController>();
  25. allPlayers = FindObjectsOfType<PlayerController>();
  26. }
  27. #if UNITY_EDITOR
  28. void OnDrawGizmos()
  29. {
  30. if (!Application.isPlaying)
  31. {
  32. puck = FindObjectOfType<PuckController>();
  33. allPlayers = FindObjectsOfType<PlayerController>();
  34. }
  35. if (showPossessionRanges && allPlayers != null)
  36. {
  37. DrawPossessionRanges();
  38. }
  39. if (showPuckTrajectory && puck != null)
  40. {
  41. DrawPuckTrajectory();
  42. }
  43. }
  44. void DrawPossessionRanges()
  45. {
  46. foreach (PlayerController player in allPlayers)
  47. {
  48. if (player == null) continue;
  49. // Choose color based on possession
  50. bool hasPuck = player.HasPuck();
  51. Gizmos.color = hasPuck ? hasPuckColor : possessionRangeColor;
  52. // Draw sphere showing pickup range
  53. Gizmos.DrawWireSphere(player.transform.position, 1.5f);
  54. // Draw label
  55. if (showPlayerLabels && player.stats != null)
  56. {
  57. Vector3 labelPos = player.transform.position + Vector3.up * 2f;
  58. string label = $"{player.stats.playerName}\n{player.stats.position}";
  59. if (hasPuck) label += "\n[HAS PUCK]";
  60. Handles.Label(labelPos, label);
  61. }
  62. }
  63. }
  64. void DrawPuckTrajectory()
  65. {
  66. if (puck == null) return;
  67. // Draw current puck position
  68. Gizmos.color = Color.yellow;
  69. Gizmos.DrawWireSphere(puck.transform.position, 0.15f);
  70. // If puck is moving, draw predicted trajectory
  71. Vector3 velocity = puck.GetVelocity();
  72. if (velocity.magnitude > 0.1f)
  73. {
  74. Gizmos.color = Color.cyan;
  75. Vector3 currentPos = puck.transform.position;
  76. float timeStep = 0.1f;
  77. for (float t = 0; t < trajectoryTime; t += timeStep)
  78. {
  79. Vector3 nextPos = currentPos + velocity * timeStep;
  80. Gizmos.DrawLine(currentPos, nextPos);
  81. currentPos = nextPos;
  82. // Approximate drag
  83. velocity *= 0.98f;
  84. }
  85. // Draw end point
  86. Gizmos.DrawWireSphere(currentPos, 0.1f);
  87. }
  88. // Draw line to current carrier
  89. if (puck.CurrentCarrier != null)
  90. {
  91. Gizmos.color = Color.green;
  92. Gizmos.DrawLine(puck.transform.position, puck.CurrentCarrier.transform.position);
  93. }
  94. }
  95. #endif
  96. // Runtime debug GUI
  97. void OnGUI()
  98. {
  99. if (!Application.isPlaying) return;
  100. GUILayout.BeginArea(new Rect(10, 10, 300, 400));
  101. GUILayout.BeginVertical("box");
  102. GUILayout.Label("<b>Puck Debug Info</b>", new GUIStyle(GUI.skin.label) { richText = true, fontSize = 14 });
  103. if (puck != null)
  104. {
  105. GUILayout.Label($"Puck Position: {puck.transform.position}");
  106. GUILayout.Label($"Puck Velocity: {puck.GetVelocity().magnitude:F2} m/s");
  107. GUILayout.Label($"Puck Speed: {puck.GetSpeed():F2} m/s");
  108. if (puck.IsLoose)
  109. {
  110. GUILayout.Label("<color=yellow>Puck Status: LOOSE</color>", new GUIStyle(GUI.skin.label) { richText = true });
  111. }
  112. else if (puck.CurrentCarrier != null)
  113. {
  114. GUILayout.Label($"<color=green>Carried by: {puck.CurrentCarrier.stats.playerName}</color>",
  115. new GUIStyle(GUI.skin.label) { richText = true });
  116. }
  117. }
  118. GUILayout.Space(10);
  119. GUILayout.Label("<b>Players with Puck</b>", new GUIStyle(GUI.skin.label) { richText = true });
  120. if (allPlayers != null)
  121. {
  122. foreach (PlayerController player in allPlayers)
  123. {
  124. if (player != null && player.HasPuck())
  125. {
  126. GUILayout.Label($"• {player.stats.playerName} ({player.stats.position})");
  127. }
  128. }
  129. }
  130. GUILayout.Space(10);
  131. // Test buttons
  132. if (GUILayout.Button("Trigger Faceoff (F)"))
  133. {
  134. FaceoffManager manager = FindObjectOfType<FaceoffManager>();
  135. if (manager != null)
  136. {
  137. manager.StartFaceoff();
  138. }
  139. }
  140. if (puck != null && puck.CurrentCarrier != null)
  141. {
  142. if (GUILayout.Button("Release Puck"))
  143. {
  144. puck.Release();
  145. }
  146. }
  147. GUILayout.EndVertical();
  148. GUILayout.EndArea();
  149. }
  150. }