@name continuum3
@inputs Camera:wirelink Vehicle:entity
@outputs 
@persist Segments:array Num Length Count DT CT WorldDown:vector OverVec:vector Player:entity Vels:array
@trigger 


if (!Num) {
    Count = 10
    Length = 100
    WorldDown = vec(0, 0, -1)
    OverVec = vec(0, 0, Length - 5)

    while (Num < Count) {
        Num++

        Segments[Num, vector] = entity():toWorld(vec(-Length*Num, 0, 0))
        
        holoCreate(Num, Segments[Num, vector])       
        holoColor(Num, vec4(255, 125, 0, 15))
        holoDisableShading(Num, 1)
        holoModel(Num, "sphere") 
        
        if (Num > 1) {
            local SegThick = sqrt(Length)
            local ID = Num + 500
            local SC = -cos((360/Count)*Num )
            holoCreate(ID, holoEntity(Num):toWorld(vec(Length/2, 0, 12 + SC*SegThick/2)), vec(24 + SC*SegThick, 24 + SC*SegThick, Length)/12, holoEntity(Num):toWorld(ang(90, 0, 0)))
            holoParent(ID, Num)
            holoModel(ID, "hexagon")
            
            #holoParent(N, N - 1)
        }
    }    

    function setup() {
        Camera["Parent", entity]   = holoEntity(2)
        Camera["Position", vector] = vec(8, 0, 100)
        Camera["Distance", number] = 0
        
        rangerHitEntities(0)
        rangerFilter(Vehicle)
        rangerPersist(1)
    }
    
    setup()
        
    CT = curtime()
    
    interval(100)
    runOnLast(1)
}
else {
    interval(90)

    #--------------
    #- Vehicle
    Player = Vehicle:driver()
    
    if (!Vehicle:parent()) {
        Vehicle:propFreeze(1)
        
        local Pos = holoEntity(2):toWorld(vec(0, 0, 15))
        local Ang = holoEntity(2):toWorld(ang(0, -90, 0))
    
        Vehicle:setPos(Pos)
        Vehicle:setAng(Ang)
        
        if ((Vehicle:pos() - Pos):length() < 0.1 & vec(Vehicle:angles() - Ang):length() < 0.1) {
            Vehicle:parentTo(holoEntity(2))
            
            setup()
        }
    }
    if (last()) { 
        Vehicle:deparent()
    }
     
    
    DT = curtime() - CT
    CT = curtime()
    
    if (Player) {
        Camera["Activated", number] = 1
        if (Player:keyReload()) { Vehicle:ejectPod() }
        
        local Dir = rangerOffset(99999, holoEntity(2):toWorld(vec(0, 0, 100)), Vehicle:toLocalAxis(Player:eye())):position() - Segments[1, vector]
        local Yaw = Dir:toAngle():yaw()
        local Vel = vec(1, 0, 0):rotate(0, Yaw, 0) * min(800, Dir:length())
        
        local Ranger = rangerOffset(Length*2, Segments[1, vector] + Vel*DT + OverVec*2, WorldDown)
        if (Ranger:hit()) {
            Segments[1, vector] = Ranger:pos()
            #Vels[1, vector] = vec()
        }
        else {
            #Vels[1, vector] = Vels[1, vector] + vec(0, 0, Length)
            Segments[1, vector] = Segments[1, vector] + Vel*DT - Vels[1, vector]*DT  - Vels[1, vector]*DT
        }
        
        holoPos(1, Segments[1, vector])
        
        for (N = 2, Num) {
            local Ranger = rangerOffset(Length, Segments[N - 1, vector] - (Segments[N - 1, vector] - Segments[N, vector]):normalized()*Length + OverVec, WorldDown)    
            
            if (Ranger:hit()) {
                Segments[N, vector] = Ranger:pos()
                Vels[N - 1, vector] = vec()
            }
            else {
                Vels[N - 1, vector] = Vels[N - 1, vector] + vec(0, 0, Length)
                Segments[N, vector] = Segments[N - 1, vector] - (Segments[N - 1, vector] - Segments[N, vector]):normalized()*Length  - Vels[N, vector]*DT
            }
            
            holoPos(N, Segments[N, vector])
            holoAng(N, (Segments[N - 1, vector] - Segments[N, vector]):toAngle())
        }
    }
    else {
        Camera["Activated", number] = 0
    }
}


#[        Positions[1, vector] = rangerOffset(100, Positions[1, vector] + MoveDir*Speed*10 + vec(0, 0, 50), vec(0, 0, -1)):position()
        holoPos(1, Positions[1, vector])
    
        for (N = 2, Segments) {
            #Positions[N, vector] = rangerOffset(SegmentLength*2, Positions[N - 1, vector] - (Positions[N - 1, vector] - Positions[N, vector]):normalized()*SegmentLength + RangerHeight, RangerDir):position()
            local Ranger = rangerOffset(SegmentLength, Positions[N - 1, vector] - (Positions[N - 1, vector] - Positions[N, vector]):normalized()*SegmentLength + RangerHeight, RangerDir):position()
            
            Positions[N, vector] = rangerOffset(SegmentLength*2, Positions[N - 1, vector] - (Positions[N - 1, vector] - Positions[N, vector]):normalized()*SegmentLength + RangerHeight, RangerDir):position()
            
            holoPos(N, Positions[N, vector])
            holoAng(N, (Positions[N - 1, vector] - Positions[N, vector]):toAngle())
        }]#
