Some more airplane game doodles

This commit is contained in:
Michal Pikulski
2025-12-08 12:56:14 +01:00
parent 861749485e
commit 41d99a1390
58 changed files with 3319 additions and 147 deletions

View File

@@ -3,11 +3,13 @@
namespace Minigames.Airplane.Interactive
{
/// <summary>
/// Gravity well that pulls airplanes toward its center.
/// Gravity well that pulls airplanes toward its Y position (vertical only).
/// Plane below the well gets pulled UP, plane above gets pulled DOWN.
/// Does NOT affect horizontal (X) velocity - plane maintains forward momentum.
/// Creates challenging "danger zones" that players must avoid or escape from.
/// </summary>
[RequireComponent(typeof(Collider2D))]
public class AirplaneGravityWell : MonoBehaviour
public class AirplaneGravityWell : MonoBehaviour, ISpawnInitializable
{
[Header("Gravity Configuration")]
[SerializeField] private float pullStrength = 5f;
@@ -37,7 +39,14 @@ namespace Minigames.Airplane.Interactive
{
collider.isTrigger = true;
}
}
/// <summary>
/// Called by spawn manager after object is positioned.
/// Caches the final Y position for pull calculations.
/// </summary>
public void Initialize()
{
centerPosition = transform.position;
}
@@ -59,31 +68,31 @@ namespace Minigames.Airplane.Interactive
var rb = other.GetComponent<Rigidbody2D>();
if (rb == null) return;
// Calculate direction and distance to center
// Calculate VERTICAL distance only (Y-axis only)
Vector2 airplanePos = rb.position;
Vector2 toCenter = centerPosition - airplanePos;
float distance = toCenter.magnitude;
float yDistance = centerPosition.y - airplanePos.y;
float absYDistance = Mathf.Abs(yDistance);
// Prevent division by zero
if (distance < minPullDistance)
if (absYDistance < minPullDistance)
{
distance = minPullDistance;
absYDistance = minPullDistance;
}
// Calculate pull force
// Calculate pull force based on vertical distance
float forceMagnitude;
if (useInverseSquare)
{
// Realistic gravity-like force (inverse square law)
forceMagnitude = pullStrength / (distance * distance);
forceMagnitude = pullStrength / (absYDistance * absYDistance);
}
else
{
// Linear falloff based on distance
// Linear falloff based on vertical distance
var collider = GetComponent<Collider2D>();
float maxDistance = collider != null ? collider.bounds.extents.magnitude : 5f;
float normalizedDistance = Mathf.Clamp01(distance / maxDistance);
float maxDistance = collider != null ? collider.bounds.extents.y * 2f : 5f; // Use height, not diagonal
float normalizedDistance = Mathf.Clamp01(absYDistance / maxDistance);
float falloff = pullFalloff.Evaluate(1f - normalizedDistance);
forceMagnitude = pullStrength * falloff;
}
@@ -91,13 +100,16 @@ namespace Minigames.Airplane.Interactive
// Clamp force
forceMagnitude = Mathf.Min(forceMagnitude, maxPullForce);
// Apply force toward center
Vector2 pullForce = toCenter.normalized * forceMagnitude;
// Apply force ONLY in Y direction, toward the well's Y position
// If plane is below (yDistance > 0), pull up (+Y)
// If plane is above (yDistance < 0), pull down (-Y)
float yForceDirection = yDistance > 0 ? 1f : -1f;
Vector2 pullForce = new Vector2(0f, yForceDirection * forceMagnitude);
rb.AddForce(pullForce, ForceMode2D.Force);
if (showDebugLogs && Time.frameCount % 30 == 0) // Log every 30 frames
{
Debug.Log($"[AirplaneGravityWell] Pulling {other.name}: force={forceMagnitude:F2}, distance={distance:F2}");
Debug.Log($"[AirplaneGravityWell] Pulling {other.name}: force={forceMagnitude:F2} {(yForceDirection > 0 ? "UP" : "DOWN")}, Y-distance={absYDistance:F2}");
}
}