SharedDiceSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Content.Shared.Examine;
  2. using Content.Shared.Interaction.Events;
  3. using Content.Shared.Popups;
  4. using Content.Shared.Throwing;
  5. using Robust.Shared.Audio.Systems;
  6. using Robust.Shared.Timing;
  7. namespace Content.Shared.Dice;
  8. public abstract class SharedDiceSystem : EntitySystem
  9. {
  10. [Dependency] private readonly IGameTiming _timing = default!;
  11. [Dependency] private readonly SharedAudioSystem _audio = default!;
  12. [Dependency] private readonly SharedPopupSystem _popup = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<DiceComponent, UseInHandEvent>(OnUseInHand);
  17. SubscribeLocalEvent<DiceComponent, LandEvent>(OnLand);
  18. SubscribeLocalEvent<DiceComponent, ExaminedEvent>(OnExamined);
  19. }
  20. private void OnUseInHand(Entity<DiceComponent> entity, ref UseInHandEvent args)
  21. {
  22. if (args.Handled)
  23. return;
  24. Roll(entity, args.User);
  25. args.Handled = true;
  26. }
  27. private void OnLand(Entity<DiceComponent> entity, ref LandEvent args)
  28. {
  29. Roll(entity);
  30. }
  31. private void OnExamined(Entity<DiceComponent> entity, ref ExaminedEvent args)
  32. {
  33. //No details check, since the sprite updates to show the side.
  34. using (args.PushGroup(nameof(DiceComponent)))
  35. {
  36. args.PushMarkup(Loc.GetString("dice-component-on-examine-message-part-1", ("sidesAmount", entity.Comp.Sides)));
  37. args.PushMarkup(Loc.GetString("dice-component-on-examine-message-part-2",
  38. ("currentSide", entity.Comp.CurrentValue)));
  39. }
  40. }
  41. private void SetCurrentSide(Entity<DiceComponent> entity, int side)
  42. {
  43. if (side < 1 || side > entity.Comp.Sides)
  44. {
  45. Log.Error($"Attempted to set die {ToPrettyString(entity)} to an invalid side ({side}).");
  46. return;
  47. }
  48. entity.Comp.CurrentValue = (side - entity.Comp.Offset) * entity.Comp.Multiplier;
  49. Dirty(entity);
  50. }
  51. public void SetCurrentValue(Entity<DiceComponent> entity, int value)
  52. {
  53. if (value % entity.Comp.Multiplier != 0 || value / entity.Comp.Multiplier + entity.Comp.Offset < 1)
  54. {
  55. Log.Error($"Attempted to set die {ToPrettyString(entity)} to an invalid value ({value}).");
  56. return;
  57. }
  58. SetCurrentSide(entity, value / entity.Comp.Multiplier + entity.Comp.Offset);
  59. }
  60. private void Roll(Entity<DiceComponent> entity, EntityUid? user = null)
  61. {
  62. var rand = new System.Random((int)_timing.CurTick.Value);
  63. var roll = rand.Next(1, entity.Comp.Sides + 1);
  64. SetCurrentSide(entity, roll);
  65. var popupString = Loc.GetString("dice-component-on-roll-land",
  66. ("die", entity),
  67. ("currentSide", entity.Comp.CurrentValue));
  68. _popup.PopupPredicted(popupString, entity, user);
  69. _audio.PlayPredicted(entity.Comp.Sound, entity, user);
  70. }
  71. }