Add body part and body manager interfaces (#1939)

* Add body part and body manager interfaces

* Merge fixes
This commit is contained in:
DrSmugleaf
2020-08-30 11:26:52 +02:00
committed by GitHub
parent a8aa088058
commit 827eab17d0
16 changed files with 393 additions and 322 deletions

View File

@@ -14,13 +14,11 @@ using Content.Shared.Damage.DamageContainer;
using Content.Shared.Damage.ResistanceSet;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Damage;
using Robust.Server.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Reflection;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
@@ -34,19 +32,10 @@ namespace Content.Server.Body
/// which coordinates functions between BodyParts, or a
/// <see cref="DroppedBodyPartComponent"/>.
/// </summary>
public class BodyPart
public class BodyPart : IBodyPart
{
/// <summary>
/// The body that this body part is in, if any.
/// </summary>
private BodyManagerComponent? _body;
private IBodyManagerComponent? _body;
/// <summary>
/// Set of all <see cref="Mechanism"/> currently inside this
/// <see cref="BodyPart"/>.
/// To add and remove from this list see <see cref="AddMechanism"/> and
/// <see cref="RemoveMechanism"/>
/// </summary>
private readonly HashSet<Mechanism> _mechanisms = new HashSet<Mechanism>();
public BodyPart(BodyPartPrototype data)
@@ -64,11 +53,8 @@ namespace Content.Server.Body
LoadFromPrototype(data);
}
/// <summary>
/// The body that this body part is in, if any.
/// </summary>
[ViewVariables]
public BodyManagerComponent? Body
public IBodyManagerComponent? Body
{
get => _body;
set
@@ -111,91 +97,48 @@ namespace Content.Server.Body
[ViewVariables]
private HashSet<IExposeData> Properties { get; }
/// <summary>
/// The name of this <see cref="BodyPart"/>, often displayed to the user.
/// For example, it could be named "advanced robotic arm".
/// </summary>
[ViewVariables]
public string Name { get; private set; }
[ViewVariables] public string Name { get; private set; }
/// <summary>
/// Plural version of this <see cref="BodyPart"/> name.
/// </summary>
[ViewVariables]
public string Plural { get; private set; }
[ViewVariables] public string Plural { get; private set; }
/// <summary>
/// Path to the RSI that represents this <see cref="BodyPart"/>.
/// </summary>
[ViewVariables]
public string RSIPath { get; private set; }
[ViewVariables] public string RSIPath { get; private set; }
/// <summary>
/// RSI state that represents this <see cref="BodyPart"/>.
/// </summary>
[ViewVariables]
public string RSIState { get; private set; }
[ViewVariables] public string RSIState { get; private set; }
/// <summary>
/// RSI map keys that this body part changes on the sprite.
/// </summary>
[ViewVariables]
public Enum? RSIMap { get; set; }
[ViewVariables] public Enum? RSIMap { get; set; }
/// <summary>
/// RSI color of this body part.
/// </summary>
// TODO: SpriteComponent rework
public Color? RSIColor { get; set; }
[ViewVariables] public Color? RSIColor { get; set; }
/// <summary>
/// <see cref="BodyPartType"/> that this <see cref="BodyPart"/> is considered
/// to be.
/// For example, <see cref="BodyPartType.Arm"/>.
/// </summary>
[ViewVariables]
public BodyPartType PartType { get; private set; }
[ViewVariables] public BodyPartType PartType { get; private set; }
/// <summary>
/// Determines many things: how many mechanisms can be fit inside this
/// <see cref="BodyPart"/>, whether a body can fit through tiny crevices, etc.
/// </summary>
[ViewVariables]
private int Size { get; set; }
[ViewVariables] public int Size { get; private set; }
/// <summary>
/// Max HP of this <see cref="BodyPart"/>.
/// </summary>
[ViewVariables]
public int MaxDurability { get; private set; }
[ViewVariables] public int MaxDurability { get; private set; }
/// <summary>
/// Current HP of this <see cref="BodyPart"/> based on sum of all damage types.
/// </summary>
[ViewVariables]
public int CurrentDurability => MaxDurability - Damage.TotalDamage;
[ViewVariables] public int CurrentDurability => MaxDurability - Damage.TotalDamage;
// TODO: Individual body part damage
/// <summary>
/// Current damage dealt to this <see cref="BodyPart"/>.
/// Current damage dealt to this <see cref="IBodyPart"/>.
/// </summary>
[ViewVariables]
public DamageContainer Damage { get; private set; }
/// <summary>
/// Armor of this <see cref="BodyPart"/> against damages.
/// Armor of this <see cref="IBodyPart"/> against damages.
/// </summary>
[ViewVariables]
public ResistanceSet Resistances { get; private set; }
/// <summary>
/// At what HP this <see cref="BodyPart"/> destroyed.
/// At what HP this <see cref="IBodyPart"/> destroyed.
/// </summary>
[ViewVariables]
public int DestroyThreshold { get; private set; }
/// <summary>
/// What types of BodyParts this <see cref="BodyPart"/> can easily attach to.
/// What types of BodyParts this <see cref="IBodyPart"/> can easily attach to.
/// For the most part, most limbs aren't universal and require extra work to
/// attach between types.
/// </summary>
@@ -204,14 +147,15 @@ namespace Content.Server.Body
/// <summary>
/// Set of all <see cref="Mechanism"/> currently inside this
/// <see cref="BodyPart"/>.
/// <see cref="IBodyPart"/>.
/// </summary>
[ViewVariables]
public IReadOnlyCollection<Mechanism> Mechanisms => _mechanisms;
/// <summary>
/// This method is called by <see cref="BodyManagerComponent.Update"/>
/// before <see cref="MetabolismComponent.Update"/> is called.
/// This method is called by
/// <see cref="IBodyManagerComponent.PreMetabolism"/> before
/// <see cref="MetabolismComponent.Update"/> is called.
/// </summary>
public void PreMetabolism(float frameTime)
{
@@ -222,8 +166,9 @@ namespace Content.Server.Body
}
/// <summary>
/// This method is called by <see cref="BodyManagerComponent.Update"/>
/// after <see cref="MetabolismComponent.Update"/> is called.
/// This method is called by
/// <see cref="IBodyManagerComponent.PostMetabolism"/> after
/// <see cref="MetabolismComponent.Update"/> is called.
/// </summary>
public void PostMetabolism(float frameTime)
{
@@ -258,9 +203,10 @@ namespace Content.Server.Body
/// <param name="property">The property if found, null otherwise.</param>
/// <typeparam name="T">The type of the property to find.</typeparam>
/// <returns>True if successful, false otherwise.</returns>
public bool TryGetProperty<T>(out T property)
public bool TryGetProperty<T>([NotNullWhen(true)] out T? property) where T : BodyPartProperty
{
property = (T) Properties.First(x => x.GetType() == typeof(T));
property = (T?) Properties.FirstOrDefault(x => x.GetType() == typeof(T));
return property != null;
}
@@ -269,20 +215,21 @@ namespace Content.Server.Body
/// The resulting <see cref="BodyPartProperty"/> will be null if unsuccessful.
/// </summary>
/// <returns>True if successful, false otherwise.</returns>
public bool TryGetProperty(Type propertyType, out BodyPartProperty property)
public bool TryGetProperty(Type propertyType, [NotNullWhen(true)] out BodyPartProperty? property)
{
property = (BodyPartProperty) Properties.First(x => x.GetType() == propertyType);
property = (BodyPartProperty?) Properties.First(x => x.GetType() == propertyType);
return property != null;
}
/// <summary>
/// Checks if the given type <see cref="T"/> is on this <see cref="BodyPart"/>.
/// Checks if the given type <see cref="T"/> is on this <see cref="IBodyPart"/>.
/// </summary>
/// <typeparam name="T">
/// The subtype of <see cref="BodyPartProperty"/> to look for.
/// </typeparam>
/// <returns>
/// True if this <see cref="BodyPart"/> has a property of type
/// True if this <see cref="IBodyPart"/> has a property of type
/// <see cref="T"/>, false otherwise.
/// </returns>
public bool HasProperty<T>() where T : BodyPartProperty
@@ -292,13 +239,13 @@ namespace Content.Server.Body
/// <summary>
/// Checks if a subtype of <see cref="BodyPartProperty"/> is on this
/// <see cref="BodyPart"/>.
/// <see cref="IBodyPart"/>.
/// </summary>
/// <param name="propertyType">
/// The subtype of <see cref="BodyPartProperty"/> to look for.
/// </param>
/// <returns>
/// True if this <see cref="BodyPart"/> has a property of type
/// True if this <see cref="IBodyPart"/> has a property of type
/// <see cref="propertyType"/>, false otherwise.
/// </returns>
public bool HasProperty(Type propertyType)
@@ -306,21 +253,11 @@ namespace Content.Server.Body
return Properties.Count(x => x.GetType() == propertyType) > 0;
}
/// <summary>
/// Checks if another <see cref="BodyPart"/> can be connected to this one.
/// </summary>
/// <param name="toBeConnected">The part to connect.</param>
/// <returns>True if it can be connected, false otherwise.</returns>
public bool CanAttachBodyPart(BodyPart toBeConnected)
public bool CanAttachPart(IBodyPart part)
{
return SurgeryData.CanAttachBodyPart(toBeConnected);
return SurgeryData.CanAttachBodyPart(part);
}
/// <summary>
/// Checks if a <see cref="Mechanism"/> can be installed on this
/// <see cref="BodyPart"/>.
/// </summary>
/// <returns>True if it can be installed, false otherwise.</returns>
public bool CanInstallMechanism(Mechanism mechanism)
{
return SizeUsed + mechanism.Size <= Size &&
@@ -336,7 +273,7 @@ namespace Content.Server.Body
/// <param name="mechanism">The mechanism to try to install.</param>
/// <returns>
/// True if successful, false if there was an error
/// (e.g. not enough room in <see cref="BodyPart"/>).
/// (e.g. not enough room in <see cref="IBodyPart"/>).
/// </returns>
private bool TryInstallMechanism(Mechanism mechanism)
{
@@ -352,7 +289,7 @@ namespace Content.Server.Body
/// <summary>
/// Tries to install a <see cref="DroppedMechanismComponent"/> into this
/// <see cref="BodyPart"/>, potentially deleting the dropped
/// <see cref="IBodyPart"/>, potentially deleting the dropped
/// <see cref="IEntity"/>.
/// </summary>
/// <param name="droppedMechanism">The mechanism to install.</param>
@@ -364,21 +301,13 @@ namespace Content.Server.Body
{
if (!TryInstallMechanism(droppedMechanism.ContainedMechanism))
{
return false; //Installing the mechanism failed for some reason.
return false; // Installing the mechanism failed for some reason.
}
droppedMechanism.Owner.Delete();
return true;
}
/// <summary>
/// Tries to remove the given <see cref="Mechanism"/> reference from
/// this <see cref="BodyPart"/>.
/// </summary>
/// <returns>
/// The newly spawned <see cref="DroppedMechanismComponent"/>, or null
/// if there was an error in spawning the entity or removing the mechanism.
/// </returns>
public bool TryDropMechanism(IEntity dropLocation, Mechanism mechanismTarget,
[NotNullWhen(true)] out DroppedMechanismComponent dropped)
{
@@ -403,11 +332,11 @@ namespace Content.Server.Body
/// <summary>
/// Tries to destroy the given <see cref="Mechanism"/> in this
/// <see cref="BodyPart"/>. Does NOT spawn a dropped entity.
/// <see cref="IBodyPart"/>. Does NOT spawn a dropped entity.
/// </summary>
/// <summary>
/// Tries to destroy the given <see cref="Mechanism"/> in this
/// <see cref="BodyPart"/>.
/// <see cref="IBodyPart"/>.
/// </summary>
/// <param name="mechanismTarget">The mechanism to destroy.</param>
/// <returns>True if successful, false otherwise.</returns>
@@ -421,18 +350,13 @@ namespace Content.Server.Body
return true;
}
/// <summary>
/// Checks if the given <see cref="SurgeryType"/> can be used on
/// the current state of this <see cref="BodyPart"/>.
/// </summary>
/// <returns>True if it can be used, false otherwise.</returns>
public bool SurgeryCheck(SurgeryType toolType)
public bool SurgeryCheck(SurgeryType surgery)
{
return SurgeryData.CheckSurgery(toolType);
return SurgeryData.CheckSurgery(surgery);
}
/// <summary>
/// Attempts to perform surgery on this <see cref="BodyPart"/> with the given
/// Attempts to perform surgery on this <see cref="IBodyPart"/> with the given
/// tool.
/// </summary>
/// <returns>True if successful, false if there was an error.</returns>
@@ -474,7 +398,7 @@ namespace Content.Server.Body
/// <summary>
/// Tries to remove the given <see cref="mechanism"/> from this
/// <see cref="BodyPart"/>.
/// <see cref="IBodyPart"/>.
/// </summary>
/// <param name="mechanism">The mechanism to remove.</param>
/// <returns>True if it was removed, false otherwise.</returns>
@@ -515,7 +439,7 @@ namespace Content.Server.Body
/// <summary>
/// Loads the given <see cref="BodyPartPrototype"/>.
/// Current data on this <see cref="BodyPart"/> will be overwritten!
/// Current data on this <see cref="IBodyPart"/> will be overwritten!
/// </summary>
protected virtual void LoadFromPrototype(BodyPartPrototype data)
{