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

------------------------------//
function ENT:SpawnFunction(ply, tr)
	if (not tr.Hit) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 1
	local ent = ents.Create(self.ClassName)
	local angle = ply:GetAimVector():Angle()
	angle = Angle(0, angle.yaw, 0)
	angle:RotateAroundAxis(angle:Up(), 180)
	ent:SetAngles(angle)
	ent:SetPos(SpawnPos)
	ent:Spawn()
	ent:Activate()

	return ent
end

function ENT:Initialize()
	self:SetModel(self.Model)
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetCollisionGroup(COLLISION_GROUP_WEAPON)
	local phys = self:GetPhysicsObject()

	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableMotion(true)
	end

	if (zrmine.config.Resource_DespawnTime ~= 0 and zrmine.config.Resource_DespawnTime ~= -1) then
		timer.Simple(zrmine.config.Resource_DespawnTime, function()
			if (IsValid(self)) then
				self:CreateEffect_Table("zrms_resource_despawn", "zrmine_sfx_resourcedespawn", self, self:GetAngles(), self:GetPos())
				self:SetNoDraw(true)
				local aphys = self:GetPhysicsObject()

				if (aphys:IsValid()) then
					aphys:Wake()
					aphys:EnableMotion(false)
				end

				timer.Simple(2, function()
					if (IsValid(self)) then
						self:Remove()
					end
				end)
			end
		end)
	end

	if (self:GetResourceType() == "Iron") then
		self:SetSkin(0)
	elseif (self:GetResourceType() == "Bronze") then
		self:SetSkin(1)
	elseif (self:GetResourceType() == "Silver") then
		self:SetSkin(2)
	elseif (self:GetResourceType() == "Gold") then
		self:SetSkin(3)
	elseif (self:GetResourceType() == "Coal") then
		self:SetSkin(4)
	end
end

function ENT:StartTouch(ent)
	if ent:GetClass() == "zrms_crusher" then
		ent:Add_Resource(self, nil)
	elseif (ent:GetClass() == "zrms_gravelcrate") then
		ent:Add_ResourceJunk(self)
	end
end

--Effects
function ENT:CreateEffect_Table(effect, sound, parent, angle, position)
	zrmine.f.CreateEffectTable(effect, sound, parent, angle, position)
end
