Fix foldable-pickup interactions. (#6057)
This commit is contained in:
35
Content.Shared/Foldable/FoldableComponent.cs
Normal file
35
Content.Shared/Foldable/FoldableComponent.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using System;
|
||||
|
||||
namespace Content.Shared.Foldable;
|
||||
|
||||
/// <summary>
|
||||
/// Used to create "foldable structures" that you can pickup like an item when folded. Used for rollerbeds and wheelchairs
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[NetworkedComponent]
|
||||
[Friend(typeof(SharedFoldableSystem))]
|
||||
public class FoldableComponent : Component
|
||||
{
|
||||
public override string Name => "Foldable";
|
||||
|
||||
[DataField("folded")]
|
||||
public bool IsFolded = false;
|
||||
}
|
||||
|
||||
// ahhh, the ol' "state thats just a copy of the component".
|
||||
[Serializable, NetSerializable]
|
||||
public class FoldableComponentState : ComponentState
|
||||
{
|
||||
public readonly bool IsFolded;
|
||||
|
||||
public FoldableComponentState(bool isFolded)
|
||||
{
|
||||
IsFolded = isFolded;
|
||||
}
|
||||
}
|
||||
62
Content.Shared/Foldable/SharedFoldableSystem.cs
Normal file
62
Content.Shared/Foldable/SharedFoldableSystem.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Content.Shared.Item;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Foldable;
|
||||
|
||||
[UsedImplicitly]
|
||||
public abstract class SharedFoldableSystem : EntitySystem
|
||||
{
|
||||
private const string FoldKey = "FoldedState";
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<FoldableComponent, ComponentGetState>(OnGetState);
|
||||
SubscribeLocalEvent<FoldableComponent, ComponentHandleState>(OnHandleState);
|
||||
|
||||
SubscribeLocalEvent<FoldableComponent, ComponentInit>(OnFoldableInit);
|
||||
SubscribeLocalEvent<FoldableComponent, AttemptItemPickupEvent>(OnPickedUpAttempt);
|
||||
}
|
||||
|
||||
private void OnGetState(EntityUid uid, FoldableComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new FoldableComponentState(component.IsFolded);
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, FoldableComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not FoldableComponentState state)
|
||||
return;
|
||||
|
||||
if (state.IsFolded != component.IsFolded)
|
||||
SetFolded(component, state.IsFolded);
|
||||
}
|
||||
|
||||
private void OnFoldableInit(EntityUid uid, FoldableComponent component, ComponentInit args)
|
||||
{
|
||||
SetFolded(component, component.IsFolded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the folded state of the given <see cref="FoldableComponent"/>
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
/// <param name="folded">If true, the component will become folded, else unfolded</param>
|
||||
public virtual void SetFolded(FoldableComponent component, bool folded)
|
||||
{
|
||||
component.IsFolded = folded;
|
||||
component.Dirty();
|
||||
|
||||
if (TryComp(component.Owner, out AppearanceComponent? appearance))
|
||||
appearance.SetData(FoldKey, folded);
|
||||
}
|
||||
|
||||
private void OnPickedUpAttempt(EntityUid uid, FoldableComponent component, AttemptItemPickupEvent args)
|
||||
{
|
||||
if (!component.IsFolded)
|
||||
args.Cancel();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user