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

/*---------------------------------------------------------
   Name: Initialize
   Desc: First function called. Use to set up your entity
---------------------------------------------------------*/
function ENT:Initialize()

	self.Entity:SetModel( "models/dav0r/tnt/tnt.mdl" )	
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	
	if (phys:IsValid()) then
	
		phys:Wake()
		
	end
	
end

/*---------------------------------------------------------
   Name: KeyValue
   Desc: Called when a keyvalue is added to us
---------------------------------------------------------*/
function ENT:KeyValue( key, value )

	if ( key == "health" && tonumber( value ) > 0 ) then
	
		self.Entity:SetMaxHealth( tonumber( value ) )
		self.Entity:SetHealth( tonumber( value ) )
		
	elseif ( key == "damage" ) then self.Damage = mathx.Max( tonumber( value ), 0 )
	elseif ( key == "remove" ) then self.Remove = utilx.tobool( value )
	end
	
end

/*---------------------------------------------------------
   Name: OnTakeDamage
   Desc: Entity takes damage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )

	if ( dmginfo:GetInflictor():GetClass() == "gmod_dynamite" ) then return end
	
	self.Entity:TakePhysicsDamage( dmginfo )
	if ( self.Entity:GetMaxHealth() > 0 ) then self.Entity:SetHealth( self.Entity:Health() - dmginfo:GetDamage() ) end
	
	// Blow it up if its health is under 0
	if ( self.Entity:Health() <= 0 && self.Entity:GetMaxHealth() != 0 ) then
	
		self:Explode( 0 )
		
	end
	
end

function ENT:Explode( delay )

	local _delay = delay or 0
	
	if ( !self.Entity:IsValid() ) then Msg("DYNAMITE: Doesn't exist!\n") return end
	
	if ( _delay == 0 ) then
	
		local radius = 300
		
	 	util.BlastDamage( self.Entity, self.Entity, self.Entity:GetPos(), radius, self.Damage )
		
		local effectdata = EffectData()
		 effectdata:SetOrigin( self.Entity:GetPos() )
 		util.Effect( "Explosion", effectdata, true, true )
		
		if ( self.Remove ) then
			self.Entity:Remove()
			return
		end
		if ( self.Entity:GetMaxHealth() > 0 && self.Entity:Health() <= 0 ) then self.Entity:SetHealth( self.Entity:GetMaxHealth() ) end
		
	else
	
		timer.Simple( delay, self.Explode, self, 0 )
		
	end
	
end

include('shared.lua')
