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

77 lines
2.5 KiB
C#
Raw Normal View History

2021-08-20 10:21:39 +02:00
using Content.Shared.Hands.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);
2021-08-20 10:21:39 +02:00
SubscribeLocalEvent<PlaceableSurfaceComponent, ComponentHandleState>(OnHandleState);
}
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
{
2021-10-06 11:40:05 +02:00
if (!Resolve(uid, ref surface))
return;
2021-08-20 10:21:39 +02:00
surface.IsPlaceable = isPlaceable;
surface.Dirty();
}
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;
surface.Dirty();
}
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;
surface.Dirty();
}
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;
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;
}
}
}