CatStation/Content.Shared/Dice/SharedDiceSystem.cs
Whatstone a88d2c5164
Cleanup: Resolve remaining PopupSystem warnings in Content.Shared (#44685)
* PopupClient to PopupEntity/Coordinates

* PopupPredictedX to PopupEntity/Cursor/Coordinates

* The rest of the damn owl

* nullability fixes

* Buckle cleanup

* cleanup, address PR comments

* ExecutionSystem: one popup method

* correct my filthy lies

* Apply most of ajcm's suggestions

* return user to SharedItemRecallSystem.cs

* Fix lingering Ensnarable broadcast popups

* targeted drain popups

* fix lingering untargeted Guardian popups

I read the comment on GuardianHostRecall.  Oops.

* target stethoscope popups

* fix the PDA vibration popup

* Fix target for encryption key removal popup

* anomaly scanner: args.User, not uid

* disposal unit comment, bcast pda vibration popup
2026-07-16 21:54:33 +00:00

87 lines
2.9 KiB
C#

using Content.Shared.Examine;
using Content.Shared.Interaction.Events;
using Content.Shared.Popups;
using Content.Shared.Random.Helpers;
using Content.Shared.Throwing;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Timing;
namespace Content.Shared.Dice;
public abstract partial class SharedDiceSystem : EntitySystem
{
[Dependency] private IGameTiming _timing = default!;
[Dependency] private SharedAudioSystem _audio = default!;
[Dependency] private SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DiceComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<DiceComponent, LandEvent>(OnLand);
SubscribeLocalEvent<DiceComponent, ExaminedEvent>(OnExamined);
}
private void OnUseInHand(Entity<DiceComponent> entity, ref UseInHandEvent args)
{
if (args.Handled)
return;
Roll(entity, args.User);
args.Handled = true;
}
private void OnLand(Entity<DiceComponent> entity, ref LandEvent args)
{
Roll(entity);
}
private void OnExamined(Entity<DiceComponent> entity, ref ExaminedEvent args)
{
//No details check, since the sprite updates to show the side.
using (args.PushGroup(nameof(DiceComponent)))
{
args.PushMarkup(Loc.GetString("dice-component-on-examine-message-part-1", ("sidesAmount", entity.Comp.Sides)));
args.PushMarkup(Loc.GetString("dice-component-on-examine-message-part-2",
("currentSide", entity.Comp.CurrentValue)));
}
}
private void SetCurrentSide(Entity<DiceComponent> entity, int side)
{
if (side < 1 || side > entity.Comp.Sides)
{
Log.Error($"Attempted to set die {ToPrettyString(entity)} to an invalid side ({side}).");
return;
}
entity.Comp.CurrentValue = (side - entity.Comp.Offset) * entity.Comp.Multiplier;
Dirty(entity);
}
public void SetCurrentValue(Entity<DiceComponent> entity, int value)
{
if (value % entity.Comp.Multiplier != 0 || value / entity.Comp.Multiplier + entity.Comp.Offset < 1)
{
Log.Error($"Attempted to set die {ToPrettyString(entity)} to an invalid value ({value}).");
return;
}
SetCurrentSide(entity, value / entity.Comp.Multiplier + entity.Comp.Offset);
}
private void Roll(Entity<DiceComponent> entity, EntityUid? user = null)
{
var rand = SharedRandomExtensions.PredictedRandom(_timing, GetNetEntity(entity));
var roll = rand.Next(1, entity.Comp.Sides + 1);
SetCurrentSide(entity, roll);
var popupString = Loc.GetString("dice-component-on-roll-land",
("die", entity),
("currentSide", entity.Comp.CurrentValue));
_popup.PopupEntity(popupString, entity);
_audio.PlayPredicted(entity.Comp.Sound, entity, user);
}
}