@name ballb
@inputs 
@outputs 
@persist Particles:table
@trigger none

interval(60)

if (Particles:count()) {
    
    
    for (I = 1, 10) {
        local T = Particles[I, array]
        
        T[3, vector] = T[3, vector] + T[4, vector] # gravity
        T[2, vector] = T[2, vector] + T[3, vector] # velocity   
        
        local Trace = rangerOffset(T[3, vector]:length(), T[2, vector], T[3, vector])
        if (Trace:hit()) {
            T[3, vector] = (T[3, vector] - (2*Trace:hitNormal():dot(T[3, vector])) * Trace:hitNormal()) / 2
            local Trace = rangerOffset(5, T[2, vector] + vec(0, 0, 2.5), vec(0,0,-1))
            if (Trace:hit()) {
                Particles[I, array]:clear()
                holoDelete(I)    
            }
        }
        
        holoPos(T[1, number], T[2, vector])
    }
    
    
    
}
else {

    for (I = 1, 10) {
        Particles[I, array] = array(I, entity():pos(), vec(30, 0, 0):rotate(-45, random(0, 360), 0), vec(0,0,-1))
        holoCreate(I)
    }

}
#[                This["Direction", vector] = This["Direction", vector] + This["Gravity", vector]*6
                This["Position", vector]  = This["Position", vector]  + This["Direction", vector]*6
                local Bounce              = rangerOffset(This["Direction", vector]:length()*6, This["Position", vector], This["Direction", vector]:normalized())
                
                holoPos(BallIdx, This["Position", vector])
                if (Bounce:hit())
                {
                    This["Direction", vector] = (This["Direction", vector] - (2 * Bounce:hitNormal():dot(This["Direction", vector])) * Bounce:hitNormal()) * 0.66
                    local HitCheck            = rangerOffset(5, holoEntity(BallIdx):pos() + vec(0, 0, 2.5), vec(0, 0, -1))
                    if (HitCheck:hit())
                    {
                        holoPos(BallIdx, HitCheck:position() + vec(0, 0, 2.5))
                        This["Mode", string] = "open"
                    }
                    holoAng(BallIdx, This["Direction", vector]:toAngle() + ang( randvec()*30 ) + ang(0, 0, 180))
                    holoEntity(BallIdx):soundPlay("bounce", 0, "items/flashlight1.wav", 0.15), soundPitch("bounce", randint(100,110))
                }]#

