@name gt3
@inputs 
@outputs T
@persist Base:entity FootControllers:table WorldDownVec:vector
@trigger none

if (first()) {
    
    WorldDownVec = vec(0,0,-1)

    function table addFootController(Name:string,Update:string) {
        FootControllers[Name, table] = table(
            "active" = 0,
            "lerp"   = 0,
            "rate"   = 0.1,
            "update" = Update,
            "uniqueid" = Name
        )
        return FootControllers[Name, table] 
    }
        
    function updateFootControllers() {
        foreach (K, Controller:table = FootControllers) {
            Controller["update", string](Controller)
        }
    }

    function void update_basic_step(Data:table) {
        if (Data["active", number]) {
            Data["lerp", number] = min(1, Data["lerp", number] + Data["rate", number])
            Data["pos", vector] = bezier(Data["point0", vector], Data["point1", vector], Data["point2", vector], Data["lerp", number])
            
            if (Data["debug", number]) { holoPos(Data["debug", number], Data["pos", vector]) }
            
            if (Data["lerp", number] >= 1) {
                Data["lerp", number] = 0
                Data["active", number] = 0
                return
            }
        }
        else {
            local Ranger = rangerOffset(1000, Data["entity", entity]:toWorld(Data["rpos", vector]), WorldDownVec)

            if (Data["pos", vector]:distance(Ranger:position()) > Data["rposl", number]) {
                
                Data["point0", vector] = Data["pos", vector]
                Data["point2", vector] = Ranger:position()
                Data["point1", vector] = (Data["point0", vector] + Data["point2", vector])/2 + Ranger:hitNormal()*Data["point0", vector]:distance(Data["point2", vector])
                
                Data["active", number] = 1
                return
            }
            
            #if (Data["debug", number]) { holoPos(Data["debug", number], Ranger:position()) }
        }
    }
    
    function table table:setControllerEntity(Entity:entity) {
        This["entity", entity] = Entity
        return This 
    }
    
    function table table:setControllerRPos(Vector:vector) {
        This["rpos", vector] = Vector
        This["rposl", number] = Vector:length()
        return This 
    }
    
    function table table:setControllerDebug() {
        This["debug", number] = hash(This["uniqueid", string])
        holoCreate(This["debug", number] ,vec(),vec(1/2))
        return This   
    }
    
    
    Base = entity()

    local Controller = addFootController("test","update_basic_step")
        Controller:setControllerEntity(Base)
        Controller:setControllerRPos(vec(50,50,0))
        Controller:setControllerDebug()

    local Controller = addFootController("test2","update_basic_step")
        Controller:setControllerEntity(Base)
        Controller:setControllerRPos(vec(50,-50,0))
        Controller:setControllerDebug()
}

interval(60)

updateFootControllers()
