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

80 lines
2.7 KiB
C#
Raw Normal View History

2021-08-20 10:21:39 +02:00
using Content.Shared.Hands.Components;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
2021-12-03 11:11:52 +01:00
using Robust.Shared.IoC;
2021-08-20 10:21:39 +02:00
using Robust.Shared.Maths;
namespace Content.Shared.Placeable
{
public class PlaceableSurfaceSystem : EntitySystem
{
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;
if(!EntityManager.TryGetComponent<SharedHandsComponent?>(args.User, out var handComponent))
2021-08-20 10:21:39 +02:00
return;
if(!handComponent.TryDropEntity(args.Used, EntityManager.GetComponent<TransformComponent>(surface.Owner).Coordinates))
2021-08-20 10:21:39 +02:00
return;
if (surface.PlaceCentered)
EntityManager.GetComponent<TransformComponent>(args.Used).LocalPosition = EntityManager.GetComponent<TransformComponent>(uid).LocalPosition + surface.PositionOffset;
2021-08-20 10:21:39 +02:00
else
EntityManager.GetComponent<TransformComponent>(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;
}
}
}