Re-organize all projects (#4166)
This commit is contained in:
93
Content.Server/Dice/DiceComponent.cs
Normal file
93
Content.Server/Dice/DiceComponent.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Throwing;
|
||||
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.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
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override string Name => "Dice";
|
||||
|
||||
[DataField("step")]
|
||||
private int _step = 1;
|
||||
private int _sides = 20;
|
||||
private int _currentSide = 20;
|
||||
[ViewVariables]
|
||||
[DataField("diceSoundCollection")]
|
||||
public string _soundCollectionName = "dice";
|
||||
[ViewVariables]
|
||||
public int Step => _step;
|
||||
[ViewVariables]
|
||||
[DataField("sides")]
|
||||
public int Sides
|
||||
{
|
||||
get => _sides;
|
||||
set
|
||||
{
|
||||
_sides = value;
|
||||
_currentSide = value;
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables]
|
||||
public int CurrentSide => _currentSide;
|
||||
|
||||
public void Roll()
|
||||
{
|
||||
_currentSide = _random.Next(1, (_sides/_step)+1) * _step;
|
||||
if (!Owner.TryGetComponent(out SpriteComponent? sprite)) return;
|
||||
sprite.LayerSetState(0, $"d{_sides}{_currentSide}");
|
||||
PlayDiceEffect();
|
||||
}
|
||||
|
||||
public void PlayDiceEffect()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(_soundCollectionName))
|
||||
{
|
||||
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(_soundCollectionName);
|
||||
var file = _random.Pick(soundCollection.PickFiles);
|
||||
SoundSystem.Play(Filter.Pvs(Owner), file, 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(
|
||||
"A dice with [color=lightgray]{0}[/color] sides.\n" + "It has landed on a [color=white]{1}[/color].",
|
||||
_sides, _currentSide));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user