Files
AppleHillsProduction/Assets/Scripts/Minigames/Airplane/Interactive/AirplaneSpeedRing.cs
tschesky c27f22ef0a MVP of the plane throwing game (#77)
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com>
Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: #77
2025-12-07 19:36:57 +00:00

123 lines
3.8 KiB
C#

using UnityEngine;
namespace Minigames.Airplane.Interactive
{
/// <summary>
/// Speed boost ring that increases airplane velocity when passed through.
/// Can be used as collectible power-ups or checkpoint rings.
/// </summary>
[RequireComponent(typeof(Collider2D))]
public class AirplaneSpeedRing : MonoBehaviour
{
[Header("Boost Configuration")]
[SerializeField] private float velocityMultiplier = 1.5f;
[SerializeField] private float boostDuration = 1f;
[SerializeField] private bool oneTimeUse = true;
[Header("Optional Constraints")]
[SerializeField] private float minResultSpeed = 5f;
[SerializeField] private float maxResultSpeed = 25f;
[Header("Visual Feedback")]
[SerializeField] private GameObject ringVisual;
[SerializeField] private ParticleSystem collectEffect;
[SerializeField] private AudioSource collectSound;
[Header("Debug")]
[SerializeField] private bool showDebugLogs;
private bool hasBeenUsed;
private void Awake()
{
// Ensure collider is trigger
var collider = GetComponent<Collider2D>();
if (collider != null)
{
collider.isTrigger = true;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
// Check if already used
if (oneTimeUse && hasBeenUsed) return;
// Check if it's an airplane
var airplane = other.GetComponent<Core.AirplaneController>();
if (airplane == null || !airplane.IsFlying) return;
var rb = other.GetComponent<Rigidbody2D>();
if (rb == null) return;
// Apply speed boost
Vector2 boostedVelocity = rb.linearVelocity * velocityMultiplier;
// Clamp to constraints
float speed = boostedVelocity.magnitude;
if (speed < minResultSpeed)
{
boostedVelocity = boostedVelocity.normalized * minResultSpeed;
}
else if (speed > maxResultSpeed)
{
boostedVelocity = boostedVelocity.normalized * maxResultSpeed;
}
rb.linearVelocity = boostedVelocity;
// Mark as used
hasBeenUsed = true;
// Trigger effects
PlayCollectEffects();
if (showDebugLogs)
{
Debug.Log($"[AirplaneSpeedRing] Boosted {other.name}: velocity={boostedVelocity.magnitude:F1}");
}
// Hide or destroy ring
if (oneTimeUse)
{
if (ringVisual != null)
{
ringVisual.SetActive(false);
}
else
{
Destroy(gameObject, collectEffect != null ? collectEffect.main.duration : 0.5f);
}
}
}
private void PlayCollectEffects()
{
// Play particle effect
if (collectEffect != null)
{
collectEffect.Play();
}
// Play sound
if (collectSound != null)
{
collectSound.Play();
}
}
private void OnDrawGizmos()
{
// Visualize ring in editor
Gizmos.color = hasBeenUsed ? Color.gray : Color.yellow;
var collider = GetComponent<Collider2D>();
if (collider != null)
{
Gizmos.DrawWireSphere(collider.bounds.center, collider.bounds.extents.magnitude);
}
}
}
}