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)
{
body.RemovePart(leg, false);
body.RemovePart(leg);
}
});

View File

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

View File

@@ -244,7 +244,7 @@ namespace Content.IntegrationTests.Tests
// Break our guy's kneecaps
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
{
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
/// were hanging off of it.
/// </summary>
void RemovePart(IBodyPart part, bool drop);
void RemovePart(IBodyPart part);
/// <summary>
/// Removes the body part in slot <see cref="slot"/> from this body,
/// if one exists.
/// </summary>
/// <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>
bool RemovePart(string slot, bool drop);
bool RemovePart(string slot);
/// <summary>
/// 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
{
new IBody? Body { get; set; }
IBody? Body { get; set; }
/// <summary>
/// <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; }
bool Drop();
/// <summary>
/// Checks if the given <see cref="SurgeryType"/> can be used on
/// the current state of this <see cref="IBodyPart"/>.

View File

@@ -45,18 +45,12 @@ namespace Content.Shared.GameObjects.Components.Body.Part
if (old != null)
{
foreach (var mechanism in _mechanisms)
{
mechanism.RemovedFromBody(old);
}
RemovedFromBody(old);
}
if (value != null)
{
foreach (var mechanism in _mechanisms)
{
mechanism.AddedToBody();
}
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)
{
return SurgeryDataComponent?.CheckSurgery(surgery) ?? false;
@@ -301,6 +288,36 @@ namespace Content.Shared.GameObjects.Components.Body.Part
mechanism.Owner.Delete();
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]

View File

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