Add GetBodyPartAdjacentParts and GetBodyPartOrganComponents (#12628)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user