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

local entity_id = GetUpdatedEntityID()
local x, y = EntityGetTransform( entity_id )

SetRandomSeed(x + GameGetFrameNum(), y + GameGetFrameNum())

if(x ~= nil)then
    local projectiles = EntityGetInRadiusWithTag( x, y, 25, "projectile" )

    if(projectiles ~= nil)then
        for i,projectile_id in ipairs(projectiles) do	
            if(projectile_id ~= entity_id)then
                if(EntityHasTag(projectile_id, "projectile_player") == false)then
                    local projectilecomponent = EntityGetFirstComponent( projectile_id, "ProjectileComponent" )

                    
                    if(projectilecomponent ~= nil)then
                        local px, py = EntityGetTransform( projectile_id )
                        local directionX = (x + Random(-5, 5)) - px
        
                        local directionY = (y + Random(-5, 5)) - py

                        local Speed = 200

                        ComponentSetValue(projectilecomponent, "friendly_fire", "1")
                        
                        ComponentSetValue(projectilecomponent, "collide_with_shooter_frames", "1")
                        
                        ComponentSetValue(projectilecomponent, "explosion_dont_damage_shooter", "0")

                        local velocitycomponent = EntityGetFirstComponent( projectile_id, "ProjectileComponent" )

                        if(velocitycomponent ~= nil)then
                            edit_component( projectile_id, "VelocityComponent", function(comp,vars)
                                local velocityX, velocityY = ComponentGetValueVector2( comp, "mVelocity")
                        

                                local vel_x = directionX
                        
                                local vel_y = directionY
                                
                                local len = math.sqrt((vel_x*vel_x) + (vel_y*vel_y))
                        
                                local dir_x = vel_x / len
                                local dir_y = vel_y / len
                        
                                local force_x = (dir_x * Speed)
                                local force_y = (dir_y * Speed)
                                
                                --shoot_projectile( player, "files/entities/physbeam.xml", playerX + (dir_x2 * 10), playerY + (dir_y2 * 10), dir_x2 * 20, dir_y2 * 20 )
                                ComponentSetValueVector2( comp, "mVelocity", force_x, force_y )
                            -- ComponentSetValueVector2( comp, "mVelocity", main_vel_x, main_vel_y)
                        
                            end)	
                        end
                        
                    end
                end
            end
        end
    end
  --[[  local projectiles = EntityGetInRadiusWithTag( x, y, 25, "projectile" )
    if ( #projectiles > 0 ) then
        for i,projectile_id in ipairs(projectiles) do	
            if(EntityHasTag(projectile_id, "projectile_player") == false)then
                local px, py = EntityGetTransform( projectile_id )
                local directionX = (x + Random(-5, 5)) - px

                local directionY = (y + Random(-5, 5)) - py
                
                local Speed = 200
                
                local velocitycomponent = EntityGetFirstComponent( projectile_id, "VelocityComponent" )

                local projectilecomponent = EntityGetFirstComponent( projectile_id, "ProjectileComponent" )
                
                if(projectilecomponent ~= nil)then
                    ComponentSetValue(projectilecomponent, "friendly_fire", "1")
                    
                    ComponentSetValue(projectilecomponent, "collide_with_shooter_frames", "1")
                    
                    ComponentSetValue(projectilecomponent, "explosion_dont_damage_shooter", "0")
                    if(velocitycomponent ~= nil)then
                        
                        edit_component( projectile_id, "VelocityComponent", function(comp,vars)
                            local velocityX, velocityY = ComponentGetValueVector2( comp, "mVelocity")
                    

                            local vel_x = directionX
                    
                            local vel_y = directionY
                            
                            local len = math.sqrt((vel_x*vel_x) + (vel_y*vel_y))
                    
                            local dir_x = vel_x / len
                            local dir_y = vel_y / len
                    
                            local force_x = (dir_x * Speed)
                            local force_y = (dir_y * Speed)
                            
                            --shoot_projectile( player, "files/entities/physbeam.xml", playerX + (dir_x2 * 10), playerY + (dir_y2 * 10), dir_x2 * 20, dir_y2 * 20 )
                            ComponentSetValue( comp, "gravity_x", 0 )
                            ComponentSetValue( comp, "gravity_y", 0 )
                            ComponentSetValueVector2( comp, "mVelocity", force_x, force_y )
                        -- ComponentSetValueVector2( comp, "mVelocity", main_vel_x, main_vel_y)
                    
                        end)	
                    
                    end
                end
            end
        end
    end]]
end