Files
tbd-station-14/Content.Server/GameObjects/Components/Power/WirePlacerComponent.cs
collinlunn b51b8cb182 Some Power nullable & ComponentDependency (#3050)
* Power nullability

* AME nullability

* re-adds EnsureComponents

* conflict fix

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/RadiationCollectorComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryStorageComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryStorageComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryDischargerComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryDischargerComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* removes duplicate component assignment

Co-authored-by: py01 <pyronetics01@gmail.com>
Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>
2021-02-01 17:19:43 +01:00

62 lines
2.3 KiB
C#

#nullable enable
using Content.Server.GameObjects.Components.Stack;
using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Utility;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using System.Threading.Tasks;
namespace Content.Server.GameObjects.Components.Power
{
[RegisterComponent]
internal class WirePlacerComponent : Component, IAfterInteract
{
[Dependency] private readonly IMapManager _mapManager = default!;
/// <inheritdoc />
public override string Name => "WirePlacer";
[ViewVariables]
private string? _wirePrototypeID;
[ViewVariables]
private WireType _blockingWireType;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _wirePrototypeID, "wirePrototypeID", "HVWire");
serializer.DataField(ref _blockingWireType, "blockingWireType", WireType.HighVoltage);
}
/// <inheritdoc />
public async Task AfterInteract(AfterInteractEventArgs eventArgs)
{
if (_wirePrototypeID == null)
return;
if (!eventArgs.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true)) return;
if(!_mapManager.TryGetGrid(eventArgs.ClickLocation.GetGridId(Owner.EntityManager), out var grid))
return;
var snapPos = grid.SnapGridCellFor(eventArgs.ClickLocation, SnapGridOffset.Center);
var snapCell = grid.GetSnapGridCell(snapPos, SnapGridOffset.Center);
if(grid.GetTileRef(snapPos).Tile.IsEmpty)
return;
foreach (var snapComp in snapCell)
{
if (snapComp.Owner.TryGetComponent<WireComponent>(out var wire) && wire.WireType == _blockingWireType)
{
return;
}
}
if (Owner.TryGetComponent<StackComponent>(out var stack) && !stack.Use(1))
return;
Owner.EntityManager.SpawnEntity(_wirePrototypeID, grid.GridTileToLocal(snapPos));
}
}
}