This commit is contained in:
Leon Friedrich
2022-01-14 03:21:30 +13:00
committed by GitHub
parent 2cbe55d179
commit 15c8aaca66
2 changed files with 78 additions and 37 deletions

View File

@@ -1,14 +1,10 @@
using System.Threading.Tasks; using Content.Server.Power.EntitySystems;
using Content.Server.Electrocution;
using Content.Server.Stack;
using Content.Server.Tools;
using Content.Shared.Interaction;
using Content.Shared.Tools; using Content.Shared.Tools;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.ViewVariables;
namespace Content.Server.Power.Components namespace Content.Server.Power.Components
{ {
@@ -16,46 +12,25 @@ namespace Content.Server.Power.Components
/// Allows the attached entity to be destroyed by a cutting tool, dropping a piece of cable. /// Allows the attached entity to be destroyed by a cutting tool, dropping a piece of cable.
/// </summary> /// </summary>
[RegisterComponent] [RegisterComponent]
public class CableComponent : Component, IInteractUsing [Friend(typeof(CableSystem))]
[ComponentProtoName("Cable")]
public class CableComponent : Component
{ {
[Dependency] private readonly IEntityManager _entMan = default!; [DataField("cableDroppedOnCutPrototype", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public readonly string CableDroppedOnCutPrototype = "CableHVStack1";
public override string Name => "Cable";
[ViewVariables]
[DataField("cableDroppedOnCutPrototype")]
private string? _cableDroppedOnCutPrototype = "CableHVStack1";
[DataField("cuttingQuality", customTypeSerializer:typeof(PrototypeIdSerializer<ToolQualityPrototype>))] [DataField("cuttingQuality", customTypeSerializer:typeof(PrototypeIdSerializer<ToolQualityPrototype>))]
private string _cuttingQuality = "Cutting"; public string CuttingQuality = "Cutting";
/// <summary> /// <summary>
/// Checked by <see cref="CablePlacerComponent"/> to determine if there is /// Checked by <see cref="CablePlacerComponent"/> to determine if there is
/// already a cable of a type on a tile. /// already a cable of a type on a tile.
/// </summary> /// </summary>
[ViewVariables]
public CableType CableType => _cableType;
[DataField("cableType")] [DataField("cableType")]
private CableType _cableType = CableType.HighVoltage; public CableType CableType = CableType.HighVoltage;
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs) [DataField("cuttingDelay")]
{ public float CuttingDelay = 0.25f;
if (_cableDroppedOnCutPrototype == null)
return false;
if (!await EntitySystem.Get<ToolSystem>().UseTool(eventArgs.Using, eventArgs.User, Owner, 0f, 0.25f, _cuttingQuality)) return false;
if (EntitySystem.Get<ElectrocutionSystem>().TryDoElectrifiedAct(Owner, eventArgs.User)) return false;
_entMan.DeleteEntity(Owner);
var droppedEnt = _entMan.SpawnEntity(_cableDroppedOnCutPrototype, eventArgs.ClickLocation);
// TODO: Literally just use a prototype that has a single thing in the stack, it's not that complicated...
if (_entMan.TryGetComponent<StackComponent?>(droppedEnt, out var stack))
EntitySystem.Get<StackSystem>().SetCount(droppedEnt, 1, stack);
return true;
}
} }
public enum CableType public enum CableType

View File

@@ -0,0 +1,66 @@
using Content.Server.Electrocution;
using Content.Server.Power.Components;
using Content.Server.Tools;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Power.EntitySystems;
public class CableSystem : EntitySystem
{
[Dependency] private readonly ToolSystem _toolSystem = default!;
[Dependency] private readonly ElectrocutionSystem _electrocutionSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CableComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<CableComponent, CuttingFinishedEvent>(OnCableCut);
SubscribeLocalEvent<CableComponent, AnchorStateChangedEvent>(OnAnchorChanged);
}
private void OnInteractUsing(EntityUid uid, CableComponent cable, InteractUsingEvent args)
{
if (args.Handled)
return;
var ev = new CuttingFinishedEvent(uid, args.User);
_toolSystem.UseTool(args.Used, args.User, uid, 0, cable.CuttingDelay, new[] { cable.CuttingQuality }, doAfterCompleteEvent: ev);
args.Handled = true;
}
private void OnCableCut(EntityUid uid, CableComponent cable, CuttingFinishedEvent args)
{
if (_electrocutionSystem.TryDoElectrifiedAct(uid, args.User))
return;
Spawn(cable.CableDroppedOnCutPrototype, Transform(uid).Coordinates);
Del(uid);
}
private void OnAnchorChanged(EntityUid uid, CableComponent cable, ref AnchorStateChangedEvent args)
{
if (args.Anchored)
return; // huh? it wasn't anchored?
// This entity should not be un-anchorable. But this can happen if the grid-tile is deleted (RCD, explosion,
// etc). In that case: behave as if the cable had been cut.
Spawn(cable.CableDroppedOnCutPrototype, Transform(uid).Coordinates);
Del(uid);
}
}
// TODO: if #5887 gets merged, just use a directed event instead of broadcast-with-target
public class CuttingFinishedEvent : EntityEventArgs
{
public EntityUid Target;
public EntityUid User;
public CuttingFinishedEvent(EntityUid target, EntityUid user)
{
Target = target;
User = user;
}
}