dofile( "data/scripts/lib/utilities.lua" )

local entity_id    = GetUpdatedEntityID()
local pos_x, pos_y = EntityGetTransform( entity_id )

local projectile_component = EntityGetFirstComponent(entity_id, "ProjectileComponent")

local nearby = EntityGetInRadiusWithTag(pos_x, pos_y, 30, "mortal")

for k, enemy in pairs(nearby)do
    if(projectile_component ~= nil)then
        local explode1 = tonumber(ComponentGetValue(projectile_component, "on_death_explode"))
        local explode2 = tonumber(ComponentGetValue(projectile_component, "on_lifetime_out_explode"))
        local damage = ComponentGetValue(projectile_component, "damage")
        local explosion_damage = ComponentObjectGetValue(projectile_component, "config_explosion", "damage")

        local final_damage = 0

        if(explode1 == 1 or explode2 == 1)then
            final_damage = damage + explosion_damage
        else
            final_damage = damage
        end

        final_damage = final_damage / 5

        if(final_damage < 0.025)then
            final_damage = 0.025
        end

        local has_damage_stored = false

        local components = EntityGetComponent( enemy, "VariableStorageComponent" )

        if( components ~= nil ) then
        
            for key,comp_id in pairs(components) do 
                local var_name = ComponentGetValue( comp_id, "name" )

                if( var_name == "damage") then
                    ComponentSetValue2(comp_id, "value_string", final_damage)
                    has_damage_stored = true
                end                   
            end
        end       
        if(has_modifiers_stored ~= true)then
            EntityAddComponent(enemy, "VariableStorageComponent", 
            { 
                name = "damage",
                value_string = final_damage,
            })
        end            

    end
end