Files
OldThink/Content.Server/Tools/ToolSystem.TilePrying.cs

97 lines
3.3 KiB
C#
Raw Normal View History

2022-01-30 14:48:18 +11:00
using System.Threading;
using Content.Server.Fluids.Components;
2022-01-30 14:48:18 +11:00
using Content.Server.Tools.Components;
using Content.Shared.Interaction;
using Content.Shared.Maps;
using Content.Shared.Tools.Components;
using Robust.Shared.Audio;
2022-01-30 14:48:18 +11:00
using Robust.Shared.Map;
using Robust.Shared.Player;
2022-06-20 12:14:35 +12:00
using Robust.Shared.Utility;
2022-01-30 14:48:18 +11:00
namespace Content.Server.Tools;
public sealed partial class ToolSystem
{
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
2022-01-30 14:48:18 +11:00
private void InitializeTilePrying()
{
SubscribeLocalEvent<TilePryingComponent, AfterInteractEvent>(OnTilePryingAfterInteract);
SubscribeLocalEvent<TilePryingComponent, TilePryingCompleteEvent>(OnTilePryComplete);
SubscribeLocalEvent<TilePryingComponent, TilePryingCancelledEvent>(OnTilePryCancelled);
}
private void OnTilePryingAfterInteract(EntityUid uid, TilePryingComponent component, AfterInteractEvent args)
{
if (args.Handled || !args.CanReach || (args.Target != null && !HasComp<PuddleComponent>(args.Target))) return;
if (TryPryTile(uid, args.User, component, args.ClickLocation))
args.Handled = true;
2022-01-30 14:48:18 +11:00
}
private void OnTilePryComplete(EntityUid uid, TilePryingComponent component, TilePryingCompleteEvent args)
{
component.CancelToken = null;
2022-06-20 12:14:35 +12:00
var gridUid = args.Coordinates.GetGridUid(EntityManager);
if (!_mapManager.TryGetGrid(gridUid, out var grid))
{
Logger.Error("Attempted to pry from a non-existent grid?");
return;
}
2023-01-07 04:24:50 -06:00
var tile = grid.GetTileRef(args.Coordinates);
_tile.PryTile(tile);
2022-01-30 14:48:18 +11:00
}
private void OnTilePryCancelled(EntityUid uid, TilePryingComponent component, TilePryingCancelledEvent args)
2022-01-30 14:48:18 +11:00
{
component.CancelToken = null;
2022-01-30 14:48:18 +11:00
}
private bool TryPryTile(EntityUid toolEntity, EntityUid user, TilePryingComponent component, EntityCoordinates clickLocation)
2022-01-30 14:48:18 +11:00
{
if (!TryComp<ToolComponent?>(toolEntity, out var tool) && component.ToolComponentNeeded || component.CancelToken != null)
2022-01-30 14:48:18 +11:00
return false;
2022-06-20 12:14:35 +12:00
if (!_mapManager.TryGetGrid(clickLocation.GetGridUid(EntityManager), out var mapGrid))
2022-01-30 14:48:18 +11:00
return false;
var tile = mapGrid.GetTileRef(clickLocation);
var coordinates = mapGrid.GridTileToLocal(tile.GridIndices);
if (!_interactionSystem.InRangeUnobstructed(user, coordinates, popup: false))
2022-01-30 14:48:18 +11:00
return false;
var tileDef = (ContentTileDefinition)_tileDefinitionManager[tile.Tile.TypeId];
if (!tileDef.CanCrowbar)
return false;
component.CancelToken = new CancellationTokenSource();
var toolEvData = new ToolEventData(new TilePryingCompleteEvent(clickLocation), cancelledEv:new TilePryingCancelledEvent() ,targetEntity:toolEntity);
if (!UseTool(toolEntity, user, null, component.Delay, new[] { component.QualityNeeded }, toolEvData, toolComponent: tool, cancelToken: component.CancelToken))
return false;
2022-01-30 14:48:18 +11:00
return true;
}
private sealed class TilePryingCompleteEvent : EntityEventArgs
{
public readonly EntityCoordinates Coordinates;
public TilePryingCompleteEvent(EntityCoordinates coordinates)
{
Coordinates = coordinates;
}
2022-01-30 14:48:18 +11:00
}
2022-05-10 19:48:59 +10:00
private sealed class TilePryingCancelledEvent : EntityEventArgs
{
}
2022-01-30 14:48:18 +11:00
}