Files
crystall-punk-14/Content.Server/GameObjects/Components/Items/ItemComponent.cs

83 lines
2.5 KiB
C#
Raw Normal View History

2017-09-30 16:56:19 +02:00
using Content.Server.Interfaces.GameObjects;
using SS14.Server.Interfaces.GameObjects;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects.Components;
using SS14.Shared.Log;
2017-09-24 21:09:26 +02:00
using System;
namespace Content.Server.GameObjects
2017-09-24 21:09:26 +02:00
{
public class ItemComponent : Component, IItemComponent
{
public override string Name => "Item";
/// <inheritdoc />
public IInventorySlot ContainingSlot { get; private set; }
private IInteractableComponent interactableComponent;
2017-09-24 21:09:26 +02:00
public void RemovedFromSlot()
{
if (ContainingSlot == null)
{
throw new InvalidOperationException("Item is not in a slot.");
}
ContainingSlot = null;
2017-09-30 16:56:19 +02:00
foreach (var component in Owner.GetComponents<ISpriteRenderableComponent>())
{
component.Visible = true;
}
2017-09-24 21:09:26 +02:00
}
public void EquippedToSlot(IInventorySlot slot)
{
if (ContainingSlot != null)
{
throw new InvalidOperationException("Item is already in a slot.");
}
ContainingSlot = slot;
2017-09-30 16:56:19 +02:00
foreach (var component in Owner.GetComponents<ISpriteRenderableComponent>())
{
component.Visible = false;
}
2017-09-24 21:09:26 +02:00
}
public override void Initialize()
{
if (Owner.TryGetComponent<IInteractableComponent>(out var interactable))
{
interactableComponent = interactable;
interactableComponent.OnAttackHand += InteractableComponent_OnAttackHand;
}
else
{
Logger.Error($"Item component must have an interactable component to function! Prototype: {Owner.Prototype.ID}");
}
base.Initialize();
}
private void InteractableComponent_OnAttackHand(object sender, AttackHandEventArgs e)
{
if (ContainingSlot != null)
{
return;
}
var hands = e.User.GetComponent<IHandsComponent>();
hands.PutInHand(this, e.HandIndex, fallback: false);
}
public override void Shutdown()
{
if (interactableComponent != null)
{
interactableComponent.OnAttackHand -= InteractableComponent_OnAttackHand;
interactableComponent = null;
}
base.Shutdown();
}
2017-09-24 21:09:26 +02:00
}
}