Random metadata component (#7894)
Co-authored-by: Moony <moonheart08@users.noreply.github.com>
This commit is contained in:
17
Content.Server/RandomMetadata/RandomMetadataComponent.cs
Normal file
17
Content.Server/RandomMetadata/RandomMetadataComponent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Content.Shared.Dataset;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.RandomMetadata;
|
||||
|
||||
/// <summary>
|
||||
/// Randomizes the description and/or the name for an entity by pulling from a dataset prototype.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class RandomMetadataComponent : Component
|
||||
{
|
||||
[DataField("descriptionSet", customTypeSerializer:typeof(PrototypeIdSerializer<DatasetPrototype>))]
|
||||
public string? DescriptionSet;
|
||||
|
||||
[DataField("nameSet", customTypeSerializer:typeof(PrototypeIdSerializer<DatasetPrototype>))]
|
||||
public string? NameSet;
|
||||
}
|
||||
36
Content.Server/RandomMetadata/RandomMetadataSystem.cs
Normal file
36
Content.Server/RandomMetadata/RandomMetadataSystem.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Content.Shared.Dataset;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.RandomMetadata;
|
||||
|
||||
public sealed class RandomMetadataSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototype = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<RandomMetadataComponent, MapInitEvent>(OnMapInit);
|
||||
}
|
||||
|
||||
// This is done on map init so that map-placed entities have it randomized each time the map loads, for fun.
|
||||
private void OnMapInit(EntityUid uid, RandomMetadataComponent component, MapInitEvent args)
|
||||
{
|
||||
var meta = MetaData(uid);
|
||||
|
||||
if (component.NameSet != null)
|
||||
{
|
||||
var nameProto = _prototype.Index<DatasetPrototype>(component.NameSet);
|
||||
meta.EntityName = _random.Pick(nameProto.Values);
|
||||
}
|
||||
|
||||
if (component.DescriptionSet != null)
|
||||
{
|
||||
var descProto = _prototype.Index<DatasetPrototype>(component.DescriptionSet);
|
||||
meta.EntityDescription = _random.Pick(descProto.Values);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user