
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

ENT.health = 30

function ENT:Initialize()
	self:SetMoveType( MOVETYPE_NONE )
	self:SetSolid( SOLID_BBOX )
	self:SetHealth(self.health)
	self:SetCollisionBounds( Vector( -20, 22, 0 ), Vector( 20, -22, 12 ) )	
	
	
	self.idlesound = CreateSound( self, "weapons/sapper_timer.wav" )
	self.idlesound:Play()
	
	self.warning_delay = 0
end

function ENT:OnTakeDamage( dmg )
	if dmg:GetInflictor():GetClass() != "tf_weapon_wrench" then return end
	self:SetHealth(self:Health() - dmg:GetDamage())
	if self:Health() < 0 and !self.broken then
		self:EmitSound("weapons/sapper_removed.wav", 100, 100)
		self.broken = true
		self:Explode()
	end
end

function ENT:Think()
	self:NextThink(CurTime() +0.3)
	self.sapent:TakeDamage( 7, self.owner, self )
	
	if CurTime() < self.warning_delay then return end
	if self.sapent.owner then self.sapent.owner:ConCommand("playgamesound misc/hud_warning.wav") end
	self.warning_delay = CurTime() +2
	return true
end

function ENT:Explode()
	self:EmitSound( "weapons/teleporter_explode.wav", 100, 100 )
	local explosion = ents.Create( "info_particle_system" )
	explosion:SetKeyValue( "effect_name", "ExplosionCore_buildings" )
	explosion:SetPos( self:GetPos() +self:OBBCenter() ) 
	explosion:SetAngles( self:GetAngles() )
	explosion:Spawn()
	explosion:Activate() 
	explosion:Fire( "Start", "", 0 )
	explosion:Fire( "Kill", "", 0.1 )
	self:Remove()
end

function ENT:OnRemove()
	if self.sapent and ValidEntity( self.sapent ) then self.sapent.sapper = NULL; self.sapent.sapped = nil; self.sapent:SetNetworkedBool("sapped", false) end
	self.idlesound:Stop()
end

