@name Entity Controller
@inputs E:entity Pod:wirelink Cam:wirelink
@outputs CamPos:vector
@persist M2 Space Active Cloak Dist
@trigger all
runOnTick(1)

#prevents problems from duplication, and tunrs on cam controller
if(first()|duped()){
reset(),Cam:setNumber("Active",1)
Dist=1
}

#define various variables used later in the code
Active=Pod:number("Active")

#only run most of the code if someone is in the chair
if(Active){

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

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

#average x y and z of the entity's size
Size=(E:boxSize():x()+E:boxSize():y()+E:boxSize():z())/3

#moving prop with WASD
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)
    }    
    
#slow down prop
if(Pod:number("Alt")){
    E:applyForce(E:vel():setZ(0)*-E:mass()/10)
    }    
    
Space=Pod:number("Space") 

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

#instructions, reset cam's distance from object
if(Active&$Active&E){
    Dist=1
    Pod:entity("Entity"):hintDriver("W A S D To move.", 6.3)
    Pod:entity("Entity"):hintDriver("Space to jump.", 6.4)
    Pod:entity("Entity"):hintDriver("Shift to boost.", 6.5)
    Pod:entity("Entity"):hintDriver("Right click to cloak.", 6.6)
    Pod:entity("Entity"):hintDriver("Alt to slow down.", 6.7)
    Pod:entity("Entity"):hintDriver("Mouse wheel to zoom", 6.8)
    Pod:entity("Entity"):hintDriver("R to reset camera", 6.9)
    }
if(Active&$Active&!E){
    Pod:setNumber("Eject",1)
    Pod:entity("Entity"):hintDriver("No selected entity.", 7)
    Pod:entity("Entity"):hintDriver("Look at an entity and press numpad 5 to select it.", 7)
    }
    else{Pod:setNumber("Eject",0)}

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

#cloaking
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)}

#mouse wheel scrolling to control distance from prop
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}

#set camera's position and angle variables
CamPos=E:massCenter() - O:eye()*4*Size*Dist
CamAng=(E:massCenter()-CamPos):toAngle()

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

Cam:setVector("Position",CamPos)

#change camera's angles
Cam:setNumber("Pitch",CamAng:pitch())
Cam:setNumber("Yaw",CamAng:yaw())
Cam:setNumber("Roll",0)

#close the code 
#(from previous if event that checks if player is in pod)
}
if(owner():keyAttack1()){
E:propBreak()}
