@name Chaqua Plane Base
@inputs Po:entity
@persist [E B]:entity Thr P Y R Ign Ntpl

interval(50)

if(first() | duped())
{
    Ntpl = 50/(500/33)
    
    E = entity()
    
    B = E:isConstrainedTo()
    B:setMass(5000)
}

Dri = Po:driver()
Act = Dri:isValid()
if(Act)
{
    W = Dri:keyPressed("W")
    A = Dri:keyPressed("A")
    S = Dri:keyPressed("S")
    D = Dri:keyPressed("D")
    Up = Dri:keyPressed("UP")
    Lft = Dri:keyPressed("LEFT")
    Dwn = Dri:keyPressed("DOWN")
    Rht = Dri:keyPressed("RIGHT")
    Shi = Dri:keyPressed("LSHIFT")
    Rk = Dri:keyPressed("R")
    
    P = Up-Dwn
    Y = A-D
    R = Lft-Rht
    
    if(changed(Rk) & Rk)
    {
        Ign = !Ign
        soundPlay("ign",0.21 * (1 + Ign),"buttons/button3.wav")
    }
    
    if(Ign)
    {
        local RD = rangerOffset(25, B:toWorld(vec(0, -200, 5)), -B:up())
        if(RD:hit())
        {
            B:applyOffsetForce((B:up() * RD:hit() * 5 * max(Thr + 0.25, 1)) * B:mass() * Ntpl, RD:pos())
        }
    }
}elseif(changed(Act))
{
    Ign = 0
}

Thr += (Ign ? 0.006125 : -0.006125) * Ntpl
Thr = clamp(Thr,0,1)

Vel = B:velL()
Mass = B:mass()
local Psh = max(12 + ((W-S)* (1+Shi))*10,0)
B:applyForce(((B:right()*Psh*Thr) + (B:toWorldAxis(-Vel*vec(0.1-(0.075*(1-Thr)),0.0025,0.25)))) * Mass * Ntpl)
B:applyAngForce((ang(R*4,Y,P*3)*(Vel:y()/-4000)*Thr*90 - B:angVel()*4) * Mass * Ntpl)
