EmoteOnDamageSystem.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. namespace Content.Server.Chat.Systems;
  2. using Content.Shared.Chat.Prototypes;
  3. using Content.Shared.Damage;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Random;
  6. using Robust.Shared.Timing;
  7. using Robust.Shared.Utility;
  8. public sealed class EmoteOnDamageSystem : EntitySystem
  9. {
  10. [Dependency] private readonly IGameTiming _gameTiming = default!;
  11. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  12. [Dependency] private readonly IRobustRandom _random = default!;
  13. [Dependency] private readonly ChatSystem _chatSystem = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<EmoteOnDamageComponent, DamageChangedEvent>(OnDamage);
  18. }
  19. private void OnDamage(EntityUid uid, EmoteOnDamageComponent emoteOnDamage, DamageChangedEvent args)
  20. {
  21. if (!args.DamageIncreased)
  22. return;
  23. if (emoteOnDamage.LastEmoteTime + emoteOnDamage.EmoteCooldown > _gameTiming.CurTime)
  24. return;
  25. if (emoteOnDamage.Emotes.Count == 0)
  26. return;
  27. if (!_random.Prob(emoteOnDamage.EmoteChance))
  28. return;
  29. var emote = _random.Pick(emoteOnDamage.Emotes);
  30. if (emoteOnDamage.WithChat)
  31. {
  32. _chatSystem.TryEmoteWithChat(uid, emote, emoteOnDamage.HiddenFromChatWindow ? ChatTransmitRange.HideChat : ChatTransmitRange.Normal);
  33. }
  34. else
  35. {
  36. _chatSystem.TryEmoteWithoutChat(uid,emote);
  37. }
  38. emoteOnDamage.LastEmoteTime = _gameTiming.CurTime;
  39. }
  40. /// <summary>
  41. /// Try to add an emote to the entity, which will be performed at an interval.
  42. /// </summary>
  43. public bool AddEmote(EntityUid uid, string emotePrototypeId, EmoteOnDamageComponent? emoteOnDamage = null)
  44. {
  45. if (!Resolve(uid, ref emoteOnDamage, logMissing: false))
  46. return false;
  47. DebugTools.Assert(emoteOnDamage.LifeStage <= ComponentLifeStage.Running);
  48. DebugTools.Assert(_prototypeManager.HasIndex<EmotePrototype>(emotePrototypeId), "Prototype not found. Did you make a typo?");
  49. return emoteOnDamage.Emotes.Add(emotePrototypeId);
  50. }
  51. /// <summary>
  52. /// Stop preforming an emote. Note that by default this will queue empty components for removal.
  53. /// </summary>
  54. public bool RemoveEmote(EntityUid uid, string emotePrototypeId, EmoteOnDamageComponent? emoteOnDamage = null, bool removeEmpty = true)
  55. {
  56. if (!Resolve(uid, ref emoteOnDamage, logMissing: false))
  57. return false;
  58. DebugTools.Assert(_prototypeManager.HasIndex<EmotePrototype>(emotePrototypeId), "Prototype not found. Did you make a typo?");
  59. if (!emoteOnDamage.Emotes.Remove(emotePrototypeId))
  60. return false;
  61. if (removeEmpty && emoteOnDamage.Emotes.Count == 0)
  62. RemCompDeferred(uid, emoteOnDamage);
  63. return true;
  64. }
  65. }