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.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Power.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
internal class CablePlacerComponent : Component, IAfterInteract
|
||||
[RegisterComponent, ComponentProtoName("CablePlacer")]
|
||||
public sealed class CablePlacerComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Name => "CablePlacer";
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("cablePrototypeID")]
|
||||
private string? _cablePrototypeID = "CableHV";
|
||||
[DataField("cablePrototypeID", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
public 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(_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;
|
||||
}
|
||||
public CableType BlockingCableType = CableType.HighVoltage;
|
||||
}
|
||||
}
|
||||
|
||||
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.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);
|
||||
|
||||
Reference in New Issue
Block a user