2021-02-01 10:19:43 -06:00
|
|
|
#nullable enable
|
|
|
|
|
using Content.Server.GameObjects.Components.Stack;
|
2020-08-13 14:40:27 +02:00
|
|
|
using Content.Shared.Interfaces.GameObjects.Components;
|
2020-08-30 11:37:06 +02:00
|
|
|
using Content.Shared.Utility;
|
2019-09-19 11:47:45 -07:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2020-06-28 09:23:26 -06:00
|
|
|
using Robust.Shared.Serialization;
|
|
|
|
|
using Robust.Shared.ViewVariables;
|
2020-12-17 10:45:04 +03:00
|
|
|
using System.Threading.Tasks;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Map;
|
2019-09-19 11:47:45 -07:00
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Power
|
|
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2020-05-23 17:23:25 +02:00
|
|
|
internal class WirePlacerComponent : Component, IAfterInteract
|
2019-09-19 11:47:45 -07:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
2019-09-19 11:47:45 -07:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override string Name => "WirePlacer";
|
|
|
|
|
|
2020-06-28 09:23:26 -06:00
|
|
|
[ViewVariables]
|
2021-02-01 10:19:43 -06:00
|
|
|
private string? _wirePrototypeID;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 11:47:45 -07:00
|
|
|
/// <inheritdoc />
|
2021-02-04 17:50:28 +01:00
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
2019-09-19 11:47:45 -07:00
|
|
|
{
|
2021-02-01 10:19:43 -06:00
|
|
|
if (_wirePrototypeID == null)
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
|
|
|
|
if (!eventArgs.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true))
|
|
|
|
|
return true;
|
2020-11-18 15:45:53 +01:00
|
|
|
if(!_mapManager.TryGetGrid(eventArgs.ClickLocation.GetGridId(Owner.EntityManager), out var grid))
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2020-01-25 01:39:14 -08:00
|
|
|
var snapPos = grid.SnapGridCellFor(eventArgs.ClickLocation, SnapGridOffset.Center);
|
2019-09-19 11:47:45 -07:00
|
|
|
var snapCell = grid.GetSnapGridCell(snapPos, SnapGridOffset.Center);
|
|
|
|
|
if(grid.GetTileRef(snapPos).Tile.IsEmpty)
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2019-09-19 11:47:45 -07:00
|
|
|
foreach (var snapComp in snapCell)
|
|
|
|
|
{
|
2020-06-28 09:23:26 -06:00
|
|
|
if (snapComp.Owner.TryGetComponent<WireComponent>(out var wire) && wire.WireType == _blockingWireType)
|
|
|
|
|
{
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2020-06-28 09:23:26 -06:00
|
|
|
}
|
2019-09-19 11:47:45 -07:00
|
|
|
}
|
2021-02-01 10:19:43 -06:00
|
|
|
if (Owner.TryGetComponent<StackComponent>(out var stack) && !stack.Use(1))
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2020-11-18 15:45:53 +01:00
|
|
|
Owner.EntityManager.SpawnEntity(_wirePrototypeID, grid.GridTileToLocal(snapPos));
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2019-09-19 11:47:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|