SliceableFoodSystem.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using Content.Server.DoAfter;
  2. using Content.Server.Nutrition.Components;
  3. using Content.Shared.Chemistry.EntitySystems;
  4. using Content.Shared.Nutrition;
  5. using Content.Shared.Nutrition.Components;
  6. using Content.Shared.Chemistry.Components;
  7. using Content.Shared.DoAfter;
  8. using Content.Shared.FixedPoint;
  9. using Content.Shared.Interaction;
  10. using Robust.Server.GameObjects;
  11. using Robust.Shared.Audio;
  12. using Robust.Shared.Audio.Systems;
  13. using Robust.Shared.Random;
  14. using Robust.Shared.Containers;
  15. using Robust.Shared.Physics.Components;
  16. using Robust.Shared.Physics.Systems;
  17. namespace Content.Server.Nutrition.EntitySystems;
  18. public sealed class SliceableFoodSystem : EntitySystem
  19. {
  20. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
  21. [Dependency] private readonly SharedAudioSystem _audio = default!;
  22. [Dependency] private readonly TransformSystem _transform = default!;
  23. [Dependency] private readonly DoAfterSystem _doAfter = default!;
  24. [Dependency] private readonly IRobustRandom _random = default!;
  25. [Dependency] private readonly SharedContainerSystem _container = default!;
  26. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  27. public override void Initialize()
  28. {
  29. base.Initialize();
  30. SubscribeLocalEvent<SliceableFoodComponent, InteractUsingEvent>(OnInteractUsing);
  31. SubscribeLocalEvent<SliceableFoodComponent, SliceFoodDoAfterEvent>(OnSlicedoAfter);
  32. SubscribeLocalEvent<SliceableFoodComponent, ComponentStartup>(OnComponentStartup);
  33. }
  34. private void OnInteractUsing(Entity<SliceableFoodComponent> entity, ref InteractUsingEvent args)
  35. {
  36. if (args.Handled)
  37. return;
  38. var doAfterArgs = new DoAfterArgs(EntityManager,
  39. args.User,
  40. entity.Comp.SliceTime,
  41. new SliceFoodDoAfterEvent(),
  42. entity,
  43. entity,
  44. args.Used)
  45. {
  46. BreakOnDamage = true,
  47. BreakOnMove = true,
  48. NeedHand = true,
  49. };
  50. _doAfter.TryStartDoAfter(doAfterArgs);
  51. }
  52. private void OnSlicedoAfter(Entity<SliceableFoodComponent> entity, ref SliceFoodDoAfterEvent args)
  53. {
  54. if (args.Cancelled || args.Handled || args.Args.Target == null)
  55. return;
  56. if (TrySliceFood(entity, args.User, args.Used, entity.Comp))
  57. args.Handled = true;
  58. }
  59. private bool TrySliceFood(EntityUid uid,
  60. EntityUid user,
  61. EntityUid? usedItem,
  62. SliceableFoodComponent? component = null,
  63. FoodComponent? food = null,
  64. TransformComponent? transform = null)
  65. {
  66. if (!Resolve(uid, ref component, ref food, ref transform) ||
  67. string.IsNullOrEmpty(component.Slice))
  68. return false;
  69. if (!_solutionContainer.TryGetSolution(uid, food.Solution, out var soln, out var solution))
  70. return false;
  71. if (!TryComp<UtensilComponent>(usedItem, out var utensil) || (utensil.Types & UtensilType.Knife) == 0)
  72. return false;
  73. var sliceVolume = solution.Volume / FixedPoint2.New(component.TotalCount);
  74. for (int i = 0; i < component.TotalCount; i++)
  75. {
  76. var sliceUid = Slice(uid, user, component, transform);
  77. var lostSolution =
  78. _solutionContainer.SplitSolution(soln.Value, sliceVolume);
  79. // Fill new slice
  80. FillSlice(sliceUid, lostSolution);
  81. }
  82. _audio.PlayPvs(component.Sound, transform.Coordinates, AudioParams.Default.WithVolume(-2));
  83. var ev = new SliceFoodEvent();
  84. RaiseLocalEvent(uid, ref ev);
  85. DeleteFood(uid, user, food);
  86. return true;
  87. }
  88. /// <summary>
  89. /// Create a new slice in the world and returns its entity.
  90. /// The solutions must be set afterwards.
  91. /// </summary>
  92. public EntityUid Slice(EntityUid uid,
  93. EntityUid user,
  94. SliceableFoodComponent? comp = null,
  95. TransformComponent? transform = null)
  96. {
  97. if (!Resolve(uid, ref comp, ref transform))
  98. return EntityUid.Invalid;
  99. var sliceUid = Spawn(comp.Slice, _transform.GetMapCoordinates(uid));
  100. // try putting the slice into the container if the food being sliced is in a container!
  101. // this lets you do things like slice a pizza up inside of a hot food cart without making a food-everywhere mess
  102. _transform.DropNextTo(sliceUid, (uid, transform));
  103. _transform.SetLocalRotation(sliceUid, 0);
  104. if (!_container.IsEntityOrParentInContainer(sliceUid))
  105. {
  106. var randVect = _random.NextVector2(2.0f, 2.5f);
  107. if (TryComp<PhysicsComponent>(sliceUid, out var physics))
  108. _physics.SetLinearVelocity(sliceUid, randVect, body: physics);
  109. }
  110. return sliceUid;
  111. }
  112. private void DeleteFood(EntityUid uid, EntityUid user, FoodComponent foodComp)
  113. {
  114. var ev = new BeforeFullySlicedEvent
  115. {
  116. User = user
  117. };
  118. RaiseLocalEvent(uid, ev);
  119. if (ev.Cancelled)
  120. return;
  121. // Locate the sliced food and spawn its trash
  122. foreach (var trash in foodComp.Trash)
  123. {
  124. var trashUid = Spawn(trash, _transform.GetMapCoordinates(uid));
  125. // try putting the trash in the food's container too, to be consistent with slice spawning?
  126. _transform.DropNextTo(trashUid, uid);
  127. _transform.SetLocalRotation(trashUid, 0);
  128. }
  129. QueueDel(uid);
  130. }
  131. private void FillSlice(EntityUid sliceUid, Solution solution)
  132. {
  133. // Replace all reagents on prototype not just copying poisons (example: slices of eaten pizza should have less nutrition)
  134. if (TryComp<FoodComponent>(sliceUid, out var sliceFoodComp) &&
  135. _solutionContainer.TryGetSolution(sliceUid, sliceFoodComp.Solution, out var itsSoln, out var itsSolution))
  136. {
  137. _solutionContainer.RemoveAllSolution(itsSoln.Value);
  138. var lostSolutionPart = solution.SplitSolution(itsSolution.AvailableVolume);
  139. _solutionContainer.TryAddSolution(itsSoln.Value, lostSolutionPart);
  140. }
  141. }
  142. private void OnComponentStartup(Entity<SliceableFoodComponent> entity, ref ComponentStartup args)
  143. {
  144. var foodComp = EnsureComp<FoodComponent>(entity);
  145. _solutionContainer.EnsureSolution(entity.Owner, foodComp.Solution, out _);
  146. }
  147. }