local f=game:GetService("\082\117\110\083\101\114\118\105\099\101")local e=require(script.Parent.createSignal)local t={}t.prototype={}t.__index=t.prototype local function I(f)assert(typeof(f)=="\110\117\109\098\101\114")local I={__goal=nil,__state={value=f,complete=true},__onComplete=e();__onStep=e()}setmetatable(I,t)return I end function t.prototype.start(e)e.__connection=f.RenderStepped:Connect(function(f)e:step(f)end)end function t.prototype.stop(f)if f.__connection~=nil then f.__connection:Disconnect()end end function t.prototype.step(e,f)assert(typeof(f)=="\110\117\109\098\101\114")if e.__state.complete then return end if e.__goal==nil then return end local t=e.__goal:step(e.__state,f)if t~=nil then e.__state=t end e.__onStep:fire(e.__state.value)if e.__state.complete then e.__onComplete:fire(e.__state.value)end end function t.prototype.setGoal(e,f)e.__goal=f e.__state.complete=false end function t.prototype.onStep(e,f)assert(typeof(f)=="\102\117\110\099\116\105\111\110")return e.__onStep:subscribe(f)end function t.prototype.onComplete(e,f)assert(typeof(f)=="\102\117\110\099\116\105\111\110")return e.__onComplete:subscribe(f)end function t.prototype.destroy(f)f:stop()end return I
local RunService = game:GetService("RunService")

local createSignal = require(script.Parent.createSignal)

local SingleMotor = {}
SingleMotor.prototype = {}
SingleMotor.__index = SingleMotor.prototype

local function createSingleMotor(initialValue)
	assert(typeof(initialValue) == "number")

	local self = {
		__goal = nil,
		__state = {
			value = initialValue,
			complete = true,
		},
		__onComplete = createSignal(),
		__onStep = createSignal(),
	}

	setmetatable(self, SingleMotor)

	return self
end

function SingleMotor.prototype:start()
	self.__connection = RunService.RenderStepped:Connect(function(dt)
		self:step(dt)
	end)
end

function SingleMotor.prototype:stop()
	if self.__connection ~= nil then
		self.__connection:Disconnect()
	end
end

function SingleMotor.prototype:step(dt)
	assert(typeof(dt) == "number")

	if self.__state.complete then
		return
	end

	if self.__goal == nil then
		return
	end

	local newState = self.__goal:step(self.__state, dt)

	if newState ~= nil then
		self.__state = newState
	end

	self.__onStep:fire(self.__state.value)

	if self.__state.complete then
		self.__onComplete:fire(self.__state.value)
	end
end

function SingleMotor.prototype:setGoal(goal)
	self.__goal = goal
	self.__state.complete = false
end

function SingleMotor.prototype:onStep(callback)
	assert(typeof(callback) == "function")

	return self.__onStep:subscribe(callback)
end

function SingleMotor.prototype:onComplete(callback)
	assert(typeof(callback) == "function")

	return self.__onComplete:subscribe(callback)
end

function SingleMotor.prototype:destroy()
	self:stop()
end

return createSingleMotor