@name Easy elevator
@inputs Floor1 Floor2 Toggle
@persist Mode D S C R Ent:entity TempEnt:entity PosR:array Ang:angle

if (first()){
    runOnTick(1)
    runOnChat(1)
    
    entity():setAlpha(0)
    entity():propNotSolid(1)
    
    printColor(vec(0,255,0),"Press numpad enter while aiming at an entity to select it for the elevator!")
    
    function number entity:move(Vec:vector) {
        local V = Vec-This:pos()
        local V = V * V:length()
        This:applyForce((V-This:vel())*This:mass())
        return 1
    }
    function number entity:rot(Ang:angle) {
        local V = This:toLocal(rotationVector(quat(Ang)/quat(This))+This:pos())
        This:applyTorque((200*V - 25*This:angVelVector())*This:inertia())
        return 1
    }
}


if (Mode==0){
    if (owner():keyPressed("PAD_ENTER")&&owner():aimEntity():isValid()){
        Ent=owner():aimEntity()
        TempEnt=propSpawn(Ent:model(),Ent:pos() + Ent:up()*20,Ent:angles(),1)
        TempEnt:setAlpha(100)
        slider(Ent,vec(),TempEnt,vec())
    
        printColor(vec(0,255,0),"Set the height of the elevator using the ghost prop.")
        printColor(vec(0,255,0),"When you're ready, say \"-speed <movement speed>\".")
        printColor(vec(0,255,0),"Example: -speed 50")
        Mode=1
    }
} elseif (Mode==1){
    if (chatClk(owner())&&owner():lastSaid():sub(1,7)=="-speed ") {hideChat(1), R=owner():lastSaid():sub(8):toNumber(), Mode=2}
} elseif (Mode==2){
    PosR=array(Ent:pos(),TempEnt:pos())
    Ang=Ent:angles()
    C=R/PosR[1,vector]:distance(PosR[2,vector])/66
    
    printColor(vec(0,0,255),"Increase per interval: "+C)
    printColor(vec(0,0,255),"Distance: "+PosR[1,vector]:distance(PosR[2,vector]))
    printColor(vec(0,0,255),"Angle: "+Ang)
    
    TempEnt:propDelete()
    Ent:propFreeze(0)
    entity():setAlpha(255)
    entity():propNotSolid(0)
    Mode=3
} elseif (Mode==3){
    if (Floor1) {S=0}
    if (Floor2) {S=1}
    if (changed(Toggle)&&Toggle) {S=!S}
    
    if (!S&&D>=0) {D-=C}
    if (S&&D<=1) {D+=C}
    
    Ent:move(mix(PosR[2,vector],PosR[1,vector],clamp(D,0,1)))
    Ent:rot(Ang)
}
