1
0

CrayonSystem.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Server.Administration.Logs;
  4. using Content.Server.Decals;
  5. using Content.Server.Nutrition.EntitySystems;
  6. using Content.Server.Popups;
  7. using Content.Shared.Crayon;
  8. using Content.Shared.Database;
  9. using Content.Shared.Decals;
  10. using Content.Shared.Interaction;
  11. using Content.Shared.Interaction.Events;
  12. using Robust.Server.GameObjects;
  13. using Robust.Shared.Audio;
  14. using Robust.Shared.Audio.Systems;
  15. using Robust.Shared.GameStates;
  16. using Robust.Shared.Player;
  17. using Robust.Shared.Prototypes;
  18. namespace Content.Server.Crayon;
  19. public sealed class CrayonSystem : SharedCrayonSystem
  20. {
  21. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  22. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  23. [Dependency] private readonly DecalSystem _decals = default!;
  24. [Dependency] private readonly PopupSystem _popup = default!;
  25. [Dependency] private readonly SharedAudioSystem _audio = default!;
  26. [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
  27. public override void Initialize()
  28. {
  29. base.Initialize();
  30. SubscribeLocalEvent<CrayonComponent, ComponentInit>(OnCrayonInit);
  31. SubscribeLocalEvent<CrayonComponent, CrayonSelectMessage>(OnCrayonBoundUI);
  32. SubscribeLocalEvent<CrayonComponent, CrayonColorMessage>(OnCrayonBoundUIColor);
  33. SubscribeLocalEvent<CrayonComponent, UseInHandEvent>(OnCrayonUse, before: new[] { typeof(FoodSystem) });
  34. SubscribeLocalEvent<CrayonComponent, AfterInteractEvent>(OnCrayonAfterInteract, after: new[] { typeof(FoodSystem) });
  35. SubscribeLocalEvent<CrayonComponent, DroppedEvent>(OnCrayonDropped);
  36. SubscribeLocalEvent<CrayonComponent, ComponentGetState>(OnCrayonGetState);
  37. }
  38. private static void OnCrayonGetState(EntityUid uid, CrayonComponent component, ref ComponentGetState args)
  39. {
  40. args.State = new CrayonComponentState(component.Color, component.SelectedState, component.Charges, component.Capacity);
  41. }
  42. private void OnCrayonAfterInteract(EntityUid uid, CrayonComponent component, AfterInteractEvent args)
  43. {
  44. if (args.Handled || !args.CanReach)
  45. return;
  46. if (component.Charges <= 0)
  47. {
  48. if (component.DeleteEmpty)
  49. UseUpCrayon(uid, args.User);
  50. else
  51. _popup.PopupEntity(Loc.GetString("crayon-interact-not-enough-left-text"), uid, args.User);
  52. args.Handled = true;
  53. return;
  54. }
  55. if (!args.ClickLocation.IsValid(EntityManager))
  56. {
  57. _popup.PopupEntity(Loc.GetString("crayon-interact-invalid-location"), uid, args.User);
  58. args.Handled = true;
  59. return;
  60. }
  61. if (!_decals.TryAddDecal(component.SelectedState, args.ClickLocation.Offset(new Vector2(-0.5f, -0.5f)), out _, component.Color, cleanable: true))
  62. return;
  63. if (component.UseSound != null)
  64. _audio.PlayPvs(component.UseSound, uid, AudioParams.Default.WithVariation(0.125f));
  65. // Decrease "Ammo"
  66. component.Charges--;
  67. Dirty(uid, component);
  68. _adminLogger.Add(LogType.CrayonDraw, LogImpact.Low, $"{EntityManager.ToPrettyString(args.User):user} drew a {component.Color:color} {component.SelectedState}");
  69. args.Handled = true;
  70. if (component.DeleteEmpty && component.Charges <= 0)
  71. UseUpCrayon(uid, args.User);
  72. else
  73. _uiSystem.ServerSendUiMessage(uid, SharedCrayonComponent.CrayonUiKey.Key, new CrayonUsedMessage(component.SelectedState));
  74. }
  75. private void OnCrayonUse(EntityUid uid, CrayonComponent component, UseInHandEvent args)
  76. {
  77. // Open crayon window if neccessary.
  78. if (args.Handled)
  79. return;
  80. if (!_uiSystem.HasUi(uid, SharedCrayonComponent.CrayonUiKey.Key))
  81. {
  82. return;
  83. }
  84. _uiSystem.TryToggleUi(uid, SharedCrayonComponent.CrayonUiKey.Key, args.User);
  85. _uiSystem.SetUiState(uid, SharedCrayonComponent.CrayonUiKey.Key, new CrayonBoundUserInterfaceState(component.SelectedState, component.SelectableColor, component.Color));
  86. args.Handled = true;
  87. }
  88. private void OnCrayonBoundUI(EntityUid uid, CrayonComponent component, CrayonSelectMessage args)
  89. {
  90. // Check if the selected state is valid
  91. if (!_prototypeManager.TryIndex<DecalPrototype>(args.State, out var prototype) || !prototype.Tags.Contains("crayon"))
  92. return;
  93. component.SelectedState = args.State;
  94. Dirty(uid, component);
  95. }
  96. private void OnCrayonBoundUIColor(EntityUid uid, CrayonComponent component, CrayonColorMessage args)
  97. {
  98. // you still need to ensure that the given color is a valid color
  99. if (!component.SelectableColor || args.Color == component.Color)
  100. return;
  101. component.Color = args.Color;
  102. Dirty(uid, component);
  103. }
  104. private void OnCrayonInit(EntityUid uid, CrayonComponent component, ComponentInit args)
  105. {
  106. component.Charges = component.Capacity;
  107. // Get the first one from the catalog and set it as default
  108. var decal = _prototypeManager.EnumeratePrototypes<DecalPrototype>().FirstOrDefault(x => x.Tags.Contains("crayon"));
  109. component.SelectedState = decal?.ID ?? string.Empty;
  110. Dirty(uid, component);
  111. }
  112. private void OnCrayonDropped(EntityUid uid, CrayonComponent component, DroppedEvent args)
  113. {
  114. // TODO: Use the existing event.
  115. _uiSystem.CloseUi(uid, SharedCrayonComponent.CrayonUiKey.Key, args.User);
  116. }
  117. private void UseUpCrayon(EntityUid uid, EntityUid user)
  118. {
  119. _popup.PopupEntity(Loc.GetString("crayon-interact-used-up-text", ("owner", uid)), user, user);
  120. EntityManager.QueueDeleteEntity(uid);
  121. }
  122. }