Files
tbd-station-14/Content.Server/Power/Components/CablePlacerComponent.cs
moonheart08 86e2f2005e Allows placing cables on lattices. (#4984)
Adds a "sturdy" tile flag, that in the future could be used by construction/etc to figure out if it's safe to build on a tile. (For example water isn't space, but it's not a sturdy building surface!)
2021-10-23 17:32:33 -07:00

69 lines
2.3 KiB
C#

using System.Threading.Tasks;
using Content.Server.Stack;
using Content.Shared.ActionBlocker;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
using Content.Shared.Maps;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Power.Components
{
[RegisterComponent]
internal class CablePlacerComponent : Component, IAfterInteract
{
[Dependency] private readonly IMapManager _mapManager = default!;
/// <inheritdoc />
public override string Name => "CablePlacer";
[ViewVariables]
[DataField("cablePrototypeID")]
private string? _cablePrototypeID = "CableHV";
[ViewVariables]
[DataField("blockingWireType")]
private CableType _blockingCableType = CableType.HighVoltage;
/// <inheritdoc />
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(eventArgs.User))
return false;
if (_cablePrototypeID == null)
return false;
if (!eventArgs.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true))
return false;
if(!_mapManager.TryGetGrid(eventArgs.ClickLocation.GetGridId(Owner.EntityManager), out var grid))
return false;
var snapPos = grid.TileIndicesFor(eventArgs.ClickLocation);
var tileDef = grid.GetTileRef(snapPos).Tile.GetContentTileDefinition();
if(!tileDef.IsSubFloor || !tileDef.Sturdy)
return false;
foreach (var anchored in grid.GetAnchoredEntities(snapPos))
{
if (Owner.EntityManager.TryGetComponent<CableComponent>(anchored, out var wire) && wire.CableType == _blockingCableType)
{
return false;
}
}
if (Owner.TryGetComponent<StackComponent>(out var stack)
&& !EntitySystem.Get<StackSystem>().Use(Owner.Uid, 1, stack))
return false;
Owner.EntityManager.SpawnEntity(_cablePrototypeID, grid.GridTileToLocal(snapPos));
return true;
}
}
}