AddCSLuaFile()

DEFINE_BASECLASS( "magic_base_entity" )

ENT.PrintName = "Waterball"
ENT.Spawnable = false
ENT.AdminOnly = false

PrecacheParticleSystem("waterball")

if SERVER
then
    function ENT:Initialize()
        self.BaseClass.Initialize(self)
        self:SetEffect("waterball")
        self:PhysicsInitSphere(32, "default")
        self:DrawShadow(false)
    end 

    function ENT:OnCollision(e)
        self:EmitSound("^weapons/underwater_explode4.wav", nil, 216)

        for k, v in ipairs(ents.FindInSphere(self:GetPos(), 200))
        do
            v:SetWet(true)
        end
        self:Remove()
    end
else
    local function onCollision(part, pos, normal)
        part:SetDieTime(0)
        sound.Play("ambient/water/water_splash" .. math.random(1, 3) .. ".wav", pos, 75, 100, 0.5)
        local part = MagicParticleEmitter3D:Add("particle/particle_noisesphere", pos + normal)
        if part
        then
            part:SetDieTime(2)
            part:SetStartAlpha(255)
            part:SetEndAlpha(0)
            part:SetStartSize(120)
            part:SetEndSize(0)
            part:SetAngles(normal:Angle())
        end
    end

    function ENT:OnRemove()
        for i=1, 15
        do
            local part = MagicParticleEmitter:Add("particle/particle_noisesphere", self:GetPos())
            if part
            then
                part:SetDieTime(1)
                part:SetStartAlpha(255)
                part:SetEndAlpha(0)
                part:SetStartSize(130)
                part:SetEndSize(120)
                part:SetCollide(true)
                part:SetCollideCallback(onCollision)
                part:SetRoll(i * 45)
                part:SetRollDelta(5)
                part:SetVelocity(VectorRand() * 305)
                part:SetGravity(Vector(0, 0, -1000))
            end
        end
    end

    local function onCollision(part, pos, normal)
        part:SetDieTime(0)
        sound.Play("ambient/water/water_splash" .. math.random(1, 3) .. ".wav", pos, 75, 100, 0.1)
        local part = MagicParticleEmitter3D:Add("particle/particle_noisesphere", pos + normal)
        if part
        then
            part:SetDieTime(2)
            part:SetStartAlpha(255)
            part:SetEndAlpha(0)
            part:SetStartSize(40)
            part:SetEndSize(0)
            part:SetAngles(normal:Angle())
        end
    end

    function ENT:Draw()
    end

    function ENT:Think()
        local part = MagicParticleEmitter:Add("particle/water/watersplash_001a_refract.vmt", self:GetPos())
        if part
        then
            part:SetDieTime(10)
            part:SetStartAlpha(100)
            part:SetEndAlpha(0)
            part:SetStartSize(30)
            part:SetEndSize(30)
            part:SetCollide(true)
            part:SetCollideCallback(onCollision)
            part:SetGravity(Vector(0, 0, -1000))
            part:SetVelocity(VectorRand() * 15)
        end
        self:SetNextClientThink(CurTime() + math.Rand(0.1, 0.2))
    end

end

