Makes DiceComponent ECS

This commit is contained in:
Vera Aguilera Puerto
2021-08-20 09:51:48 +02:00
parent f58e3d6fd5
commit 3610abecbd
5 changed files with 86 additions and 84 deletions

View File

@@ -1,35 +1,19 @@
using Content.Shared.Audio;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Sound; using Content.Shared.Sound;
using Content.Shared.Throwing; using Robust.Shared.Analyzers;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects; 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.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
namespace Content.Server.Dice namespace Content.Server.Dice
{ {
[RegisterComponent] [RegisterComponent, Friend(typeof(DiceSystem))]
public class DiceComponent : Component, IActivate, IUse, ILand, IExamine public class DiceComponent : Component
{ {
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
public override string Name => "Dice"; public override string Name => "Dice";
private int _sides = 20;
[ViewVariables] [ViewVariables]
[DataField("sound")] [DataField("sound")]
private readonly SoundSpecifier _sound = new SoundCollectionSpecifier("Dice"); public SoundSpecifier Sound { get; } = new SoundCollectionSpecifier("Dice");
[ViewVariables] [ViewVariables]
[DataField("step")] [DataField("step")]
@@ -37,60 +21,9 @@ namespace Content.Server.Dice
[ViewVariables] [ViewVariables]
[DataField("sides")] [DataField("sides")]
public int Sides public int Sides { get; } = 20;
{
get => _sides;
set
{
_sides = value;
CurrentSide = value;
}
}
[ViewVariables] [ViewVariables]
public int CurrentSide { get; private set; } = 20; public int CurrentSide { get; 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)));
}
} }
} }

View 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)));
}
}
}

View File

@@ -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-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]. dice-component-on-examine-message-part-2 = It has landed on a [color=white]{$currentSide}[/color].

View File

@@ -24441,7 +24441,7 @@ entities:
- canCollide: False - canCollide: False
type: Physics type: Physics
- uid: 2378 - uid: 2378
type: d20 type: d20Dice
components: components:
- pos: -23.834803,-5.3811545 - pos: -23.834803,-5.3811545
parent: 853 parent: 853
@@ -24449,7 +24449,7 @@ entities:
- canCollide: False - canCollide: False
type: Physics type: Physics
- uid: 2379 - uid: 2379
type: d8 type: d8Dice
components: components:
- pos: -24.287928,-6.3967795 - pos: -24.287928,-6.3967795
parent: 853 parent: 853

View File

@@ -6,10 +6,11 @@
- type: Dice - type: Dice
- type: Sprite - type: Sprite
sprite: Objects/Fun/dice.rsi sprite: Objects/Fun/dice.rsi
noRot: true # If their sprites rotate, the number becomes even more illegible than usual.
- type: entity - type: entity
parent: BaseDice parent: BaseDice
id: d100 id: d100Dice
name: d100 name: d100
description: A die with one hundred sides! Probably not fairly weighted... description: A die with one hundred sides! Probably not fairly weighted...
components: components:
@@ -21,7 +22,7 @@
- type: entity - type: entity
parent: BaseDice parent: BaseDice
id: d20 id: d20Dice
name: d20 name: d20
description: A die with twenty sides. The preferred die to throw at the GM. description: A die with twenty sides. The preferred die to throw at the GM.
components: components:
@@ -32,7 +33,7 @@
- type: entity - type: entity
parent: BaseDice parent: BaseDice
id: d12 id: d12Dice
name: d12 name: d12
description: A die with twelve sides. There's an air of neglect about it. description: A die with twelve sides. There's an air of neglect about it.
components: components:
@@ -43,7 +44,7 @@
- type: entity - type: entity
parent: BaseDice parent: BaseDice
id: d10 id: d10Dice
name: d10 name: d10
description: A die with ten sides. Useful for percentages. description: A die with ten sides. Useful for percentages.
components: components:
@@ -54,7 +55,7 @@
- type: entity - type: entity
parent: BaseDice parent: BaseDice
id: d8 id: d8Dice
name: d8 name: d8
description: A die with eight sides. It feels... lucky. description: A die with eight sides. It feels... lucky.
components: components:
@@ -65,7 +66,7 @@
- type: entity - type: entity
parent: BaseDice parent: BaseDice
id: d6 id: d6Dice
name: d6 name: d6
description: A die with six sides. Basic and serviceable. description: A die with six sides. Basic and serviceable.
components: components:
@@ -76,7 +77,7 @@
- type: entity - type: entity
parent: BaseDice parent: BaseDice
id: d4 id: d4Dice
name: d4 name: d4
description: A die with four sides. The nerd's caltrop. description: A die with four sides. The nerd's caltrop.
components: components: