public sealed class DiceSystem : SharedDiceSystem
{
- protected override void UpdateVisuals(EntityUid uid, DiceComponent? die = null)
+ public override void Initialize()
{
- if (!Resolve(uid, ref die) || !TryComp(uid, out SpriteComponent? sprite))
+ base.Initialize();
+
+ SubscribeLocalEvent<DiceComponent, AfterAutoHandleStateEvent>(OnDiceAfterHandleState);
+ }
+
+ private void OnDiceAfterHandleState(Entity<DiceComponent> entity, ref AfterAutoHandleStateEvent args)
+ {
+ if (!TryComp<SpriteComponent>(entity, out var sprite))
return;
- // TODO maybe just move each diue to its own RSI?
+ // TODO maybe just move each die to its own RSI?
var state = sprite.LayerGetState(0).Name;
if (state == null)
return;
var prefix = state.Substring(0, state.IndexOf('_'));
- sprite.LayerSetState(0, $"{prefix}_{die.CurrentValue}");
+ sprite.LayerSetState(0, $"{prefix}_{entity.Comp.CurrentValue}");
}
}
using Content.Shared.Dice;
-using Content.Shared.Popups;
-using JetBrains.Annotations;
-using Robust.Shared.Audio;
-using Robust.Shared.Audio.Systems;
-using Robust.Shared.Random;
namespace Content.Server.Dice;
-[UsedImplicitly]
-public sealed class DiceSystem : SharedDiceSystem
-{
- [Dependency] private readonly IRobustRandom _random = default!;
- [Dependency] private readonly SharedAudioSystem _audio = default!;
- [Dependency] private readonly SharedPopupSystem _popup = default!;
-
- public override void Roll(EntityUid uid, DiceComponent? die = null)
- {
- if (!Resolve(uid, ref die))
- return;
-
- var roll = _random.Next(1, die.Sides + 1);
- SetCurrentSide(uid, roll, die);
-
- _popup.PopupEntity(Loc.GetString("dice-component-on-roll-land", ("die", uid), ("currentSide", die.CurrentValue)), uid);
- _audio.PlayPvs(die.Sound, uid);
- }
-}
+public sealed class DiceSystem : SharedDiceSystem;
using Content.Shared.Examine;
using Content.Shared.Interaction.Events;
+using Content.Shared.Popups;
using Content.Shared.Throwing;
-using Robust.Shared.GameStates;
+using Robust.Shared.Audio.Systems;
+using Robust.Shared.Timing;
namespace Content.Shared.Dice;
public abstract class SharedDiceSystem : EntitySystem
{
+ [Dependency] private readonly IGameTiming _timing = default!;
+ [Dependency] private readonly SharedAudioSystem _audio = default!;
+ [Dependency] private readonly SharedPopupSystem _popup = default!;
+
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DiceComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<DiceComponent, LandEvent>(OnLand);
SubscribeLocalEvent<DiceComponent, ExaminedEvent>(OnExamined);
- SubscribeLocalEvent<DiceComponent, AfterAutoHandleStateEvent>(OnDiceAfterHandleState);
- }
-
- private void OnDiceAfterHandleState(EntityUid uid, DiceComponent component, ref AfterAutoHandleStateEvent args)
- {
- UpdateVisuals(uid, component);
}
- private void OnUseInHand(EntityUid uid, DiceComponent component, UseInHandEvent args)
+ private void OnUseInHand(Entity<DiceComponent> entity, ref UseInHandEvent args)
{
if (args.Handled)
return;
+ Roll(entity, args.User);
args.Handled = true;
- Roll(uid, component);
}
- private void OnLand(EntityUid uid, DiceComponent component, ref LandEvent args)
+ private void OnLand(Entity<DiceComponent> entity, ref LandEvent args)
{
- Roll(uid, component);
+ Roll(entity);
}
- private void OnExamined(EntityUid uid, DiceComponent dice, ExaminedEvent args)
+ 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", dice.Sides)));
+ 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", dice.CurrentValue)));
+ ("currentSide", entity.Comp.CurrentValue)));
}
}
- public void SetCurrentSide(EntityUid uid, int side, DiceComponent? die = null)
+ private void SetCurrentSide(Entity<DiceComponent> entity, int side)
{
- if (!Resolve(uid, ref die))
- return;
-
- if (side < 1 || side > die.Sides)
+ if (side < 1 || side > entity.Comp.Sides)
{
- Log.Error($"Attempted to set die {ToPrettyString(uid)} to an invalid side ({side}).");
+ Log.Error($"Attempted to set die {ToPrettyString(entity)} to an invalid side ({side}).");
return;
}
- die.CurrentValue = (side - die.Offset) * die.Multiplier;
- Dirty(uid, die);
- UpdateVisuals(uid, die);
+ entity.Comp.CurrentValue = (side - entity.Comp.Offset) * entity.Comp.Multiplier;
+ Dirty(entity);
}
- public void SetCurrentValue(EntityUid uid, int value, DiceComponent? die = null)
+ public void SetCurrentValue(Entity<DiceComponent> entity, int value)
{
- if (!Resolve(uid, ref die))
- return;
-
- if (value % die.Multiplier != 0 || value/ die.Multiplier + die.Offset < 1)
+ if (value % entity.Comp.Multiplier != 0 || value / entity.Comp.Multiplier + entity.Comp.Offset < 1)
{
- Log.Error($"Attempted to set die {ToPrettyString(uid)} to an invalid value ({value}).");
+ Log.Error($"Attempted to set die {ToPrettyString(entity)} to an invalid value ({value}).");
return;
}
- SetCurrentSide(uid, value / die.Multiplier + die.Offset, die);
+ SetCurrentSide(entity, value / entity.Comp.Multiplier + entity.Comp.Offset);
}
- protected virtual void UpdateVisuals(EntityUid uid, DiceComponent? die = null)
+ private void Roll(Entity<DiceComponent> entity, EntityUid? user = null)
{
- // See client system.
- }
+ var rand = new System.Random((int)_timing.CurTick.Value);
- public virtual void Roll(EntityUid uid, DiceComponent? die = null)
- {
- // See the server system, client cannot predict rolling.
+ 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.PopupPredicted(popupString, entity, user);
+ _audio.PlayPredicted(entity.Comp.Sound, entity, user);
}
}