@name HoloGravSimu
@persist Count I
@persist E:entity
@model models/props_junk/watermelon01.mdl

#Press your Walk Key (by default, Alt) to spawn holos
#Press your Use Key (by default, E) to despawn holos.

if(first())
{
    runOnKeys(owner(), 1)
    
    E = entity()
    E["radius", number] = 75
    
    function createGBody(Index, Base:entity)
    {
        local Offset = (randvec(-1, 1):setZ(0):normalized() * Base["radius", number])
        local H = holoCreate(Index, Base:pos() + Offset, vec(0.5))
        holoModel(Index, "hq_sphere")
        holoColor(Index, vec(255, 100, 0))
        holoDisableShading(Index, 1)
        
        H:setTrails(5, 0, 10, "effects/beam_generic01", vec(255), 255)
        
        #H["vel", vector] = (Offset:cross(vec(0, 0, 1)):normalized() * sqrt(2400 / Base["radius", number])) #the sqrt(...) part makes it so it orbits in a perfect circle...
        H["vel", vector] = (Offset:cross(randvec()):normalized() * sqrt(2400 / Base["radius", number])) #the sqrt(...) part makes it so it orbits in a perfect circle...
    }
    
    function entity:bodyGravity(ForeignBody:entity, Gravity)
    {
        local GravF = (Gravity / (ForeignBody:pos():distance(This:pos()) ^ 2))
        local GravD = (ForeignBody:pos() - This:pos()):normalized()
        
        This["vel", vector] = This["vel", vector] + (GravD * GravF)
    }
    
    function handleGBody(Index, Base:entity, Gravity)
    {
        local H = holoEntity(Index)
        
        H:bodyGravity(Base, Gravity)
        
        holoPos(Index, H:pos() + H["vel", vector])
    }
    
    timer("bodies", 100)
}

if(clk("bodies"))
{
    for(I = 1, Count)
    {
        handleGBody(I, E, 2400)
    }
    
    timer("bodies", 100)
}

if(keyClk(owner()))
{
    if(owner():keyWalk())
    {
        Count++
        createGBody(Count, E)
        
        print(4, toString(Count))
    }
    elseif(owner():keyUse())
    {
        if(Count > 0)
        {
            holoDelete(Count)
            Count--
        }
        
        print(4, toString(Count))
    }
}
