-- ================================================================================
-- PUBLIC FUNCTIONS
-- ================================================================================

function Checkpoint.new(collisionBody, manager)
	assert(collisionBody, "Trying to set partless checkpoint")	
	
	local newCheckpoint = {CollisionBody = collisionBody, Manager = manager}
	setmetatable(newCheckpoint, Checkpoint)	
	
	newCheckpoint.PlayersTouched = {}
	
	newCheckpoint.CollisionBody.Touched:connect(function(otherPart)
		if otherPart and otherPart.Parent then
			local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
			if player then
				newCheckpoint.Manager:PlayerCrossedCheckpoint(player, newCheckpoint)
			end	
		end
	end)
	
	newCheckpoint.CollisionConnection = nil
	
	return newCheckpoint
end -- Checkpoint.new()

function Checkpoint:Reset()
	self.PlayersTouched = {}
end -- Checkpoint:Reset()

function Checkpoint:SetPlayerPassed(player, lapNumber)
	local alreadyPassed = (self.PlayersTouched[player] == lapNumber)
	self.PlayersTouched[player] = lapNumber
	return alreadyPassed
end -- Checkpoint:SetPlayerPassed()

function Checkpoint:PlayerLapPassed(player)
	return self.PlayersTouched[player]
end -- Checkpoint:PlayerLapPassed()