@name anim/libraries/animate2
@persist [DeltaTime CurTime] WorldVecDown:vector WorldAngZero:angle
@persist BaseIndex BaseEnt:entity BasePos:vector BaseAng:angle BaseDir:vector BaseSpeed
@persist Feet:table

@persist [MaxSpeed Acceleration]


WorldVecDown = vec(0, 0, -1)
WorldAngZero = ang()
BaseSpeed = 5


#--------------------------------------
#-- DeltaTime Interval(N)

function dt( Interval ) {
    interval( Interval )

    DeltaTime = curtime() - CurTime
    CurTime = curtime()
}

CurTime = curtime()


#--------------------------------------
#-- Create Base Entity

function createBase( Index ) {
    BaseIndex = Index
    BaseEnt = holoEntity( Index ) ?: holoCreate( Index )
    BasePos = BaseEnt:pos()
    BaseAng = BaseEnt:angles()
}


#--------------------------------------
#-- Add a foot controller

function addFoot( UniqueID:string, LocalPos:vector, StepOrder, StepLength, StepHeight, GroundAdjust ) {
    Feet[UniqueID, table] = table(
        "LocalPos" = LocalPos,
        "StepOrder" = StepOrder,
        "StepLength" = StepLength,
        "StepHeight" = StepHeight,
        "StepGround" = GroundAdjust,
        "StepCircumf" = 2*StepLength*pi(),
        "StepDegrees" = 0
    )
}


#--------------------------------------
#-- Update all foot controllers

function updateFeet( Debug ) {
    local BaseYaw = BaseAng:yaw()
    local BaseSpeedFract = BaseSpeed/MaxSpeed

    foreach ( K, Foot:table = Feet ) {
        Foot["StepDegrees", number] = Foot["StepDegrees", number] + 360*( ( BaseSpeed/Foot["StepCircumf", number] )*DeltaTime )

        local Sin = sin( Foot["StepDegrees", number] + Foot["StepOrder", number] )*Foot["StepLength", number]*BaseSpeedFract
        local Cos = cos( Foot["StepDegrees", number] + Foot["StepOrder", number] )*Foot["StepHeight", number]*BaseSpeedFract

        local Trace = rangerOffset( 100, toWorld( Foot["LocalPos", vector] + vec( Sin, 0, 0 ), WorldAngZero, BasePos, BaseAng ), WorldVecDown )
        
        local Angle = Trace:hitNormal():toAngle()
            Angle = Angle:rotateAroundAxis( Angle:forward(), BaseYaw )
            Angle = Angle:rotateAroundAxis( Angle:right(), Sin - 90 )

        Foot["Position", vector] = Trace:position() + Trace:hitNormal()*max( Foot["StepGround", number], Cos )
        Foot["Angle", angle] = Angle

        if ( Debug ) {
            if ( !Foot["Debug", number] ) {
                Foot["Debug", number] = hash( K )

                holoCreate( Foot["Debug", number] )
                holoAlpha( Foot["Debug", number], 255 )
                continue
            }

            holoPos( Foot["Debug", number], Foot["Position", vector] )
            holoAng( Foot["Debug", number], Foot["Angle", angle] )
        }
    }
}


#[
function table:setSpeed( Units:string, Value ) {
    This["Speed", number] = fromUnit( Units, Value )
}

function table:update() {
    This["Position", vector] = This["Position", vector] + ( This["MoveDir", vector]*This["Speed", number] )*DeltaTime

    holoPos( This["Index", number], This["Position", vector] )

    This:updateFootControllers()
}

CurTime = curtime()


Test = createBodyController( holoCreate( 1 ) )
    local Controller1 = Test:addFootController( "Test1", 0, vec( 0, 20, 0 ), 60, 35 )
    local Controller2 = Test:addFootController( "Test2", 180, vec( 0, -20, 0 ), 60, 35 )
Test:setSpeed( "mph", 15 )
}

interval( 60 )

DeltaTime = curtime() - CurTime
CurTime = curtime()


Test:update()

DeltaTime = curtime() - CurTime
CurTime = curtime()

MPH   = 5
Units = fromUnit( "mph", MPH ) #units per sec MPH*17.6

Vel = vec( Units, 0, 0 )*DeltaTime
Pos = Pos + Vel


Rev = ( Units/( Diameter*pi() ) )*DeltaTime
Ang = Ang + 360*Rev

holoAng( 1, ang( Ang, 0, 90 ) )

local N = 1
for ( I = 1, Count ) {
    N +  +
    local Sin = sin( Ang + ( 360/Count )*N )*Radius
    local Cos = cos( Ang + ( 360/Count )*N )*Radius
    holoPos( N, Pos + vec( Sin, 0, max( 0, Cos ) - Radius ) )
}]#

