ECS random sprotes (#6355)
This commit is contained in:
@@ -1,46 +1,18 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Robust.Server.GameObjects;
|
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.IoC;
|
|
||||||
using Robust.Shared.Maths;
|
using Robust.Shared.Maths;
|
||||||
using Robust.Shared.Random;
|
|
||||||
using Robust.Shared.Serialization.Manager.Attributes;
|
using Robust.Shared.Serialization.Manager.Attributes;
|
||||||
|
|
||||||
namespace Content.Server.Sprite.Components
|
namespace Content.Server.Sprite.Components
|
||||||
{
|
{
|
||||||
[RegisterComponent]
|
[RegisterComponent, ComponentProtoName("RandomSpriteColor")]
|
||||||
public class RandomSpriteColorComponent : Component, IMapInit
|
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")]
|
[DataField("colors")] public readonly Dictionary<string, Color> Colors = new();
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,14 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Robust.Server.GameObjects;
|
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.IoC;
|
|
||||||
using Robust.Shared.Random;
|
|
||||||
using Robust.Shared.Serialization.Manager.Attributes;
|
using Robust.Shared.Serialization.Manager.Attributes;
|
||||||
|
|
||||||
namespace Content.Server.Sprite.Components
|
namespace Content.Server.Sprite.Components
|
||||||
{
|
{
|
||||||
[RegisterComponent]
|
[RegisterComponent, ComponentProtoName("RandomSpriteState")]
|
||||||
public class RandomSpriteStateComponent : Component
|
public class RandomSpriteStateComponent : Component
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IRobustRandom _random = default!;
|
[DataField("spriteStates")] public List<string>? SpriteStates;
|
||||||
public override string Name => "RandomSpriteState";
|
|
||||||
|
|
||||||
[DataField("spriteStates")]
|
[DataField("spriteLayer")] public int SpriteLayer;
|
||||||
private List<string>? _spriteStates;
|
|
||||||
|
|
||||||
[DataField("spriteLayer")]
|
|
||||||
private int _spriteLayer;
|
|
||||||
|
|
||||||
protected override void Initialize()
|
|
||||||
{
|
|
||||||
base.Initialize();
|
|
||||||
if (_spriteStates == null) return;
|
|
||||||
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out SpriteComponent? spriteComponent)) return;
|
|
||||||
spriteComponent.LayerSetState(_spriteLayer, _random.Pick(_spriteStates));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
48
Content.Server/Sprite/RandomSpriteSystem.cs
Normal file
48
Content.Server/Sprite/RandomSpriteSystem.cs
Normal file
@@ -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<RandomSpriteColorComponent, ComponentStartup>(OnSpriteColorStartup);
|
||||||
|
SubscribeLocalEvent<RandomSpriteColorComponent, MapInitEvent>(OnSpriteColorMapInit);
|
||||||
|
|
||||||
|
SubscribeLocalEvent<RandomSpriteStateComponent, MapInitEvent>(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<SpriteComponent>(uid, out var spriteComponent)) return;
|
||||||
|
spriteComponent.LayerSetState(component.SpriteLayer, _random.Pick(component.SpriteStates));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateColor(RandomSpriteColorComponent component)
|
||||||
|
{
|
||||||
|
if (!TryComp<SpriteComponent>(component.Owner, out var spriteComponent) || component.SelectedColor == null) return;
|
||||||
|
|
||||||
|
spriteComponent.LayerSetState(0, component.BaseState);
|
||||||
|
spriteComponent.LayerSetColor(0, component.Colors[component.SelectedColor]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user