Files
crystall-punk-14/Content.Shared/Placeable/PlaceableSurfaceSystem.cs

91 lines
3.0 KiB
C#
Raw Permalink Normal View History

using System.Numerics;
2022-03-17 20:13:31 +13:00
using Content.Shared.Hands.EntitySystems;
2021-08-20 10:21:39 +02:00
using Content.Shared.Interaction;
using Content.Shared.Storage;
using Content.Shared.Storage.Components;
2021-08-20 10:21:39 +02:00
namespace Content.Shared.Placeable;
public sealed class PlaceableSurfaceSystem : EntitySystem
2021-08-20 10:21:39 +02:00
{
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
2022-03-17 20:13:31 +13:00
public override void Initialize()
{
base.Initialize();
2021-08-20 10:21:39 +02:00
SubscribeLocalEvent<PlaceableSurfaceComponent, AfterInteractUsingEvent>(OnAfterInteractUsing);
SubscribeLocalEvent<PlaceableSurfaceComponent, StorageInteractUsingAttemptEvent>(OnStorageInteractUsingAttempt);
SubscribeLocalEvent<PlaceableSurfaceComponent, StorageAfterOpenEvent>(OnStorageAfterOpen);
SubscribeLocalEvent<PlaceableSurfaceComponent, StorageAfterCloseEvent>(OnStorageAfterClose);
}
2022-05-08 15:50:31 +10:00
public void SetPlaceable(EntityUid uid, bool isPlaceable, PlaceableSurfaceComponent? surface = null)
{
if (!Resolve(uid, ref surface, false))
return;
2021-10-06 11:40:05 +02:00
if (surface.IsPlaceable == isPlaceable)
return;
surface.IsPlaceable = isPlaceable;
Dirty(uid, surface);
}
2021-08-20 10:21:39 +02:00
public void SetPlaceCentered(EntityUid uid, bool placeCentered, PlaceableSurfaceComponent? surface = null)
{
if (!Resolve(uid, ref surface))
return;
2021-10-06 11:40:05 +02:00
surface.PlaceCentered = placeCentered;
Dirty(uid, surface);
}
2021-08-20 10:21:39 +02:00
public void SetPositionOffset(EntityUid uid, Vector2 offset, PlaceableSurfaceComponent? surface = null)
{
if (!Resolve(uid, ref surface))
return;
2021-10-06 11:40:05 +02:00
surface.PositionOffset = offset;
Dirty(uid, surface);
}
2021-08-20 10:21:39 +02:00
private void OnAfterInteractUsing(EntityUid uid, PlaceableSurfaceComponent surface, AfterInteractUsingEvent args)
{
if (args.Handled || !args.CanReach)
return;
2021-08-20 10:21:39 +02:00
if (!surface.IsPlaceable)
return;
2021-08-20 10:21:39 +02:00
// 99% of the time they want to dump the stuff inside on the table, they can manually place with q if they really need to.
// Just causes prediction CBT otherwise.
if (HasComp<DumpableComponent>(args.Used))
return;
if (!_handsSystem.TryDrop(args.User, args.Used))
return;
2021-08-20 10:21:39 +02:00
_transformSystem.SetCoordinates(args.Used,
surface.PlaceCentered ? Transform(uid).Coordinates.Offset(surface.PositionOffset) : args.ClickLocation);
2021-08-20 10:21:39 +02:00
args.Handled = true;
2021-08-20 10:21:39 +02:00
}
private void OnStorageInteractUsingAttempt(Entity<PlaceableSurfaceComponent> ent, ref StorageInteractUsingAttemptEvent args)
{
args.Cancelled = true;
}
private void OnStorageAfterOpen(Entity<PlaceableSurfaceComponent> ent, ref StorageAfterOpenEvent args)
{
SetPlaceable(ent.Owner, true, ent.Comp);
}
private void OnStorageAfterClose(Entity<PlaceableSurfaceComponent> ent, ref StorageAfterCloseEvent args)
{
SetPlaceable(ent.Owner, false, ent.Comp);
}
2021-08-20 10:21:39 +02:00
}