#
# Ripper Fragmentation Plasma 01
#
# Author: Resin_Smoker
#
local EffectTemplate = import('/lua/EffectTemplates.lua')
local TPlasmaCannonProjectile = import('/lua/terranprojectiles.lua').TPlasmaCannonProjectile
local RandomFloat = import('/lua/utilities.lua').GetRandomFloat

Ripper_Fragmentation_plasma01 = Class(TPlasmaCannonProjectile) {
               
    OnImpact = function(self, TargetType, TargetEntity)
        # Checks the type of target before splitting the shells 
        if TargetEntity and table.find(TargetEntity:GetBlueprint().Categories,'AIR') then
            local FxFlashEffect = EffectTemplate.FlashSml01 
            local ChildProjectileBP = '/mods/4th_Dimension 191/hook/projectiles/Ripper_Fragmentation_plasma02/Ripper_Fragmentation_plasma02_proj.bp' 
        
            # Split effects
            for k, v in FxFlashEffect do
                CreateEmitterAtEntity( self, self:GetArmy(), v )
            end

            local numProjectiles = 5 #Total projectiles is numProjectiles + 1
            local angle = (2*math.pi) / numProjectiles
            local angleInitial = RandomFloat( 0, angle ) 
            local vx, vy, vz = self:GetVelocity()
            local velocity = self:GetVelocity() 

            # Divides the damage between the number of projectiles created
            self.DamageData.DamageAmount = self.DamageData.DamageAmount / (numProjectiles + 1) 
    
	    # One initial projectile following same directional path as the original
            self:CreateChildProjectile(ChildProjectileBP):SetVelocity(vx, vy, vz):SetVelocity(velocity):PassDamageData(self.DamageData)

            # Create several other projectiles in a dispersal pattern and Randomization of the spread
            local xVec = 0 
            local yVec = vy
            local zVec = 0

            # Launch projectiles at semi-random angles away from split location
            for i = 0, (numProjectiles -1) do
                local angleVariation = angle * Random(2, 0.5) # Adjusts angle variance spread
                local spreadMul = Random(2, 0.5) # Adjusts the width of the dispersal  
                xVec = vx + (math.sin(angleInitial + (i*angle) + RandomFloat(-angleVariation, angleVariation))) * spreadMul
                zVec = vz + (math.cos(angleInitial + (i*angle) + RandomFloat(-angleVariation, angleVariation))) * spreadMul 
                local proj = self:CreateChildProjectile(ChildProjectileBP)
                proj:SetVelocity(xVec,yVec,zVec)
                proj:SetVelocity(velocity)
                proj:PassDamageData(self.DamageData)
            end

            local pos = self:GetPosition()
            local spec = {
                X = pos[1],
                Z = pos[3],
                Radius = self.Data.Radius,
                LifeTime = self.Data.Lifetime,
                Army = self.Data.Army,
                Omni = false,
                WaterVision = false,
            }
        self:Destroy()
        else
	    # Do damage normally if target impacted isnt an air target
            TPlasmaCannonProjectile.OnImpact(self, TargetType, TargetEntity)
        end
    end,
    
}

TypeClass = Ripper_Fragmentation_plasma01