Files
OldThink/Content.Server/Sprite/RandomSpriteSystem.cs

54 lines
1.6 KiB
C#
Raw Normal View History

using System.Linq;
using Content.Shared.Decals;
using Content.Shared.Sprite;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
2022-01-31 01:31:09 +11:00
using Robust.Shared.Random;
namespace Content.Server.Sprite;
public sealed class RandomSpriteSystem: SharedRandomSpriteSystem
2022-01-31 01:31:09 +11:00
{
[Dependency] private readonly IPrototypeManager _prototype = default!;
2022-01-31 01:31:09 +11:00
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RandomSpriteComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<RandomSpriteComponent, MapInitEvent>(OnMapInit);
2022-01-31 01:31:09 +11:00
}
private void OnMapInit(EntityUid uid, RandomSpriteComponent component, MapInitEvent args)
2022-01-31 01:31:09 +11:00
{
if (component.Selected.Count > 0)
return;
2022-01-31 01:31:09 +11:00
if (component.Available.Count == 0)
return;
2022-01-31 01:31:09 +11:00
var group = _random.Pick(component.Available);
component.Selected.EnsureCapacity(group.Count);
foreach (var layer in group)
{
Color? color = null;
if (!string.IsNullOrEmpty(layer.Value.Color))
color = _random.Pick(_prototype.Index<ColorPalettePrototype>(layer.Value.Color).Colors.Values);
component.Selected.Add(layer.Key, (layer.Value.State, color));
}
Dirty(component);
2022-01-31 01:31:09 +11:00
}
private void OnGetState(EntityUid uid, RandomSpriteComponent component, ref ComponentGetState args)
2022-01-31 01:31:09 +11:00
{
args.State = new RandomSpriteColorComponentState()
{
Selected = component.Selected,
};
2022-01-31 01:31:09 +11:00
}
}