-- Constructor/Destructor

function WaypointController.new(player)
	local self = setmetatable({}, WaypointController)
	
	-- Player variables
	self.player = player
	self.character = player.Character or player.CharacterAdded:wait()
	self.humanoid = self.character:WaitForChild("Humanoid")
	self.humanoidRoot = self.character.HumanoidRootPart
	self.isPlayerMoving = false
	
	-- Starting at 2 b/c it's the first waypoint after Start (1)
	self.currentWaypointIndex = 2
	self.currentWaypoint = waypoints[self.currentWaypointIndex]
	
	self._CooldownFinishedEvent = Instance.new("BindableEvent")
	self.CooldownFinished = self._CooldownFinishedEvent.Event
	
	self.waypointPath = WaypointPath.new(self.player, self.humanoidRoot.Position,
		self.currentWaypoint.Position)
	self.WaypointPathReached = self.waypointPath.WaypointReached:Connect(
		function()
			self._CooldownFinishedEvent:Fire()
			self:UpdateWaypoint()
		end)
	self.NodeReached = self.waypointPath.NodeReached:Connect(
		function()
			self._CooldownFinishedEvent:Fire()
		end)
	self.FinishedMoving = self.waypointPath.FinishedMoving:Connect(
		function()
			self:StopTracking()
		end)
	
	self.movingThread = nil
	
	return self
end

function WaypointController:Destroy()
	if self.WaypointPathReached then
		self.WaypointPathReached:Disconnect()
	end
	if self.NodeReached then
		self.NodeReached:Disconnect()
	end
	if self.FinishedMoving then
		self.FinishedMoving:Disconnect()
	end
end

