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

@@ -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);
}
}