Map-Init guidebook entities. (#14823)

This commit is contained in:
Leon Friedrich
2023-03-24 22:00:29 +13:00
committed by GitHub
parent 51778afe6f
commit 473a02120d
4 changed files with 7 additions and 4 deletions

View File

@@ -0,0 +1,47 @@
using Robust.Shared.Containers;
using Robust.Shared.Map;
namespace Content.Shared.Containers;
public sealed class ContainerFillSystem : EntitySystem
{
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ContainerFillComponent, MapInitEvent>(OnMapInit);
}
private void OnMapInit(EntityUid uid, ContainerFillComponent component, MapInitEvent args)
{
if (!TryComp(uid, out ContainerManagerComponent? containerComp))
{
Logger.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} has no {nameof(ContainerManagerComponent)}.");
return;
}
var xform = Transform(uid);
var coords = new EntityCoordinates(uid, Vector2.Zero);
foreach (var (contaienrId, prototypes) in component.Containers)
{
if (!_containerSystem.TryGetContainer(uid, contaienrId, out var container, containerComp))
{
Logger.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} is missing a container ({contaienrId}).");
continue;
}
foreach (var proto in prototypes)
{
var ent = Spawn(proto, coords);
if (!container.Insert(ent, EntityManager, null, xform))
{
Logger.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} failed to insert an entity: {ToPrettyString(ent)}.");
Transform(ent).AttachToGridOrMap();
break;
}
}
}
}
}