ECS CablePlacer (#6371)
This commit is contained in:
@@ -1,69 +1,20 @@
|
|||||||
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.GameObjects;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.Prototypes;
|
||||||
using Robust.Shared.Map;
|
|
||||||
using Robust.Shared.Serialization.Manager.Attributes;
|
using Robust.Shared.Serialization.Manager.Attributes;
|
||||||
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||||
using Robust.Shared.ViewVariables;
|
using Robust.Shared.ViewVariables;
|
||||||
|
|
||||||
namespace Content.Server.Power.Components
|
namespace Content.Server.Power.Components
|
||||||
{
|
{
|
||||||
[RegisterComponent]
|
[RegisterComponent, ComponentProtoName("CablePlacer")]
|
||||||
internal class CablePlacerComponent : Component, IAfterInteract
|
public sealed class CablePlacerComponent : Component
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
|
||||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override string Name => "CablePlacer";
|
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
[DataField("cablePrototypeID")]
|
[DataField("cablePrototypeID", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||||
private string? _cablePrototypeID = "CableHV";
|
public string? CablePrototypeId = "CableHV";
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
[DataField("blockingWireType")]
|
[DataField("blockingWireType")]
|
||||||
private CableType _blockingCableType = CableType.HighVoltage;
|
public 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(_entMan), 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 (_entMan.TryGetComponent<CableComponent>(anchored, out var wire) && wire.CableType == _blockingCableType)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_entMan.TryGetComponent<StackComponent?>(Owner, out var stack)
|
|
||||||
&& !EntitySystem.Get<StackSystem>().Use(Owner, 1, stack))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
_entMan.SpawnEntity(_cablePrototypeID, grid.GridTileToLocal(snapPos));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
47
Content.Server/Power/EntitySystems/CableSystem.Placer.cs
Normal file
47
Content.Server/Power/EntitySystems/CableSystem.Placer.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +1,29 @@
|
|||||||
using Content.Server.Electrocution;
|
using Content.Server.Electrocution;
|
||||||
using Content.Server.Power.Components;
|
using Content.Server.Power.Components;
|
||||||
|
using Content.Server.Stack;
|
||||||
using Content.Server.Tools;
|
using Content.Server.Tools;
|
||||||
|
using Content.Shared.ActionBlocker;
|
||||||
using Content.Shared.Interaction;
|
using Content.Shared.Interaction;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Map;
|
||||||
|
|
||||||
namespace Content.Server.Power.EntitySystems;
|
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 ToolSystem _toolSystem = default!;
|
||||||
|
[Dependency] private readonly StackSystem _stack = default!;
|
||||||
[Dependency] private readonly ElectrocutionSystem _electrocutionSystem = default!;
|
[Dependency] private readonly ElectrocutionSystem _electrocutionSystem = default!;
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
|
|
||||||
|
InitializeCablePlacer();
|
||||||
|
|
||||||
SubscribeLocalEvent<CableComponent, InteractUsingEvent>(OnInteractUsing);
|
SubscribeLocalEvent<CableComponent, InteractUsingEvent>(OnInteractUsing);
|
||||||
SubscribeLocalEvent<CableComponent, CuttingFinishedEvent>(OnCableCut);
|
SubscribeLocalEvent<CableComponent, CuttingFinishedEvent>(OnCableCut);
|
||||||
SubscribeLocalEvent<CableComponent, AnchorStateChangedEvent>(OnAnchorChanged);
|
SubscribeLocalEvent<CableComponent, AnchorStateChangedEvent>(OnAnchorChanged);
|
||||||
|
|||||||
Reference in New Issue
Block a user