ECS CablePlacer (#6371)

This commit is contained in:
metalgearsloth
2022-01-29 22:45:57 +11:00
committed by GitHub
parent 803d6056d8
commit b5f816c815
3 changed files with 63 additions and 57 deletions

View File

@@ -0,0 +1,47 @@
using Content.Server.Power.Components;
using Content.Server.Stack;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
using Content.Shared.Maps;
using Robust.Shared.GameObjects;
namespace Content.Server.Power.EntitySystems;
public sealed partial class CableSystem
{
private void InitializeCablePlacer()
{
SubscribeLocalEvent<CablePlacerComponent, AfterInteractEvent>(OnCablePlacerAfterInteract);
}
private void OnCablePlacerAfterInteract(EntityUid uid, CablePlacerComponent component, AfterInteractEvent args)
{
if (args.Handled) return;
if (component.CablePrototypeId == null) return;
if (!args.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true))
return;
if(!_mapManager.TryGetGrid(args.ClickLocation.GetGridId(EntityManager), out var grid))
return;
var snapPos = grid.TileIndicesFor(args.ClickLocation);
var tileDef = (ContentTileDefinition) _tileManager[grid.GetTileRef(snapPos).Tile.TypeId];
if (!tileDef.IsSubFloor || !tileDef.Sturdy)
return;
foreach (var anchored in grid.GetAnchoredEntities(snapPos))
{
if (TryComp<CableComponent>(anchored, out var wire) && wire.CableType == component.BlockingCableType)
return;
}
if (TryComp<StackComponent>(component.Owner, out var stack) && !_stack.Use(component.Owner, 1, stack))
return;
EntityManager.SpawnEntity(component.CablePrototypeId, grid.GridTileToLocal(snapPos));
args.Handled = true;
}
}

View File

@@ -1,21 +1,29 @@
using Content.Server.Electrocution;
using Content.Server.Power.Components;
using Content.Server.Stack;
using Content.Server.Tools;
using Content.Shared.ActionBlocker;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Server.Power.EntitySystems;
public class CableSystem : EntitySystem
public sealed partial class CableSystem : EntitySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileManager = default!;
[Dependency] private readonly ToolSystem _toolSystem = default!;
[Dependency] private readonly StackSystem _stack = default!;
[Dependency] private readonly ElectrocutionSystem _electrocutionSystem = default!;
public override void Initialize()
{
base.Initialize();
InitializeCablePlacer();
SubscribeLocalEvent<CableComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<CableComponent, CuttingFinishedEvent>(OnCableCut);
SubscribeLocalEvent<CableComponent, AnchorStateChangedEvent>(OnAnchorChanged);