Modify container/spawn helper methods (#5030)

* Modify container/spawn helper methods

* A
This commit is contained in:
Leon Friedrich
2024-04-14 15:47:21 +12:00
committed by GitHub
parent cd67c67a5c
commit caa8ff0f2d
4 changed files with 59 additions and 30 deletions

View File

@@ -135,6 +135,25 @@ public abstract partial class SharedContainerSystem
return true;
}
/// <summary>
/// Attempts to insert an entity into a container. If it fails, it will instead drop the entity next to the
/// container entity.
/// </summary>
/// <returns>Whether or not the entity was successfully inserted</returns>
public bool InsertOrDrop(Entity<TransformComponent?, MetaDataComponent?, PhysicsComponent?> toInsert,
BaseContainer container,
TransformComponent? containerXform = null)
{
if (!Resolve(toInsert.Owner, ref toInsert.Comp1) || !Resolve(container.Owner, ref containerXform))
return false;
if (Insert(toInsert, container, containerXform))
return true;
_transform.DropNextTo(toInsert, (container.Owner, containerXform));
return false;
}
/// <summary>
/// Checks if the entity can be inserted into the given container.
/// </summary>

View File

@@ -111,32 +111,18 @@ public partial class EntityManager
if (!xform.ParentUid.IsValid())
return false;
if (!MetaQuery.TryGetComponent(target, out var meta))
return false;
if ((meta.Flags & MetaDataFlags.InContainer) == 0)
if (!_containers.TryGetContainingContainer(target, out var container))
{
uid = SpawnAttachedTo(protoName, xform.Coordinates, overrides);
uid = SpawnNextToOrDrop(protoName, target, xform, overrides);
return true;
}
if (!TryGetComponent(xform.ParentUid, out ContainerManagerComponent? containerComp))
return false;
foreach (var container in containerComp.Containers.Values)
{
if (!container.Contains(target))
continue;
uid = Spawn(protoName, overrides);
if (_containers.Insert(uid.Value, container))
return true;
DeleteEntity(uid.Value);
uid = null;
return false;
}
uid = Spawn(protoName, overrides);
if (_containers.Insert(uid.Value, container))
return true;
DeleteEntity(uid.Value);
uid = null;
return false;
}
@@ -183,14 +169,28 @@ public partial class EntityManager
TransformComponent? xform = null,
ContainerManagerComponent? containerComp = null,
ComponentRegistry? overrides = null)
{
return SpawnInContainerOrDrop(protoName, containerUid, containerId, out _, xform, containerComp, overrides);
}
public EntityUid SpawnInContainerOrDrop(
string? protoName,
EntityUid containerUid,
string containerId,
out bool inserted,
TransformComponent? xform = null,
ContainerManagerComponent? containerComp = null,
ComponentRegistry? overrides = null)
{
var uid = Spawn(protoName, overrides);
inserted = true;
if ((containerComp == null && !TryGetComponent(containerUid, out containerComp))
|| !containerComp.Containers.TryGetValue(containerId, out var container)
|| !_containers.Insert(uid, container))
{
inserted = false;
xform ??= TransformQuery.GetComponent(containerUid);
if (xform.ParentUid.IsValid())
_xforms.DropNextTo(uid, (containerUid, xform));

View File

@@ -46,7 +46,7 @@ public partial interface IEntityManager
EntityUid SpawnAtPosition(string? protoName, EntityCoordinates coordinates, ComponentRegistry? overrides = null);
/// <summary>
/// Attempts to spawn an entity inside of a container.
/// Attempt to spawn an entity and insert it into a container. If the insertion fails, the entity gets deleted.
/// </summary>
bool TrySpawnInContainer(
string? protoName,
@@ -58,9 +58,9 @@ public partial interface IEntityManager
/// <summary>
/// Attempts to spawn an entity inside of a container. If it fails to insert into the container, it will
/// instead attempt to spawn the entity next to the target.
/// instead drop the entity next to the target (see <see cref="SpawnNextToOrDrop"/>).
/// </summary>
public EntityUid SpawnInContainerOrDrop(
EntityUid SpawnInContainerOrDrop(
string? protoName,
EntityUid containerUid,
string containerId,
@@ -68,9 +68,20 @@ public partial interface IEntityManager
ContainerManagerComponent? containerComp = null,
ComponentRegistry? overrides = null);
/// <inheritdoc cref="SpawnInContainerOrDrop(string?,Robust.Shared.GameObjects.EntityUid,string,Robust.Shared.GameObjects.TransformComponent?,Robust.Shared.Containers.ContainerManagerComponent?,Robust.Shared.Prototypes.ComponentRegistry?)"/>
EntityUid SpawnInContainerOrDrop(
string? protoName,
EntityUid containerUid,
string containerId,
out bool inserted,
TransformComponent? xform = null,
ContainerManagerComponent? containerComp = null,
ComponentRegistry? overrides = null);
/// <summary>
/// Attempts to spawn an entity adjacent to some other entity. If the other entity is in a container, this will
/// attempt to insert the new entity into the same container.
/// Attempts to spawn an entity adjacent to some other target entity. If the target entity is in
/// a container, this will attempt to insert the spawned entity into the same container. If the insertion fails,
/// the entity is deleted. If the entity is not in a container, this behaves like <see cref="SpawnNextToOrDrop"/>.
/// </summary>
bool TrySpawnNextTo(
string? protoName,

View File

@@ -31,15 +31,14 @@ public sealed class TrySpawnNextToTest : EntitySpawnHelpersTest
Assert.That(EntMan.EntityExists(uid), Is.False);
});
// Spawning next to an entity that is not in a container will simply spawn it in the same position
// Spawning next to an entity that is not in a container will drop it
await Server.WaitPost(() =>
{
Assert.That(EntMan.TrySpawnNextTo(null, GrandChildB, out var uid));
Assert.That(EntMan.EntityExists(uid));
Assert.That(Xforms.GetParentUid(uid!.Value), Is.EqualTo(ChildB));
Assert.That(Container.IsEntityInContainer(uid.Value), Is.False);
Assert.That(Xforms.GetParentUid(uid!.Value), Is.EqualTo(Parent));
Assert.That(Container.IsEntityInContainer(uid.Value));
Assert.That(Container.IsEntityOrParentInContainer(uid.Value));
Assert.That(EntMan.GetComponent<TransformComponent>(uid.Value).Coordinates, Is.EqualTo(GrandChildBPos));
});
// Spawning "next to" a nullspace entity will fail.