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:
Nemanja
2024-08-09 22:12:40 -04:00
committed by GitHub
parent b692ab1850
commit 437c861622
19 changed files with 1198 additions and 566 deletions

View File

@@ -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;
}
}
}
}
}