Entity Tables (EntitySpawnEntry replacement) (#30579)
* Entity table code * entity table examples * fix dat shit * access * tests tests tests * sloth review
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Numerics;
|
||||
using Content.Shared.EntityTable;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
@@ -7,11 +8,14 @@ namespace Content.Shared.Containers;
|
||||
public sealed class ContainerFillSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
|
||||
[Dependency] private readonly EntityTableSystem _entityTable = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ContainerFillComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<EntityTableContainerFillComponent, MapInitEvent>(OnTableMapInit);
|
||||
}
|
||||
|
||||
private void OnMapInit(EntityUid uid, ContainerFillComponent component, MapInitEvent args)
|
||||
@@ -42,4 +46,37 @@ public sealed class ContainerFillSystem : EntitySystem
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTableMapInit(Entity<EntityTableContainerFillComponent> ent, ref MapInitEvent args)
|
||||
{
|
||||
if (!TryComp(ent, out ContainerManagerComponent? containerComp))
|
||||
return;
|
||||
|
||||
if (TerminatingOrDeleted(ent) || !Exists(ent))
|
||||
return;
|
||||
|
||||
var xform = Transform(ent);
|
||||
var coords = new EntityCoordinates(ent, Vector2.Zero);
|
||||
|
||||
foreach (var (containerId, table) in ent.Comp.Containers)
|
||||
{
|
||||
if (!_containerSystem.TryGetContainer(ent, containerId, out var container, containerComp))
|
||||
{
|
||||
Log.Error($"Entity {ToPrettyString(ent)} with a {nameof(EntityTableContainerFillComponent)} is missing a container ({containerId}).");
|
||||
continue;
|
||||
}
|
||||
|
||||
var spawns = _entityTable.GetSpawns(table);
|
||||
foreach (var proto in spawns)
|
||||
{
|
||||
var spawn = Spawn(proto, coords);
|
||||
if (!_containerSystem.Insert(spawn, container, containerXform: xform))
|
||||
{
|
||||
Log.Error($"Entity {ToPrettyString(ent)} with a {nameof(EntityTableContainerFillComponent)} failed to insert an entity: {ToPrettyString(spawn)}.");
|
||||
_transform.AttachToGridOrMap(spawn);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user