SpillWhenWornSystem.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Content.Shared.Chemistry.EntitySystems;
  2. using Content.Shared.Clothing;
  3. using Content.Shared.Fluids.Components;
  4. namespace Content.Shared.Fluids.EntitySystems;
  5. /// <inheritdoc cref="SpillWhenWornComponent"/>
  6. public sealed class SpillWhenWornSystem : EntitySystem
  7. {
  8. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
  9. [Dependency] private readonly SharedPuddleSystem _puddle = default!;
  10. public override void Initialize()
  11. {
  12. base.Initialize();
  13. SubscribeLocalEvent<SpillWhenWornComponent, ClothingGotEquippedEvent>(OnGotEquipped);
  14. SubscribeLocalEvent<SpillWhenWornComponent, ClothingGotUnequippedEvent>(OnGotUnequipped);
  15. SubscribeLocalEvent<SpillWhenWornComponent, SolutionAccessAttemptEvent>(OnSolutionAccessAttempt);
  16. }
  17. private void OnGotEquipped(Entity<SpillWhenWornComponent> ent, ref ClothingGotEquippedEvent args)
  18. {
  19. if (_solutionContainer.TryGetSolution(ent.Owner, ent.Comp.Solution, out var soln, out var solution)
  20. && solution.Volume > 0)
  21. {
  22. // Spill all solution on the player
  23. var drainedSolution = _solutionContainer.Drain(ent.Owner, soln.Value, solution.Volume);
  24. _puddle.TrySplashSpillAt(ent.Owner, Transform(args.Wearer).Coordinates, drainedSolution, out _);
  25. }
  26. // Flag as worn after draining, otherwise we'll block ourself from accessing!
  27. ent.Comp.IsWorn = true;
  28. Dirty(ent);
  29. }
  30. private void OnGotUnequipped(Entity<SpillWhenWornComponent> ent, ref ClothingGotUnequippedEvent args)
  31. {
  32. ent.Comp.IsWorn = false;
  33. Dirty(ent);
  34. }
  35. private void OnSolutionAccessAttempt(Entity<SpillWhenWornComponent> ent, ref SolutionAccessAttemptEvent args)
  36. {
  37. // If we're not being worn right now, we don't care
  38. if (!ent.Comp.IsWorn)
  39. return;
  40. // Make sure it's the right solution
  41. if (ent.Comp.Solution != args.SolutionName)
  42. return;
  43. args.Cancelled = true;
  44. }
  45. }