ECS random sprotes (#6355)

This commit is contained in:
metalgearsloth
2022-01-31 01:31:09 +11:00
committed by GitHub
parent 9a5a8a55e1
commit eece26bb7b
3 changed files with 58 additions and 54 deletions

View File

@@ -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<string, Color> _colors = new();
void IMapInit.MapInit()
{
var random = IoCManager.Resolve<IRobustRandom>();
_selectedColor = random.Pick(_colors.Keys);
UpdateColor();
}
protected override void Startup()
{
base.Startup();
UpdateColor();
}
private void UpdateColor()
{
if (IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out SpriteComponent? spriteComponent) && _selectedColor != null)
{
spriteComponent.LayerSetState(0, _baseState);
spriteComponent.LayerSetColor(0, _colors[_selectedColor]);
}
}
[DataField("colors")] public readonly Dictionary<string, Color> Colors = new();
}
}