@name JoyCam
@inputs [JAna JHat JBut CamCon]:wirelink
@outputs Activated Angle:angle X Y Z
@persist Activated Angle:angle Y X Z
@trigger 
interval(50)
if(JAna:entity()){
    Xaxis=JAna["1",number] #Left stick X
    Yaxis=JAna["2",number] #Left Stick Y
    Xaxis2=JAna["3",number] #Right Stick X
    Yaxis2=JAna["4",number] #Right Stick Y
    Zaxis=JAna["5",number] #Trigger analog (Left down, right up)
    Lstick=JAna["6",number] #Left Stick button
    Rstick=JAna["7",number] #Right stick button
    But9=JAna["8",number] #Center Ring left 
}
if(JHat:entity()){
    HatU=JHat["1",number] #Hat up
    HatD=JHat["2",number] #Hat Down
    HatL=JHat["3",number] #Hat Left
    HatR=JHat["4",number] #Hat Right
    HatC=JHat["5",number] #Hat Centered
    But9=JHat["6",number] #Center Ring Left
    But10=JHat["7",number] #Center Ring Right
    But13=JHat["8",number] #Center Ring Middle
}
if(JBut:entity()){
    But1=JBut["1",number] #Square / 1
    But2=JBut["2",number] #X / 2
    But3=JBut["3",number] #Circle / 3
    But4=JBut["4",number] #Triangle /4
    But5=JBut["5",number] #Left front button
    But6=JBut["6",number] #Right front button
    But7=JBut["7",number] #Left trigger
    But8=JBut["8",number] #Right trigger
}


Rate=5 
Angle+=angnorm(ang(-Yaxis2*Rate,-Xaxis2*Rate,0))
SPEED=6
Run=SPEED*2
Sprint=Run*2
if(first()|duped()){
    Position=entity():pos()+vec(0,0,100) 
    X=Position:x()
    Y=Position:y()
    Z=Position:z()
}
if(But9){Activated=1}
if(But10){Activated=0}

W=Yaxis>0
S=Yaxis<0
A=Xaxis<0
D=Xaxis>0
Space=But8
Alt=But7
R=But1
Mouse1=But6
Mouse2=But5
Use=But1
Shift=Lstick
if(Activated){
    SinY0=sin(Angle:yaw())
    SinP90=sin(Angle:pitch()+90)
    CosP90=cos(Angle:pitch()+90)
    CosY0=cos(Angle:yaw())
    if(W) {Y +=SinY0*SinP90*SPEED*((Shift*Run)+1)
           X +=CosY0*SinP90*SPEED*((Shift*Run)+1)
           Z +=CosP90*SPEED*((Shift*Run)+1)}
    if(S) {Y -=SinY0*SPEED*SinP90*((Shift*Run)+1)
           X -=CosY0*SPEED*SinP90*((Shift*Run)+1)
           Z -=CosP90*SPEED*((Shift*Run)+1)}
    if(A) {Y +=CosY0*SPEED*((Shift*Run)+1)
           X -=SinY0*SPEED*((Shift*Run)+1)}
    if(D) {Y -=CosY0*SPEED*((Shift*Run)+1)
           X +=SinY0*SPEED*((Shift*Run)+1)}
    if(Space) {Z +=1*(Shift*Sprint)+1}
    if(Alt) {Z -=1*(Shift*Sprint)+1}
    
    if(R){
        Position=entity():pos()+vec(0,0,100) 
        X=Position:x()
        Y=Position:y()
        Z=Position:z()
    }
    if(vec(X,Y,Z):isInWorld()){Position=vec(X,Y,Z)}
    #Direction = 

}


CamCon:setNumber("Activated",Activated)
#CamCon:setNumber("Zoom",Zoom)
#CamCon:setNumber("X",X)
#CamCon:setNumber("Y",Y)
#CamCon:setNumber("Z",Z)
#CamCon:setNumber("Pitch",Pitch)
#CamCon:setNumber("Yaw",Yaw)
#CamCon:setNumber("Roll",Roll)
CamCon:setAngle("Angle",Angle)
CamCon:setVector("Position",Position)
#CamCon:setVector("Direction",Direction)
#CamCon:setVector("Velocity",Velocity)
#CamCon:setEntity("Parent",Parent)

#Output
#On=CamCon:number("On")
#X=CamCon:number("X")
#Y=CamCon:number("Y")
#Z=CamCon:number("Z")

