Convert RandomPlant to entity system (#5952)
Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
@@ -1,80 +1,22 @@
|
|||||||
using System.Collections.Generic;
|
using Content.Server.Plants.Systems;
|
||||||
using JetBrains.Annotations;
|
using Robust.Shared.Analyzers;
|
||||||
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;
|
||||||
|
using Robust.Shared.ViewVariables;
|
||||||
|
|
||||||
namespace Content.Server.Plants.Components
|
namespace Content.Server.Plants.Components
|
||||||
{
|
{
|
||||||
[RegisterComponent]
|
[RegisterComponent]
|
||||||
public class RandomPottedPlantComponent : Component, IMapInit
|
[Friend(typeof(RandomPottedPlantSystem))]
|
||||||
|
[ComponentProtoName("RandomPottedPlant")]
|
||||||
|
public class RandomPottedPlantComponent : Component
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
[ViewVariables(VVAccess.ReadOnly)]
|
||||||
|
|
||||||
public override string Name => "RandomPottedPlant";
|
|
||||||
|
|
||||||
private static readonly string[] RegularPlantStates;
|
|
||||||
private static readonly string[] PlasticPlantStates;
|
|
||||||
|
|
||||||
[DataField("selected")]
|
[DataField("selected")]
|
||||||
private string? _selectedState;
|
public string? State;
|
||||||
|
|
||||||
|
[ViewVariables(VVAccess.ReadOnly)]
|
||||||
[DataField("plastic")]
|
[DataField("plastic")]
|
||||||
private bool _plastic;
|
public bool Plastic;
|
||||||
|
|
||||||
// for shared string dict, since we don't define these anywhere in content
|
|
||||||
[UsedImplicitly]
|
|
||||||
public static readonly string[] plantIdStrings =
|
|
||||||
{
|
|
||||||
"plant-01", "plant-02", "plant-03", "plant-04", "plant-05",
|
|
||||||
"plant-06", "plant-07", "plant-08", "plant-09", "plant-10",
|
|
||||||
"plant-11", "plant-12", "plant-13", "plant-14", "plant-15",
|
|
||||||
"plant-16", "plant-17", "plant-18", "plant-19", "plant-20",
|
|
||||||
"plant-21", "plant-22", "plant-23", "plant-24", "plant-25",
|
|
||||||
"plant-26", "plant-27", "plant-28", "plant-29", "plant-30",
|
|
||||||
};
|
|
||||||
|
|
||||||
static RandomPottedPlantComponent()
|
|
||||||
{
|
|
||||||
// ReSharper disable once StringLiteralTypo
|
|
||||||
var states = new List<string> {"applebush"};
|
|
||||||
|
|
||||||
for (var i = 1; i < 25; i++)
|
|
||||||
{
|
|
||||||
states.Add($"plant-{i:D2}");
|
|
||||||
}
|
|
||||||
|
|
||||||
RegularPlantStates = states.ToArray();
|
|
||||||
|
|
||||||
states.Clear();
|
|
||||||
|
|
||||||
for (var i = 26; i < 30; i++)
|
|
||||||
{
|
|
||||||
states.Add($"plant-{i:D2}");
|
|
||||||
}
|
|
||||||
|
|
||||||
PlasticPlantStates = states.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void Startup()
|
|
||||||
{
|
|
||||||
base.Startup();
|
|
||||||
|
|
||||||
if (_selectedState != null)
|
|
||||||
{
|
|
||||||
_entMan.GetComponent<SpriteComponent>(Owner).LayerSetState(0, _selectedState);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void MapInit()
|
|
||||||
{
|
|
||||||
var random = IoCManager.Resolve<IRobustRandom>();
|
|
||||||
|
|
||||||
var list = _plastic ? PlasticPlantStates : RegularPlantStates;
|
|
||||||
_selectedState = random.Pick(list);
|
|
||||||
|
|
||||||
_entMan.GetComponent<SpriteComponent>(Owner).LayerSetState(0, _selectedState);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
Content.Server/Plants/Systems/RandomPottedPlantSystem.cs
Normal file
37
Content.Server/Plants/Systems/RandomPottedPlantSystem.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using Content.Server.Plants.Components;
|
||||||
|
using Robust.Server.GameObjects;
|
||||||
|
using Robust.Shared.GameObjects;
|
||||||
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Random;
|
||||||
|
|
||||||
|
namespace Content.Server.Plants.Systems
|
||||||
|
{
|
||||||
|
public class RandomPottedPlantSystem : EntitySystem
|
||||||
|
{
|
||||||
|
[Dependency] private readonly IRobustRandom _random = default!;
|
||||||
|
|
||||||
|
private static readonly string[] RegularPlantStates =
|
||||||
|
{
|
||||||
|
"plant-01", "plant-02", "plant-03", "plant-04", "plant-05",
|
||||||
|
"plant-06", "plant-07", "plant-08", "plant-09", "plant-10",
|
||||||
|
"plant-11", "plant-12", "plant-13", "plant-14", "plant-15",
|
||||||
|
"plant-16", "plant-17", "plant-18", "plant-19", "plant-20",
|
||||||
|
"plant-21", "plant-22", "plant-23", "plant-24", "applebush"
|
||||||
|
};
|
||||||
|
private static readonly string[] PlasticPlantStates =
|
||||||
|
{
|
||||||
|
"plant-26", "plant-27", "plant-28", "plant-29"
|
||||||
|
};
|
||||||
|
|
||||||
|
public override void Initialize()
|
||||||
|
{
|
||||||
|
SubscribeLocalEvent<RandomPottedPlantComponent, MapInitEvent>(OnMapInit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnMapInit(EntityUid uid, RandomPottedPlantComponent component, MapInitEvent args)
|
||||||
|
{
|
||||||
|
component.State ??= _random.Pick(component.Plastic ? PlasticPlantStates : RegularPlantStates);
|
||||||
|
Comp<SpriteComponent>(uid).LayerSetState(0, component.State);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user