diff --git a/Content.Server/Dice/DiceComponent.cs b/Content.Server/Dice/DiceComponent.cs index 99bdf36eaa..57165e882a 100644 --- a/Content.Server/Dice/DiceComponent.cs +++ b/Content.Server/Dice/DiceComponent.cs @@ -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; } } diff --git a/Content.Server/Dice/DiceSystem.cs b/Content.Server/Dice/DiceSystem.cs new file mode 100644 index 0000000000..82d8438197 --- /dev/null +++ b/Content.Server/Dice/DiceSystem.cs @@ -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(OnActivate); + SubscribeLocalEvent(OnUse); + SubscribeLocalEvent(OnLand); + SubscribeLocalEvent(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))); + } + } +} diff --git a/Resources/Locale/en-US/dice/dice-component.ftl b/Resources/Locale/en-US/dice/dice-component.ftl index 2a8d8634c8..fa74e80dfb 100644 --- a/Resources/Locale/en-US/dice/dice-component.ftl +++ b/Resources/Locale/en-US/dice/dice-component.ftl @@ -1,2 +1,2 @@ -dice-component-on-examine-message-part-1 = A dice with [color=lightgray]{$sidesAmount}[/color] sides. -dice-component-on-examine-message-part-2 = It has landed on a [color=white]{$currentSide}[/color]. \ No newline at end of file +dice-component-on-examine-message-part-1 = A die with [color=lightgray]{$sidesAmount}[/color] sides. +dice-component-on-examine-message-part-2 = It has landed on a [color=white]{$currentSide}[/color]. diff --git a/Resources/Maps/saltern.yml b/Resources/Maps/saltern.yml index 87db4ae268..012e7232d3 100644 --- a/Resources/Maps/saltern.yml +++ b/Resources/Maps/saltern.yml @@ -24441,7 +24441,7 @@ entities: - canCollide: False type: Physics - uid: 2378 - type: d20 + type: d20Dice components: - pos: -23.834803,-5.3811545 parent: 853 @@ -24449,7 +24449,7 @@ entities: - canCollide: False type: Physics - uid: 2379 - type: d8 + type: d8Dice components: - pos: -24.287928,-6.3967795 parent: 853 diff --git a/Resources/Prototypes/Entities/Objects/Fun/dice.yml b/Resources/Prototypes/Entities/Objects/Fun/dice.yml index 93ae58dd7c..d810b5c318 100644 --- a/Resources/Prototypes/Entities/Objects/Fun/dice.yml +++ b/Resources/Prototypes/Entities/Objects/Fun/dice.yml @@ -6,10 +6,11 @@ - type: Dice - type: Sprite sprite: Objects/Fun/dice.rsi + noRot: true # If their sprites rotate, the number becomes even more illegible than usual. - type: entity parent: BaseDice - id: d100 + id: d100Dice name: d100 description: A die with one hundred sides! Probably not fairly weighted... components: @@ -21,7 +22,7 @@ - type: entity parent: BaseDice - id: d20 + id: d20Dice name: d20 description: A die with twenty sides. The preferred die to throw at the GM. components: @@ -32,7 +33,7 @@ - type: entity parent: BaseDice - id: d12 + id: d12Dice name: d12 description: A die with twelve sides. There's an air of neglect about it. components: @@ -43,7 +44,7 @@ - type: entity parent: BaseDice - id: d10 + id: d10Dice name: d10 description: A die with ten sides. Useful for percentages. components: @@ -54,7 +55,7 @@ - type: entity parent: BaseDice - id: d8 + id: d8Dice name: d8 description: A die with eight sides. It feels... lucky. components: @@ -65,7 +66,7 @@ - type: entity parent: BaseDice - id: d6 + id: d6Dice name: d6 description: A die with six sides. Basic and serviceable. components: @@ -76,7 +77,7 @@ - type: entity parent: BaseDice - id: d4 + id: d4Dice name: d4 description: A die with four sides. The nerd's caltrop. components: