mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-18 05:59:03 +03:00
Add ContainerComp (#31311)
Applies EntProtoId changes upon insertion / removal from container. Can also be useful for borgs / mechs / vehicles in future but atm I just used it for AI. (cherry picked from commit 006acf3a376c5fdb163297b758ef86fd7d7638ef)
This commit is contained in:
44
Content.Shared/Containers/ContainerCompSystem.cs
Normal file
44
Content.Shared/Containers/ContainerCompSystem.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Containers;
|
||||
|
||||
/// <summary>
|
||||
/// Applies / removes an entity prototype from a child entity when it's inserted into a container.
|
||||
/// </summary>
|
||||
public sealed class ContainerCompSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _proto = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ContainerCompComponent, EntInsertedIntoContainerMessage>(OnConInsert);
|
||||
SubscribeLocalEvent<ContainerCompComponent, EntRemovedFromContainerMessage>(OnConRemove);
|
||||
}
|
||||
|
||||
private void OnConRemove(Entity<ContainerCompComponent> ent, ref EntRemovedFromContainerMessage args)
|
||||
{
|
||||
if (args.Container.ID != ent.Comp.Container)
|
||||
return;
|
||||
|
||||
if (_proto.TryIndex(ent.Comp.Container, out var entProto))
|
||||
{
|
||||
foreach (var entry in entProto.Components.Values)
|
||||
{
|
||||
RemComp(args.Entity, entry.Component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnConInsert(Entity<ContainerCompComponent> ent, ref EntInsertedIntoContainerMessage args)
|
||||
{
|
||||
if (args.Container.ID != ent.Comp.Container)
|
||||
return;
|
||||
|
||||
if (_proto.TryIndex(ent.Comp.Proto, out var entProto))
|
||||
{
|
||||
EntityManager.AddComponents(args.Entity, entProto.Components);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user