@name Entity Controller
@inputs E:entity Pod:wirelink Cam:wirelink
@outputs CamPos:vector
@model models/bull/dynamicbuttonflat.mdl
@persist M2 Space Active Cloak Dist
@trigger all
runOnTick(1)

entity():setColor(hsv2rgb((curtime()*10)%360,1,1))
if(first()|duped()){
reset(),Cam:setNumber("Active",1)
Dist=1
}


Active=Pod:number("Active")


if(Active){

O=Pod:entity("Entity"):driver()
if(E:type()=="prop_ragdoll"){Rag=4}else{Rag=1}
if(Pod:number("Shift")){Boost=9}else{Boost=1}


EyeDir=O:eye():setZ(0)*E:mass()

Size=(E:boxSize():x()+E:boxSize():y()+E:boxSize():z())/3


if(Pod:number("W")){
    E:applyForce(EyeDir*Rag*Boost*12)
    }
    
if(Pod:number("S")){
    E:applyForce(EyeDir*Rag*Boost*-12)
    }
    
if(Pod:number("A")){
    E:applyForce(EyeDir:rotate(ang(0,90,0))*Rag*Boost*12) 
    }

if(Pod:number("D")){
    E:applyForce(EyeDir:rotate(ang(0,-90,0))*Rag*Boost*12)
    }    
    

if(Pod:number("Alt")){
    E:applyForce(E:vel():setZ(0)*-E:mass()/10)
    }    
    
Space=Pod:number("Space") 


if(Space&$Space&inrange(E:vel():z(),-5,10)){E:applyForce(vec(0,0,650*E:mass()*Rag))}


if(Active&$Active&E){
    Dist=1
    Pod:entity("Entity")
    Pod:entity("Entity")
    Pod:entity("Entity")
    Pod:entity("Entity")
    Pod:entity("Entity")
    Pod:entity("Entity")
    Pod:entity("Entity")
    }
if(Active&$Active&!E){
    Pod:setNumber("Eject",1)
    Pod:entity("Entity")
    Pod:entity("Entity")
    }
    else{Pod:setNumber("Eject",0)}

M1=Pod:number("Mouse1")
M2=Pod:number("Mouse2")


if(M2&$M2){Cloak=!Cloak, E:soundPlay(0,0.2,"common/bugreporter_succeeded.wav")}
if(Cloak){E:setAlpha(0), soundPitch(0,100)}else{E:setAlpha(255), soundPitch(0,80)}


if(Pod:number("PrevWeapon")&Dist>0.6){Dist-=0.3}
if(Pod:number("NextWeapon")&Dist<100/Size){Dist+=0.3}
if(Pod:number("R")){Dist=1}


CamPos=E:massCenter() - O:eye()*4*Size*Dist
CamAng=(E:massCenter()-CamPos):toAngle()


rangerFilter(E)
Ranger=rangerOffset(E:massCenter(), CamPos)
if(Ranger:hit()){CamPos=Ranger:position() + O:eye()*30}

Cam:setVector("Position",CamPos)


Cam:setNumber("Pitch",CamAng:pitch())
Cam:setNumber("Yaw",CamAng:yaw())
Cam:setNumber("Roll",0)


}
