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()
	zrmine.f.SetOwnerID(ent, ply)

	return ent
end

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

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

	self:UseClientSideAnimation()
	self.PhysgunDisabled = true
end

function ENT:Use(activator, caller)
	if not zrmine.f.IsOwner(activator, self) then
		zrmine.f.Notify(activator, zrmine.language.Module_DontOwn, 1)

		return
	end

	if (self:GetParent():GetCurrentState() == "WAITING_FOR_ORDER") then
		self:GetParent():MineCart_Down(activator)
	end
end

-- Logic
function ENT:Minecart_Unload()
	self:CreateAnim_Table("unloading", 0.5)

	timer.Simple(1, function()
		if (IsValid(self)) then
			self:SetBodygroup(0, 0)
		end
	end)
end

function ENT:Minecart_RollDown()
	self:CreateAnim_Table("rolling_down", 1)
end

-- Animation
function ENT:CreateAnim_Table(anim, speed)
	if (self:LookupSequence(anim) == self:GetSequence()) then return end
	zrmine.f.CreateAnimTable(self, anim, speed)
end
