Tile prying do_after (#6341)
This commit is contained in:
@@ -42,7 +42,7 @@ namespace Content.Server.DoAfter
|
||||
UserGrid = entityManager.GetComponent<TransformComponent>(eventArgs.User).Coordinates;
|
||||
}
|
||||
|
||||
if (eventArgs.BreakOnTargetMove)
|
||||
if (eventArgs.Target != null && eventArgs.BreakOnTargetMove)
|
||||
{
|
||||
// Target should never be null if the bool is set.
|
||||
TargetGrid = entityManager.GetComponent<TransformComponent>(eventArgs.Target!.Value).Coordinates;
|
||||
@@ -122,8 +122,9 @@ namespace Content.Server.DoAfter
|
||||
return true;
|
||||
}
|
||||
|
||||
if (EventArgs.BreakOnTargetMove && !entityManager.GetComponent<TransformComponent>(EventArgs.Target!.Value).Coordinates.InRange(
|
||||
entityManager, TargetGrid, EventArgs.MovementThreshold))
|
||||
if (EventArgs.Target != null &&
|
||||
EventArgs.BreakOnTargetMove &&
|
||||
!entityManager.GetComponent<TransformComponent>(EventArgs.Target!.Value).Coordinates.InRange(entityManager, TargetGrid, EventArgs.MovementThreshold))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,61 +1,30 @@
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Maps;
|
||||
using System.Threading;
|
||||
using Content.Shared.Tools;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Tools.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class TilePryingComponent : Component, IAfterInteract
|
||||
[RegisterComponent, ComponentProtoName("TilePrying")]
|
||||
public sealed class TilePryingComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
|
||||
public override string Name => "TilePrying";
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("toolComponentNeeded")]
|
||||
private bool _toolComponentNeeded = true;
|
||||
public bool ToolComponentNeeded = true;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("qualityNeeded", customTypeSerializer:typeof(PrototypeIdSerializer<ToolQualityPrototype>))]
|
||||
private string _qualityNeeded = "Prying";
|
||||
public string QualityNeeded = "Prying";
|
||||
|
||||
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
||||
{
|
||||
TryPryTile(eventArgs.User, eventArgs.ClickLocation);
|
||||
return true;
|
||||
}
|
||||
[ViewVariables]
|
||||
[DataField("delay")]
|
||||
public float Delay = 1f;
|
||||
|
||||
public async void TryPryTile(EntityUid user, EntityCoordinates clickLocation)
|
||||
{
|
||||
if (!_entMan.TryGetComponent<ToolComponent?>(Owner, out var tool) && _toolComponentNeeded)
|
||||
return;
|
||||
|
||||
if (!_mapManager.TryGetGrid(clickLocation.GetGridId(_entMan), out var mapGrid))
|
||||
return;
|
||||
|
||||
var tile = mapGrid.GetTileRef(clickLocation);
|
||||
|
||||
var coordinates = mapGrid.GridTileToLocal(tile.GridIndices);
|
||||
|
||||
if (!user.InRangeUnobstructed(coordinates, popup: false))
|
||||
return;
|
||||
|
||||
var tileDef = (ContentTileDefinition)_tileDefinitionManager[tile.Tile.TypeId];
|
||||
|
||||
if (!tileDef.CanCrowbar)
|
||||
return;
|
||||
|
||||
if (_toolComponentNeeded && !await EntitySystem.Get<ToolSystem>().UseTool(Owner, user, null, 0f, 0f, _qualityNeeded, toolComponent:tool))
|
||||
return;
|
||||
|
||||
coordinates.PryTile(_entMan, _mapManager);
|
||||
}
|
||||
/// <summary>
|
||||
/// Used for do_afters.
|
||||
/// </summary>
|
||||
public CancellationTokenSource? CancelToken = null;
|
||||
}
|
||||
}
|
||||
|
||||
86
Content.Server/Tools/ToolSystem.TilePrying.cs
Normal file
86
Content.Server/Tools/ToolSystem.TilePrying.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Content.Server.Tools.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Maps;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Server.Tools;
|
||||
|
||||
public sealed partial class ToolSystem
|
||||
{
|
||||
private void InitializeTilePrying()
|
||||
{
|
||||
SubscribeLocalEvent<TilePryingComponent, AfterInteractEvent>(OnTilePryingAfterInteract);
|
||||
SubscribeLocalEvent<TilePryingComponent, TilePryingCompleteEvent>(OnTilePryComplete);
|
||||
}
|
||||
|
||||
private void OnTilePryComplete(EntityUid uid, TilePryingComponent component, TilePryingCompleteEvent args)
|
||||
{
|
||||
component.CancelToken = null;
|
||||
args.Coordinates.PryTile(EntityManager, _mapManager);
|
||||
}
|
||||
|
||||
private void OnTilePryingAfterInteract(EntityUid uid, TilePryingComponent component, AfterInteractEvent args)
|
||||
{
|
||||
if (args.Handled) return;
|
||||
|
||||
if (TryPryTile(args.User, component, args.ClickLocation))
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private bool TryPryTile(EntityUid user, TilePryingComponent component, EntityCoordinates clickLocation)
|
||||
{
|
||||
if (component.CancelToken != null)
|
||||
{
|
||||
component.CancelToken.Cancel();
|
||||
component.CancelToken = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TryComp<ToolComponent?>(component.Owner, out var tool) && component.ToolComponentNeeded)
|
||||
return false;
|
||||
|
||||
if (!_mapManager.TryGetGrid(clickLocation.GetGridId(EntityManager), out var mapGrid))
|
||||
return false;
|
||||
|
||||
var tile = mapGrid.GetTileRef(clickLocation);
|
||||
|
||||
var coordinates = mapGrid.GridTileToLocal(tile.GridIndices);
|
||||
|
||||
if (!user.InRangeUnobstructed(coordinates, popup: false))
|
||||
return false;
|
||||
|
||||
var tileDef = (ContentTileDefinition)_tileDefinitionManager[tile.Tile.TypeId];
|
||||
|
||||
if (!tileDef.CanCrowbar)
|
||||
return false;
|
||||
|
||||
var token = new CancellationTokenSource();
|
||||
component.CancelToken = token;
|
||||
|
||||
UseTool(
|
||||
component.Owner,
|
||||
user,
|
||||
null,
|
||||
0f,
|
||||
component.Delay,
|
||||
new [] {component.QualityNeeded},
|
||||
new TilePryingCompleteEvent
|
||||
{
|
||||
Coordinates = clickLocation,
|
||||
},
|
||||
toolComponent: tool,
|
||||
doAfterEventTarget: component.Owner,
|
||||
cancelToken: token.Token);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private sealed class TilePryingCompleteEvent : EntityEventArgs
|
||||
{
|
||||
public EntityCoordinates Coordinates { get; init; }
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
@@ -11,6 +12,7 @@ using Content.Shared.Audio;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
@@ -18,6 +20,8 @@ namespace Content.Server.Tools
|
||||
{
|
||||
public partial class ToolSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
||||
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
|
||||
@@ -30,6 +34,7 @@ namespace Content.Server.Tools
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
InitializeTilePrying();
|
||||
InitializeWelders();
|
||||
InitializeMultipleTools();
|
||||
|
||||
@@ -94,14 +99,24 @@ namespace Content.Server.Tools
|
||||
/// <param name="doAfterEventTarget">Where to direct the do-after events. If null, events are broadcast</param>
|
||||
/// <param name="doAfterCheck">An optional check to perform for the doAfter.</param>
|
||||
/// <param name="toolComponent">The tool component.</param>
|
||||
/// <param name="cancelToken">Token to provide to do_after for cancelling</param>
|
||||
/// <returns>Whether initially, using the tool succeeded. If there's a doAfter delay, you'll need to listen to
|
||||
/// the <see cref="doAfterCompleteEvent"/> and <see cref="doAfterCancelledEvent"/> being broadcast
|
||||
/// to see whether using the tool succeeded or not. If the <see cref="doAfterDelay"/> is zero,
|
||||
/// this simply returns whether using the tool succeeded or not.</returns>
|
||||
public bool UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
|
||||
float doAfterDelay, IEnumerable<string> toolQualitiesNeeded,
|
||||
object? doAfterCompleteEvent = null, object? doAfterCancelledEvent = null, EntityUid? doAfterEventTarget = null,
|
||||
Func<bool>? doAfterCheck = null, ToolComponent? toolComponent = null)
|
||||
public bool UseTool(
|
||||
EntityUid tool,
|
||||
EntityUid user,
|
||||
EntityUid? target,
|
||||
float fuel,
|
||||
float doAfterDelay,
|
||||
IEnumerable<string> toolQualitiesNeeded,
|
||||
object? doAfterCompleteEvent = null,
|
||||
object? doAfterCancelledEvent = null,
|
||||
EntityUid? doAfterEventTarget = null,
|
||||
Func<bool>? doAfterCheck = null,
|
||||
ToolComponent? toolComponent = null,
|
||||
CancellationToken? cancelToken = null)
|
||||
{
|
||||
// No logging here, after all that'd mean the caller would need to check if the component is there or not.
|
||||
if (!Resolve(tool, ref toolComponent, false))
|
||||
@@ -112,7 +127,7 @@ namespace Content.Server.Tools
|
||||
|
||||
if (doAfterDelay > 0f)
|
||||
{
|
||||
var doAfterArgs = new DoAfterEventArgs(user, doAfterDelay / toolComponent.SpeedModifier, default, target)
|
||||
var doAfterArgs = new DoAfterEventArgs(user, doAfterDelay / toolComponent.SpeedModifier, cancelToken ?? default, target)
|
||||
{
|
||||
ExtraCheck = doAfterCheck,
|
||||
BreakOnDamage = true,
|
||||
|
||||
Reference in New Issue
Block a user