Refactored trajectory component
This commit is contained in:
@@ -44,12 +44,6 @@ namespace Minigames.Airplane.Core
|
||||
[Tooltip("Gravity multiplier for arc calculation")]
|
||||
[SerializeField] private float gravity = 9.81f;
|
||||
|
||||
[Tooltip("Mass of the airplane")]
|
||||
[SerializeField] private float mass = 1f;
|
||||
|
||||
[Tooltip("Maximum flight time before timeout (seconds)")]
|
||||
[SerializeField] private float maxFlightTime = 10f;
|
||||
|
||||
[Header("Visual")]
|
||||
[Tooltip("Should airplane rotate to face velocity direction?")]
|
||||
[SerializeField] private bool rotateToVelocity = true;
|
||||
@@ -68,6 +62,10 @@ namespace Minigames.Airplane.Core
|
||||
private float flightTimer = 0f;
|
||||
private string lastHitTarget = null;
|
||||
|
||||
// Runtime values loaded from settings
|
||||
private float mass;
|
||||
private float maxFlightTime;
|
||||
|
||||
public bool IsFlying => isFlying;
|
||||
public Vector2 CurrentVelocity => currentVelocity;
|
||||
public string LastHitTarget => lastHitTarget;
|
||||
@@ -80,6 +78,20 @@ namespace Minigames.Airplane.Core
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Load settings
|
||||
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IAirplaneSettings>();
|
||||
if (settings != null)
|
||||
{
|
||||
mass = settings.AirplaneMass;
|
||||
maxFlightTime = settings.MaxFlightTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("[AirplaneController] AirplaneSettings not found, using defaults!");
|
||||
mass = 1f;
|
||||
maxFlightTime = 10f;
|
||||
}
|
||||
|
||||
// Cache components
|
||||
rb2D = GetComponent<Rigidbody2D>();
|
||||
airplaneCollider = GetComponent<Collider2D>();
|
||||
|
||||
@@ -36,6 +36,20 @@ namespace Minigames.Airplane.Core
|
||||
return airplanePrefab;
|
||||
}
|
||||
|
||||
protected override float GetProjectileMass()
|
||||
{
|
||||
var settings = GameManager.GetSettingsObject<IAirplaneSettings>();
|
||||
if (settings == null)
|
||||
{
|
||||
if (showDebugLogs)
|
||||
Logging.Warning("[AirplaneLaunchController] AirplaneSettings not found!");
|
||||
return 1f; // Default fallback
|
||||
}
|
||||
|
||||
// Read from AirplaneSettings - same mass that AirplaneController uses
|
||||
return settings.AirplaneMass;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Inspector Properties
|
||||
@@ -84,92 +98,8 @@ namespace Minigames.Airplane.Core
|
||||
|
||||
#region Visual Feedback
|
||||
|
||||
protected override void ShowPreview()
|
||||
{
|
||||
// Show anchor visual
|
||||
if (anchorVisual != null)
|
||||
{
|
||||
anchorVisual.SetActive(true);
|
||||
}
|
||||
|
||||
// Show trajectory line (will be updated during drag)
|
||||
if (trajectoryLine != null)
|
||||
{
|
||||
trajectoryLine.enabled = false; // Only show during drag
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneLaunchController] Preview shown");
|
||||
}
|
||||
|
||||
protected override void HidePreview()
|
||||
{
|
||||
// Hide anchor visual
|
||||
if (anchorVisual != null)
|
||||
{
|
||||
anchorVisual.SetActive(false);
|
||||
}
|
||||
|
||||
// Hide trajectory line
|
||||
if (trajectoryLine != null)
|
||||
{
|
||||
trajectoryLine.enabled = false;
|
||||
}
|
||||
|
||||
if (showDebugLogs) Logging.Debug("[AirplaneLaunchController] Preview hidden");
|
||||
}
|
||||
|
||||
protected override void UpdateVisuals(Vector2 currentPosition, Vector2 direction, float force, float dragDistance, float mass)
|
||||
{
|
||||
// Show trajectory line during drag
|
||||
if (trajectoryLine != null && trajectoryLine.enabled == false && dragDistance > 0.1f)
|
||||
{
|
||||
trajectoryLine.enabled = true;
|
||||
}
|
||||
|
||||
// Update trajectory preview
|
||||
if (trajectoryLine != null && trajectoryLine.enabled)
|
||||
{
|
||||
UpdateTrajectoryPreview(direction, force, mass);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the trajectory preview line
|
||||
/// </summary>
|
||||
private void UpdateTrajectoryPreview(Vector2 direction, float force, float mass)
|
||||
{
|
||||
if (trajectoryLine == null) return;
|
||||
|
||||
var config = Config;
|
||||
if (config == null) return;
|
||||
|
||||
if (mass <= 0f)
|
||||
{
|
||||
if (showDebugLogs) Logging.Warning("[AirplaneLaunchController] Cannot calculate trajectory with zero mass!");
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 startPos = launchAnchor.position;
|
||||
float initialSpeed = force / mass;
|
||||
Vector2 velocity = direction * initialSpeed;
|
||||
|
||||
// Get gravity from prefab's Rigidbody2D (Physics2D.gravity.magnitude * rb.gravityScale)
|
||||
float gravity = GetGravity();
|
||||
|
||||
trajectoryLine.positionCount = config.trajectoryPoints;
|
||||
|
||||
// Calculate trajectory points using config values
|
||||
for (int i = 0; i < config.trajectoryPoints; i++)
|
||||
{
|
||||
float time = i * config.trajectoryTimeStep;
|
||||
|
||||
// Calculate position at this time
|
||||
float x = startPos.x + velocity.x * time;
|
||||
float y = startPos.y + velocity.y * time - 0.5f * gravity * time * time;
|
||||
|
||||
trajectoryLine.SetPosition(i, new Vector3(x, y, 0));
|
||||
}
|
||||
}
|
||||
// Base class handles trajectory preview via TrajectoryPreview component
|
||||
// No custom visual feedback needed for airplane - using default implementation
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
Reference in New Issue
Block a user