FlintRockSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Content.Server.DoAfter;
  2. using Content.Shared.Rocks;
  3. using Content.Shared.Interaction;
  4. using Content.Shared.DoAfter;
  5. using Robust.Server.GameObjects;
  6. using Robust.Shared.Random;
  7. using Robust.Shared.Timing;
  8. using Content.Shared.Popups;
  9. using Content.Shared.Verbs;
  10. using Content.Shared.Examine;
  11. using Content.Shared.Hands.EntitySystems;
  12. namespace Content.Server.Rocks;
  13. public sealed partial class FlintRockSystem : EntitySystem
  14. {
  15. [Dependency] private readonly IRobustRandom Random = default!;
  16. [Dependency] private readonly IGameTiming _gameTiming = default!;
  17. [Dependency] private readonly SharedPopupSystem _popup = default!;
  18. [Dependency] private readonly DoAfterSystem _doAfter = default!;
  19. [Dependency] private readonly SharedHandsSystem _hands = default!;
  20. public override void Initialize()
  21. {
  22. base.Initialize();
  23. SubscribeLocalEvent<FlintRockComponent, MapInitEvent>(OnMapInit);
  24. SubscribeLocalEvent<FlintRockComponent, CollectFlintDoAfterEvent>(OnDoAfter);
  25. SubscribeLocalEvent<FlintRockComponent, GetVerbsEvent<AlternativeVerb>>(OnGetVerbs);
  26. SubscribeLocalEvent<FlintRockComponent, ExaminedEvent>(OnExamined);
  27. }
  28. private void OnMapInit(EntityUid uid, FlintRockComponent component, MapInitEvent args)
  29. {
  30. component.CurrentFlints = Random.Next(1, component.MaxFlints + 1);
  31. }
  32. public override void Update(float frameTime)
  33. {
  34. base.Update(frameTime);
  35. var query = EntityQueryEnumerator<FlintRockComponent>();
  36. while (query.MoveNext(out var uid, out var component))
  37. {
  38. var currentTime = _gameTiming.CurTime;
  39. if (currentTime >= component.LastRegenerationTime + TimeSpan.FromHours(component.RegenerationTime))
  40. {
  41. if (component.CurrentFlints < component.MaxFlints)
  42. {
  43. component.CurrentFlints++;
  44. component.LastRegenerationTime = currentTime;
  45. }
  46. }
  47. }
  48. }
  49. private void OnGetVerbs(EntityUid uid, FlintRockComponent component, ref GetVerbsEvent<AlternativeVerb> args)
  50. {
  51. if (!args.CanAccess || !args.CanInteract || component.CurrentFlints <= 0)
  52. return;
  53. var user = args.User;
  54. var verb = new AlternativeVerb
  55. {
  56. Text = "Collect Flint",
  57. Act = () => StartCollectingFlint(uid, component, user)
  58. };
  59. args.Verbs.Add(verb);
  60. }
  61. private void StartCollectingFlint(EntityUid rockUid, FlintRockComponent component, EntityUid user)
  62. {
  63. var doAfterArgs = new DoAfterArgs(EntityManager, user, component.CollectionTime, new CollectFlintDoAfterEvent(), rockUid)
  64. {
  65. BreakOnMove = true,
  66. BreakOnDamage = true,
  67. NeedHand = true
  68. };
  69. _doAfter.TryStartDoAfter(doAfterArgs);
  70. }
  71. private void OnDoAfter(EntityUid uid, FlintRockComponent component, ref CollectFlintDoAfterEvent args)
  72. {
  73. if (args.Cancelled || args.Handled)
  74. return;
  75. component.CurrentFlints--;
  76. var spawnPos = Transform(uid).MapPosition;
  77. var flint = Spawn("Flint", spawnPos);
  78. _hands.TryPickupAnyHand(args.Args.User, flint);
  79. _popup.PopupEntity("You successfully collect a flint.", uid, args.Args.User);
  80. args.Handled = true;
  81. }
  82. private void OnExamined(EntityUid uid, FlintRockComponent component, ExaminedEvent args)
  83. {
  84. if (!args.IsInDetailsRange)
  85. return;
  86. var flintCount = component.CurrentFlints;
  87. if (flintCount > 0)
  88. {
  89. var message = flintCount == 1
  90. ? "A single piece of flint is partially exposed in the rock."
  91. : $"There are {flintCount} loose pieces of flint in the rock.";
  92. args.PushMarkup(message);
  93. }
  94. }
  95. }