Predicted dice rolls (#34863)

* 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>
This commit is contained in:
Plykiya
2025-02-14 07:46:25 -08:00
committed by GitHub
parent 63b9255e71
commit 862a2a744e
4 changed files with 45 additions and 65 deletions

View File

@@ -5,17 +5,24 @@ namespace Content.Client.Dice;
public sealed class DiceSystem : SharedDiceSystem 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; 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; var state = sprite.LayerGetState(0).Name;
if (state == null) if (state == null)
return; return;
var prefix = state.Substring(0, state.IndexOf('_')); var prefix = state.Substring(0, state.IndexOf('_'));
sprite.LayerSetState(0, $"{prefix}_{die.CurrentValue}"); sprite.LayerSetState(0, $"{prefix}_{entity.Comp.CurrentValue}");
} }
} }

View File

@@ -1,28 +1,5 @@
using Content.Shared.Dice; 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; namespace Content.Server.Dice;
[UsedImplicitly] public sealed class DiceSystem : SharedDiceSystem;
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);
}
}

View File

@@ -1,6 +1,5 @@
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Dice; namespace Content.Shared.Dice;

View File

@@ -1,12 +1,18 @@
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Interaction.Events; using Content.Shared.Interaction.Events;
using Content.Shared.Popups;
using Content.Shared.Throwing; using Content.Shared.Throwing;
using Robust.Shared.GameStates; using Robust.Shared.Audio.Systems;
using Robust.Shared.Timing;
namespace Content.Shared.Dice; namespace Content.Shared.Dice;
public abstract class SharedDiceSystem : EntitySystem 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() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
@@ -14,76 +20,67 @@ public abstract class SharedDiceSystem : EntitySystem
SubscribeLocalEvent<DiceComponent, UseInHandEvent>(OnUseInHand); SubscribeLocalEvent<DiceComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<DiceComponent, LandEvent>(OnLand); SubscribeLocalEvent<DiceComponent, LandEvent>(OnLand);
SubscribeLocalEvent<DiceComponent, ExaminedEvent>(OnExamined); SubscribeLocalEvent<DiceComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<DiceComponent, AfterAutoHandleStateEvent>(OnDiceAfterHandleState);
} }
private void OnDiceAfterHandleState(EntityUid uid, DiceComponent component, ref AfterAutoHandleStateEvent args) private void OnUseInHand(Entity<DiceComponent> entity, ref UseInHandEvent args)
{
UpdateVisuals(uid, component);
}
private void OnUseInHand(EntityUid uid, DiceComponent component, UseInHandEvent args)
{ {
if (args.Handled) if (args.Handled)
return; return;
Roll(entity, args.User);
args.Handled = true; 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. //No details check, since the sprite updates to show the side.
using (args.PushGroup(nameof(DiceComponent))) 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", 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)) if (side < 1 || side > entity.Comp.Sides)
return;
if (side < 1 || side > die.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; return;
} }
die.CurrentValue = (side - die.Offset) * die.Multiplier; entity.Comp.CurrentValue = (side - entity.Comp.Offset) * entity.Comp.Multiplier;
Dirty(uid, die); Dirty(entity);
UpdateVisuals(uid, die);
} }
public void SetCurrentValue(EntityUid uid, int value, DiceComponent? die = null) public void SetCurrentValue(Entity<DiceComponent> entity, int value)
{ {
if (!Resolve(uid, ref die)) if (value % entity.Comp.Multiplier != 0 || value / entity.Comp.Multiplier + entity.Comp.Offset < 1)
return;
if (value % die.Multiplier != 0 || value/ die.Multiplier + die.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; 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) var roll = rand.Next(1, entity.Comp.Sides + 1);
{ SetCurrentSide(entity, roll);
// See the server system, client cannot predict rolling.
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);
} }
} }