2020-10-10 15:25:13 +02:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
|
using System.Linq;
|
2020-11-02 11:37:37 +01:00
|
|
|
|
using Content.Shared.GameObjects.Components.Body;
|
2020-10-10 15:25:13 +02:00
|
|
|
|
using Content.Shared.GameObjects.Components.Body.Behavior;
|
2020-10-17 12:26:39 +02:00
|
|
|
|
using Content.Shared.GameObjects.Components.Body.Part;
|
2020-10-10 15:25:13 +02:00
|
|
|
|
|
2020-11-02 11:37:37 +01:00
|
|
|
|
namespace Content.Server.GameObjects.Components.Body.Behavior
|
2020-10-10 15:25:13 +02:00
|
|
|
|
{
|
|
|
|
|
|
public static class MechanismExtensions
|
|
|
|
|
|
{
|
2020-11-02 11:37:37 +01:00
|
|
|
|
public static bool HasMechanismBehavior<T>(this IBody body) where T : IMechanismBehavior
|
2020-10-10 15:25:13 +02:00
|
|
|
|
{
|
2020-10-17 12:26:39 +02:00
|
|
|
|
return body.Parts.Values.Any(p => p.HasMechanismBehavior<T>());
|
2020-10-10 15:25:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-02 11:37:37 +01:00
|
|
|
|
public static bool HasMechanismBehavior<T>(this IBodyPart part) where T : IMechanismBehavior
|
2020-10-16 14:42:33 +02:00
|
|
|
|
{
|
2020-11-02 11:37:37 +01:00
|
|
|
|
return part.Mechanisms.Any(m => m.HasBehavior<T>());
|
2020-10-17 12:26:39 +02:00
|
|
|
|
}
|
2020-10-16 14:42:33 +02:00
|
|
|
|
|
2020-10-17 12:26:39 +02:00
|
|
|
|
public static IEnumerable<IMechanismBehavior> GetMechanismBehaviors(this IBody body)
|
|
|
|
|
|
{
|
2020-10-16 14:42:33 +02:00
|
|
|
|
foreach (var part in body.Parts.Values)
|
|
|
|
|
|
foreach (var mechanism in part.Mechanisms)
|
2020-11-02 11:37:37 +01:00
|
|
|
|
foreach (var behavior in mechanism.Behaviors.Values)
|
2020-10-16 14:42:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
yield return behavior;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-17 12:26:39 +02:00
|
|
|
|
public static bool TryGetMechanismBehaviors(this IBody body,
|
2020-10-16 14:42:33 +02:00
|
|
|
|
[NotNullWhen(true)] out List<IMechanismBehavior>? behaviors)
|
|
|
|
|
|
{
|
2020-10-17 12:26:39 +02:00
|
|
|
|
behaviors = body.GetMechanismBehaviors().ToList();
|
2020-10-16 14:42:33 +02:00
|
|
|
|
|
|
|
|
|
|
if (behaviors.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
behaviors = null;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-17 12:26:39 +02:00
|
|
|
|
public static IEnumerable<T> GetMechanismBehaviors<T>(this IBody body) where T : class, IMechanismBehavior
|
2020-10-10 15:25:13 +02:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var part in body.Parts.Values)
|
|
|
|
|
|
foreach (var mechanism in part.Mechanisms)
|
2020-11-02 11:37:37 +01:00
|
|
|
|
foreach (var behavior in mechanism.Behaviors.Values)
|
2020-10-10 15:25:13 +02:00
|
|
|
|
{
|
2020-11-02 11:37:37 +01:00
|
|
|
|
if (behavior is T tBehavior)
|
2020-10-10 15:25:13 +02:00
|
|
|
|
{
|
2020-11-02 11:37:37 +01:00
|
|
|
|
yield return tBehavior;
|
2020-10-10 15:25:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-17 12:26:39 +02:00
|
|
|
|
public static bool TryGetMechanismBehaviors<T>(this IBody entity, [NotNullWhen(true)] out List<T>? behaviors)
|
2020-10-10 15:25:13 +02:00
|
|
|
|
where T : class, IMechanismBehavior
|
|
|
|
|
|
{
|
|
|
|
|
|
behaviors = entity.GetMechanismBehaviors<T>().ToList();
|
|
|
|
|
|
|
|
|
|
|
|
if (behaviors.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
behaviors = null;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|