Files
tbd-station-14/Content.Server/GameObjects/Components/Atmos/GasCanisterComponent.cs
py01 5aa866548b Gas Canisters and Ports (#2151)
* CanisterComponent start

* GasCanisterPort

* canister

* GasCanister yaml

* More Gas Canisters

* Canister & port fixes

* Placeholder canister and port sprites

* GasMixture serialization

* Component ignores

* Fix duplicate component ignore

Co-authored-by: py01 <pyronetics01@gmail.com>
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
2020-10-10 13:37:52 +02:00

92 lines
2.7 KiB
C#

using Content.Server.Atmos;
using Content.Server.GameObjects.Components.Atmos.Piping;
using Content.Server.Interfaces;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using System;
using System.Linq;
namespace Content.Server.GameObjects.Components.Atmos
{
[RegisterComponent]
public class GasCanisterComponent : Component, IGasMixtureHolder
{
public override string Name => "GasCanister";
[ViewVariables]
public GasMixture Air { get; set; }
[ViewVariables]
public bool Anchored => !Owner.TryGetComponent<ICollidableComponent>(out var collidable) || collidable.Anchored;
[ViewVariables]
public GasCanisterPortComponent ConnectedPort { get; private set; }
[ViewVariables]
public bool ConnectedToPort => ConnectedPort != null;
private const float DefaultVolume = 10;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(this, x => Air, "gasMixture", new GasMixture(DefaultVolume));
}
public override void Initialize()
{
base.Initialize();
if (Owner.TryGetComponent<ICollidableComponent>(out var collidable))
{
AnchorUpdate();
collidable.AnchoredChanged += AnchorUpdate;
}
}
public override void OnRemove()
{
base.OnRemove();
if (Owner.TryGetComponent<ICollidableComponent>(out var collidable))
{
collidable.AnchoredChanged -= AnchorUpdate;
}
DisconnectFromPort();
}
public void TryConnectToPort()
{
if (!Owner.TryGetComponent<SnapGridComponent>(out var snapGrid)) return;
var port = snapGrid.GetLocal()
.Select(entity => entity.TryGetComponent<GasCanisterPortComponent>(out var port) ? port : null)
.Where(port => port != null)
.Where(port => !port.ConnectedToCanister)
.FirstOrDefault();
if (port == null) return;
ConnectedPort = port;
ConnectedPort.ConnectGasCanister(this);
}
public void DisconnectFromPort()
{
ConnectedPort?.DisconnectGasCanister();
ConnectedPort = null;
}
private void AnchorUpdate()
{
if (Anchored)
{
TryConnectToPort();
}
else
{
DisconnectFromPort();
}
}
}
}