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

65 lines
1.8 KiB
C#
Raw Normal View History

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
2023-05-06 23:14:54 -04:00
var groups = new List<Dictionary<string, (string, string?)>>();
if (component.GetAllGroups)
{
groups = component.Available;
}
else
{
groups.Add(_random.Pick(component.Available));
}
component.Selected.EnsureCapacity(groups.Count);
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;
2023-05-06 23:14:54 -04:00
if (!string.IsNullOrEmpty(layer.Value.Item2))
color = _random.Pick(_prototype.Index<ColorPalettePrototype>(layer.Value.Item2).Colors.Values);
2023-05-06 23:14:54 -04:00
component.Selected.Add(layer.Key, (layer.Value.Item1, 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
}
}