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

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

local projectile_component = EntityGetFirstComponent( entity_id, "ProjectileComponent" )

local components = EntityGetComponent( entity_id, "VariableStorageComponent" )

local self_filename = nil

local max_speed = ComponentGetValue(projectile_component, "speed_max")

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

    if(var_name == "projectile_filename")then
        local value =  ComponentGetValue( comp_id, "value_string" ) 
        self_filename = value
    end
end

if(self_filename ~= nil)then
    local time  = GameGetFrameNum() / 60.0
    local angle = (r + 1.5) + (math.sin(time * 5) / 2)
    local vel_x = math.cos(angle) * max_speed
    local vel_y = math.sin(angle) * max_speed

    GameEntityPlaySound( entity_id, "duplicate" )
    local shot_entity = shoot_projectile_from_projectile( entity_id, self_filename, pos_x, pos_y, vel_y, -vel_x )

    local extra_entities = ComponentObjectGetValue(projectile_component, "config", "extra_entities")

    if(extra_entities ~= nil)then
        for word in string.gmatch(extra_entities, '([^,]+)') do
            if(word ~= "mods/spellbound_bundle/files/entities/modifiers/bubble_shot_2.xml")then
                local modifier = EntityLoad(word, 0, -500)
                EntityAddChild(shot_entity, modifier)
            end
        end
    end
end