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

116 lines
3.9 KiB
C#
Raw 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;
using Robust.Shared.Random;
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 IRobustRandom _random = default!;
[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);
SubscribeLocalEvent<PlaceableSurfaceComponent, GetDumpableVerbEvent>(OnGetDumpableVerb);
SubscribeLocalEvent<PlaceableSurfaceComponent, DumpEvent>(OnDump);
}
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);
}
private void OnGetDumpableVerb(Entity<PlaceableSurfaceComponent> ent, ref GetDumpableVerbEvent args)
{
args.Verb = Loc.GetString("dump-placeable-verb-name", ("surface", ent));
}
private void OnDump(Entity<PlaceableSurfaceComponent> ent, ref DumpEvent args)
{
if (args.Handled)
return;
args.Handled = true;
args.PlaySound = true;
var (targetPos, targetRot) = _transformSystem.GetWorldPositionRotation(ent);
foreach (var entity in args.DumpQueue)
{
_transformSystem.SetWorldPositionRotation(entity, targetPos + _random.NextVector2Box() / 4, targetRot);
}
}
2021-08-20 10:21:39 +02:00
}