HandHelpers.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Linq;
  2. using Content.Shared.Hands.EntitySystems;
  3. namespace Content.Shared.Hands.Components;
  4. /// <summary>
  5. /// These helpers exist to make getting basic information out of the hands component more convenient, without
  6. /// needing to resolve hands system or something like that.
  7. /// </summary>
  8. public static class HandHelpers
  9. {
  10. /// <summary>
  11. /// Returns true if any hand is free. This is a LinQ method, not a property, so
  12. /// cache it instead of accessing this multiple times.
  13. /// </summary>
  14. public static bool IsAnyHandFree(this HandsComponent component) => component.Hands.Values.Any(hand => hand.IsEmpty);
  15. /// <summary>
  16. /// Get the number of hands that are not currently holding anything. This is a LinQ method, not a property, so
  17. /// cache it instead of accessing this multiple times.
  18. /// </summary>
  19. public static int CountFreeHands(this HandsComponent component) => component.Hands.Values.Count(hand => hand.IsEmpty);
  20. /// <summary>
  21. /// Get the number of hands that are not currently holding anything. This is a LinQ method, not a property, so
  22. /// cache it instead of accessing this multiple times.
  23. /// </summary>
  24. public static int CountFreeableHands(this Entity<HandsComponent> component, SharedHandsSystem system)
  25. {
  26. return system.CountFreeableHands(component);
  27. }
  28. /// <summary>
  29. /// Get a list of hands that are currently holding nothing. This is a LinQ method, not a property, so cache
  30. /// it instead of accessing this multiple times.
  31. /// </summary>
  32. public static IEnumerable<Hand> GetFreeHands(this HandsComponent component) => component.Hands.Values.Where(hand => !hand.IsEmpty);
  33. /// <summary>
  34. /// Get a list of hands that are currently holding nothing. This is a LinQ method, not a property, so cache
  35. /// it instead of accessing this multiple times.
  36. /// </summary>
  37. public static IEnumerable<string> GetFreeHandNames(this HandsComponent component) => GetFreeHands(component).Select(hand => hand.Name);
  38. }