WoolySystem.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Content.Shared.Chemistry.EntitySystems;
  2. using Content.Shared.Mobs.Systems;
  3. using Content.Shared.Nutrition;
  4. using Content.Shared.Nutrition.Components;
  5. using Content.Shared.Nutrition.EntitySystems;
  6. using Robust.Shared.Timing;
  7. namespace Content.Shared.Animals;
  8. /// <summary>
  9. /// Gives ability to produce fiber reagents;
  10. /// produces endlessly if the owner has no HungerComponent.
  11. /// </summary>
  12. public sealed class WoolySystem : EntitySystem
  13. {
  14. [Dependency] private readonly HungerSystem _hunger = default!;
  15. [Dependency] private readonly IGameTiming _timing = default!;
  16. [Dependency] private readonly MobStateSystem _mobState = default!;
  17. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
  18. public override void Initialize()
  19. {
  20. base.Initialize();
  21. SubscribeLocalEvent<WoolyComponent, BeforeFullyEatenEvent>(OnBeforeFullyEaten);
  22. SubscribeLocalEvent<WoolyComponent, MapInitEvent>(OnMapInit);
  23. }
  24. private void OnMapInit(EntityUid uid, WoolyComponent component, MapInitEvent args)
  25. {
  26. component.NextGrowth = _timing.CurTime + component.GrowthDelay;
  27. }
  28. public override void Update(float frameTime)
  29. {
  30. base.Update(frameTime);
  31. var query = EntityQueryEnumerator<WoolyComponent>();
  32. while (query.MoveNext(out var uid, out var wooly))
  33. {
  34. if (_timing.CurTime < wooly.NextGrowth)
  35. continue;
  36. wooly.NextGrowth += wooly.GrowthDelay;
  37. if (_mobState.IsDead(uid))
  38. continue;
  39. if (!_solutionContainer.ResolveSolution(uid, wooly.SolutionName, ref wooly.Solution, out var solution))
  40. continue;
  41. if (solution.AvailableVolume == 0)
  42. continue;
  43. // Actually there is food digestion so no problem with instant reagent generation "OnFeed"
  44. if (EntityManager.TryGetComponent(uid, out HungerComponent? hunger))
  45. {
  46. // Is there enough nutrition to produce reagent?
  47. if (_hunger.GetHungerThreshold(hunger) < HungerThreshold.Okay)
  48. continue;
  49. _hunger.ModifyHunger(uid, -wooly.HungerUsage, hunger);
  50. }
  51. _solutionContainer.TryAddReagent(wooly.Solution.Value, wooly.ReagentId, wooly.Quantity, out _);
  52. }
  53. }
  54. private void OnBeforeFullyEaten(Entity<WoolyComponent> ent, ref BeforeFullyEatenEvent args)
  55. {
  56. // don't want moths to delete goats after eating them
  57. args.Cancel();
  58. }
  59. }