монетка

This commit is contained in:
BIGZi0348
2024-11-23 18:41:21 +03:00
parent 08332da6df
commit c6dde1628f
9 changed files with 216 additions and 5 deletions

View File

@@ -0,0 +1,36 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared._White.CoinDice;
[RegisterComponent, NetworkedComponent, Access(typeof(SharedCoinDiceSystem))]
[AutoGenerateComponentState(true)]
public sealed partial class CoinDiceComponent : Component
{
[DataField]
public SoundSpecifier Sound { get; private set; } = new SoundCollectionSpecifier("Dice");
/// <summary>
/// Multiplier for the value of a die. Applied after the <see cref="Offset"/>.
/// </summary>
[DataField]
public int Multiplier { get; private set; } = 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]
public int Offset { get; private set; } = 0;
[DataField]
public int Sides { get; private set; } = 20;
/// <summary>
/// The currently displayed value.
/// </summary>
[DataField]
[AutoNetworkedField]
public int CurrentValue { get; set; } = 20;
}

View File

@@ -0,0 +1,104 @@
using Content.Shared.Examine;
using Content.Shared.Interaction.Events;
using Content.Shared.Throwing;
using Robust.Shared.Timing;
namespace Content.Shared._White.CoinDice;
public abstract class SharedCoinDiceSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CoinDiceComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<CoinDiceComponent, LandEvent>(OnLand);
SubscribeLocalEvent<CoinDiceComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<CoinDiceComponent, AfterAutoHandleStateEvent>(OnDiceAfterHandleState);
}
private void OnDiceAfterHandleState(EntityUid uid, CoinDiceComponent component, ref AfterAutoHandleStateEvent args)
{
UpdateVisuals(uid, component);
}
private void OnUseInHand(EntityUid uid, CoinDiceComponent component, UseInHandEvent args)
{
if (args.Handled)
return;
args.Handled = true;
Roll(uid, component);
}
private void OnLand(EntityUid uid, CoinDiceComponent component, ref LandEvent args)
{
Roll(uid, component);
}
private void OnExamined(EntityUid uid, CoinDiceComponent dice, ExaminedEvent args)
{
//No details check, since the sprite updates to show the side.
using (args.PushGroup(nameof(CoinDiceComponent)))
{
args.PushMarkup(Loc.GetString("coindice-component-on-examine-message-part-1"));
var coindiceResult = "";
switch (dice.CurrentValue)
{
case 1:
coindiceResult = "орёл";
break;
case 2:
coindiceResult = "решка";
break;
default:
coindiceResult = "ребро";
break;
}
args.PushMarkup(Loc.GetString("coindice-component-on-examine-message-part-2",
("currentSide", coindiceResult)));
}
}
public void SetCurrentSide(EntityUid uid, int side, CoinDiceComponent? die = null)
{
if (!Resolve(uid, ref die))
return;
if (side < 1 || side > die.Sides)
{
Log.Error($"Attempted to set die {ToPrettyString(uid)} to an invalid side ({side}).");
return;
}
die.CurrentValue = (side - die.Offset) * die.Multiplier;
Dirty(uid, die);
UpdateVisuals(uid, die);
}
public void SetCurrentValue(EntityUid uid, int value, CoinDiceComponent? die = null)
{
if (!Resolve(uid, ref die))
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}).");
return;
}
SetCurrentSide(uid, value / die.Multiplier + die.Offset, die);
}
protected virtual void UpdateVisuals(EntityUid uid, CoinDiceComponent? die = null)
{
// See client system.
}
public virtual void Roll(EntityUid uid, CoinDiceComponent? die = null)
{
// See the server system, client cannot predict rolling.
}
}