Makes PlaceableSurface ECS
This commit is contained in:
51
Content.Shared/Placeable/PlaceableSurfaceComponent.cs
Normal file
51
Content.Shared/Placeable/PlaceableSurfaceComponent.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.Placeable
|
||||
{
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
[Friend(typeof(PlaceableSurfaceSystem))]
|
||||
public class PlaceableSurfaceComponent : Component
|
||||
{
|
||||
public override string Name => "PlaceableSurface";
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("isPlaceable")]
|
||||
public bool IsPlaceable { get; set; } = true;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("placeCentered")]
|
||||
public bool PlaceCentered { get; set; }
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("positionOffset")]
|
||||
public Vector2 PositionOffset { get; set; }
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession session)
|
||||
{
|
||||
return new PlaceableSurfaceComponentState(IsPlaceable,PlaceCentered, PositionOffset);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public class PlaceableSurfaceComponentState : ComponentState
|
||||
{
|
||||
public readonly bool IsPlaceable;
|
||||
public readonly bool PlaceCentered;
|
||||
public readonly Vector2 PositionOffset;
|
||||
|
||||
public PlaceableSurfaceComponentState(bool placeable, bool centered, Vector2 offset)
|
||||
{
|
||||
IsPlaceable = placeable;
|
||||
PlaceCentered = centered;
|
||||
PositionOffset = offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
69
Content.Shared/Placeable/PlaceableSurfaceSystem.cs
Normal file
69
Content.Shared/Placeable/PlaceableSurfaceSystem.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
namespace Content.Shared.Placeable
|
||||
{
|
||||
public class PlaceableSurfaceSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<PlaceableSurfaceComponent, InteractUsingEvent>(OnInteractUsing);
|
||||
SubscribeLocalEvent<PlaceableSurfaceComponent, ComponentHandleState>(OnHandleState);
|
||||
}
|
||||
|
||||
public void SetPlaceable(PlaceableSurfaceComponent surface, bool isPlaceable)
|
||||
{
|
||||
surface.IsPlaceable = isPlaceable;
|
||||
surface.Dirty();
|
||||
}
|
||||
|
||||
public void SetPlaceCentered(PlaceableSurfaceComponent surface, bool placeCentered)
|
||||
{
|
||||
surface.PlaceCentered = placeCentered;
|
||||
surface.Dirty();
|
||||
}
|
||||
|
||||
public void SetPositionOffset(PlaceableSurfaceComponent surface, Vector2 offset)
|
||||
{
|
||||
surface.PositionOffset = offset;
|
||||
surface.Dirty();
|
||||
}
|
||||
|
||||
private void OnInteractUsing(EntityUid uid, PlaceableSurfaceComponent surface, InteractUsingEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
if (!surface.IsPlaceable)
|
||||
return;
|
||||
|
||||
if(!args.User.TryGetComponent<SharedHandsComponent>(out var handComponent))
|
||||
return;
|
||||
|
||||
if(!handComponent.TryDropEntity(args.Used, surface.Owner.Transform.Coordinates))
|
||||
return;
|
||||
|
||||
if (surface.PlaceCentered)
|
||||
args.Used.Transform.LocalPosition = args.Target.Transform.LocalPosition + surface.PositionOffset;
|
||||
else
|
||||
args.Used.Transform.Coordinates = args.ClickLocation;
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, PlaceableSurfaceComponent component, ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not PlaceableSurfaceComponentState state)
|
||||
return;
|
||||
|
||||
component.IsPlaceable = state.IsPlaceable;
|
||||
component.PlaceCentered = state.PlaceCentered;
|
||||
component.PositionOffset = state.PositionOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
using System;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Placeable
|
||||
{
|
||||
[NetworkedComponent()]
|
||||
public abstract class SharedPlaceableSurfaceComponent : Component
|
||||
{
|
||||
public override string Name => "PlaceableSurface";
|
||||
public virtual bool IsPlaceable { get; set; }
|
||||
public virtual bool PlaceCentered { get; set; }
|
||||
public virtual Vector2 PositionOffset { get; set; }
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public class PlaceableSurfaceComponentState : ComponentState
|
||||
{
|
||||
public readonly bool IsPlaceable;
|
||||
public readonly bool PlaceCentered;
|
||||
public readonly Vector2 PositionOffset;
|
||||
|
||||
public PlaceableSurfaceComponentState(bool placeable, bool centered, Vector2 offset)
|
||||
{
|
||||
IsPlaceable = placeable;
|
||||
PlaceCentered = centered;
|
||||
PositionOffset = offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user