SharedItemComponent (#3513)

* StorableComponent refactor

* ItemComponent refactor

* conflict fixes

* removes redundant null check

* removes redundant item throwing code

* fix conflicts

* ExplosionLaunchedComponent

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
collinlunn
2021-04-02 04:01:03 -06:00
committed by GitHub
parent d9781c6f1c
commit f7aba5f701
21 changed files with 284 additions and 361 deletions

View File

@@ -1,46 +1,23 @@
#nullable enable
using Content.Server.GameObjects.Components.GUI;
using Content.Server.Interfaces.GameObjects.Components.Items;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Items;
using Content.Shared.GameObjects.Components.Storage;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.GameObjects.EntitySystems.ActionBlocker;
using Content.Shared.GameObjects.Verbs;
using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Utility;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Physics;
using Robust.Shared.Players;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.GameObjects.Components.Items.Storage
{
[RegisterComponent]
[ComponentReference(typeof(StorableComponent))]
[ComponentReference(typeof(SharedStorableComponent))]
[ComponentReference(typeof(IItemComponent))]
public class ItemComponent : StorableComponent, IInteractHand, IExAct, IEquipped, IUnequipped, IItemComponent
[ComponentReference(typeof(SharedItemComponent))]
public class ItemComponent : SharedItemComponent
{
public override string Name => "Item";
public override uint? NetID => ContentNetIDs.ITEM;
[DataField("HeldPrefix")]
private string? _equippedPrefix;
public string? EquippedPrefix
{
get => _equippedPrefix;
set
{
_equippedPrefix = value;
Dirty();
}
}
public void RemovedFromSlot()
public override void RemovedFromSlot()
{
foreach (var component in Owner.GetAllComponents<ISpriteRenderableComponent>())
{
@@ -48,7 +25,7 @@ namespace Content.Server.GameObjects.Components.Items.Storage
}
}
public void EquippedToSlot()
public override void EquippedToSlot()
{
foreach (var component in Owner.GetAllComponents<ISpriteRenderableComponent>())
{
@@ -56,44 +33,20 @@ namespace Content.Server.GameObjects.Components.Items.Storage
}
}
public virtual void Equipped(EquippedEventArgs eventArgs)
public override bool TryPutInHand(IEntity user)
{
EquippedToSlot();
}
public virtual void Unequipped(UnequippedEventArgs eventArgs)
{
RemovedFromSlot();
}
public bool CanPickup(IEntity user)
{
if (!ActionBlockerSystem.CanPickup(user))
{
if (!CanPickup(user))
return false;
}
if (user.Transform.MapID != Owner.Transform.MapID)
{
if (!user.TryGetComponent(out IHandsComponent? hands))
return false;
}
if (Owner.TryGetComponent(out IPhysBody? physics) &&
physics.BodyType == BodyType.Static)
{
var activeHand = hands.ActiveHand;
if (activeHand == null)
return false;
}
return user.InRangeUnobstructed(Owner, ignoreInsideBlocker: true, popup: true);
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
if (!CanPickup(eventArgs.User) ||
!eventArgs.User.TryGetComponent(out IHandsComponent? hands) ||
hands.ActiveHand == null) return false;
hands.PutInHand(this, hands.ActiveHand, false);
hands.PutInHand(this, activeHand, false);
return true;
}
@@ -110,7 +63,7 @@ namespace Content.Server.GameObjects.Components.Items.Storage
return;
}
data.Text = Loc.GetString("Pick up");
data.Text = Loc.GetString("Pick Up");
}
protected override void Activate(IEntity user, ItemComponent component)
@@ -121,34 +74,5 @@ namespace Content.Server.GameObjects.Components.Items.Storage
}
}
}
public override ComponentState GetComponentState(ICommonSession session)
{
return new ItemComponentState(EquippedPrefix);
}
public void OnExplosion(ExplosionEventArgs eventArgs)
{
var sourceLocation = eventArgs.Source;
var targetLocation = eventArgs.Target.Transform.Coordinates;
var dirVec = (targetLocation.ToMapPos(Owner.EntityManager) - sourceLocation.ToMapPos(Owner.EntityManager)).Normalized;
float throwForce;
switch (eventArgs.Severity)
{
case ExplosionSeverity.Destruction:
throwForce = 30.0f;
break;
case ExplosionSeverity.Heavy:
throwForce = 20.0f;
break;
default:
throwForce = 10.0f;
break;
}
Owner.TryThrow(dirVec * throwForce);
}
}
}