2021-07-17 02:37:09 +02:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2021-02-13 17:52:06 +01:00
|
|
|
|
using Content.Shared.Physics;
|
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
|
using Robust.Shared.Maths;
|
2021-07-21 21:15:12 +10:00
|
|
|
|
using Robust.Shared.Physics;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
using Robust.Shared.Physics.Broadphase;
|
2021-02-13 17:52:06 +01:00
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
|
namespace Content.Shared.Spawning
|
2021-02-13 17:52:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
public static class EntitySystemExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static IEntity? SpawnIfUnobstructed(
|
|
|
|
|
|
this IEntityManager entityManager,
|
|
|
|
|
|
string? prototypeName,
|
|
|
|
|
|
EntityCoordinates coordinates,
|
|
|
|
|
|
CollisionGroup collisionLayer,
|
|
|
|
|
|
in Box2? box = null,
|
2021-10-10 14:18:19 +11:00
|
|
|
|
SharedPhysicsSystem? physicsManager = null)
|
2021-02-13 17:52:06 +01:00
|
|
|
|
{
|
2021-10-10 14:18:19 +11:00
|
|
|
|
physicsManager ??= EntitySystem.Get<SharedPhysicsSystem>();
|
2021-02-13 17:52:06 +01:00
|
|
|
|
var mapCoordinates = coordinates.ToMap(entityManager);
|
|
|
|
|
|
|
|
|
|
|
|
return entityManager.SpawnIfUnobstructed(prototypeName, mapCoordinates, collisionLayer, box, physicsManager);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static IEntity? SpawnIfUnobstructed(
|
|
|
|
|
|
this IEntityManager entityManager,
|
|
|
|
|
|
string? prototypeName,
|
|
|
|
|
|
MapCoordinates coordinates,
|
|
|
|
|
|
CollisionGroup collisionLayer,
|
|
|
|
|
|
in Box2? box = null,
|
2021-10-10 14:18:19 +11:00
|
|
|
|
SharedPhysicsSystem? collision = null)
|
2021-02-13 17:52:06 +01:00
|
|
|
|
{
|
2021-08-03 18:49:25 +10:00
|
|
|
|
var boxOrDefault = box.GetValueOrDefault(Box2.UnitCentered).Translated(coordinates.Position);
|
2021-10-10 14:18:19 +11:00
|
|
|
|
collision ??= EntitySystem.Get<SharedPhysicsSystem>();
|
2021-02-13 17:52:06 +01:00
|
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
|
foreach (var body in collision.GetCollidingEntities(coordinates.MapId, in boxOrDefault))
|
2021-02-13 17:52:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (!body.Hard)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
|
// TODO: wtf fix this
|
2021-02-13 17:52:06 +01:00
|
|
|
|
if (collisionLayer == 0 || (body.CollisionMask & (int) collisionLayer) == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return entityManager.SpawnEntity(prototypeName, coordinates);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool TrySpawnIfUnobstructed(
|
|
|
|
|
|
this IEntityManager entityManager,
|
|
|
|
|
|
string? prototypeName,
|
|
|
|
|
|
EntityCoordinates coordinates,
|
|
|
|
|
|
CollisionGroup collisionLayer,
|
|
|
|
|
|
[NotNullWhen(true)] out IEntity? entity,
|
|
|
|
|
|
Box2? box = null,
|
2021-10-10 14:18:19 +11:00
|
|
|
|
SharedPhysicsSystem? physicsManager = null)
|
2021-02-13 17:52:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
entity = entityManager.SpawnIfUnobstructed(prototypeName, coordinates, collisionLayer, box, physicsManager);
|
|
|
|
|
|
|
|
|
|
|
|
return entity != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool TrySpawnIfUnobstructed(
|
|
|
|
|
|
this IEntityManager entityManager,
|
|
|
|
|
|
string? prototypeName,
|
|
|
|
|
|
MapCoordinates coordinates,
|
|
|
|
|
|
CollisionGroup collisionLayer,
|
|
|
|
|
|
[NotNullWhen(true)] out IEntity? entity,
|
|
|
|
|
|
in Box2? box = null,
|
2021-10-10 14:18:19 +11:00
|
|
|
|
SharedPhysicsSystem? physicsManager = null)
|
2021-02-13 17:52:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
entity = entityManager.SpawnIfUnobstructed(prototypeName, coordinates, collisionLayer, box, physicsManager);
|
|
|
|
|
|
|
|
|
|
|
|
return entity != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|