/// <summary> /// Links the given slave device to the next master device in the given direction. /// <para>If the distance to the closest master device exceeds the multitool connection's range, /// then no action will take place.</para> /// <remarks><see cref="InitDeviceLists(MultitoolConnectionType)"/> is assumed to have been run.</remarks> /// </summary> /// <param name="direction">Expecting <c>1</c> for the next closest device, <c>-1</c> for closer.</param> /// <returns>The distance to the next master device. <c>-1</c> if no masters found.</returns> public static float TryLinkToNextMaster(IMultitoolSlaveable slave, int direction) { if (Masters.Count < 1) { return(-1); } SortMastersToPosition(slave.gameObject.transform.position); var index = slave.Master == null ? 0 : Masters.IndexOf(slave.Master); index = Mathf.Clamp(index + direction, 0, Masters.Count - 1); var master = Masters[index]; var distance = Vector3.Distance(slave.gameObject.transform.position, master.gameObject.transform.position); if (distance <= master.MaxDistance) { slave.SetMasterEditor(master); } return(distance); }