SpookySpeakerSystem.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Content.Server.Chat.Systems;
  2. using Content.Server.Ghost.Components;
  3. using Content.Shared.Random.Helpers;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Random;
  6. using Robust.Shared.Timing;
  7. namespace Content.Server.Ghost;
  8. public sealed class SpookySpeakerSystem : EntitySystem
  9. {
  10. [Dependency] private readonly IPrototypeManager _proto = default!;
  11. [Dependency] private readonly IRobustRandom _random = default!;
  12. [Dependency] private readonly IGameTiming _timing = default!;
  13. [Dependency] private readonly ChatSystem _chat = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<SpookySpeakerComponent, GhostBooEvent>(OnGhostBoo);
  18. }
  19. private void OnGhostBoo(Entity<SpookySpeakerComponent> entity, ref GhostBooEvent args)
  20. {
  21. // Only activate sometimes, so groups don't all trigger together
  22. if (!_random.Prob(entity.Comp.SpeakChance))
  23. return;
  24. var curTime = _timing.CurTime;
  25. // Enforce a delay between messages to prevent spam
  26. if (curTime < entity.Comp.NextSpeakTime)
  27. return;
  28. if (!_proto.TryIndex(entity.Comp.MessageSet, out var messages))
  29. return;
  30. // Grab a random localized message from the set
  31. var message = _random.Pick(messages);
  32. // Chatcode moment: messages starting with '.' are considered radio messages unless prefixed with '>'
  33. // So this is a stupid trick to make the "...Oooo"-style messages work.
  34. message = '>' + message;
  35. // Say the message
  36. _chat.TrySendInGameICMessage(entity, message, InGameICChatType.Speak, hideChat: true);
  37. // Set the delay for the next message
  38. entity.Comp.NextSpeakTime = curTime + entity.Comp.Cooldown;
  39. args.Handled = true;
  40. }
  41. }