using UnityEngine;
using UnityEngine.InputSystem;
/// <summary>
/// GAME 1303 Tier 3 — Extra jumps in the air. Apep's Trial, Spark of Corruption,
/// Roach Invasion (the bee).
///
/// SETUP
/// - Add to the player next to PlayerController2D (v1.1). That's it — it listens to
/// the same "Jump" action. Extra Jumps = 1 for a double jump, 2 for triple.
/// - Air Jump Force is usually a bit less than the ground jump.
/// - Optional "DoubleJump" Animator trigger and "jump2" sound.
///
/// How it works: PlayerInput's Send Messages calls OnJump on EVERY component on the
/// object. PlayerController2D handles ground/coyote jumps; this script only acts when
/// the controller can't (in the air past coyote time), then clears the jump buffer so
/// the controller doesn't also jump on landing.
/// </summary>
public class DoubleJump : MonoBehaviour
{
public int extraJumps = 1;
public float airJumpForce = 12f;
public string animatorTrigger = "DoubleJump";
public string sfxName = "jump";
public int JumpsLeft { get; private set; }
private PlayerController2D controller;
private Animator anim;
private WallSlideJump wallSlide;
private LadderClimber ladder;
void Awake()
{
controller = GetComponent<PlayerController2D>();
anim = GetComponent<Animator>();
wallSlide = GetComponent<WallSlideJump>();
ladder = GetComponent<LadderClimber>();
JumpsLeft = extraJumps;
}
void OnEnable() { controller.OnLanded.AddListener(Refill); }
void OnDisable() { controller.OnLanded.RemoveListener(Refill); }
void OnJump(InputValue v)
{
if (!v.isPressed || !controller.InputEnabled || controller.MovementLocked) return;
if (controller.IsGrounded || controller.CanCoyoteJump) return; // controller handles it
if (wallSlide != null && (wallSlide.IsWallSliding || wallSlide.TouchingWall)) return; // wall jump wins
if (ladder != null && ladder.IsClimbing) return; // ladder jump wins
if (JumpsLeft <= 0) return;
JumpsLeft--;
controller.ForceJump(airJumpForce);
controller.ClearJumpBuffer();
if (anim != null && !string.IsNullOrEmpty(animatorTrigger)) anim.SetTrigger(animatorTrigger);
if (!string.IsNullOrEmpty(sfxName)) AudioManager.Instance?.PlaySFX(sfxName);
}
/// <summary>WallSlideJump and Ladder call this so touching a wall/ladder refreshes jumps.</summary>
public void Refill() => JumpsLeft = extraJumps;
}