Теперь предметы можно передавать из рук в руки (#462)

* add: Теперь предеты можно передовать из рук в руки

* Кудато не туда уехало
This commit is contained in:
Spatison
2024-07-18 01:33:28 +03:00
committed by GitHub
parent 8cfb81bbac
commit d32eb158d4
27 changed files with 579 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
using Robust.Shared.GameStates;
namespace Content.Shared._White.OfferItem;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
[Access(typeof(SharedOfferItemSystem))]
public sealed partial class OfferItemComponent : Component
{
[ViewVariables(VVAccess.ReadWrite), DataField, AutoNetworkedField]
public bool IsInOfferMode;
[DataField, AutoNetworkedField]
public bool IsInReceiveMode;
[DataField, AutoNetworkedField]
public string? Hand;
[DataField, AutoNetworkedField]
public EntityUid? Item;
[DataField, AutoNetworkedField]
public EntityUid? Target;
[DataField]
public float MaxOfferDistance = 2f;
}

View File

@@ -0,0 +1,74 @@
using Content.Shared.Popups;
using Content.Shared.ActionBlocker;
using Content.Shared.Input;
using Content.Shared.Hands.Components;
using Robust.Shared.Input.Binding;
using Robust.Shared.Player;
namespace Content.Shared._White.OfferItem;
public abstract partial class SharedOfferItemSystem
{
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
private void InitializeInteractions()
{
CommandBinds.Builder
.Bind(ContentKeyFunctions.OfferItem, InputCmdHandler.FromDelegate(SetInOfferMode, handle: false, outsidePrediction: false))
.Register<SharedOfferItemSystem>();
}
public override void Shutdown()
{
base.Shutdown();
CommandBinds.Unregister<SharedOfferItemSystem>();
}
private void SetInOfferMode(ICommonSession? session)
{
if (session is not { } playerSession)
return;
if ((playerSession.AttachedEntity is not { Valid: true } uid || !Exists(uid)) ||
!_actionBlocker.CanInteract(uid, null))
return;
if (!TryComp<OfferItemComponent>(uid, out var offerItem))
return;
if (!TryComp<HandsComponent>(uid, out var hands) || hands.ActiveHand == null)
return;
offerItem.Item = hands.ActiveHand.HeldEntity;
if (offerItem.IsInOfferMode == false)
{
if (offerItem.Item == null)
{
_popup.PopupEntity(Loc.GetString("offer-item-empty-hand"), uid, uid);
return;
}
if (offerItem.Hand == null || offerItem.Target == null)
{
offerItem.IsInOfferMode = true;
offerItem.Hand = hands.ActiveHand.Name;
Dirty(uid, offerItem);
return;
}
}
if (offerItem.Target != null)
{
UnReceive(offerItem.Target.Value, offerItem: offerItem);
offerItem.IsInOfferMode = false;
Dirty(uid, offerItem);
return;
}
UnOffer(uid, offerItem);
}
}

View File

@@ -0,0 +1,158 @@
using Content.Shared.Interaction;
using Content.Shared.IdentityManagement;
using Content.Shared.Hands.Components;
namespace Content.Shared._White.OfferItem;
public abstract partial class SharedOfferItemSystem : EntitySystem
{
[Dependency] private readonly SharedTransformSystem _transform = default!;
public override void Initialize()
{
SubscribeLocalEvent<OfferItemComponent, InteractUsingEvent>(SetInReceiveMode);
SubscribeLocalEvent<OfferItemComponent, MoveEvent>(OnMove);
InitializeInteractions();
}
private void SetInReceiveMode(EntityUid uid, OfferItemComponent component, InteractUsingEvent args)
{
if (!TryComp<OfferItemComponent>(args.User, out var offerItem))
return;
if (args.User == uid || component.IsInReceiveMode || !offerItem.IsInOfferMode ||
(offerItem.IsInReceiveMode && offerItem.Target != uid))
return;
component.IsInReceiveMode = true;
component.Target = args.User;
Dirty(uid, component);
offerItem.Target = uid;
offerItem.IsInOfferMode = false;
Dirty(args.User, offerItem);
if (offerItem.Item == null)
return;
_popup.PopupEntity(Loc.GetString("offer-item-try-give",
("item", Identity.Entity(offerItem.Item.Value, EntityManager)),
("target", Identity.Entity(uid, EntityManager))), component.Target.Value, component.Target.Value);
_popup.PopupEntity(Loc.GetString("offer-item-try-give-target",
("user", Identity.Entity(component.Target.Value, EntityManager)),
("item", Identity.Entity(offerItem.Item.Value, EntityManager))), component.Target.Value, uid);
args.Handled = true;
}
private void OnMove(EntityUid uid, OfferItemComponent component, MoveEvent args)
{
if (component.Target == null ||
args.NewPosition.InRange(EntityManager, _transform,
Transform(component.Target.Value).Coordinates, component.MaxOfferDistance))
return;
UnOffer(uid, component);
}
/// <summary>
/// Resets the <see cref="OfferItemComponent"/> of the user and the target
/// </summary>
protected void UnOffer(EntityUid uid, OfferItemComponent component)
{
if (!TryComp<HandsComponent>(uid, out var hands) || hands.ActiveHand == null)
return;
if (TryComp<OfferItemComponent>(component.Target, out var offerItem) && component.Target != null)
{
if (component.Item != null)
{
_popup.PopupEntity(Loc.GetString("offer-item-no-give",
("item", Identity.Entity(component.Item.Value, EntityManager)),
("target", Identity.Entity(component.Target.Value, EntityManager))), uid, uid);
_popup.PopupEntity(Loc.GetString("offer-item-no-give-target",
("user", Identity.Entity(uid, EntityManager)),
("item", Identity.Entity(component.Item.Value, EntityManager))), uid, component.Target.Value);
}
else if (offerItem.Item != null)
{
_popup.PopupEntity(Loc.GetString("offer-item-no-give",
("item", Identity.Entity(offerItem.Item.Value, EntityManager)),
("target", Identity.Entity(uid, EntityManager))), component.Target.Value, component.Target.Value);
_popup.PopupEntity(Loc.GetString("offer-item-no-give-target",
("user", Identity.Entity(component.Target.Value, EntityManager)),
("item", Identity.Entity(offerItem.Item.Value, EntityManager))), component.Target.Value, uid);
}
offerItem.IsInOfferMode = false;
offerItem.IsInReceiveMode = false;
offerItem.Hand = null;
offerItem.Target = null;
offerItem.Item = null;
Dirty(component.Target.Value, offerItem);
}
component.IsInOfferMode = false;
component.IsInReceiveMode = false;
component.Hand = null;
component.Target = null;
component.Item = null;
Dirty(uid, component);
}
/// <summary>
/// Cancels the transfer of the item
/// </summary>
protected void UnReceive(EntityUid uid, OfferItemComponent? component = null, OfferItemComponent? offerItem = null)
{
if (component == null && !TryComp(uid, out component))
return;
if (offerItem == null && !TryComp(component.Target, out offerItem))
return;
if (!TryComp<HandsComponent>(uid, out var hands) || hands.ActiveHand == null ||
component.Target == null)
return;
if (offerItem.Item != null)
{
_popup.PopupEntity(Loc.GetString("offer-item-no-give",
("item", Identity.Entity(offerItem.Item.Value, EntityManager)),
("target", Identity.Entity(uid, EntityManager))), component.Target.Value, component.Target.Value);
_popup.PopupEntity(Loc.GetString("offer-item-no-give-target",
("user", Identity.Entity(component.Target.Value, EntityManager)),
("item", Identity.Entity(offerItem.Item.Value, EntityManager))), component.Target.Value, uid);
}
if (!offerItem.IsInReceiveMode)
{
offerItem.Target = null;
component.Target = null;
}
offerItem.Item = null;
offerItem.Hand = null;
component.IsInReceiveMode = false;
Dirty(uid, component);
}
/// <summary>
/// Returns true if <see cref="OfferItemComponent.IsInOfferMode"/> = true
/// </summary>
protected bool IsInOfferMode(EntityUid? entity, OfferItemComponent? component = null)
{
return entity != null && Resolve(entity.Value, ref component, false) && component.IsInOfferMode;
}
}