2021-02-01 10:19:43 -06:00
|
|
|
#nullable enable
|
2021-06-09 22:19:39 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Content.Server.Stack;
|
|
|
|
|
using Content.Shared.Interaction;
|
|
|
|
|
using Content.Shared.Interaction.Helpers;
|
2019-09-19 11:47:45 -07:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Map;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
|
|
|
|
using Robust.Shared.ViewVariables;
|
2019-09-19 11:47:45 -07:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Wires.Components
|
2019-09-19 11:47:45 -07:00
|
|
|
{
|
|
|
|
|
[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-03-05 01:08:38 +01:00
|
|
|
[DataField("wirePrototypeID")]
|
|
|
|
|
private string? _wirePrototypeID = "HVWire";
|
2020-06-28 09:23:26 -06:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("blockingWireType")]
|
|
|
|
|
private WireType _blockingWireType = WireType.HighVoltage;
|
2020-06-28 09:23:26 -06:00
|
|
|
|
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;
|
2021-04-28 10:49:37 -07:00
|
|
|
var snapPos = grid.TileIndicesFor(eventArgs.ClickLocation);
|
2019-09-19 11:47:45 -07:00
|
|
|
if(grid.GetTileRef(snapPos).Tile.IsEmpty)
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2021-04-28 10:49:37 -07:00
|
|
|
foreach (var anchored in grid.GetAnchoredEntities(snapPos))
|
2019-09-19 11:47:45 -07:00
|
|
|
{
|
2021-04-28 10:49:37 -07:00
|
|
|
if (Owner.EntityManager.ComponentManager.TryGetComponent<WireComponent>(anchored, out var wire) && wire.WireType == _blockingWireType)
|
2020-06-28 09:23:26 -06:00
|
|
|
{
|
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-05-26 10:20:57 +02:00
|
|
|
|
|
|
|
|
if (Owner.HasComponent<StackComponent>())
|
|
|
|
|
{
|
|
|
|
|
var stackUse = new StackUseEvent(){Amount = 1};
|
|
|
|
|
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, stackUse);
|
|
|
|
|
|
|
|
|
|
if(!stackUse.Result)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|