@name camera_pod
@persist [Entity Vehicle Owner Driver]:entity Scale
@persist Camera:wirelink 
    
if (first()) {    
    function setupCamera( Parent:entity, Position:vector, Distance, ScaleFactor ) {
        Entity["CamParent", entity] = Parent
        Entity["CamPosition", vector] = Position*ScaleFactor
        Entity["CamDistance", number] = Distance*ScaleFactor
    }
    
    function setupVehicle( Parent:entity, Position:vector, Angle:angle, ScaleFactor ) {
        Entity["VehLinked", number] = 0
        Entity["VehParent", entity] = Parent
        Entity["VehPosition", vector] = Position*ScaleFactor
        Entity["VehAngle", angle] = Angle   
    }
    
    function initCamera() {
        Camera = Entity["Camera", entity]:wirelink()
    
        Camera["Activated", number] = 0
        Camera["Parent", entity] = Entity["CamParent", entity]
        Camera["Position", vector] = Entity["CamPosition", vector]
        Camera["Distance", number] = Entity["CamDistance", number]
    }
    
    function number initVehicle() {
        Vehicle:setPos(Entity["VehParent", entity]:toWorld(Entity["VehPosition", vector]))
        Vehicle:setAng(Entity["VehParent", entity]:toWorld(Entity["VehAngle", angle]))
        Vehicle:propFreeze(1)
    
        if (Entity["VehParent", entity]:toWorld(Entity["VehPosition", vector]):distance(Vehicle:pos()) < 0.25) {
            Entity["VehLinked", number] = 1
            Vehicle:parentTo(Entity["VehParent", entity])
    
            rangerFilter(Vehicle)
    
            return 1
        } else { return 0 }    
    }
    
    function pFuncInit() {
        if (!Entity["VehLinked", number]) {
            if (!Entity["Vehicle", entity] | !Entity["Camera", entity]) {
                local AimEnt = Owner:aimEntity()
                local Type = AimEnt:type()
    
                if (!AimEnt | AimEnt:owner() == Owner & (Type == "gmod_wire_cameracontroller" | Type == "prop_vehicle_prisoner_pod")) {
                    if (changed(AimEnt)) {
                        Entity["PrevAimEnt", entity]:setColor(Entity["PrevAimEnt", entity]["OriginalColor", vector4])
                        if (AimEnt) {
                            AimEnt["OriginalColor", vector4] = AimEnt:getColor4()
                            Entity["PrevAimEnt", entity] = AimEnt
    
                            AimEnt:setColor(vec4(0, 255, 0, 125))
                        }
                    }
    
                    if (AimEnt) {
                        local Click = Owner:keyAttack2()
    
                        if (changed(Click) & Click) {
                            if (!Entity["Vehicle", entity] & Type == "prop_vehicle_prisoner_pod") { Entity["Vehicle", entity] = AimEnt, Owner:soundPlay("Click", 0.253, "@^ui/hint.wav") }
                            if (!Entity["Camera", entity] & Type == "gmod_wire_cameracontroller") { Entity["Camera", entity] = AimEnt,  Owner:soundPlay("Click", 0.253, "@^ui/hint.wav") }
    
                            AimEnt:setColor(AimEnt["OriginalColor", vector4])
                        }
                    }
                }
            } else {
                Vehicle = Entity["Vehicle", entity]
                if (initVehicle()) {
                    Owner:soundPlay("Click", 0.204, "@^common/bugreporter_succeeded.wav")
                    initCamera()
                }
            }
        } else {
            Camera["Activated", number] = Vehicle:driver():isValid()
        }
    }
    
    Scale = 1
    Entity = entity()
    Owner = owner()

    setupVehicle(Entity, vec(0,0,50), ang(0,-90,0), Scale)
    setupCamera(Entity, vec(0,0,50), 200, Scale)
}

interval(60)

pFuncInit()
