From eece26bb7b42c13549fe2e2498b0f20aef7966ff Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 31 Jan 2022 01:31:09 +1100 Subject: [PATCH] ECS random sprotes (#6355) --- .../Components/RandomSpriteColorComponent.cs | 42 +++------------- .../Components/RandomSpriteStateComponent.cs | 22 ++------- Content.Server/Sprite/RandomSpriteSystem.cs | 48 +++++++++++++++++++ 3 files changed, 58 insertions(+), 54 deletions(-) create mode 100644 Content.Server/Sprite/RandomSpriteSystem.cs diff --git a/Content.Server/Sprite/Components/RandomSpriteColorComponent.cs b/Content.Server/Sprite/Components/RandomSpriteColorComponent.cs index 4dca98070b..b453d3fe6f 100644 --- a/Content.Server/Sprite/Components/RandomSpriteColorComponent.cs +++ b/Content.Server/Sprite/Components/RandomSpriteColorComponent.cs @@ -1,46 +1,18 @@ using System.Collections.Generic; -using Robust.Server.GameObjects; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; using Robust.Shared.Maths; -using Robust.Shared.Random; using Robust.Shared.Serialization.Manager.Attributes; namespace Content.Server.Sprite.Components { - [RegisterComponent] - public class RandomSpriteColorComponent : Component, IMapInit + [RegisterComponent, ComponentProtoName("RandomSpriteColor")] + public class RandomSpriteColorComponent : Component { - public override string Name => "RandomSpriteColor"; + // This should handle random states + colors for layers. + // Saame with RandomSpriteState + [DataField("selected")] public string? SelectedColor; + [DataField("state")] public string BaseState = "error"; - [DataField("selected")] - private string? _selectedColor; - [DataField("state")] - private string _baseState = "error"; - - [DataField("colors")] private readonly Dictionary _colors = new(); - - void IMapInit.MapInit() - { - var random = IoCManager.Resolve(); - _selectedColor = random.Pick(_colors.Keys); - UpdateColor(); - } - - protected override void Startup() - { - base.Startup(); - - UpdateColor(); - } - - private void UpdateColor() - { - if (IoCManager.Resolve().TryGetComponent(Owner, out SpriteComponent? spriteComponent) && _selectedColor != null) - { - spriteComponent.LayerSetState(0, _baseState); - spriteComponent.LayerSetColor(0, _colors[_selectedColor]); - } - } + [DataField("colors")] public readonly Dictionary Colors = new(); } } diff --git a/Content.Server/Sprite/Components/RandomSpriteStateComponent.cs b/Content.Server/Sprite/Components/RandomSpriteStateComponent.cs index eafeede655..d6bc037b3f 100644 --- a/Content.Server/Sprite/Components/RandomSpriteStateComponent.cs +++ b/Content.Server/Sprite/Components/RandomSpriteStateComponent.cs @@ -1,30 +1,14 @@ using System.Collections.Generic; -using Robust.Server.GameObjects; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Random; using Robust.Shared.Serialization.Manager.Attributes; namespace Content.Server.Sprite.Components { - [RegisterComponent] + [RegisterComponent, ComponentProtoName("RandomSpriteState")] public class RandomSpriteStateComponent : Component { - [Dependency] private readonly IRobustRandom _random = default!; - public override string Name => "RandomSpriteState"; + [DataField("spriteStates")] public List? SpriteStates; - [DataField("spriteStates")] - private List? _spriteStates; - - [DataField("spriteLayer")] - private int _spriteLayer; - - protected override void Initialize() - { - base.Initialize(); - if (_spriteStates == null) return; - if (!IoCManager.Resolve().TryGetComponent(Owner, out SpriteComponent? spriteComponent)) return; - spriteComponent.LayerSetState(_spriteLayer, _random.Pick(_spriteStates)); - } + [DataField("spriteLayer")] public int SpriteLayer; } } diff --git a/Content.Server/Sprite/RandomSpriteSystem.cs b/Content.Server/Sprite/RandomSpriteSystem.cs new file mode 100644 index 0000000000..68a343edc1 --- /dev/null +++ b/Content.Server/Sprite/RandomSpriteSystem.cs @@ -0,0 +1,48 @@ +using Content.Server.Sprite.Components; +using Content.Shared.Random.Helpers; +using Robust.Server.GameObjects; +using Robust.Shared.GameObjects; +using Robust.Shared.IoC; +using Robust.Shared.Random; + +namespace Content.Server.Sprite; + +public sealed class RandomSpriteSystem: EntitySystem +{ + [Dependency] private readonly IRobustRandom _random = default!; + + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnSpriteColorStartup); + SubscribeLocalEvent(OnSpriteColorMapInit); + + SubscribeLocalEvent(OnSpriteStateMapInit); + } + + private void OnSpriteColorStartup(EntityUid uid, RandomSpriteColorComponent component, ComponentStartup args) + { + UpdateColor(component); + } + + private void OnSpriteColorMapInit(EntityUid uid, RandomSpriteColorComponent component, MapInitEvent args) + { + component.SelectedColor = _random.Pick(component.Colors.Keys); + UpdateColor(component); + } + + private void OnSpriteStateMapInit(EntityUid uid, RandomSpriteStateComponent component, MapInitEvent args) + { + if (component.SpriteStates == null) return; + if (!TryComp(uid, out var spriteComponent)) return; + spriteComponent.LayerSetState(component.SpriteLayer, _random.Pick(component.SpriteStates)); + } + + private void UpdateColor(RandomSpriteColorComponent component) + { + if (!TryComp(component.Owner, out var spriteComponent) || component.SelectedColor == null) return; + + spriteComponent.LayerSetState(0, component.BaseState); + spriteComponent.LayerSetColor(0, component.Colors[component.SelectedColor]); + } +}