local Q=require(script.Parent.BaseMotor)local R=require(script.Parent.SingleMotor)local w=require(script.Parent.isMotor)local S=setmetatable({},Q)S.__index=S local function u(Q)if w(Q)then return Q end local u=typeof(Q)if u=="\110\117\109\098\101\114"then return R.new(Q,false)elseif u=="\116\097\098\108\101"then return S.new(Q,false)end error(("\085\110\097\098\108\101 \116\111 \099\111\110\118\101\114\116 \037\113 \116\111 \109\111\116\111\114\059 \116\121\112\101 \037\115 \105\115 \117\110\115\117\112\112\111\114\116\101\100"):format(Q,u),2)end function S.new(R,w)assert(R,"\077\105\115\115\105\110\103 \097\114\103\117\109\101\110\116 \035\049\058 \105\110\105\116\105\097\108\086\097\108\117\101\115")assert(typeof(R)=="\116\097\098\108\101","\105\110\105\116\105\097\108\086\097\108\117\101\115 \109\117\115\116 \098\101 \097 \116\097\098\108\101\033")assert(not R.step,"\105\110\105\116\105\097\108\086\097\108\117\101\115 \099\111\110\116\097\105\110\115 \100\105\115\097\108\108\111\119\101\100 \112\114\111\112\101\114\116\121 \"\115\116\101\112\"\046 \068\105\100 \121\111\117 \109\101\097\110 \116\111 \112\117\116 \097 \116\097\098\108\101 \111\102 \118\097\108\117\101\115 \104\101\114\101\063")local l=setmetatable(Q.new(),S)if w~=nil then l._useImplicitConnections=w else l._useImplicitConnections=true end l._complete=true l._motors={}for Q,R in pairs(R)do l._motors[Q]=u(R)end return l end function S.step(R,Q)if R._complete then return true end local w=true for R,S in pairs(R._motors)do local u=S:step(Q)if not u then w=false end end R._onStep:fire(R:getValue())if w then if R._useImplicitConnections then R:stop()end R._complete=true R._onComplete:fire()end return w end function S.setGoal(R,Q)assert(not Q.step,"\103\111\097\108\115 \099\111\110\116\097\105\110\115 \100\105\115\097\108\108\111\119\101\100 \112\114\111\112\101\114\116\121 \"\115\116\101\112\"\046 \068\105\100 \121\111\117 \109\101\097\110 \116\111 \112\117\116 \097 \116\097\098\108\101 \111\102 \103\111\097\108\115 \104\101\114\101\063")R._complete=false R._onStart:fire()for Q,w in pairs(Q)do local S=assert(R._motors[Q],("\085\110\107\110\111\119\110 \109\111\116\111\114 \102\111\114 \107\101\121 \037\115"):format(Q))S:setGoal(w)end if R._useImplicitConnections then R:start()end end function S.getValue(Q)local R={}for Q,w in pairs(Q._motors)do R[Q]=w:getValue()end return R end function S.__tostring(Q)return"\077\111\116\111\114\040\071\114\111\117\112\041"end return S
local BaseMotor = require(script.Parent.BaseMotor)
local SingleMotor = require(script.Parent.SingleMotor)

local isMotor = require(script.Parent.isMotor)

local GroupMotor = setmetatable({}, BaseMotor)
GroupMotor.__index = GroupMotor

local function toMotor(value)
	if isMotor(value) then
		return value
	end

	local valueType = typeof(value)

	if valueType == "number" then
		return SingleMotor.new(value, false)
	elseif valueType == "table" then
		return GroupMotor.new(value, false)
	end

	error(("Unable to convert %q to motor; type %s is unsupported"):format(value, valueType), 2)
end

function GroupMotor.new(initialValues, useImplicitConnections)
	assert(initialValues, "Missing argument #1: initialValues")
	assert(typeof(initialValues) == "table", "initialValues must be a table!")
	assert(not initialValues.step, "initialValues contains disallowed property \"step\". Did you mean to put a table of values here?")

	local self = setmetatable(BaseMotor.new(), GroupMotor)

	if useImplicitConnections ~= nil then
		self._useImplicitConnections = useImplicitConnections
	else
		self._useImplicitConnections = true
	end

	self._complete = true
	self._motors = {}

	for key, value in pairs(initialValues) do
		self._motors[key] = toMotor(value)
	end

	return self
end

function GroupMotor:step(deltaTime)
	if self._complete then
		return true
	end

	local allMotorsComplete = true

	for _, motor in pairs(self._motors) do
		local complete = motor:step(deltaTime)
		if not complete then
			-- If any of the sub-motors are incomplete, the group motor will not be complete either
			allMotorsComplete = false
		end
	end

	self._onStep:fire(self:getValue())

	if allMotorsComplete then
		if self._useImplicitConnections then
			self:stop()
		end

		self._complete = true
		self._onComplete:fire()
	end

	return allMotorsComplete
end

function GroupMotor:setGoal(goals)
	assert(not goals.step, "goals contains disallowed property \"step\". Did you mean to put a table of goals here?")

	self._complete = false
	self._onStart:fire()

	for key, goal in pairs(goals) do
		local motor = assert(self._motors[key], ("Unknown motor for key %s"):format(key))
		motor:setGoal(goal)
	end

	if self._useImplicitConnections then
		self:start()
	end
end

function GroupMotor:getValue()
	local values = {}

	for key, motor in pairs(self._motors) do
		values[key] = motor:getValue()
	end

	return values
end

function GroupMotor:__tostring()
	return "Motor(Group)"
end

return GroupMotor
