Files
OldThink/Content.Shared/Placeable/PlaceableSurfaceSystem.cs

88 lines
3.1 KiB
C#
Raw Normal View History

using Content.Shared.Storage.Components;
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 Robust.Shared.GameStates;
namespace Content.Shared.Placeable
{
public sealed class PlaceableSurfaceSystem : EntitySystem
2021-08-20 10:21:39 +02:00
{
2022-03-17 20:13:31 +13:00
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
2021-08-20 10:21:39 +02:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PlaceableSurfaceComponent, AfterInteractUsingEvent>(OnAfterInteractUsing);
2022-05-08 15:50:31 +10:00
SubscribeLocalEvent<PlaceableSurfaceComponent, ComponentGetState>(OnGetState);
2021-08-20 10:21:39 +02:00
SubscribeLocalEvent<PlaceableSurfaceComponent, ComponentHandleState>(OnHandleState);
}
2022-05-08 15:50:31 +10:00
private void OnGetState(EntityUid uid, PlaceableSurfaceComponent component, ref ComponentGetState args)
{
args.State = new PlaceableSurfaceComponentState(component.IsPlaceable, component.PlaceCentered, component.PositionOffset);
}
2021-10-06 11:40:05 +02:00
public void SetPlaceable(EntityUid uid, bool isPlaceable, PlaceableSurfaceComponent? surface = null)
2021-08-20 10:21:39 +02:00
{
if (!Resolve(uid, ref surface, false))
2021-10-06 11:40:05 +02:00
return;
2021-08-20 10:21:39 +02:00
surface.IsPlaceable = isPlaceable;
2022-05-08 15:50:31 +10:00
Dirty(surface);
2021-08-20 10:21:39 +02:00
}
2021-10-06 11:40:05 +02:00
public void SetPlaceCentered(EntityUid uid, bool placeCentered, PlaceableSurfaceComponent? surface = null)
2021-08-20 10:21:39 +02:00
{
2021-10-06 11:40:05 +02:00
if (!Resolve(uid, ref surface))
return;
2021-08-20 10:21:39 +02:00
surface.PlaceCentered = placeCentered;
2022-05-08 15:50:31 +10:00
Dirty(surface);
2021-08-20 10:21:39 +02:00
}
2021-10-06 11:40:05 +02:00
public void SetPositionOffset(EntityUid uid, Vector2 offset, PlaceableSurfaceComponent? surface = null)
2021-08-20 10:21:39 +02:00
{
2021-10-06 11:40:05 +02:00
if (!Resolve(uid, ref surface))
return;
2021-08-20 10:21:39 +02:00
surface.PositionOffset = offset;
2022-05-08 15:50:31 +10:00
Dirty(surface);
2021-08-20 10:21:39 +02:00
}
private void OnAfterInteractUsing(EntityUid uid, PlaceableSurfaceComponent surface, AfterInteractUsingEvent args)
2021-08-20 10:21:39 +02:00
{
if (args.Handled || !args.CanReach)
2021-08-20 10:21:39 +02:00
return;
if (!surface.IsPlaceable)
return;
// 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;
2022-03-17 20:13:31 +13:00
if (!_handsSystem.TryDrop(args.User, args.Used))
2021-08-20 10:21:39 +02:00
return;
if (surface.PlaceCentered)
2022-03-17 20:13:31 +13:00
Transform(args.Used).LocalPosition = Transform(uid).LocalPosition + surface.PositionOffset;
2021-08-20 10:21:39 +02:00
else
2022-03-17 20:13:31 +13:00
Transform(args.Used).Coordinates = args.ClickLocation;
2021-08-20 10:21:39 +02:00
args.Handled = true;
}
private void OnHandleState(EntityUid uid, PlaceableSurfaceComponent component, ref ComponentHandleState args)
2021-08-20 10:21:39 +02:00
{
if (args.Current is not PlaceableSurfaceComponentState state)
return;
component.IsPlaceable = state.IsPlaceable;
component.PlaceCentered = state.PlaceCentered;
component.PositionOffset = state.PositionOffset;
}
}
}