/////////////////////////////////////////////////////////////////////////// public Tool GetNearestTool(Vector2 referencePos, bool forceWithinRange, Vector2?requireDirectionTowards) { Tool bestTool = null; float bestDist = float.MaxValue; for (int c = 0; c < m_AllSlots.Length; ++c) { TrolleySlot curSlot = m_AllSlots[c]; if (!curSlot.OccupiedWith) { continue; } Vector2 slotPos = GetSlotPositionWS(curSlot.PositionOS).xz(); float dist = (referencePos - slotPos).magnitude; if (forceWithinRange && dist > ToolPickupDistance) { continue; } if (dist > bestDist) { continue; } if (requireDirectionTowards.HasValue) { float dot = Vector2.Dot(slotPos - referencePos, requireDirectionTowards.Value); if (dot < 0) { continue; } } bestTool = curSlot.OccupiedWith; bestDist = dist; } return(bestTool); }
/////////////////////////////////////////////////////////////////////////// public void SwitchTool(ref Tool returnedTool, Tool grabbedTool) { Debug.Assert(grabbedTool); for (int s = 0; s < m_AllSlots.Length; ++s) { TrolleySlot curSlot = m_AllSlots[s]; if (curSlot.OccupiedWith != grabbedTool) { continue; } curSlot.OccupiedWith = returnedTool; returnedTool = grabbedTool; m_AllSlots[s] = curSlot; return; } Debug.LogWarning("Could not grab " + grabbedTool.name.AddBrackets()); }