diff --git a/Content.Client/Dice/DiceSystem.cs b/Content.Client/Dice/DiceSystem.cs index 2d36488257..2890de5f2f 100644 --- a/Content.Client/Dice/DiceSystem.cs +++ b/Content.Client/Dice/DiceSystem.cs @@ -5,17 +5,24 @@ namespace Content.Client.Dice; 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(OnDiceAfterHandleState); + } + + private void OnDiceAfterHandleState(Entity entity, ref AfterAutoHandleStateEvent args) + { + if (!TryComp(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}"); } } diff --git a/Content.Server/Dice/DiceSystem.cs b/Content.Server/Dice/DiceSystem.cs index 2d13679bd0..c2cb62a250 100644 --- a/Content.Server/Dice/DiceSystem.cs +++ b/Content.Server/Dice/DiceSystem.cs @@ -1,28 +1,5 @@ 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; diff --git a/Content.Shared/Dice/DiceComponent.cs b/Content.Shared/Dice/DiceComponent.cs index c01ad3c451..27f7bd70e0 100644 --- a/Content.Shared/Dice/DiceComponent.cs +++ b/Content.Shared/Dice/DiceComponent.cs @@ -1,6 +1,5 @@ using Robust.Shared.Audio; using Robust.Shared.GameStates; -using Robust.Shared.Serialization; namespace Content.Shared.Dice; diff --git a/Content.Shared/Dice/SharedDiceSystem.cs b/Content.Shared/Dice/SharedDiceSystem.cs index 8e2868e791..71a51584d3 100644 --- a/Content.Shared/Dice/SharedDiceSystem.cs +++ b/Content.Shared/Dice/SharedDiceSystem.cs @@ -1,12 +1,18 @@ 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(); @@ -14,76 +20,67 @@ public abstract class SharedDiceSystem : EntitySystem SubscribeLocalEvent(OnUseInHand); SubscribeLocalEvent(OnLand); SubscribeLocalEvent(OnExamined); - SubscribeLocalEvent(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 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 entity, ref LandEvent args) { - Roll(uid, component); + Roll(entity); } - private void OnExamined(EntityUid uid, DiceComponent dice, ExaminedEvent args) + private void OnExamined(Entity 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 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 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 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); } }