Interactable component system. (#9)
* InteractableComponent v1. Broken edition * It works!
This commit is contained in:
committed by
GitHub
parent
8c1fa84c6e
commit
7597cd9172
@@ -0,0 +1,80 @@
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using SS14.Server.Interfaces.GameObjects;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.Interfaces.GameObjects.Components;
|
||||
using SS14.Shared.Log;
|
||||
using System;
|
||||
|
||||
namespace Content.Server.GameObjects
|
||||
{
|
||||
public class InteractableComponent : Component, IInteractableComponent
|
||||
{
|
||||
public override string Name => "Interactable";
|
||||
|
||||
/// <inheritdoc />
|
||||
public event EventHandler<AttackHandEventArgs> OnAttackHand;
|
||||
|
||||
/// <inheritdoc />
|
||||
public event EventHandler<AttackByEventArgs> OnAttackBy;
|
||||
|
||||
private IClickableComponent clickableComponent;
|
||||
private IServerTransformComponent transform;
|
||||
private const float INTERACTION_RANGE = 2;
|
||||
private const float INTERACTION_RANGE_SQUARED = INTERACTION_RANGE * INTERACTION_RANGE;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
transform = Owner.GetComponent<IServerTransformComponent>();
|
||||
if (Owner.TryGetComponent<IClickableComponent>(out var component))
|
||||
{
|
||||
clickableComponent = component;
|
||||
clickableComponent.OnClick += ClickableComponent_OnClick;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error($"Interactable component must also have a clickable component to function! Prototype: {Owner.Prototype.ID}");
|
||||
}
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
if (clickableComponent != null)
|
||||
{
|
||||
clickableComponent.OnClick -= ClickableComponent_OnClick;
|
||||
clickableComponent = null;
|
||||
}
|
||||
transform = null;
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
private void ClickableComponent_OnClick(object sender, ClickEventArgs e)
|
||||
{
|
||||
if (!e.User.TryGetComponent<IServerTransformComponent>(out var userTransform))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var distance = (userTransform.WorldPosition - transform.WorldPosition).LengthSquared;
|
||||
if (distance > INTERACTION_RANGE_SQUARED)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!e.User.TryGetComponent<IHandsComponent>(out var hands))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var item = hands.GetHand(hands.ActiveIndex);
|
||||
if (item != null)
|
||||
{
|
||||
OnAttackBy?.Invoke(this, new AttackByEventArgs(Owner, e.User, item, hands.ActiveIndex));
|
||||
}
|
||||
else
|
||||
{
|
||||
OnAttackHand?.Invoke(this, new AttackHandEventArgs(Owner, e.User, hands.ActiveIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
using SS14.Server.Interfaces.GameObjects;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.Interfaces.GameObjects.Components;
|
||||
using SS14.Shared.Log;
|
||||
using System;
|
||||
|
||||
namespace Content.Server.GameObjects
|
||||
@@ -12,6 +13,7 @@ namespace Content.Server.GameObjects
|
||||
|
||||
/// <inheritdoc />
|
||||
public IInventorySlot ContainingSlot { get; private set; }
|
||||
private IInteractableComponent interactableComponent;
|
||||
|
||||
public void RemovedFromSlot()
|
||||
{
|
||||
@@ -42,5 +44,39 @@ namespace Content.Server.GameObjects
|
||||
component.Visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,6 @@ namespace Content.Server.GameObjects
|
||||
}
|
||||
|
||||
Owner.SubscribeEvent<BoundKeyChangeEventArgs>(OnKeyChange, this);
|
||||
Owner.SubscribeEvent<ClickedOnEntityEventArgs>(OnClick, this);
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
@@ -258,24 +257,6 @@ namespace Content.Server.GameObjects
|
||||
ActiveIndex = orderedHands[index];
|
||||
}
|
||||
|
||||
public void OnClick(object sender, EntityEventArgs uncast)
|
||||
{
|
||||
var cast = (ClickedOnEntityEventArgs)uncast;
|
||||
if (cast.MouseButton != MouseClickType.Left || Owner.EntityManager.GetEntity(cast.Clicker) != Owner)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var target = Owner.EntityManager.GetEntity(cast.Clicked);
|
||||
var targetTransform = target.GetComponent<IServerTransformComponent>();
|
||||
if (!target.TryGetComponent<IItemComponent>(out var item) || (targetTransform.WorldPosition - transform.WorldPosition).Length > PICKUP_RANGE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PutInHand(item, ActiveIndex, fallback: false);
|
||||
}
|
||||
|
||||
public override void HandleNetworkMessage(IncomingEntityComponentMessage message, NetConnection sender)
|
||||
{
|
||||
if (message.MessageParameters.Count != 1)
|
||||
|
||||
Reference in New Issue
Block a user