local Q={}Q.__index=Q function Q.new(R,w)assert(R,"\077\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\049\058 \116\097\114\103\101\116\086\097\108\117\101")w=w or{}return setmetatable({_targetValue=R;_velocity=w.velocity or 1},Q)end function Q.step(w,Q,R)local S=Q.value local u=w._velocity local l=w._targetValue local h=R*u local o=h>=math.abs(l-S)S=S+h*(l>S and 1 or-1)if o then S=w._targetValue u=0 end return{complete=o;value=S;velocity=u}end return Q
local Linear = {}
Linear.__index = Linear

function Linear.new(targetValue, options)
	assert(targetValue, "Missing argument #1: targetValue")
	
	options = options or {}

	return setmetatable({
		_targetValue = targetValue,
		_velocity = options.velocity or 1,
	}, Linear)
end

function Linear:step(state, dt)
	local position = state.value
	local velocity = self._velocity -- Linear motion ignores the state's velocity
	local goal = self._targetValue

	local dPos = dt * velocity

	local complete = dPos >= math.abs(goal - position)
	position = position + dPos * (goal > position and 1 or -1)
	if complete then
		position = self._targetValue
		velocity = 0
	end
	
	return {
		complete = complete,
		value = position,
		velocity = velocity,
	}
end

return Linear
