#@name mech movement system
@inputs 
@outputs 
@persist [Modes Flags]:table Mode:string
@persist [Jump Dash Slash Walk Neck Tail]:table DownVector:vector [SinBreathe CosBreathe MphNoZ]
#- Input Keys
@persist [W A S D Space Shift Reload] [Owner Pilot PreviousPilot]:entity Entity:entity Inertia:angle Mass
@trigger none

if (first()) {   
    DownVector = vec(0, 0, -1)

    Modes = table(
        "Normal" = "handleNormalMode",
        "Jump" = "handleJumpMode",
        "Dash" = "handleDashMode",
        "Slash" = "handleSlashMode"
    )
    
    Flags = table(
        "CanJump" = 0,
        "CanDash" = 0,
        "CanSlash" = 0
    )
    
    function number getFlag( Flag:string ) {
        return Flags[Flag, number]    
    }
    
    function setFlag( Flag:string, Value ) {
        Flags[Flag, number] = Value    
    }
    
    function setNewPilot(NewPilot:entity) {
        print(Pilot:name() + " is now in control")
        runOnKeys(PreviousPilot, 0)
        runOnKeys(Pilot, 1)
        PreviousPilot = Pilot
    }
    
    function getPilotKeys() {
        W = Pilot:keyForward()
        A = Pilot:keyLeft()
        S = Pilot:keyBack()
        D = Pilot:keyRight()
        
        Space = Pilot:keyJump()
        Shift = Pilot:keySprint()
        Reload = Pilot:keyReload()
    }
    
    
    function handleNormalMode() {
        local Ranger = rangerOffset(150 + Walk["Diff", number], Entity:pos(), DownVector)
        if (Ranger:hit()) {
            Jump["Power", number] = Jump["power", number] + clamp(Space - Jump["power", number], -0.5, 0.5)
            
            local ForceF = Entity:forward() * (W - S) * 15 * (1 - Jump["Power", number])
            local ForceR = Entity:right() * (clamp(Walk["Diff", number] / 4 * (W | S), -4, 4) + (A - D) * 15)
            local ForceU = vec(0, 0, Neck["Ang", angle][1] + SinBreathe + abs(Walk["Diff", number] / 2) + (85 - MphNoZ / 3) - Jump["Power", number]*22.5 - Ranger:distance())

            Entity:propGravity(0)
        } else {
            Entity:propGravity(1)
        }
    }
    
    
#[                 local Ranger = rangerOffset(150 + LftRgtDif, EntityPos, DownVector)
                if (Ranger:hit()) {
                    
                    Jump["power", number] = Jump["power", number] + clamp(Space - Jump["power", number], -0.5, 0.5)
                    TailWag = cos(Time * 2) * 15 * Jump["power", number]
                    Tail[1, table]["Offset", vector] = vec(-34, TailWag, 3)
                    
                    local ForceF = Entity:forward() * (W - S) * 15 * (1 - Jump["power", number])
                    local ForceR = Entity:right() * (clamp(LftRgtDif / 4 * (W | S), -4, 4) + (A - D) * 15)
                    local ForceU = vec(0, 0, NeckDir[1] + (SinT / MphNoZ) + abs(LftRgtDif / 2) + (85 - MphNoZ / 3) - Jump["power", number]*22.5 - Ranger:distance())

                    Entity:applyForce(((ForceF + ForceR + ForceU)*ForceP - EntityVel/ForceD) * EntityMass)
                    Entity:applyAngForce((Entity:toLocal(ang((Jump["power", number]*22.5 + CosT / MphNoZ) + abs(LftRgtDif / 2) + NeckDir[1], EntityAng[2] - Bearing + TailWag / 10, 0))*200 - Entity:angVel()*20) * EntityInertia)
                    Entity:propGravity(0)
                } else {
                    Entity:propGravity(1) 
                }   ]#
    
   
    Owner = Pilot = owner()
    Mode = "Normal"
 
    timer("RunPhysics", 100)
}

if (keyClk(Pilot)) {
    
}

if (clk("RunPhysics")) {
    timer("RunPhysics", 60)
    
    if (changed(Pilot) & Pilot) { setNewPilot(Pilot) }
    
    Modes[Mode, string]( )

}
