Refactored trajectory component

This commit is contained in:
Michal Pikulski
2025-12-05 11:03:47 +01:00
committed by Michal Pikulski
parent a4363fecb3
commit 11833ba503
11 changed files with 177 additions and 428 deletions

View File

@@ -126,21 +126,6 @@ namespace Minigames.FortFight.Core
[Tooltip("Downward velocity when dropping (m/s)")]
[SerializeField] private float ceilingFanDropSpeed = 20f;
[Header("Slingshot Settings")]
[Tooltip("Base launch force multiplier - higher values = projectiles fly farther")]
[SerializeField] private float baseLaunchForce = 20f;
[Tooltip("Minimum force multiplier (0-1, e.g. 0.1 = 10% of max force required to launch)")]
[Range(0f, 1f)]
[SerializeField] private float minForceMultiplier = 0.1f;
[Tooltip("Maximum force multiplier (0-1, e.g. 1.0 = 100% at max drag distance)")]
[Range(0f, 2f)]
[SerializeField] private float maxForceMultiplier = 1f;
[Tooltip("How long to keep trajectory visible after launching (seconds)")]
[SerializeField] private float trajectoryLockDuration = 2f;
[Header("Physics Layers")]
[Tooltip("Layer for fort blocks - projectiles will collide with these (Default: Layer 8 'FortBlock')")]
[AppleHills.Core.Settings.Layer]
@@ -180,11 +165,6 @@ namespace Minigames.FortFight.Core
public Color DamageColorTint => damageColorTint;
public float BaseLaunchForce => baseLaunchForce;
public float MinForceMultiplier => minForceMultiplier;
public float MaxForceMultiplier => maxForceMultiplier;
public float TrajectoryLockDuration => trajectoryLockDuration;
public float VacuumSlideSpeed => vacuumSlideSpeed;
public int VacuumDestroyBlockCount => vacuumDestroyBlockCount;
public float VacuumBlockDamage => vacuumBlockDamage;

View File

@@ -16,9 +16,7 @@ namespace Minigames.FortFight.Core
{
#region Inspector Properties
[Header("FortFight Specific")]
[Tooltip("Trajectory preview component")]
[SerializeField] private TrajectoryPreview trajectoryPreview;
// Note: trajectoryPreview is inherited from DragLaunchController base class
#endregion
@@ -86,28 +84,13 @@ namespace Minigames.FortFight.Core
{
base.OnManagedAwake();
// Base class handles launchAnchor (previously projectileSpawnPoint)
if (trajectoryPreview == null)
{
trajectoryPreview = GetComponent<TrajectoryPreview>();
}
// Base class handles launchAnchor and trajectoryPreview
// Set debug logging from developer settings
showDebugLogs = CachedDevSettings?.SlingshotShowDebugLogs ?? false;
}
internal override void OnManagedStart()
{
base.OnManagedStart();
// Hide trajectory by default
if (trajectoryPreview != null)
{
trajectoryPreview.Hide();
}
}
#endregion
#region Override Methods
@@ -129,6 +112,19 @@ namespace Minigames.FortFight.Core
LaunchProjectile(direction, force);
}
protected override float GetProjectileMass()
{
if (_currentAmmo == null)
{
if (showDebugLogs)
Logging.Warning("[SlingshotController] No ammo selected, cannot get mass!");
return 1f; // Default fallback
}
// Read from ProjectileConfig settings - same source as ProjectileBase.Initialize()
return _currentAmmo.mass;
}
#endregion
#region Projectile Management
@@ -173,7 +169,7 @@ namespace Minigames.FortFight.Core
// Lock trajectory to show the shot path
if (trajectoryPreview != null)
{
float lockDuration = CachedSettings?.TrajectoryLockDuration ?? 2f;
float lockDuration = Config?.trajectoryLockDuration ?? 2f;
trajectoryPreview.LockTrajectory(lockDuration);
}

View File

@@ -1,153 +0,0 @@
using Core;
using UnityEngine;
namespace Minigames.FortFight.Core
{
/// <summary>
/// Displays trajectory prediction line for projectile launches.
/// Shows dotted line preview of projectile arc.
/// </summary>
[RequireComponent(typeof(LineRenderer))]
public class TrajectoryPreview : MonoBehaviour
{
[Header("Trajectory Settings")]
[Tooltip("Number of points to simulate (physics steps)")]
[SerializeField] private int simulationSteps = 50;
[Header("Visual")]
[Tooltip("Color of trajectory line")]
[SerializeField] private Color lineColor = Color.yellow;
[Tooltip("Width of trajectory line")]
[SerializeField] private float lineWidth = 0.1f;
private LineRenderer lineRenderer;
private bool isLocked = false;
private float lockTimer = 0f;
private float lockDuration = 0f;
private void Awake()
{
lineRenderer = GetComponent<LineRenderer>();
// Configure line renderer
if (lineRenderer != null)
{
lineRenderer.startWidth = lineWidth;
lineRenderer.endWidth = lineWidth;
lineRenderer.startColor = lineColor;
lineRenderer.endColor = lineColor;
lineRenderer.positionCount = simulationSteps;
lineRenderer.enabled = false;
}
}
private void Update()
{
if (isLocked)
{
lockTimer += Time.deltaTime;
if (lockTimer >= lockDuration)
{
isLocked = false;
Hide();
}
}
}
/// <summary>
/// Show the trajectory preview
/// </summary>
public void Show()
{
if (lineRenderer != null)
{
lineRenderer.enabled = true;
}
}
/// <summary>
/// Hide the trajectory preview (unless locked)
/// </summary>
public void Hide()
{
// Don't hide if trajectory is locked
if (isLocked)
return;
if (lineRenderer != null)
{
lineRenderer.enabled = false;
}
}
/// <summary>
/// Lock the current trajectory display for a duration
/// </summary>
public void LockTrajectory(float duration)
{
isLocked = true;
lockTimer = 0f;
lockDuration = duration;
// Ensure line is visible
if (lineRenderer != null)
{
lineRenderer.enabled = true;
}
}
/// <summary>
/// Update the trajectory preview with new parameters.
/// Uses Physics.fixedDeltaTime for accurate simulation matching Unity's physics.
/// </summary>
/// <param name="startPosition">Starting position of trajectory</param>
/// <param name="direction">Launch direction (normalized)</param>
/// <param name="force">Launch force (impulse)</param>
/// <param name="mass">Projectile mass</param>
public void UpdateTrajectory(Vector2 startPosition, Vector2 direction, float force, float mass = 1f)
{
if (lineRenderer == null) return;
// Calculate initial velocity: impulse force F gives velocity v = F/m
Vector2 startVelocity = (direction * force) / mass;
// Get gravity with projectile gravity scale from settings
var settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IFortFightSettings>();
float gravityScale = settings?.ProjectileGravityScale ?? 1f;
Vector2 gravity = new Vector2(Physics2D.gravity.x, Physics2D.gravity.y) * gravityScale;
// Simulate trajectory using Unity's physics time step
Vector3[] points = new Vector3[simulationSteps];
Vector2 pos = startPosition;
Vector2 vel = startVelocity;
for (int i = 0; i < simulationSteps; i++)
{
// Set current position
points[i] = new Vector3(pos.x, pos.y, 0);
// Update velocity (gravity applied over fixedDeltaTime)
vel = vel + gravity * Time.fixedDeltaTime;
// Update position (velocity applied over fixedDeltaTime)
pos = pos + vel * Time.fixedDeltaTime;
// Optional: Stop if hits ground (y < threshold)
if (pos.y < -10f)
{
// Fill remaining points at ground level
for (int j = i + 1; j < simulationSteps; j++)
{
points[j] = new Vector3(pos.x, -10f, 0);
}
break;
}
}
lineRenderer.positionCount = simulationSteps;
lineRenderer.SetPositions(points);
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: b1e26667c6d4415f8dc51e4a58ba9479
timeCreated: 1764682615