using System;using System.Linq;
 public sealed class LashGroundStrikeState:LashCastState
 {
  float highest;bool startedInAir,loopStarted,landed,finishing;Vector3 direction;
  public override void OnEnter()
  {
   base.OnEnter();highest=characterBody.footPosition.y;startedInAir=characterMotor&&!characterMotor.isGrounded;
   if(characterMotor)characterMotor.onHitGroundAuthority+=Landed;
   UpdateDirection();controller?.Voice(1);
   if(startedInAir)controller?.Motion("ability_groundstrike_airdrop_start",.3f,true);
   else controller?.GroundStrikeFinish(false);
   LashAudio.Play(gameObject,"lash_ground_strike_downstrike_",.75f);
   if(startedInAir)LashEffects.Cast(characterBody,"groundstrike_air",.7f);
  }
  void UpdateDirection()
  {
   var aim=Vector3.ProjectOnPlane(GetAimRay().direction,Vector3.up);
   if(aim.sqrMagnitude>.01f)direction=aim.normalized;
   else if(direction.sqrMagnitude<.01f)direction=characterDirection.forward;
  }
  void Landed(ref CharacterMotor.HitGroundInfo hit){landed=true;}
  public override void OnExit(){if(characterMotor)characterMotor.onHitGroundAuthority-=Landed;base.OnExit();}
  public override void FixedUpdate()
  {
   base.FixedUpdate();if(finishing)return;highest=Mathf.Max(highest,characterBody.footPosition.y);UpdateDirection();
   bool contact=landed||(characterMotor&&characterMotor.isGrounded);
   if(startedInAir&&!contact&&characterMotor&&characterMotor.velocity.y<=0)
   {
    var capsule=characterMotor.GetComponent<CapsuleCollider>();var probe=characterBody.footPosition;
    if(capsule){var bounds=capsule.bounds;probe=new Vector3(bounds.center.x,bounds.min.y,bounds.center.z);}
    contact=Physics.Raycast(probe+Vector3.up*.15f,Vector3.down,out var hit,.4f,LayerIndex.world.mask,QueryTriggerInteraction.Ignore)&&hit.normal.y>.35f;
   }
   if((!startedInAir&&fixedAge>=.2f)||(startedInAir&&contact)||fixedAge>8)
   {
    // An explicit networked state transition ends the loop on every peer.
    finishing=true;
    if(isAuthority)outer.SetNextState(new LashGroundStrikeImpactState{Aerial=startedInAir,Height=Mathf.Clamp(highest-characterBody.footPosition.y,0,25),Direction=direction});
    return;
   }
   if(startedInAir&&fixedAge>.15f)
   {
    if(!loopStarted){loopStarted=true;controller?.Motion("ability_groundstrike_airdrop_loop",8f,true);}
    if(isAuthority&&characterMotor)
    {
     // Steer with the current aim and movement input throughout the descent.
     var aim=GetAimRay().direction;float forward=Mathf.Clamp(Vector3.ProjectOnPlane(aim,Vector3.up).magnitude,0,1);
     var steer=direction*(26f*forward)+(inputBank?inputBank.moveVector:Vector3.zero)*8f;
     steer=Vector3.ClampMagnitude(steer,32f);
     characterMotor.velocity=steer+Vector3.down*32f;
    }
   }
  }
 }
 public sealed class LashGroundStrikeImpactState:LashCastState
 {
  internal bool Aerial;internal float Height;internal Vector3 Direction;
  public override void OnEnter()
  {
   base.OnEnter();if(isAuthority&&characterMotor){var v=characterMotor.velocity;v.x=0;v.z=0;v.y=Mathf.Max(0,v.y);characterMotor.velocity=v;}
   if(Aerial)controller?.GroundStrikeFinish(true);
   LashEffects.Cast(characterBody,"groundstrike_impact",1.4f);LashAudio.Play(gameObject,"lash_ground_strike_impact",.8f);
   if(NetworkServer.active)foreach(var target in LashTargeting.Nearby(characterBody,characterBody.footPosition,10).ToArray())
   {
    var offset=Vector3.ProjectOnPlane(target.corePosition-characterBody.corePosition,Vector3.up);
    if(offset.sqrMagnitude<4||Vector3.Dot(offset.normalized,Direction)>=Mathf.Cos(60*Mathf.Deg2Rad))LashTargeting.Damage(characterBody,target,5+Height*.45f);
   }
  }
  public override void FixedUpdate(){base.FixedUpdate();if(fixedAge>=.25f&&isAuthority)outer.SetNextStateToMain();}
  public override void OnSerialize(NetworkWriter writer){base.OnSerialize(writer);writer.Write(Aerial);writer.Write(Height);writer.Write(Direction);}
  public override void OnDeserialize(NetworkReader reader){base.OnDeserialize(reader);Aerial=reader.ReadBoolean();Height=reader.ReadSingle();Direction=reader.ReadVector3();}
 }
 internal static class LashSlamTrajectory
 {
  internal const float PullSeconds=.3f,SlamSeconds=.45f,TotalSeconds=PullSeconds+SlamSeconds;
  internal static Vector3 Lift(Vector3 start,Vector3 caster,float age)
  {
   var top=start;top.y=Mathf.Max(start.y+2, caster.y+2);
   return Vector3.Lerp(start,top,Mathf.SmoothStep(0,1,Mathf.Clamp01(age/.4f)));
  }
  internal static Vector3 Sample(Vector3 origin,Vector3 gather,Vector3 destination,float age)
  {
   if(age<PullSeconds)return Vector3.Lerp(origin,gather,Mathf.SmoothStep(0,1,Mathf.Clamp01(age/PullSeconds)));
   float t=Mathf.Clamp01((age-PullSeconds)/SlamSeconds);return Vector3.Lerp(gather,destination,t*t);
  }
 }
