//********************************//
//     Minecraft Block Entity     //
//			 (c) McKay			  //
//********************************//

include('shared.lua')

ENT.RenderGroup = RENDERGROUP_BOTH

CreateConVar( "minecraft_particles", "1", { FCVAR_REPLICATED } )

CreateConVar( "minecraft_particles_count", "32", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_particles_lifetime_min", "0.25", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_particles_lifetime_max", "0.35", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_particles_size_min", "32", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_particles_size_max", "48", { FCVAR_REPLICATED } )

CreateConVar( "minecraft_particles_dietime_min", "1", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_particles_dietime_max", "2", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_particles_gravity", "400", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_particles_airresistance", "5", { FCVAR_REPLICATED } )

CreateConVar( "minecraft_particles_startsize_min", "1", { FCVAR_REPLICATED } )
CreateConVar( "minecraft_particles_startsize_max", "2", { FCVAR_REPLICATED } )

local GlowMaterial = CreateMaterial( "arcadiumsoft/glow", "UnlitGeneric", {
[ "$basetexture" ]    = "sprites/light_glow01",
[ "$additive" ]        = "1",
[ "$vertexcolor" ]    = "1",
[ "$vertexalpha" ]    = "1", } );
    
local EFFECT = {};
    
function EFFECT:Init( data )
        
        local pos = data:GetOrigin();
        local normal = data:GetNormal();
        
        self.Position = pos;
        self.Normal = normal;
        
        self.LifeTime = math.Rand( GetConVar("minecraft_particles_lifetime_min"):GetFloat(), GetConVar("minecraft_particles_lifetime_max"):GetFloat() );    // 0.25, 0.35
        self.DieTime = CurTime() + self.LifeTime;
        //self.Size = math.Rand( GetConVar("minecraft_particles_size_min"):GetInt(), GetConVar("minecraft_particles_size_max"):GetInt() );
        
        // impact particles
        local emitter = ParticleEmitter( pos );
        for i = 1, GetConVar("minecraft_particles_count"):GetInt() do
										//"sprites/glow04_noz"
            local particle = emitter:Add( "models/sparkle", pos + normal * 2 );
            particle:SetVelocity( ( normal + VectorRand() * 0.75 ):GetNormal() * math.Rand( 75, 125 ) );
            particle:SetDieTime( math.Rand( GetConVar("minecraft_particles_dietime_min"):GetInt(), GetConVar("minecraft_particles_dietime_max"):GetInt() ) );
            particle:SetStartAlpha( 255 );
            particle:SetEndAlpha( 0 );
            particle:SetStartSize( math.Rand( GetConVar("minecraft_particles_startsize_min"):GetInt(), GetConVar("minecraft_particles_startsize_max"):GetInt() ) );
            particle:SetEndSize( 0 );
            particle:SetRoll( 0 );
            //particle:SetColor( 255, 128, 255 );
			particle:SetColor( 255, 255, 255 );
            particle:SetGravity( Vector( 0, 0, -GetConVar("minecraft_particles_gravity"):GetInt() ) );
            particle:SetCollide( true );
            particle:SetBounce( 0.3 );
            particle:SetAirResistance( GetConVar("minecraft_particles_airresistance"):GetInt() );

        end
        emitter:Finish();           
end
    
function EFFECT:Think()
    return self.DieTime >= CurTime();
end

function EFFECT:Render()
	return
end

effects.Register( EFFECT, "MCMagic" );