Fix foldable-pickup interactions. (#6057)

This commit is contained in:
Leon Friedrich
2022-01-07 19:09:42 +13:00
committed by GitHub
parent 106f176d13
commit c29489ff4d
10 changed files with 145 additions and 106 deletions

View 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;
}
}

View 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();
}
}

View File

@@ -14,6 +14,7 @@ using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -497,11 +498,14 @@ namespace Content.Shared.Hands.Components
/// </summary>
protected bool CanInsertEntityIntoHand(Hand hand, EntityUid entity)
{
var handContainer = hand.Container;
if (handContainer == null) return false;
if (!_entMan.HasComponent<SharedItemComponent>(entity))
return false;
var handContainer = hand.Container;
if (handContainer == null) return false;
if (_entMan.TryGetComponent(entity, out IPhysBody? physics) && physics.BodyType == BodyType.Static)
return false;
if (!handContainer.CanInsert(entity)) return false;

View File

@@ -81,9 +81,6 @@ namespace Content.Shared.Hands
return;
}
if (TryComp(entity, out SharedSpriteComponent? component))
component.Visible = true;
hands.Dirty();
var unequippedHandMessage = new UnequippedHandEvent(uid, entity, hand);
@@ -115,9 +112,6 @@ namespace Content.Shared.Hands
_adminLogSystem.Add(LogType.Pickup, LogImpact.Low, $"{ToPrettyString(uid):user} picked up {ToPrettyString(entity):entity}");
if (TryComp(entity, out SharedSpriteComponent? component))
component.Visible = false;
hands.Dirty();
var equippedHandMessage = new EquippedHandEvent(uid, entity, hand);

View File

@@ -53,11 +53,11 @@ namespace Content.Shared.Item
args.Using != null ||
!args.CanAccess ||
!args.CanInteract ||
!component.CanPickup(args.User, popup: false))
!args.Hands.CanPickupEntityToActiveHand(args.Target))
return;
Verb verb = new();
verb.Act = () => args.Hands.PutInHand(args.Target);
verb.Act = () => args.Hands.TryPickupEntityToActiveHand(args.Target);
verb.IconTexture = "/Textures/Interface/VerbIcons/pickup.svg.192dpi.png";
// if the item already in a container (that is not the same as the user's), then change the text.

View File

@@ -1,5 +1,4 @@
using System;
using Content.Shared.ActionBlocker;
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Interaction;
@@ -11,7 +10,6 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
@@ -101,28 +99,11 @@ namespace Content.Shared.Item
[DataField("sprite")]
private string? _rsiPath;
/// <summary>
/// If a player can pick up this item.
/// </summary>
public bool CanPickup(EntityUid user, bool popup = true)
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanPickup(user))
return false;
if (_entMan.GetComponent<TransformComponent>(user).MapID != _entMan.GetComponent<TransformComponent>(Owner).MapID)
return false;
if (!_entMan.TryGetComponent(Owner, out IPhysBody? physics) || physics.BodyType == BodyType.Static)
return false;
return user.InRangeUnobstructed(Owner, ignoreInsideBlocker: true, popup: popup);
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
var user = eventArgs.User;
if (!CanPickup(user))
if (!user.InRangeUnobstructed(Owner, ignoreInsideBlocker: true))
return false;
if (!_entMan.TryGetComponent(user, out SharedHandsComponent hands))
@@ -133,8 +114,8 @@ namespace Content.Shared.Item
if (activeHand == null)
return false;
hands.TryPickupEntityToActiveHand(Owner, animateUser: true);
return true;
// hands checks action blockers
return hands.TryPickupEntityToActiveHand(Owner, animateUser: true);
}
private void OnEquippedPrefixChange()