Add GetBodyPartAdjacentParts and GetBodyPartOrganComponents (#12628)

This commit is contained in:
DrSmugleaf
2022-11-16 11:24:03 +01:00
committed by GitHub
parent f92f288047
commit dffcd38345
2 changed files with 121 additions and 9 deletions

View File

@@ -174,7 +174,7 @@ public partial class SharedBodySystem
/// Returns a list of ValueTuples of <see cref="T"/> and OrganComponent on each organ
/// in the given body.
/// </summary>
/// <param name="uid">The entity to check for the component on.</param>
/// <param name="uid">The body entity id to check on.</param>
/// <param name="body">The body to check for organs on.</param>
/// <typeparam name="T">The component to check for.</typeparam>
public List<(T Comp, OrganComponent Organ)> GetBodyOrganComponents<T>(
@@ -185,7 +185,7 @@ public partial class SharedBodySystem
if (!Resolve(uid, ref body))
return new List<(T Comp, OrganComponent Organ)>();
var query = EntityManager.GetEntityQuery<T>();
var query = GetEntityQuery<T>();
var list = new List<(T Comp, OrganComponent Organ)>(3);
foreach (var organ in GetBodyOrgans(uid, body))
{
@@ -200,7 +200,7 @@ public partial class SharedBodySystem
/// Tries to get a list of ValueTuples of <see cref="T"/> and OrganComponent on each organs
/// in the given body.
/// </summary>
/// <param name="uid">The entity to check for the component on.</param>
/// <param name="uid">The body entity id to check on.</param>
/// <param name="comps">The list of components.</param>
/// <param name="body">The body to check for organs on.</param>
/// <typeparam name="T">The component to check for.</typeparam>
@@ -219,12 +219,10 @@ public partial class SharedBodySystem
comps = GetBodyOrganComponents<T>(uid, body);
if (comps.Count == 0)
{
if (comps.Count != 0)
return true;
comps = null;
return false;
}
return true;
}
}

View File

@@ -350,4 +350,118 @@ public partial class SharedBodySystem
return child.ParentSlot?.Child == parentId;
}
public IEnumerable<EntityUid> GetBodyPartAdjacentParts(EntityUid partId, BodyPartComponent? part = null)
{
if (!Resolve(partId, ref part, false))
yield break;
if (part.ParentSlot != null)
yield return part.ParentSlot.Parent;
foreach (var slot in part.Children.Values)
{
if (slot.Child != null)
yield return slot.Child.Value;
}
}
public IEnumerable<(EntityUid AdjacentId, T Component)> GetBodyPartAdjacentPartsComponents<T>(
EntityUid partId,
BodyPartComponent? part = null)
where T : Component
{
if (!Resolve(partId, ref part, false))
yield break;
var query = GetEntityQuery<T>();
foreach (var adjacentId in GetBodyPartAdjacentParts(partId, part))
{
if (query.TryGetComponent(adjacentId, out var component))
yield return (adjacentId, component);
}
}
public bool TryGetBodyPartAdjacentPartsComponents<T>(
EntityUid partId,
[NotNullWhen(true)] out List<(EntityUid AdjacentId, T Component)>? comps,
BodyPartComponent? part = null)
where T : Component
{
if (!Resolve(partId, ref part, false))
{
comps = null;
return false;
}
var query = GetEntityQuery<T>();
comps = new List<(EntityUid AdjacentId, T Component)>();
foreach (var adjacentId in GetBodyPartAdjacentParts(partId, part))
{
if (query.TryGetComponent(adjacentId, out var component))
comps.Add((adjacentId, component));
}
if (comps.Count != 0)
return true;
comps = null;
return false;
}
/// <summary>
/// Returns a list of ValueTuples of <see cref="T"/> and OrganComponent on each organ
/// in the given part.
/// </summary>
/// <param name="uid">The part entity id to check on.</param>
/// <param name="part">The part to check for organs on.</param>
/// <typeparam name="T">The component to check for.</typeparam>
public List<(T Comp, OrganComponent Organ)> GetBodyPartOrganComponents<T>(
EntityUid uid,
BodyPartComponent? part = null)
where T : Component
{
if (!Resolve(uid, ref part))
return new List<(T Comp, OrganComponent Organ)>();
var query = GetEntityQuery<T>();
var list = new List<(T Comp, OrganComponent Organ)>();
foreach (var organ in GetPartOrgans(uid, part))
{
if (query.TryGetComponent(organ.Id, out var comp))
list.Add((comp, organ.Component));
}
return list;
}
/// <summary>
/// Tries to get a list of ValueTuples of <see cref="T"/> and OrganComponent on each organs
/// in the given part.
/// </summary>
/// <param name="uid">The part entity id to check on.</param>
/// <param name="comps">The list of components.</param>
/// <param name="part">The part to check for organs on.</param>
/// <typeparam name="T">The component to check for.</typeparam>
/// <returns>Whether any were found.</returns>
public bool TryGetBodyPartOrganComponents<T>(
EntityUid uid,
[NotNullWhen(true)] out List<(T Comp, OrganComponent Organ)>? comps,
BodyPartComponent? part = null)
where T : Component
{
if (!Resolve(uid, ref part))
{
comps = null;
return false;
}
comps = GetBodyPartOrganComponents<T>(uid, part);
if (comps.Count != 0)
return true;
comps = null;
return false;
}
}