CapersProject/Assets/0_Voyage/_Scripts/Ship/VoyagePlayerShipMovementDebug.cs

256 lines
9.0 KiB
C#
Raw Normal View History

2025-07-15 10:56:39 +00:00
using UnityEngine;
namespace Voyage
{
#if UNITY_EDITOR
2025-07-15 11:10:46 +00:00
/// <summary>
/// 배의 움직임을 시각적으로 디버깅하기 위한 컴포넌트
/// </summary>
[RequireComponent(typeof(VoyagePlayerShipMovement))]
[RequireComponent(typeof(VoyagePlayerShipMovementVisual))]
2025-07-15 10:56:39 +00:00
public class VoyagePlayerShipDebug : MonoBehaviour
{
2025-07-15 11:10:46 +00:00
#region Debug Settings
[System.Serializable]
public class DebugSettings
{
public bool showDebugVisuals = true;
public float lineLength = 5f;
public float lineWidth = 0.1f;
[Header("라인 색상")]
public Color speedLineColor = Color.green;
public Color rotationSpeedLineColor = Color.magenta;
public Color rotationDeltaLineColor = Color.yellow;
public Color tiltLineColor = Color.red;
public Color waveHeightLineColor = Color.blue;
public Color wavePatternLineColor = Color.cyan;
}
[SerializeField] private DebugSettings settings = new();
#endregion
#region Line Renderers
private class DebugLines
{
public LineRenderer Speed { get; set; }
public LineRenderer RotationSpeed { get; set; }
public LineRenderer RotationDelta { get; set; }
public LineRenderer Tilt { get; set; }
public LineRenderer WaveHeight { get; set; }
public LineRenderer WavePattern { get; set; }
}
2025-07-15 10:56:39 +00:00
2025-07-15 11:10:46 +00:00
private DebugLines lines = new();
#endregion
#region Components
2025-07-15 10:56:39 +00:00
private VoyagePlayerShipMovement movement;
private VoyagePlayerShipMovementVisual movementVisual;
2025-07-15 11:10:46 +00:00
#endregion
2025-07-15 10:56:39 +00:00
2025-07-15 11:10:46 +00:00
#region Unity Messages
2025-07-15 10:56:39 +00:00
private void Start()
{
2025-07-15 11:10:46 +00:00
if (!settings.showDebugVisuals) return;
2025-07-15 10:56:39 +00:00
2025-07-15 11:10:46 +00:00
InitializeComponents();
InitializeDebugLines();
2025-07-15 10:56:39 +00:00
}
private void Update()
{
2025-07-15 11:10:46 +00:00
if (!settings.showDebugVisuals) return;
UpdateAllDebugLines();
2025-07-15 10:56:39 +00:00
}
2025-07-15 11:10:46 +00:00
#endregion
2025-07-15 10:56:39 +00:00
2025-07-15 11:10:46 +00:00
#region Initialization
private void InitializeComponents()
2025-07-15 10:56:39 +00:00
{
2025-07-15 11:10:46 +00:00
movement = GetComponent<VoyagePlayerShipMovement>();
movementVisual = GetComponent<VoyagePlayerShipMovementVisual>();
2025-07-15 10:56:39 +00:00
}
2025-07-15 11:10:46 +00:00
private void InitializeDebugLines()
2025-07-15 10:56:39 +00:00
{
2025-07-15 11:10:46 +00:00
lines.Speed = CreateLineRenderer("SpeedLine", settings.speedLineColor);
lines.RotationSpeed = CreateLineRenderer("RotationSpeedLine", settings.rotationSpeedLineColor);
lines.RotationDelta = CreateLineRenderer("RotationDeltaLine", settings.rotationDeltaLineColor);
lines.Tilt = CreateLineRenderer("TiltLine", settings.tiltLineColor);
lines.WaveHeight = CreateLineRenderer("WaveHeightLine", settings.waveHeightLineColor);
lines.WavePattern = CreateLineRenderer("WavePatternLine", settings.wavePatternLineColor, 50);
}
#endregion
2025-07-15 10:56:39 +00:00
2025-07-15 11:10:46 +00:00
#region Line Updates
private void UpdateAllDebugLines()
{
2025-07-15 10:56:39 +00:00
UpdateSpeedLine();
2025-07-15 11:10:46 +00:00
UpdateRotationLines();
2025-07-15 10:56:39 +00:00
UpdateTiltLine();
UpdateWaveVisualization();
}
private void UpdateSpeedLine()
{
2025-07-15 11:10:46 +00:00
Vector3 start = GetDebugLineStart(1.5f);
Vector3 direction = transform.forward * (movement.CurrentSpeed / movement.MaxSpeed);
Vector3 end = start + direction * (settings.lineLength * 2f);
DrawLine(lines.Speed, start, end);
2025-07-15 10:56:39 +00:00
}
2025-07-15 11:10:46 +00:00
private void UpdateRotationLines()
2025-07-15 10:56:39 +00:00
{
2025-07-15 11:10:46 +00:00
UpdateRotationSpeedArc();
UpdateRotationDeltaArc();
2025-07-15 10:56:39 +00:00
}
2025-07-15 11:10:46 +00:00
private void UpdateRotationSpeedArc()
2025-07-15 10:56:39 +00:00
{
2025-07-15 11:10:46 +00:00
if (Mathf.Abs(movement.CurrentRotationSpeed) <= 0.1f)
2025-07-15 10:56:39 +00:00
{
2025-07-15 11:10:46 +00:00
lines.RotationSpeed.positionCount = 0;
return;
2025-07-15 10:56:39 +00:00
}
2025-07-15 11:10:46 +00:00
DrawArc(lines.RotationSpeed,
GetDebugLineStart(1.2f),
settings.lineLength,
movement.CurrentRotationSpeed);
}
private void UpdateRotationDeltaArc()
{
if (movement.CurrentInput.magnitude <= 0.1f)
2025-07-15 10:56:39 +00:00
{
2025-07-15 11:10:46 +00:00
lines.RotationDelta.positionCount = 0;
return;
2025-07-15 10:56:39 +00:00
}
2025-07-15 11:10:46 +00:00
float deltaAngle = CalculateRotationDeltaAngle();
if (Mathf.Abs(deltaAngle) <= 0.1f)
2025-07-15 10:56:39 +00:00
{
2025-07-15 11:10:46 +00:00
lines.RotationDelta.positionCount = 0;
return;
2025-07-15 10:56:39 +00:00
}
2025-07-15 11:10:46 +00:00
DrawArc(lines.RotationDelta,
GetDebugLineStart(1.2f),
settings.lineLength * 1.05f,
deltaAngle);
2025-07-15 10:56:39 +00:00
}
private void UpdateTiltLine()
{
2025-07-15 11:10:46 +00:00
Vector3 start = GetDebugLineStart(1.5f);
2025-07-15 10:56:39 +00:00
Vector3 tiltDirection = movementVisual.MeshTransform.up;
2025-07-15 11:10:46 +00:00
DrawLine(lines.Tilt, start, start + tiltDirection * (settings.lineLength * 0.4f));
2025-07-15 10:56:39 +00:00
}
private void UpdateWaveVisualization()
2025-07-15 11:10:46 +00:00
{
UpdateWaveHeightLine();
UpdateWavePatternLine();
}
private void UpdateWaveHeightLine()
2025-07-15 10:56:39 +00:00
{
// 현재 파도 높이 표시
Vector3 waveStart = transform.position + Vector3.up * 1.5f - transform.forward * 1.5f;
2025-07-15 11:10:46 +00:00
Vector3 waveEnd = waveStart + Vector3.up * (movementVisual.CurrentWaveHeight * settings.lineLength);
DrawLine(lines.WaveHeight, waveStart, waveEnd);
}
2025-07-15 10:56:39 +00:00
2025-07-15 11:10:46 +00:00
private void UpdateWavePatternLine()
{
2025-07-15 10:56:39 +00:00
// 파도 패턴 시각화
2025-07-15 11:10:46 +00:00
Vector3[] wavePoints = new Vector3[lines.WavePattern.positionCount];
float waveLength = settings.lineLength * 2f;
2025-07-15 10:56:39 +00:00
for (int i = 0; i < wavePoints.Length; i++)
{
2025-07-15 11:10:46 +00:00
float t = (float)i / (lines.WavePattern.positionCount - 1);
2025-07-15 10:56:39 +00:00
float x = t * waveLength - waveLength * 0.5f;
2025-07-15 11:10:46 +00:00
float currentSpeedByUnit = movement.CurrentSpeed / movementVisual.WaveUnitSpeed;
2025-07-15 10:56:39 +00:00
currentSpeedByUnit = Mathf.Clamp01(currentSpeedByUnit);
2025-07-15 11:10:46 +00:00
float waveHeight = Mathf.Lerp(movementVisual.MinSpeedWaveHeight, movementVisual.MaxSpeedWaveHeight, currentSpeedByUnit);
float y = Mathf.Sin((movementVisual.WaveTime + x) * movementVisual.BaseWaveFrequency) * waveHeight;
2025-07-15 10:56:39 +00:00
wavePoints[i] = transform.position +
Vector3.right * x +
Vector3.up * (y + 2f); // 높이 오프셋
wavePoints[i] += Vector3.back * 3f + Vector3.down * 1f;
}
2025-07-15 11:10:46 +00:00
lines.WavePattern.SetPositions(wavePoints);
}
#endregion
#region Helper Methods
private Vector3 GetDebugLineStart(float heightOffset)
{
return transform.position + Vector3.up * heightOffset;
}
private float CalculateRotationDeltaAngle()
{
Vector3 inputDirection = new Vector3(movement.CurrentInput.x, 0, movement.CurrentInput.y).normalized;
Quaternion targetRotation = Quaternion.LookRotation(inputDirection, Vector3.up);
return Quaternion.Angle(transform.rotation, targetRotation);
2025-07-15 10:56:39 +00:00
}
2025-07-15 11:10:46 +00:00
private void DrawArc(LineRenderer lineRenderer, Vector3 center, float radius, float totalAngle)
{
const int ArcSegments = 10;
Vector3[] arcPoints = new Vector3[ArcSegments];
float angleStep = totalAngle / (ArcSegments - 1);
for (int i = 0; i < ArcSegments; i++)
{
float angle = angleStep * i;
Vector3 point = center + Quaternion.Euler(0, angle, 0) * transform.forward * radius;
arcPoints[i] = point;
}
lineRenderer.positionCount = ArcSegments;
lineRenderer.SetPositions(arcPoints);
}
2025-07-15 10:56:39 +00:00
2025-07-15 11:10:46 +00:00
private LineRenderer CreateLineRenderer(string name, Color color, int pointCount = 2)
2025-07-15 10:56:39 +00:00
{
2025-07-15 11:10:46 +00:00
GameObject lineObj = new GameObject($"Debug_{name}");
2025-07-15 10:56:39 +00:00
lineObj.transform.SetParent(transform);
2025-07-15 11:10:46 +00:00
2025-07-15 10:56:39 +00:00
LineRenderer line = lineObj.AddComponent<LineRenderer>();
2025-07-15 11:10:46 +00:00
InitializeLineRenderer(line, color, pointCount);
return line;
}
2025-07-15 10:56:39 +00:00
2025-07-15 11:10:46 +00:00
private void InitializeLineRenderer(LineRenderer line, Color color, int pointCount)
{
line.startWidth = settings.lineWidth;
line.endWidth = settings.lineWidth;
2025-07-15 10:56:39 +00:00
line.material = new Material(Shader.Find("Universal Render Pipeline/Unlit"));
2025-07-15 11:10:46 +00:00
line.startColor = line.endColor = color;
line.positionCount = pointCount;
2025-07-15 10:56:39 +00:00
line.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
line.receiveShadows = false;
line.material.color = color;
}
private void DrawLine(LineRenderer line, Vector3 start, Vector3 end)
{
if (line is null) return;
2025-07-15 11:10:46 +00:00
2025-07-15 10:56:39 +00:00
line.positionCount = 2;
line.SetPosition(0, start);
line.SetPosition(1, end);
}
2025-07-15 11:10:46 +00:00
#endregion
2025-07-15 10:56:39 +00:00
}
#endif
}