Fix mechanism events not being called properly, add test (#2279)
* Add mechanism events when added/removed to/from body/parts * Change old usages * Add TODO * Remove BodyExtensions and IHasBody * Remove unnecessary extensions and fix wrong event call in mechanism behavior component * Complete test and fix event calls
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
#nullable enable
|
||||
using Content.Shared.GameObjects.Components.Body.Mechanism;
|
||||
using Content.Shared.GameObjects.Components.Body.Part;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Body.Behavior
|
||||
{
|
||||
public interface IMechanismBehavior : IHasBody
|
||||
public interface IMechanismBehavior : IComponent
|
||||
{
|
||||
IBody? Body { get; }
|
||||
|
||||
IBodyPart? Part { get; }
|
||||
|
||||
/// <summary>
|
||||
@@ -33,6 +36,14 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior
|
||||
/// </summary>
|
||||
void AddedToPart();
|
||||
|
||||
/// <summary>
|
||||
/// Called when the parent <see cref="IMechanism"/> is added to a
|
||||
/// <see cref="IBodyPart"/> that is attached to a <see cref="IBody"/>.
|
||||
/// For instance, adding a brain to a head that is attached to a body.
|
||||
/// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE!
|
||||
/// </summary>
|
||||
void AddedToPartInBody();
|
||||
|
||||
/// <summary>
|
||||
/// Called when the parent <see cref="IBodyPart"/> is removed from a
|
||||
/// <see cref="IBody"/>.
|
||||
@@ -50,14 +61,6 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior
|
||||
/// </summary>
|
||||
void RemovedFromPart(IBodyPart old);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the parent <see cref="IMechanism"/> is added to a
|
||||
/// <see cref="IBodyPart"/> that is attached to a <see cref="IBody"/>.
|
||||
/// For instance, adding a brain to a head that is attached to a body.
|
||||
/// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE!
|
||||
/// </summary>
|
||||
void AddedToPartInBody();
|
||||
|
||||
/// <summary>
|
||||
/// Called when the parent <see cref="IMechanism"/> is removed from a
|
||||
/// <see cref="IBodyPart"/> that is attached to a <see cref="IBody"/>.
|
||||
|
||||
@@ -13,6 +13,25 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior
|
||||
|
||||
public IMechanism? Mechanism => Owner.GetComponentOrNull<IMechanism>();
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
if (Part == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Body == null)
|
||||
{
|
||||
AddedToPart();
|
||||
}
|
||||
else
|
||||
{
|
||||
AddedToPartInBody();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void Update(float frameTime);
|
||||
|
||||
public void AddedToBody()
|
||||
@@ -37,7 +56,7 @@ namespace Content.Shared.GameObjects.Components.Body.Behavior
|
||||
|
||||
public void AddedToPartInBody()
|
||||
{
|
||||
OnAddedToPart();
|
||||
OnAddedToPartInBody();
|
||||
}
|
||||
|
||||
public void RemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart)
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#nullable enable
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Body
|
||||
{
|
||||
public static class BodyExtensions
|
||||
{
|
||||
public static T? GetBody<T>(this IEntity entity) where T : class, IBody
|
||||
{
|
||||
return entity.GetComponentOrNull<T>();
|
||||
}
|
||||
|
||||
public static bool TryGetBody<T>(this IEntity entity, [NotNullWhen(true)] out T? body) where T : class, IBody
|
||||
{
|
||||
return (body = entity.GetBody<T>()) != null;
|
||||
}
|
||||
|
||||
public static IBody? GetBody(this IEntity entity)
|
||||
{
|
||||
return entity.GetComponentOrNull<IBody>();
|
||||
}
|
||||
|
||||
public static bool TryGetBody(this IEntity entity, [NotNullWhen(true)] out IBody? body)
|
||||
{
|
||||
return (body = entity.GetBody()) != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
#nullable enable
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Body
|
||||
{
|
||||
public interface IHasBody : IComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// The body that this component is currently a part of, if any.
|
||||
/// </summary>
|
||||
IBody? Body { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
#nullable enable
|
||||
using Content.Shared.GameObjects.Components.Body.Part;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
{
|
||||
public interface IMechanism : IHasBody
|
||||
public interface IMechanism : IComponent
|
||||
{
|
||||
IBody? Body { get; }
|
||||
|
||||
IBodyPart? Part { get; set; }
|
||||
|
||||
/// <summary>
|
||||
@@ -70,6 +73,14 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
/// </summary>
|
||||
void AddedToPart();
|
||||
|
||||
/// <summary>
|
||||
/// Called when the parent <see cref="IMechanism"/> is added to a
|
||||
/// <see cref="IBodyPart"/> that is attached to a <see cref="IBody"/>.
|
||||
/// For instance, adding a brain to a head that is attached to a body.
|
||||
/// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE!
|
||||
/// </summary>
|
||||
void AddedToPartInBody();
|
||||
|
||||
/// <summary>
|
||||
/// Called when the parent <see cref="IBodyPart"/> is removed from a
|
||||
/// <see cref="IBody"/>.
|
||||
@@ -87,14 +98,6 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
/// </summary>
|
||||
void RemovedFromPart(IBodyPart old);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the parent <see cref="IMechanism"/> is added to a
|
||||
/// <see cref="IBodyPart"/> that is attached to a <see cref="IBody"/>.
|
||||
/// For instance, adding a brain to a head that is attached to a body.
|
||||
/// DO NOT CALL THIS DIRECTLY FROM OUTSIDE BODY SYSTEM CODE!
|
||||
/// </summary>
|
||||
void AddedToPartInBody();
|
||||
|
||||
/// <summary>
|
||||
/// Called when the parent <see cref="IMechanism"/> is removed from a
|
||||
/// <see cref="IBodyPart"/> that is attached to a <see cref="IBody"/>.
|
||||
|
||||
@@ -3,26 +3,29 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Shared.GameObjects.Components.Body.Behavior;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Content.Shared.GameObjects.Components.Body.Part;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
{
|
||||
public static class MechanismExtensions
|
||||
{
|
||||
public static bool HasMechanismBehavior<T>(this IEntity entity) where T : IMechanismBehavior
|
||||
public static bool HasMechanismBehavior<T>(this IBody body)
|
||||
{
|
||||
// TODO BODY optimize
|
||||
return entity.TryGetBody(out var body) &&
|
||||
body.Parts.Values.Any(p => p.Mechanisms.Any(m => m.Owner.HasComponent<T>()));
|
||||
return body.Parts.Values.Any(p => p.HasMechanismBehavior<T>());
|
||||
}
|
||||
|
||||
public static IEnumerable<IMechanismBehavior> GetMechanismBehaviors(this IEntity entity)
|
||||
public static bool HasMechanismBehavior<T>(this IBodyPart part)
|
||||
{
|
||||
if (!entity.TryGetBody(out var body))
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
return part.Mechanisms.Any(m => m.Owner.HasComponent<T>());
|
||||
}
|
||||
|
||||
public static bool HasMechanismBehavior<T>(this IMechanism mechanism)
|
||||
{
|
||||
return mechanism.Owner.HasComponent<T>();
|
||||
}
|
||||
|
||||
public static IEnumerable<IMechanismBehavior> GetMechanismBehaviors(this IBody body)
|
||||
{
|
||||
foreach (var part in body.Parts.Values)
|
||||
foreach (var mechanism in part.Mechanisms)
|
||||
foreach (var behavior in mechanism.Owner.GetAllComponents<IMechanismBehavior>())
|
||||
@@ -31,10 +34,10 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetMechanismBehaviors(this IEntity entity,
|
||||
public static bool TryGetMechanismBehaviors(this IBody body,
|
||||
[NotNullWhen(true)] out List<IMechanismBehavior>? behaviors)
|
||||
{
|
||||
behaviors = entity.GetMechanismBehaviors().ToList();
|
||||
behaviors = body.GetMechanismBehaviors().ToList();
|
||||
|
||||
if (behaviors.Count == 0)
|
||||
{
|
||||
@@ -45,13 +48,8 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
return true;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> GetMechanismBehaviors<T>(this IEntity entity) where T : class, IMechanismBehavior
|
||||
public static IEnumerable<T> GetMechanismBehaviors<T>(this IBody body) where T : class, IMechanismBehavior
|
||||
{
|
||||
if (!entity.TryGetBody(out var body))
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
foreach (var part in body.Parts.Values)
|
||||
foreach (var mechanism in part.Mechanisms)
|
||||
{
|
||||
@@ -62,7 +60,7 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetMechanismBehaviors<T>(this IEntity entity, [NotNullWhen(true)] out List<T>? behaviors)
|
||||
public static bool TryGetMechanismBehaviors<T>(this IBody entity, [NotNullWhen(true)] out List<T>? behaviors)
|
||||
where T : class, IMechanismBehavior
|
||||
{
|
||||
behaviors = entity.GetMechanismBehaviors<T>().ToList();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.GameObjects.Components.Body.Behavior;
|
||||
using Content.Shared.GameObjects.Components.Body.Part;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
@@ -39,12 +40,26 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
|
||||
if (old != null)
|
||||
{
|
||||
OnRemovedFromPart(old);
|
||||
if (old.Body == null)
|
||||
{
|
||||
RemovedFromPart(old);
|
||||
}
|
||||
else
|
||||
{
|
||||
RemovedFromPartInBody(old.Body, old);
|
||||
}
|
||||
}
|
||||
|
||||
if (value != null)
|
||||
{
|
||||
OnAddedToPart();
|
||||
if (value.Body == null)
|
||||
{
|
||||
AddedToPart();
|
||||
}
|
||||
else
|
||||
{
|
||||
AddedToPartInBody();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,7 +109,7 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
|
||||
OnAddedToBody();
|
||||
|
||||
foreach (var behavior in Owner.GetMechanismBehaviors())
|
||||
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
|
||||
{
|
||||
behavior.AddedToBody();
|
||||
}
|
||||
@@ -104,7 +119,7 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
{
|
||||
OnRemovedFromBody(old);
|
||||
|
||||
foreach (var behavior in Owner.GetMechanismBehaviors())
|
||||
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
|
||||
{
|
||||
behavior.RemovedFromBody(old);
|
||||
}
|
||||
@@ -117,23 +132,12 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
Owner.Transform.AttachParent(Part!.Owner);
|
||||
OnAddedToPart();
|
||||
|
||||
foreach (var behavior in Owner.GetMechanismBehaviors())
|
||||
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
|
||||
{
|
||||
behavior.AddedToPart();
|
||||
}
|
||||
}
|
||||
|
||||
public void RemovedFromPart(IBodyPart old)
|
||||
{
|
||||
Owner.Transform.AttachToGridOrMap();
|
||||
OnRemovedFromPart(old);
|
||||
|
||||
foreach (var behavior in Owner.GetMechanismBehaviors())
|
||||
{
|
||||
behavior.RemovedFromPart(old);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddedToPartInBody()
|
||||
{
|
||||
DebugTools.AssertNotNull(Body);
|
||||
@@ -142,18 +146,29 @@ namespace Content.Shared.GameObjects.Components.Body.Mechanism
|
||||
Owner.Transform.AttachParent(Part!.Owner);
|
||||
OnAddedToPartInBody();
|
||||
|
||||
foreach (var behavior in Owner.GetMechanismBehaviors())
|
||||
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
|
||||
{
|
||||
behavior.AddedToPartInBody();
|
||||
}
|
||||
}
|
||||
|
||||
public void RemovedFromPart(IBodyPart old)
|
||||
{
|
||||
Owner.Transform.AttachToGridOrMap();
|
||||
OnRemovedFromPart(old);
|
||||
|
||||
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
|
||||
{
|
||||
behavior.RemovedFromPart(old);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemovedFromPartInBody(IBody? oldBody, IBodyPart? oldPart)
|
||||
{
|
||||
Owner.Transform.AttachToGridOrMap();
|
||||
OnRemovedFromPartInBody();
|
||||
|
||||
foreach (var behavior in Owner.GetMechanismBehaviors())
|
||||
foreach (var behavior in Owner.GetAllComponents<IMechanismBehavior>())
|
||||
{
|
||||
behavior.RemovedFromPartInBody(oldBody, oldPart);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Body.Part
|
||||
{
|
||||
public interface IBodyPart : IHasBody, IBodyPartContainer
|
||||
public interface IBodyPart : IComponent, IBodyPartContainer
|
||||
{
|
||||
new IBody? Body { get; set; }
|
||||
|
||||
|
||||
@@ -114,15 +114,6 @@ namespace Content.Shared.GameObjects.Components.Body.Part
|
||||
mechanism.Part = this;
|
||||
SizeUsed += mechanism.Size;
|
||||
|
||||
if (Body == null)
|
||||
{
|
||||
mechanism.AddedToPart();
|
||||
}
|
||||
else
|
||||
{
|
||||
mechanism.AddedToPartInBody();
|
||||
}
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
@@ -132,15 +123,6 @@ namespace Content.Shared.GameObjects.Components.Body.Part
|
||||
mechanism.Part = null;
|
||||
SizeUsed -= mechanism.Size;
|
||||
|
||||
if (Body == null)
|
||||
{
|
||||
mechanism.RemovedFromPart(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
mechanism.RemovedFromPartInBody(Body, this);
|
||||
}
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user