Makes DiceComponent ECS
This commit is contained in:
@@ -1,35 +1,19 @@
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Sound;
|
||||
using Content.Shared.Throwing;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Dice
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class DiceComponent : Component, IActivate, IUse, ILand, IExamine
|
||||
[RegisterComponent, Friend(typeof(DiceSystem))]
|
||||
public class DiceComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override string Name => "Dice";
|
||||
|
||||
private int _sides = 20;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("sound")]
|
||||
private readonly SoundSpecifier _sound = new SoundCollectionSpecifier("Dice");
|
||||
public SoundSpecifier Sound { get; } = new SoundCollectionSpecifier("Dice");
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("step")]
|
||||
@@ -37,60 +21,9 @@ namespace Content.Server.Dice
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("sides")]
|
||||
public int Sides
|
||||
{
|
||||
get => _sides;
|
||||
set
|
||||
{
|
||||
_sides = value;
|
||||
CurrentSide = value;
|
||||
}
|
||||
}
|
||||
public int Sides { get; } = 20;
|
||||
|
||||
[ViewVariables]
|
||||
public int CurrentSide { get; private set; } = 20;
|
||||
|
||||
public void Roll()
|
||||
{
|
||||
CurrentSide = _random.Next(1, (_sides/Step)+1) * Step;
|
||||
|
||||
PlayDiceEffect();
|
||||
|
||||
if (!Owner.TryGetComponent(out SpriteComponent? sprite))
|
||||
return;
|
||||
|
||||
sprite.LayerSetState(0, $"d{_sides}{CurrentSide}");
|
||||
}
|
||||
|
||||
public void PlayDiceEffect()
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _sound.GetSound(), Owner, AudioParams.Default);
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
Roll();
|
||||
}
|
||||
|
||||
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
Roll();
|
||||
return false;
|
||||
}
|
||||
|
||||
void ILand.Land(LandEventArgs eventArgs)
|
||||
{
|
||||
Roll();
|
||||
}
|
||||
|
||||
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
|
||||
{
|
||||
//No details check, since the sprite updates to show the side.
|
||||
message.AddMarkup(Loc.GetString("dice-component-on-examine-message-part-1",
|
||||
("sidesAmount", _sides))
|
||||
+ "\n" +
|
||||
Loc.GetString("dice-component-on-examine-message-part-2",
|
||||
("currentSide", CurrentSide)));
|
||||
}
|
||||
public int CurrentSide { get; set; } = 20;
|
||||
}
|
||||
}
|
||||
|
||||
68
Content.Server/Dice/DiceSystem.cs
Normal file
68
Content.Server/Dice/DiceSystem.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Throwing;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Dice
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class DiceSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<DiceComponent, ActivateInWorldEvent>(OnActivate);
|
||||
SubscribeLocalEvent<DiceComponent, UseInHandEvent>(OnUse);
|
||||
SubscribeLocalEvent<DiceComponent, LandEvent>(OnLand);
|
||||
SubscribeLocalEvent<DiceComponent, ExaminedEvent>(OnExamined);
|
||||
}
|
||||
|
||||
public void Roll(EntityUid uid, DiceComponent die)
|
||||
{
|
||||
die.CurrentSide = _random.Next(1, (die.Sides/die.Step)+1) * die.Step;
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(die.Owner), die.Sound.GetSound(), die.Owner, AudioHelpers.WithVariation(0.05f));
|
||||
|
||||
if (!ComponentManager.TryGetComponent(uid, out SpriteComponent? sprite))
|
||||
return;
|
||||
|
||||
// TODO DICE: Use a visualizer instead.
|
||||
sprite.LayerSetState(0, $"d{die.Sides}{die.CurrentSide}");
|
||||
}
|
||||
|
||||
private void OnActivate(EntityUid uid, DiceComponent component, ActivateInWorldEvent args)
|
||||
{
|
||||
Roll(uid, component);
|
||||
}
|
||||
|
||||
private void OnUse(EntityUid uid, DiceComponent component, UseInHandEvent args)
|
||||
{
|
||||
Roll(uid, component);
|
||||
}
|
||||
|
||||
private void OnLand(EntityUid uid, DiceComponent component, 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.Message.PushNewline();
|
||||
args.Message.AddMarkup(Loc.GetString("dice-component-on-examine-message-part-1", ("sidesAmount", dice.Sides)));
|
||||
args.Message.PushNewline();
|
||||
args.Message.AddMarkup(Loc.GetString("dice-component-on-examine-message-part-2", ("currentSide", dice.CurrentSide)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user