Add IEquipped and IUnequipped interfaces (#837)

This commit is contained in:
Víctor Aguilera Puerto
2020-04-21 14:38:35 +02:00
committed by GitHub
parent d77dea6498
commit 3fc4725df7
3 changed files with 168 additions and 4 deletions

View File

@@ -110,7 +110,7 @@ namespace Content.Server.GameObjects
return false;
}
item.EquippedToSlot();
_entitySystemManager.GetEntitySystem<InteractionSystem>().EquippedInteraction(Owner, item.Owner, slot);
Dirty();
return true;
@@ -166,11 +166,12 @@ namespace Content.Server.GameObjects
return false;
}
item.RemovedFromSlot();
// TODO: The item should be dropped to the container our owner is in, if any.
var itemTransform = item.Owner.GetComponent<ITransformComponent>();
itemTransform.GridPosition = Owner.GetComponent<ITransformComponent>().GridPosition;
_entitySystemManager.GetEntitySystem<InteractionSystem>().UnequippedInteraction(Owner, item.Owner, slot);
Dirty();
return true;
}

View File

@@ -27,7 +27,7 @@ namespace Content.Server.GameObjects
{
[RegisterComponent]
[ComponentReference(typeof(StoreableComponent))]
public class ItemComponent : StoreableComponent, IAttackHand, IExAct
public class ItemComponent : StoreableComponent, IAttackHand, IExAct, IEquipped, IUnequipped
{
public override string Name => "Item";
public override uint? NetID => ContentNetIDs.ITEM;
@@ -69,6 +69,16 @@ namespace Content.Server.GameObjects
}
}
public void Equipped(EquippedEventArgs eventArgs)
{
EquippedToSlot();
}
public void Unequipped(UnequippedEventArgs eventArgs)
{
RemovedFromSlot();
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);

View File

@@ -3,6 +3,7 @@ using System.Linq;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.Components.Timing;
using Content.Server.Interfaces.GameObjects;
using Content.Shared.GameObjects.Components.Inventory;
using Content.Shared.Input;
using Content.Shared.Physics;
using JetBrains.Annotations;
@@ -168,6 +169,46 @@ namespace Content.Server.GameObjects.EntitySystems
public GridCoordinates LandingLocation { get; }
}
/// <summary>
/// This interface gives components behavior when their owner is put in an inventory slot.
/// </summary>
public interface IEquipped
{
void Equipped(EquippedEventArgs eventArgs);
}
public class EquippedEventArgs : EventArgs
{
public EquippedEventArgs(IEntity user, EquipmentSlotDefines.Slots slot)
{
User = user;
Slot = slot;
}
public IEntity User { get; }
public EquipmentSlotDefines.Slots Slot { get; }
}
/// <summary>
/// This interface gives components behavior when their owner is removed from an inventory slot.
/// </summary>
public interface IUnequipped
{
void Unequipped(UnequippedEventArgs eventArgs);
}
public class UnequippedEventArgs : EventArgs
{
public UnequippedEventArgs(IEntity user, EquipmentSlotDefines.Slots slot)
{
User = user;
Slot = slot;
}
public IEntity User { get; }
public EquipmentSlotDefines.Slots Slot { get; }
}
/// <summary>
/// This interface gives components behavior when being used to "attack".
/// </summary>
@@ -710,6 +751,50 @@ namespace Content.Server.GameObjects.EntitySystems
}
}
/// <summary>
/// Calls Equipped on all components that implement the IEquipped interface
/// on an entity that has been equipped.
/// </summary>
public void EquippedInteraction(IEntity user, IEntity equipped, EquipmentSlotDefines.Slots slot)
{
var equipMsg = new EquippedMessage(user, equipped, slot);
RaiseLocalEvent(equipMsg);
if (equipMsg.Handled)
{
return;
}
var comps = equipped.GetAllComponents<IEquipped>().ToList();
// Call Thrown on all components that implement the interface
foreach (var comp in comps)
{
comp.Equipped(new EquippedEventArgs(user, slot));
}
}
/// <summary>
/// Calls Unequipped on all components that implement the IUnequipped interface
/// on an entity that has been equipped.
/// </summary>
public void UnequippedInteraction(IEntity user, IEntity equipped, EquipmentSlotDefines.Slots slot)
{
var unequipMsg = new UnequippedMessage(user, equipped, slot);
RaiseLocalEvent(unequipMsg);
if (unequipMsg.Handled)
{
return;
}
var comps = equipped.GetAllComponents<IUnequipped>().ToList();
// Call Thrown on all components that implement the interface
foreach (var comp in comps)
{
comp.Unequipped(new UnequippedEventArgs(user, slot));
}
}
/// <summary>
/// Activates the Dropped behavior of an object
/// Verifies that the user is capable of doing the drop interaction first
@@ -1104,6 +1189,74 @@ namespace Content.Server.GameObjects.EntitySystems
}
}
/// <summary>
/// Raised when equipping the entity in an inventory slot.
/// </summary>
[PublicAPI]
public class EquippedMessage : EntitySystemMessage
{
/// <summary>
/// If this message has already been "handled" by a previous system.
/// </summary>
public bool Handled { get; set; }
/// <summary>
/// Entity that equipped the item.
/// </summary>
public IEntity User { get; }
/// <summary>
/// Item that was equipped.
/// </summary>
public IEntity Equipped { get; }
/// <summary>
/// Slot where the item was placed.
/// </summary>
public EquipmentSlotDefines.Slots Slot { get; }
public EquippedMessage(IEntity user, IEntity equipped, EquipmentSlotDefines.Slots slot)
{
User = user;
Equipped = equipped;
Slot = slot;
}
}
/// <summary>
/// Raised when removing the entity from an inventory slot.
/// </summary>
[PublicAPI]
public class UnequippedMessage : EntitySystemMessage
{
/// <summary>
/// If this message has already been "handled" by a previous system.
/// </summary>
public bool Handled { get; set; }
/// <summary>
/// Entity that equipped the item.
/// </summary>
public IEntity User { get; }
/// <summary>
/// Item that was equipped.
/// </summary>
public IEntity Equipped { get; }
/// <summary>
/// Slot where the item was removed from.
/// </summary>
public EquipmentSlotDefines.Slots Slot { get; }
public UnequippedMessage(IEntity user, IEntity equipped, EquipmentSlotDefines.Slots slot)
{
User = user;
Equipped = equipped;
Slot = slot;
}
}
/// <summary>
/// Raised when an entity that was thrown lands.
/// </summary>