AddCSLuaFile()

ENT.Type 			= "anim"
ENT.Base 			= "base_anim"
ENT.PrintName		= "Scrap Box"
ENT.Category		= "Other"

ENT.Spawnable		= true
ENT.AdminOnly 		= false
ENT.DoNotDuplicate 	= false
ENT.AlreadyUsed 	= false

function ENT:SetupDataTables()
	self:NetworkVar( "Int", 0, "Scrap" )
end

function ENT:SpawnFunction(ply, tr, cls)

	if (!tr.Hit) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 20
	local SpawnAng = ply:EyeAngles()
	
	local ent = ents.Create(cls)
	
	ent:SetPos(SpawnPos)
	ent:SetAngles(SpawnAng)
	ent:Spawn()
	ent:Activate()
	
	return ent
end

function ENT:Initialize()
	if CLIENT then return end
	self:SetModel("models/static/nmrih_tool_box_01.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:DrawShadow(true)
	
	self:SetTrigger(true)
	self:UseTriggerBounds(true,14)
	self:SetCollisionGroup(COLLISION_GROUP_WEAPON)
	self:SetUseType(SIMPLE_USE)
	
	local phys = self:GetPhysicsObject()
	if IsValid(phys) then
		phys:Wake()
		phys:SetMass(35)
		phys:SetMaterial( "metal" )
	end
	
	self:SetScrap( 0 )
	--SafeRemoveEntityDelayed( self, 30 )
end

function ENT:OnTakeDamage( damage )
	self:TakePhysicsDamage(damage)
end

function ENT:PhysicsCollide(data, phys)
	if data.DeltaTime > 0.2 then
		self:EmitSound(data.Speed > 250 and "Metal_Box.ImpactHard" or "Metal_Box.ImpactSoft")
	end
end
function ENT:StartTouch( activator )
	if activator:IsPlayer() then self:Use( activator ) end
end
function ENT:Use(activator, caller)
	if self.AlreadyUsed then return end
	if activator:IsPlayer() then
		activator:SetNWInt("Scrap",activator:GetNWInt("Scrap")+self:GetScrap())
		self:EmitSound("ui/item_metal_scrap_pickup.wav")
		self:EmitSound("misc/scrap_sound/scrap_rebel.wav",75,math.random(95,105))
		self:Remove()
		
		net.Start("DisplayScrap")
		net.WriteInt(1000,11)
		net.Send(activator)
		self.AlreadyUsed = true
	end
end

if CLIENT then

function ENT:OnRemove()
	local ed = EffectData()
	ed:SetOrigin(self:GetPos())
	ed:SetEntity(self)
	util.Effect( "entity_remove", ed, true, true )
end

function ENT:BeingLookedAtByLocalPlayer()
	local lp = LocalPlayer()
	local trace = util.TraceHull( {
		start = lp:GetShootPos(),
		endpos = lp:GetShootPos() + lp:GetAimVector() * 200,
		filter = lp,
		mins = Vector( -3, -3, -3 ),
		maxs = Vector( 3, 3, 3 ),
		mask = MASK_SHOT,
	} )

	if trace.Entity ~= self then return false end
	if trace.HitPos:Distance(LocalPlayer():GetShootPos()) > 200 then return false end

	return true
end

local halos = {}
local halos_inv = {}

function ENT:DrawEntityOutline()
	if halos_inv[self] then return end
	halos[#halos+1] = self
	halos_inv[self] = true
end

local Mat = Material( "sprites/light_ignorez" )

function ENT:Draw()
	self:DrawModel()
	
	if self:BeingLookedAtByLocalPlayer() then
		if self.RenderGroup == RENDERGROUP_OPAQUE then
			self.OldRenderGroup = self.RenderGroup
			self.RenderGroup = RENDERGROUP_TRANSLUCENT
		end
		self:DrawEntityOutline()
		self:DrawModel()
		AddWorldTip( self:EntIndex(), "Scraps: "..self:GetScrap(), nil, self:WorldSpaceCenter(), nil )
	else
		if self.OldRenderGroup then
			self.RenderGroup = self.OldRenderGroup
			self.OldRenderGroup = nil
		end
		self:DrawModel()
	render.SetMaterial( Mat ) render.DrawSprite( self:WorldSpaceCenter(), 32 +math.sin( SysTime()*2 )*4, 32 +math.sin( SysTime()*2 )*4, Color( 255, 64, 92, 192 ) )
	render.DrawSprite( self:WorldSpaceCenter(), 8 +math.cos( SysTime()*2 )*2, 64 +math.cos( SysTime()*2 )*4, Color( 255, 64, 92, 192 ) )
	end
end
end
