@name df_v1
@inputs 
@outputs SpaceTime Jump IsGliding
@persist [Dragon Rider]:entity IsGliding Inertia:angle Mass [Space SpaceTime]
@persist [JumpV0 JumpV1]:vector JumpTime Jump
@trigger none
@model models/hunter/blocks/cube05x05x05.mdl

interval(90)

Rider = owner()
Space = Rider:keyJump()

Dragon  = entity()
Inertia = shiftL(ang(Dragon:inertia()))

Dragon:setMass(5000)
Mass    = Dragon:mass()

if ( !IsGliding ) {
    SpaceTime  += clamp( Space - SpaceTime, -0.25, 0.25 )
    if ( Space & SpaceTime >= 1 & !Jump ) {
        IsGliding = 1
        SpaceTime = 0 
    }
    
    #- Jumping
    if ( !Jump ) {
         if ( changed( Space ) & Space ) {
            Jump = 1
            JumpTime = 0
            JumpV0 = Dragon:pos()
            JumpV1 = Dragon:pos() + vec( 0, 0, 60 )
            
            Dragon:propGravity( 0 )
        }       
    } else {
        JumpTime = min( JumpTime + 0.1, 1 )
        
        local Force = mix( JumpV1, JumpV0, JumpTime ) + vec( 0, 0, sin( JumpTime*180 )*100 ) - Dragon:pos()
        
        Dragon:applyForce( ( Force*10 - Dragon:vel() )*Mass )
        
        if ( JumpTime >= 1 ) {
            Jump = 0
            Dragon:propGravity( 1 )
        }  
    }


} else {
    if ( changed( Space ) & Space ) {
        IsGliding = 0 
    }
    
    #- Gliding
    local Force = vec( 100, 0, 0 ) - Dragon:velL() * vec( 0.1, 0.1, 0 )
    local Gravity = vec( 0, 0, 9.015 - Dragon:vel():z() )
    
    Dragon:applyForce( ( Gravity + Dragon:toWorldAxis( Force ) ) * Mass )
}


