Slay bobby pt. 1 (#5632)
This commit is contained in:
@@ -2,19 +2,15 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Shared.Body.Behavior;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Body.Part.Property;
|
||||
using Content.Shared.Body.Prototypes;
|
||||
using Content.Shared.CharacterAppearance.Systems;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Damage.Prototypes;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Standing;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
@@ -34,11 +30,11 @@ namespace Content.Shared.Body.Components
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("template", required: true)]
|
||||
private string? TemplateId { get; } = default;
|
||||
private string? TemplateId { get; }
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("preset", required: true)]
|
||||
private string? PresetId { get; } = default;
|
||||
private string? PresetId { get; }
|
||||
|
||||
[ViewVariables]
|
||||
public BodyTemplatePrototype? Template => TemplateId == null
|
||||
@@ -449,93 +445,11 @@ namespace Content.Shared.Body.Components
|
||||
}
|
||||
}
|
||||
|
||||
/// <returns>A list of parts with that property.</returns>
|
||||
public IEnumerable<(SharedBodyPartComponent part, IBodyPartProperty property)> GetPartsWithProperty(Type type)
|
||||
{
|
||||
foreach (var slot in SlotIds.Values)
|
||||
{
|
||||
if (slot.Part != null && slot.Part.TryGetProperty(type, out var property))
|
||||
{
|
||||
yield return (slot.Part, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<(SharedBodyPartComponent part, T property)> GetPartsWithProperty<T>() where T : class, IBodyPartProperty
|
||||
{
|
||||
foreach (var part in SlotParts.Keys)
|
||||
{
|
||||
if (part.TryGetProperty<T>(out var property))
|
||||
{
|
||||
yield return (part, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void OnBodyChanged()
|
||||
{
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public float DistanceToNearestFoot(SharedBodyPartComponent source)
|
||||
{
|
||||
if (source.PartType == BodyPartType.Foot &&
|
||||
source.TryGetProperty<ExtensionComponent>(out var extension))
|
||||
{
|
||||
return extension.Distance;
|
||||
}
|
||||
|
||||
return LookForFootRecursion(source);
|
||||
}
|
||||
|
||||
private float LookForFootRecursion(SharedBodyPartComponent current, HashSet<BodyPartSlot>? searched = null)
|
||||
{
|
||||
searched ??= new HashSet<BodyPartSlot>();
|
||||
|
||||
if (!current.TryGetProperty<ExtensionComponent>(out var extProperty))
|
||||
{
|
||||
return float.MinValue;
|
||||
}
|
||||
|
||||
if (!TryGetSlot(current, out var slot))
|
||||
{
|
||||
return float.MinValue;
|
||||
}
|
||||
|
||||
foreach (var connection in slot.Connections)
|
||||
{
|
||||
if (connection.PartType == BodyPartType.Foot &&
|
||||
!searched.Contains(connection))
|
||||
{
|
||||
return extProperty.Distance;
|
||||
}
|
||||
}
|
||||
|
||||
var distances = new List<float>();
|
||||
foreach (var connection in slot.Connections)
|
||||
{
|
||||
if (connection.Part == null || !searched.Contains(connection))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var result = LookForFootRecursion(connection.Part, searched);
|
||||
|
||||
if (Math.Abs(result - float.MinValue) > 0.001f)
|
||||
{
|
||||
distances.Add(result);
|
||||
}
|
||||
}
|
||||
|
||||
if (distances.Count > 0)
|
||||
{
|
||||
return distances.Min<float>() + extProperty.Distance;
|
||||
}
|
||||
|
||||
return float.MinValue;
|
||||
}
|
||||
|
||||
// TODO BODY optimize this
|
||||
public BodyPartSlot SlotAt(int index)
|
||||
{
|
||||
@@ -601,62 +515,6 @@ namespace Content.Shared.Body.Components
|
||||
part.Gib();
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetMechanismBehaviors([NotNullWhen(true)] out List<SharedMechanismBehavior>? behaviors)
|
||||
{
|
||||
behaviors = GetMechanismBehaviors().ToList();
|
||||
|
||||
if (behaviors.Count == 0)
|
||||
{
|
||||
behaviors = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool HasMechanismBehavior<T>() where T : SharedMechanismBehavior
|
||||
{
|
||||
return Parts.Any(p => p.Key.HasMechanismBehavior<T>());
|
||||
}
|
||||
|
||||
// TODO cache these 2 methods jesus
|
||||
public IEnumerable<SharedMechanismBehavior> GetMechanismBehaviors()
|
||||
{
|
||||
foreach (var (part, _) in Parts)
|
||||
foreach (var mechanism in part.Mechanisms)
|
||||
foreach (var behavior in mechanism.Behaviors.Values)
|
||||
{
|
||||
yield return behavior;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetMechanismBehaviors<T>() where T : SharedMechanismBehavior
|
||||
{
|
||||
foreach (var (part, _) in Parts)
|
||||
foreach (var mechanism in part.Mechanisms)
|
||||
foreach (var behavior in mechanism.Behaviors.Values)
|
||||
{
|
||||
if (behavior is T tBehavior)
|
||||
{
|
||||
yield return tBehavior;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetMechanismBehaviors<T>([NotNullWhen(true)] out List<T>? behaviors)
|
||||
where T : SharedMechanismBehavior
|
||||
{
|
||||
behaviors = GetMechanismBehaviors<T>().ToList();
|
||||
|
||||
if (behaviors.Count == 0)
|
||||
{
|
||||
behaviors = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
|
||||
Reference in New Issue
Block a user