]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Predicted dice rolls (#34863)
authorPlykiya <58439124+Plykiya@users.noreply.github.com>
Fri, 14 Feb 2025 15:46:25 +0000 (07:46 -0800)
committerGitHub <noreply@github.com>
Fri, 14 Feb 2025 15:46:25 +0000 (16:46 +0100)
* Predicted dice rolls

* Removed server-side dice system, make Shared no longer abstract, move visual code to client-side system

* cleanup

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Content.Client/Dice/DiceSystem.cs
Content.Server/Dice/DiceSystem.cs
Content.Shared/Dice/DiceComponent.cs
Content.Shared/Dice/SharedDiceSystem.cs

index 2d36488257bd71d29bc209bf60746d70845abd4c..2890de5f2f1d28ca6f04ada3070e907c041264da 100644 (file)
@@ -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<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}");
     }
 }
index 2d13679bd099f31d00d77fe7876324d131b4053c..c2cb62a250fc75fd325fade933706dfbcafaeff9 100644 (file)
@@ -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;
index c01ad3c451886ecdef3aed571569f3facfb4540c..27f7bd70e06fe4f4fa793e7f7228a005924b635b 100644 (file)
@@ -1,6 +1,5 @@
 using Robust.Shared.Audio;
 using Robust.Shared.GameStates;
-using Robust.Shared.Serialization;
 
 namespace Content.Shared.Dice;
 
index 8e2868e791ddfc7b5e9802da301e577bf3992395..71a51584d36b7972b09693bb8d8ede076284e34e 100644 (file)
@@ -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<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);
     }
 }