1
0

EggLayerSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Content.Server.Actions;
  2. using Content.Server.Animals.Components;
  3. using Content.Server.Popups;
  4. using Content.Shared.Actions.Events;
  5. using Content.Shared.Mobs.Systems;
  6. using Content.Shared.Nutrition.Components;
  7. using Content.Shared.Nutrition.EntitySystems;
  8. using Content.Shared.Storage;
  9. using Robust.Server.Audio;
  10. using Robust.Shared.Player;
  11. using Robust.Shared.Random;
  12. using Robust.Shared.Timing;
  13. namespace Content.Server.Animals.Systems;
  14. /// <summary>
  15. /// Gives the ability to lay eggs/other things;
  16. /// produces endlessly if the owner does not have a HungerComponent.
  17. /// </summary>
  18. public sealed class EggLayerSystem : EntitySystem
  19. {
  20. [Dependency] private readonly IRobustRandom _random = default!;
  21. [Dependency] private readonly ActionsSystem _actions = default!;
  22. [Dependency] private readonly AudioSystem _audio = default!;
  23. [Dependency] private readonly HungerSystem _hunger = default!;
  24. [Dependency] private readonly IGameTiming _timing = default!;
  25. [Dependency] private readonly PopupSystem _popup = default!;
  26. [Dependency] private readonly MobStateSystem _mobState = default!;
  27. public override void Initialize()
  28. {
  29. base.Initialize();
  30. SubscribeLocalEvent<EggLayerComponent, MapInitEvent>(OnMapInit);
  31. SubscribeLocalEvent<EggLayerComponent, EggLayInstantActionEvent>(OnEggLayAction);
  32. }
  33. public override void Update(float frameTime)
  34. {
  35. base.Update(frameTime);
  36. var query = EntityQueryEnumerator<EggLayerComponent>();
  37. while (query.MoveNext(out var uid, out var eggLayer))
  38. {
  39. // Players should be using the action.
  40. if (HasComp<ActorComponent>(uid))
  41. continue;
  42. if (_timing.CurTime < eggLayer.NextGrowth)
  43. continue;
  44. // Randomize next growth time for more organic egglaying.
  45. eggLayer.NextGrowth += TimeSpan.FromSeconds(_random.NextFloat(eggLayer.EggLayCooldownMin, eggLayer.EggLayCooldownMax));
  46. if (_mobState.IsDead(uid))
  47. continue;
  48. // Hungerlevel check/modification is done in TryLayEgg()
  49. // so it's used for player controlled chickens as well.
  50. TryLayEgg(uid, eggLayer);
  51. }
  52. }
  53. private void OnMapInit(EntityUid uid, EggLayerComponent component, MapInitEvent args)
  54. {
  55. _actions.AddAction(uid, ref component.Action, component.EggLayAction);
  56. component.NextGrowth = _timing.CurTime + TimeSpan.FromSeconds(_random.NextFloat(component.EggLayCooldownMin, component.EggLayCooldownMax));
  57. }
  58. private void OnEggLayAction(EntityUid uid, EggLayerComponent egglayer, EggLayInstantActionEvent args)
  59. {
  60. // Cooldown is handeled by ActionAnimalLayEgg in types.yml.
  61. args.Handled = TryLayEgg(uid, egglayer);
  62. }
  63. public bool TryLayEgg(EntityUid uid, EggLayerComponent? egglayer)
  64. {
  65. if (!Resolve(uid, ref egglayer))
  66. return false;
  67. if (_mobState.IsDead(uid))
  68. return false;
  69. // Allow infinitely laying eggs if they can't get hungry.
  70. if (TryComp<HungerComponent>(uid, out var hunger))
  71. {
  72. if (_hunger.GetHunger(hunger) < egglayer.HungerUsage)
  73. {
  74. _popup.PopupEntity(Loc.GetString("action-popup-lay-egg-too-hungry"), uid, uid);
  75. return false;
  76. }
  77. _hunger.ModifyHunger(uid, -egglayer.HungerUsage, hunger);
  78. }
  79. foreach (var ent in EntitySpawnCollection.GetSpawns(egglayer.EggSpawn, _random))
  80. {
  81. Spawn(ent, Transform(uid).Coordinates);
  82. }
  83. // Sound + popups
  84. _audio.PlayPvs(egglayer.EggLaySound, uid);
  85. _popup.PopupEntity(Loc.GetString("action-popup-lay-egg-user"), uid, uid);
  86. _popup.PopupEntity(Loc.GetString("action-popup-lay-egg-others", ("entity", uid)), uid, Filter.PvsExcept(uid), true);
  87. return true;
  88. }
  89. }