Dice tweaks (#13514)

This commit is contained in:
Leon Friedrich
2023-01-21 12:51:26 +13:00
committed by GitHub
parent a0b6e052a1
commit 2904a368f7
92 changed files with 274 additions and 181 deletions

View File

@@ -0,0 +1,44 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Dice;
[RegisterComponent, NetworkedComponent, Access(typeof(SharedDiceSystem))]
public sealed class DiceComponent : Component
{
[DataField("sound")]
public SoundSpecifier Sound { get; } = new SoundCollectionSpecifier("Dice");
/// <summary>
/// Multiplier for the value of a die. Applied after the <see cref="Offset"/>.
/// </summary>
[DataField("multiplier")]
public int Multiplier { get; } = 1;
/// <summary>
/// Quantity that is subtracted from the value of a die. Can be used to make dice that start at "0". Applied
/// before the <see cref="Multiplier"/>
/// </summary>
[DataField("offset")]
public int Offset { get; } = 0;
[DataField("sides")]
public int Sides { get; } = 20;
/// <summary>
/// The currently displayed value.
/// </summary>
[DataField("currentValue")]
public int CurrentValue { get; set; } = 20;
[Serializable, NetSerializable]
public sealed class DiceState : ComponentState
{
public int CurrentSide { get; set; } = 20;
public DiceState(int side)
{
CurrentSide = side;
}
}
}

View File

@@ -0,0 +1,93 @@
using Content.Shared.Examine;
using Content.Shared.Interaction.Events;
using Content.Shared.Throwing;
using Robust.Shared.GameStates;
namespace Content.Shared.Dice;
public abstract class SharedDiceSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DiceComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<DiceComponent, LandEvent>(OnLand);
SubscribeLocalEvent<DiceComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<DiceComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<DiceComponent, ComponentHandleState>(OnHandleState);
}
private void OnHandleState(EntityUid uid, DiceComponent component, ref ComponentHandleState args)
{
if (args.Current is DiceComponent.DiceState state)
component.CurrentValue = state.CurrentSide;
UpdateVisuals(uid, component);
}
private void OnGetState(EntityUid uid, DiceComponent component, ref ComponentGetState args)
{
args.State = new DiceComponent.DiceState(component.CurrentValue);
}
private void OnUseInHand(EntityUid uid, DiceComponent component, UseInHandEvent args)
{
if (args.Handled) return;
args.Handled = true;
Roll(uid, component);
}
private void OnLand(EntityUid uid, DiceComponent component, ref LandEvent args)
{
Roll(uid, component);
}
private void OnExamined(EntityUid uid, DiceComponent dice, ExaminedEvent args)
{
//No details check, since the sprite updates to show the side.
args.PushMarkup(Loc.GetString("dice-component-on-examine-message-part-1", ("sidesAmount", dice.Sides)));
args.PushMarkup(Loc.GetString("dice-component-on-examine-message-part-2", ("currentSide", dice.CurrentValue)));
}
public void SetCurrentSide(EntityUid uid, int side, DiceComponent? die = null)
{
if (!Resolve(uid, ref die))
return;
if (side < 1 || side > die.Sides)
{
Logger.Error($"Attempted to set die {ToPrettyString(uid)} to an invalid side ({side}).");
return;
}
die.CurrentValue = (side - die.Offset) * die.Multiplier;
Dirty(die);
UpdateVisuals(uid, die);
}
public void SetCurrentValue(EntityUid uid, int value, DiceComponent? die = null)
{
if (!Resolve(uid, ref die))
return;
if (value % die.Multiplier != 0 || value/ die.Multiplier + die.Offset < 1)
{
Logger.Error($"Attempted to set die {ToPrettyString(uid)} to an invalid value ({value}).");
return;
}
SetCurrentSide(uid, value / die.Multiplier + die.Offset, die);
}
protected virtual void UpdateVisuals(EntityUid uid, DiceComponent? die = null)
{
// See client system.
}
public virtual void Roll(EntityUid uid, DiceComponent? die = null)
{
// See the server system, client cannot predict rolling.
}
}