AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
      ENT.radius = 100
      ENT.mode = 1
      ENT.nextUseTime = CurTime()
      ENT.ents = {}function ENT:SpawnFunction( ply, tr )
 
	if ( !tr.Hit ) then return end
 
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
 
	local ent = ents.Create( "sent_lowgrav" )
        ent:SetPos( SpawnPos )
        ent:Spawn()
        ent:Activate()
 
	return ent
 
end
 
 
/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()
 
	// Use the helibomb model just for the shadow (because it's about the same size)
	self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
 
	// Don't use the model's physics - create a sphere instead
	self.Entity:PhysicsInitSphere( 16, "metal_bouncy" )
 
	// Wake the physics object up. It's time to have fun!
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
 
	// Set collision bounds exactly
	self.Entity:SetCollisionBounds( Vector( -16, -16, -16 ), Vector( 16, 16, 16 ) )
 
end
 
 
/*---------------------------------------------------------
   Name: PhysicsCollide
---------------------------------------------------------*/
function ENT:PhysicsCollide( data, physobj )
 
	// Play sound on bounce
	if (data.Speed > 80 && data.DeltaTime > 0.2 ) then
		self.Entity:EmitSound( "Rubber.BulletImpact" )
	end
 
	// Bounce like a crazy bitch
	local LastSpeed = data.OurOldVelocity:Length()
	local NewVelocity = physobj:GetVelocity()
	NewVelocity:Normalize()
 
	LastSpeed = math.max( NewVelocity:Length(), LastSpeed )
 
	local TargetVelocity = NewVelocity * LastSpeed * 0.9
 
	physobj:SetVelocity( TargetVelocity )
 
end
 
/*---------------------------------------------------------
   Name: OnTakeDamage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )
 
	// React physically when shot/getting blown
	self.Entity:TakePhysicsDamage( dmginfo )
 
end
 
 
/*---------------------------------------------------------
   Name: Use
---------------------------------------------------------*/
function ENT:Use( activator, caller )
	if ( activator:IsPlayer() ) then
        if self.nextUseTime > CurTime() then return end
            self.nextUseTime = CurTime()+0.2
        if self.mode == 0 then
            activator:PrintMessage(3, "Mode: Disable Gravity")
            self.mode = 1
        elseif self.mode == 1 then
            activator:PrintMessage(3, "Mode: Enable Gravity")
            self.mode = 0
        end
	end
end
function ENT:IsInTable(Table, WhatToFind)
    local Return = false
    for k,v in pairs(Table) do
        if v == WhatToFind then
            Return = true
        end
    end
    return Return
end
function ENT:Check()
local find = ents.FindInSphere(self.Entity:GetPos(),self.radius)
    for k,v in pairs( self.ents ) do
            if not self:IsInTable(find, v) then
                if v and v:IsValid() then
                local phys = v:GetPhysicsObject();
                if phys:IsValid() then
                    phys:EnableGravity( true )
                    phys:Wake()
                end
                end
                table.remove(self.ents,k)
            end
        end
end
function ENT:OnRemove()
    for k,v in pairs( self.ents ) do
        if v and v:IsValid() then
            local phys = v:GetPhysicsObject()
            if phys:IsValid() then
                phys:EnableGravity(true)
                phys:Wake();
            end
        end
    end
end
function ENT:Think()
    self:Check()
	local find = ents.FindInSphere( self.Entity:GetPos(), self.radius )
    for k,v in pairs( find ) do
        local entphys = v:GetPhysicsObject();
        if entphys:IsValid() then
            if self.mode == 1 then
                self.Entity:GetPhysicsObject():EnableGravity( false )
                self.Entity:GetPhysicsObject():Wake()
                if self.ents[k] != v and tostring(v:GetClass()) != "sent_lowgrav" and tostring(v:GetClass()) != "player" then
                    self.ents[k] = v
                end
                if tostring(v:GetClass( )) != "sent_lowgrav" and tostring(v:GetClass()) != "player" then
                    entphys:EnableGravity( false )
                    entphys:Wake()
                end
            elseif self.mode == 0 then
                self.Entity:GetPhysicsObject():EnableGravity( true )
                self.Entity:GetPhysicsObject():Wake()
                if tostring(v:GetClass( )) != "sent_lowgrav" and tostring(v:GetClass()) != "player" then
                    entphys:EnableGravity( true )
                    entphys:Wake()
                end
            end
        end 
    end
end