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

77 lines
2.2 KiB
C#
Raw Permalink Normal View History

using Content.Shared.Decals;
using Content.Shared.Random.Helpers;
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 groups = new List<Dictionary<string, Dictionary<string, string?>>>();
2023-05-06 23:14:54 -04:00
if (component.GetAllGroups)
{
groups = component.Available;
}
else
{
groups.Add(_random.Pick(component.Available));
}
component.Selected.EnsureCapacity(groups.Count);
Color? previousColor = null;
2023-05-06 23:14:54 -04:00
foreach (var group in groups)
{
2023-05-06 23:14:54 -04:00
foreach (var layer in group)
{
Color? color = null;
var selectedState = _random.Pick(layer.Value);
if (!string.IsNullOrEmpty(selectedState.Value))
{
if (selectedState.Value == $"Inherit")
color = previousColor;
else
{
color = _random.Pick(_prototype.Index<ColorPalettePrototype>(selectedState.Value).Colors.Values);
previousColor = color;
}
}
component.Selected.Add(layer.Key, (selectedState.Key, color));
2023-05-06 23:14:54 -04:00
}
}
Dirty(uid, 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
}
}