Remove drop argument in part removal methods and reorganize code (#2289)

* Remove IBodyPart.Drop and move attach/detach logic to body part code

* Fix stack overflow
This commit is contained in:
DrSmugleaf
2020-10-18 11:12:17 +02:00
committed by GitHub
parent 7053352e18
commit 34e0330187
9 changed files with 47 additions and 49 deletions

View File

@@ -43,7 +43,7 @@ namespace Content.IntegrationTests.Tests.Body
foreach (var leg in legs) foreach (var leg in legs)
{ {
body.RemovePart(leg, false); body.RemovePart(leg);
} }
}); });

View File

@@ -180,7 +180,7 @@ namespace Content.IntegrationTests.Tests.Body
component.ResetAll(); component.ResetAll();
body.RemovePart(centerPart, true); body.RemovePart(centerPart);
Assert.That(component.NoAdded); Assert.That(component.NoAdded);
Assert.That(component.WasRemovedFromBody); Assert.That(component.WasRemovedFromBody);

View File

@@ -244,7 +244,7 @@ namespace Content.IntegrationTests.Tests
// Break our guy's kneecaps // Break our guy's kneecaps
foreach (var leg in legs) foreach (var leg in legs)
{ {
body.RemovePart(leg, false); body.RemovePart(leg);
} }
}); });

View File

@@ -185,7 +185,7 @@ namespace Content.Server.GameObjects.Components.Body
} }
else else
{ {
body.RemovePart(hand.Value, true); body.RemovePart(hand.Value);
} }
} }
} }

View File

@@ -61,18 +61,15 @@ namespace Content.Shared.GameObjects.Components.Body
/// dropping other <see cref="IBodyPart">BodyParts</see> if they /// dropping other <see cref="IBodyPart">BodyParts</see> if they
/// were hanging off of it. /// were hanging off of it.
/// </summary> /// </summary>
void RemovePart(IBodyPart part, bool drop); void RemovePart(IBodyPart part);
/// <summary> /// <summary>
/// Removes the body part in slot <see cref="slot"/> from this body, /// Removes the body part in slot <see cref="slot"/> from this body,
/// if one exists. /// if one exists.
/// </summary> /// </summary>
/// <param name="slot">The slot to remove it from.</param> /// <param name="slot">The slot to remove it from.</param>
/// <param name="drop">
/// Whether or not to drop the removed <see cref="IBodyPart"/>.
/// </param>
/// <returns>True if the part was removed, false otherwise.</returns> /// <returns>True if the part was removed, false otherwise.</returns>
bool RemovePart(string slot, bool drop); bool RemovePart(string slot);
/// <summary> /// <summary>
/// Removes the body part from this body, if one exists. /// Removes the body part from this body, if one exists.

View File

@@ -9,7 +9,7 @@ namespace Content.Shared.GameObjects.Components.Body.Part
{ {
public interface IBodyPart : IComponent, IBodyPartContainer public interface IBodyPart : IComponent, IBodyPartContainer
{ {
new IBody? Body { get; set; } IBody? Body { get; set; }
/// <summary> /// <summary>
/// <see cref="BodyPartType"/> that this <see cref="IBodyPart"/> is considered /// <see cref="BodyPartType"/> that this <see cref="IBodyPart"/> is considered
@@ -46,8 +46,6 @@ namespace Content.Shared.GameObjects.Components.Body.Part
public BodyPartSymmetry Symmetry { get; } public BodyPartSymmetry Symmetry { get; }
bool Drop();
/// <summary> /// <summary>
/// Checks if the given <see cref="SurgeryType"/> can be used on /// Checks if the given <see cref="SurgeryType"/> can be used on
/// the current state of this <see cref="IBodyPart"/>. /// the current state of this <see cref="IBodyPart"/>.

View File

@@ -45,18 +45,12 @@ namespace Content.Shared.GameObjects.Components.Body.Part
if (old != null) if (old != null)
{ {
foreach (var mechanism in _mechanisms) RemovedFromBody(old);
{
mechanism.RemovedFromBody(old);
}
} }
if (value != null) if (value != null)
{ {
foreach (var mechanism in _mechanisms) AddedToBody();
{
mechanism.AddedToBody();
}
} }
} }
} }
@@ -189,13 +183,6 @@ namespace Content.Shared.GameObjects.Components.Body.Part
} }
} }
public bool Drop()
{
Body = null;
Owner.Transform.AttachToGridOrMap();
return true;
}
public bool SurgeryCheck(SurgeryType surgery) public bool SurgeryCheck(SurgeryType surgery)
{ {
return SurgeryDataComponent?.CheckSurgery(surgery) ?? false; return SurgeryDataComponent?.CheckSurgery(surgery) ?? false;
@@ -301,6 +288,36 @@ namespace Content.Shared.GameObjects.Components.Body.Part
mechanism.Owner.Delete(); mechanism.Owner.Delete();
return true; return true;
} }
private void AddedToBody()
{
Owner.Transform.AttachParent(Body!.Owner);
OnAddedToBody();
foreach (var mechanism in _mechanisms)
{
mechanism.AddedToBody();
}
}
private void RemovedFromBody(IBody old)
{
if (!Owner.Transform.Deleted)
{
Owner.Transform.AttachToGridOrMap();
}
OnRemovedFromBody(old);
foreach (var mechanism in _mechanisms)
{
mechanism.RemovedFromBody(old);
}
}
protected virtual void OnAddedToBody() { }
protected virtual void OnRemovedFromBody(IBody old) { }
} }
[Serializable, NetSerializable] [Serializable, NetSerializable]

View File

@@ -68,7 +68,6 @@ namespace Content.Shared.GameObjects.Components.Body
protected virtual void OnAddPart(string slot, IBodyPart part) protected virtual void OnAddPart(string slot, IBodyPart part)
{ {
part.Owner.Transform.AttachParent(Owner);
part.Body = this; part.Body = this;
var argsAdded = new BodyPartAddedEventArgs(part, slot); var argsAdded = new BodyPartAddedEventArgs(part, slot);
@@ -84,12 +83,6 @@ namespace Content.Shared.GameObjects.Components.Body
protected virtual void OnRemovePart(string slot, IBodyPart part) protected virtual void OnRemovePart(string slot, IBodyPart part)
{ {
// TODO BODY Move to Body part
if (!part.Owner.Transform.Deleted)
{
part.Owner.Transform.AttachToGridOrMap();
}
part.Body = null; part.Body = null;
var args = new BodyPartRemovedEventArgs(part, slot); var args = new BodyPartRemovedEventArgs(part, slot);
@@ -152,7 +145,7 @@ namespace Content.Shared.GameObjects.Components.Body
return _parts.ContainsKey(slot); return _parts.ContainsKey(slot);
} }
public void RemovePart(IBodyPart part, bool drop) public void RemovePart(IBodyPart part)
{ {
DebugTools.AssertNotNull(part); DebugTools.AssertNotNull(part);
@@ -163,11 +156,11 @@ namespace Content.Shared.GameObjects.Components.Body
return; return;
} }
RemovePart(slotName, drop); RemovePart(slotName);
} }
// TODO BODY invert this behavior with the one above // TODO BODY invert this behavior with the one above
public bool RemovePart(string slot, bool drop) public bool RemovePart(string slot)
{ {
DebugTools.AssertNotNull(slot); DebugTools.AssertNotNull(slot);
@@ -176,11 +169,6 @@ namespace Content.Shared.GameObjects.Components.Body
return false; return false;
} }
if (drop)
{
part.Drop();
}
OnRemovePart(slot, part); OnRemovePart(slot, part);
if (TryGetSlotConnections(slot, out var connections)) if (TryGetSlotConnections(slot, out var connections))
@@ -189,7 +177,7 @@ namespace Content.Shared.GameObjects.Components.Body
{ {
if (TryGetPart(connectionName, out var result) && !ConnectedToCenter(result)) if (TryGetPart(connectionName, out var result) && !ConnectedToCenter(result))
{ {
RemovePart(connectionName, drop); RemovePart(connectionName);
} }
} }
} }
@@ -209,7 +197,7 @@ namespace Content.Shared.GameObjects.Components.Body
return false; return false;
} }
if (RemovePart(pair.Key, false)) if (RemovePart(pair.Key))
{ {
slotName = pair.Key; slotName = pair.Key;
return true; return true;
@@ -235,8 +223,6 @@ namespace Content.Shared.GameObjects.Components.Body
return false; return false;
} }
part.Drop();
dropped = new List<IBodyPart> {part}; dropped = new List<IBodyPart> {part};
// Call disconnect on all limbs that were hanging off this limb. // Call disconnect on all limbs that were hanging off this limb.
if (TryGetSlotConnections(slotName, out var connections)) if (TryGetSlotConnections(slotName, out var connections))
@@ -246,7 +232,7 @@ namespace Content.Shared.GameObjects.Components.Body
{ {
if (TryGetPart(connectionName, out var result) && if (TryGetPart(connectionName, out var result) &&
!ConnectedToCenter(result) && !ConnectedToCenter(result) &&
RemovePart(connectionName, true)) RemovePart(connectionName))
{ {
dropped.Add(result); dropped.Add(result);
} }
@@ -694,7 +680,7 @@ namespace Content.Shared.GameObjects.Components.Body
if (!newParts.TryGetValue(slot, out var newPart) || if (!newParts.TryGetValue(slot, out var newPart) ||
newPart != oldPart) newPart != oldPart)
{ {
RemovePart(oldPart, false); RemovePart(oldPart);
} }
} }

View File

@@ -260,7 +260,7 @@ namespace Content.Shared.GameObjects.Components.Body.Surgery
performer.PopupMessage(Loc.GetString("Saw off the limb!")); performer.PopupMessage(Loc.GetString("Saw off the limb!"));
// TODO BODY do_after: Delay // TODO BODY do_after: Delay
body.RemovePart(Parent, true); body.RemovePart(Parent);
} }
} }
} }