UtensilSystem.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Content.Shared.Containers.ItemSlots;
  2. using Content.Server.Nutrition.Components;
  3. using Content.Server.Popups;
  4. using Content.Shared.Interaction;
  5. using Content.Shared.Nutrition.Components;
  6. using Content.Shared.Nutrition.EntitySystems;
  7. using Content.Shared.Tools.EntitySystems;
  8. using Robust.Shared.Audio;
  9. using Robust.Shared.Audio.Systems;
  10. using Robust.Shared.Random;
  11. namespace Content.Server.Nutrition.EntitySystems
  12. {
  13. /// <summary>
  14. /// Handles usage of the utensils on the food items
  15. /// </summary>
  16. internal sealed class UtensilSystem : SharedUtensilSystem
  17. {
  18. [Dependency] private readonly IRobustRandom _robustRandom = default!;
  19. [Dependency] private readonly FoodSystem _foodSystem = default!;
  20. [Dependency] private readonly PopupSystem _popupSystem = default!;
  21. [Dependency] private readonly SharedAudioSystem _audio = default!;
  22. [Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. SubscribeLocalEvent<UtensilComponent, AfterInteractEvent>(OnAfterInteract, after: new[] { typeof(ItemSlotsSystem), typeof(ToolOpenableSystem) });
  27. }
  28. /// <summary>
  29. /// Clicked with utensil
  30. /// </summary>
  31. private void OnAfterInteract(Entity<UtensilComponent> entity, ref AfterInteractEvent ev)
  32. {
  33. if (ev.Handled || ev.Target == null || !ev.CanReach)
  34. return;
  35. var result = TryUseUtensil(ev.User, ev.Target.Value, entity);
  36. ev.Handled = result.Handled;
  37. }
  38. public (bool Success, bool Handled) TryUseUtensil(EntityUid user, EntityUid target, Entity<UtensilComponent> utensil)
  39. {
  40. if (!EntityManager.TryGetComponent(target, out FoodComponent? food))
  41. return (false, false);
  42. //Prevents food usage with a wrong utensil
  43. if ((food.Utensil & utensil.Comp.Types) == 0)
  44. {
  45. _popupSystem.PopupEntity(Loc.GetString("food-system-wrong-utensil", ("food", target), ("utensil", utensil.Owner)), user, user);
  46. return (false, true);
  47. }
  48. if (!_interactionSystem.InRangeUnobstructed(user, target, popup: true))
  49. return (false, true);
  50. return _foodSystem.TryFeed(user, user, target, food);
  51. }
  52. /// <summary>
  53. /// Attempt to break the utensil after interaction.
  54. /// </summary>
  55. /// <param name="uid">Utensil.</param>
  56. /// <param name="userUid">User of the utensil.</param>
  57. public void TryBreak(EntityUid uid, EntityUid userUid, UtensilComponent? component = null)
  58. {
  59. if (!Resolve(uid, ref component))
  60. return;
  61. if (_robustRandom.Prob(component.BreakChance))
  62. {
  63. _audio.PlayPvs(component.BreakSound, userUid, AudioParams.Default.WithVolume(-2f));
  64. EntityManager.DeleteEntity(uid);
  65. }
  66. }
  67. }
  68. }