Files
tbd-station-14/Content.Server/Body/Network/BodyNetwork.cs
DrSmugleaf 097e02ab4f Separate part management from the rest of body manager component (#2017)
* Separate part management from the rest of body manager component

* Component reference

* Move more methods over

* Fix docs and move over BodyPreset

* Fix up body preset

* Create initialize method and remove constructor for BodyPreset

* Do the same for BodyTemplate and add Initialized properties

* Fix BodyTemplate HashCode test

* BodyTemplate test PLS
2020-09-10 00:51:24 +02:00

84 lines
2.5 KiB
C#

using System;
using Content.Server.GameObjects.Components.Body;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.Body.Network
{
/// <summary>
/// Represents a "network" such as a bloodstream or electrical power that
/// is coordinated throughout an entire <see cref="BodyManagerComponent"/>.
/// </summary>
public abstract class BodyNetwork : IExposeData
{
[ViewVariables]
public abstract string Name { get; }
protected IEntity Owner { get; private set; }
public virtual void ExposeData(ObjectSerializer serializer) { }
public void OnAdd(IEntity entity)
{
Owner = entity;
OnAdd();
}
protected virtual void OnAdd() { }
public virtual void OnRemove() { }
/// <summary>
/// Called every update by
/// <see cref="BodyManagerComponent.PreMetabolism"/>.
/// </summary>
public virtual void PreMetabolism(float frameTime) { }
/// <summary>
/// Called every update by
/// <see cref="BodyManagerComponent.PostMetabolism"/>.
/// </summary>
public virtual void PostMetabolism(float frameTime) { }
}
public static class BodyNetworkExtensions
{
public static void TryAddNetwork(this IEntity entity, Type type)
{
if (!entity.TryGetComponent(out BodyManagerComponent body))
{
return;
}
body.EnsureNetwork(type);
}
public static void TryAddNetwork<T>(this IEntity entity) where T : BodyNetwork
{
if (!entity.TryGetComponent(out BodyManagerComponent body))
{
return;
}
body.EnsureNetwork<T>();
}
public static bool TryGetBodyNetwork(this IEntity entity, Type type, out BodyNetwork network)
{
network = null;
return entity.TryGetComponent(out BodyManagerComponent body) &&
body.TryGetNetwork(type, out network);
}
public static bool TryGetBodyNetwork<T>(this IEntity entity, out T network) where T : BodyNetwork
{
entity.TryGetBodyNetwork(typeof(T), out var unCastNetwork);
network = (T) unCastNetwork;
return network != null;
}
}
}