Files
tbd-station-14/Content.Server/Chemistry/Components/RehydratableComponent.cs
2021-06-21 02:13:54 +02:00

58 lines
1.9 KiB
C#

#nullable enable
using Content.Server.Notification;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Chemistry.Components
{
/// <summary>
/// Basically, monkey cubes.
/// But specifically, this component deletes the entity and spawns in a new entity when the entity is exposed to a given reagent.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(ISolutionChange))]
public class RehydratableComponent : Component, ISolutionChange
{
public override string Name => "Rehydratable";
[ViewVariables]
[DataField("catalyst")]
private string _catalystPrototype = "Water";
[ViewVariables]
[DataField("target")]
private string? _targetPrototype = default!;
private bool _expanding;
void ISolutionChange.SolutionChanged(SolutionChangeEventArgs eventArgs)
{
var solution = eventArgs.Owner.GetComponent<SolutionContainerComponent>();
if (solution.Solution.GetReagentQuantity(_catalystPrototype) > ReagentUnit.Zero)
{
Expand();
}
}
// Try not to make this public if you can help it.
private void Expand()
{
if (_expanding)
{
return;
}
_expanding = true;
Owner.PopupMessageEveryone(Loc.GetString("rehydratable-component-expands-message",("owner", Owner)));
if (!string.IsNullOrEmpty(_targetPrototype))
{
var ent = Owner.EntityManager.SpawnEntity(_targetPrototype, Owner.Transform.Coordinates);
ent.Transform.AttachToGridOrMap();
}
Owner.Delete();
}
}
}