1
0

BotanySwabSystem.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Content.Server.Botany.Components;
  2. using Content.Server.Popups;
  3. using Content.Shared.DoAfter;
  4. using Content.Shared.Examine;
  5. using Content.Shared.Interaction;
  6. using Content.Shared.Swab;
  7. namespace Content.Server.Botany.Systems;
  8. public sealed class BotanySwabSystem : EntitySystem
  9. {
  10. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  11. [Dependency] private readonly PopupSystem _popupSystem = default!;
  12. [Dependency] private readonly MutationSystem _mutationSystem = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<BotanySwabComponent, ExaminedEvent>(OnExamined);
  17. SubscribeLocalEvent<BotanySwabComponent, AfterInteractEvent>(OnAfterInteract);
  18. SubscribeLocalEvent<BotanySwabComponent, BotanySwabDoAfterEvent>(OnDoAfter);
  19. }
  20. /// <summary>
  21. /// This handles swab examination text
  22. /// so you can tell if they are used or not.
  23. /// </summary>
  24. private void OnExamined(EntityUid uid, BotanySwabComponent swab, ExaminedEvent args)
  25. {
  26. if (args.IsInDetailsRange)
  27. {
  28. if (swab.SeedData != null)
  29. args.PushMarkup(Loc.GetString("swab-used"));
  30. else
  31. args.PushMarkup(Loc.GetString("swab-unused"));
  32. }
  33. }
  34. /// <summary>
  35. /// Handles swabbing a plant.
  36. /// </summary>
  37. private void OnAfterInteract(EntityUid uid, BotanySwabComponent swab, AfterInteractEvent args)
  38. {
  39. if (args.Target == null || !args.CanReach || !HasComp<PlantHolderComponent>(args.Target))
  40. return;
  41. _doAfterSystem.TryStartDoAfter(new DoAfterArgs(EntityManager, args.User, swab.SwabDelay, new BotanySwabDoAfterEvent(), uid, target: args.Target, used: uid)
  42. {
  43. Broadcast = true,
  44. BreakOnMove = true,
  45. NeedHand = true,
  46. });
  47. }
  48. /// <summary>
  49. /// Save seed data or cross-pollenate.
  50. /// </summary>
  51. private void OnDoAfter(EntityUid uid, BotanySwabComponent swab, DoAfterEvent args)
  52. {
  53. if (args.Cancelled || args.Handled || !TryComp<PlantHolderComponent>(args.Args.Target, out var plant))
  54. return;
  55. if (swab.SeedData == null)
  56. {
  57. // Pick up pollen
  58. swab.SeedData = plant.Seed;
  59. _popupSystem.PopupEntity(Loc.GetString("botany-swab-from"), args.Args.Target.Value, args.Args.User);
  60. }
  61. else
  62. {
  63. var old = plant.Seed;
  64. if (old == null)
  65. return;
  66. plant.Seed = _mutationSystem.Cross(swab.SeedData, old); // Cross-pollenate
  67. swab.SeedData = old; // Transfer old plant pollen to swab
  68. _popupSystem.PopupEntity(Loc.GetString("botany-swab-to"), args.Args.Target.Value, args.Args.User);
  69. }
  70. args.Handled = true;
  71. }
  72. }