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
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Minigames.Airplane.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Turbulence zone that applies random chaotic forces to airplanes.
|
||||
/// Creates unpredictable movement, adding challenge to navigation.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class AirplaneTurbulenceZone : MonoBehaviour
|
||||
{
|
||||
[Header("Turbulence Configuration")]
|
||||
[SerializeField] private float turbulenceStrength = 3f;
|
||||
[SerializeField] private float changeFrequency = 0.1f;
|
||||
|
||||
[Header("Optional Modifiers")]
|
||||
[SerializeField] private bool preventUpwardForce = false;
|
||||
[SerializeField] private float maxTotalForce = 10f;
|
||||
|
||||
[Header("Visual Feedback (Optional)")]
|
||||
[SerializeField] private ParticleSystem turbulenceParticles;
|
||||
|
||||
[Header("Debug")]
|
||||
[SerializeField] private bool showDebugLogs;
|
||||
|
||||
private float nextChangeTime;
|
||||
private Vector2 currentTurbulenceDirection;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Ensure collider is trigger
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
collider.isTrigger = true;
|
||||
}
|
||||
|
||||
// Initialize random direction
|
||||
UpdateTurbulenceDirection();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Change turbulence direction periodically
|
||||
if (Time.time >= nextChangeTime)
|
||||
{
|
||||
UpdateTurbulenceDirection();
|
||||
nextChangeTime = Time.time + changeFrequency;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTurbulenceDirection()
|
||||
{
|
||||
currentTurbulenceDirection = Random.insideUnitCircle.normalized;
|
||||
|
||||
// Prevent upward force if configured
|
||||
if (preventUpwardForce && currentTurbulenceDirection.y > 0)
|
||||
{
|
||||
currentTurbulenceDirection.y = -currentTurbulenceDirection.y;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerStay2D(Collider2D other)
|
||||
{
|
||||
// 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 turbulence force
|
||||
Vector2 turbulenceForce = currentTurbulenceDirection * turbulenceStrength;
|
||||
|
||||
// Clamp total force
|
||||
if (turbulenceForce.magnitude > maxTotalForce)
|
||||
{
|
||||
turbulenceForce = turbulenceForce.normalized * maxTotalForce;
|
||||
}
|
||||
|
||||
rb.AddForce(turbulenceForce, ForceMode2D.Force);
|
||||
|
||||
if (showDebugLogs && Time.frameCount % 30 == 0) // Log every 30 frames to avoid spam
|
||||
{
|
||||
Debug.Log($"[AirplaneTurbulenceZone] Applied turbulence: {turbulenceForce} to {other.name}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// Visualize turbulence zone in editor
|
||||
Gizmos.color = new Color(1f, 0.5f, 0f, 0.3f); // Orange transparent
|
||||
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider != null)
|
||||
{
|
||||
Gizmos.DrawCube(collider.bounds.center, collider.bounds.size);
|
||||
|
||||
// Draw current direction
|
||||
Gizmos.color = Color.red;
|
||||
Vector3 center = collider.bounds.center;
|
||||
Gizmos.DrawRay(center, (Vector3)currentTurbulenceDirection * 2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user