Interaction Entity System (#26)
* Interaction Entity System * ye * Update submodule * Requires engine update to function, but doesn't use shitcode * Fix conflicts * Fix conflicts but for real * Update submodule
This commit is contained in:
@@ -1,41 +1,40 @@
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using System;
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using Content.Shared.GameObjects;
|
||||
using SS14.Server.GameObjects;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
using SS14.Shared.Interfaces.GameObjects.Components;
|
||||
using SS14.Shared.Log;
|
||||
using SS14.Shared.Maths;
|
||||
using SS14.Shared.IoC;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
|
||||
namespace Content.Server.GameObjects
|
||||
{
|
||||
public class ServerDoorComponent : SharedDoorComponent
|
||||
public class ServerDoorComponent : SharedDoorComponent, IAttackHand
|
||||
{
|
||||
public bool Opened { get; private set; }
|
||||
|
||||
private float OpenTimeCounter;
|
||||
|
||||
private IInteractableComponent interactableComponent;
|
||||
|
||||
private CollidableComponent collidableComponent;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
interactableComponent = Owner.GetComponent<IInteractableComponent>();
|
||||
interactableComponent.OnAttackHand += OnAttackHand;
|
||||
collidableComponent = Owner.GetComponent<CollidableComponent>();
|
||||
collidableComponent.OnBump += OnBump;
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
interactableComponent.OnAttackHand -= OnAttackHand;
|
||||
interactableComponent = null;
|
||||
collidableComponent.OnBump -= OnBump;
|
||||
collidableComponent = null;
|
||||
}
|
||||
|
||||
private void OnAttackHand(object sender, AttackHandEventArgs args)
|
||||
public bool Attackhand(IEntity user)
|
||||
{
|
||||
if (Opened)
|
||||
{
|
||||
@@ -45,6 +44,7 @@ namespace Content.Server.GameObjects
|
||||
{
|
||||
Open();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnBump(object sender, BumpEventArgs args)
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
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,19 +1,17 @@
|
||||
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;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Server.GameObjects
|
||||
{
|
||||
public class ItemComponent : Component, IItemComponent
|
||||
public class ItemComponent : Component, IItemComponent, EntitySystems.IAttackHand
|
||||
{
|
||||
public override string Name => "Item";
|
||||
|
||||
/// <inheritdoc />
|
||||
public IInventorySlot ContainingSlot { get; private set; }
|
||||
private IInteractableComponent interactableComponent;
|
||||
|
||||
public void RemovedFromSlot()
|
||||
{
|
||||
@@ -45,38 +43,15 @@ namespace Content.Server.GameObjects
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
public bool Attackhand(IEntity user)
|
||||
{
|
||||
if (ContainingSlot != null)
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
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();
|
||||
var hands = user.GetComponent<IHandsComponent>();
|
||||
hands.PutInHand(this, hands.ActiveIndex, fallback: false);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,11 @@
|
||||
using Content.Shared.GameObjects;
|
||||
using SS14.Server.GameObjects.Events;
|
||||
using SS14.Server.Interfaces.GameObjects;
|
||||
using SS14.Shared;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using YamlDotNet.RepresentationModel;
|
||||
using Lidgren.Network;
|
||||
using SS14.Shared.Enums;
|
||||
|
||||
namespace Content.Server.GameObjects
|
||||
|
||||
149
Content.Server/GameObjects/EntitySystems/InteractionSystem.cs
Normal file
149
Content.Server/GameObjects/EntitySystems/InteractionSystem.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using SS14.Server.Interfaces.GameObjects;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Shared.GameObjects.System;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
using SS14.Shared.Interfaces.GameObjects.Components;
|
||||
using SS14.Shared.IoC;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
/// <summary>
|
||||
/// This interface gives components behavior when being clicked on or "attacked" by a user with an object in their hand
|
||||
/// </summary>
|
||||
public interface IAttackby
|
||||
{
|
||||
bool Attackby(IEntity user, IEntity attackwith);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This interface gives components behavior when being clicked on or "attacked" by a user with an empty hand
|
||||
/// </summary>
|
||||
public interface IAttackHand
|
||||
{
|
||||
bool Attackhand(IEntity user);
|
||||
}
|
||||
|
||||
public interface IUse
|
||||
{
|
||||
bool UseEntity(IEntity user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Governs interactions during clicking on entities
|
||||
/// </summary>
|
||||
public class InteractionSystem : EntitySystem
|
||||
{
|
||||
private const float INTERACTION_RANGE = 2;
|
||||
private const float INTERACTION_RANGE_SQUARED = INTERACTION_RANGE * INTERACTION_RANGE;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeEvent<ClickedOnEntityEventArgs>(UserInteraction, this);
|
||||
}
|
||||
|
||||
public void UserInteraction(object sender, EntityEventArgs arg)
|
||||
{
|
||||
ClickedOnEntityEventArgs e = (ClickedOnEntityEventArgs)arg;
|
||||
if (e.MouseButton != Clicktype.Left)
|
||||
return;
|
||||
|
||||
IEntity user = EntityManager.GetEntity(e.Clicker);
|
||||
IEntity attacked = EntityManager.GetEntity(e.Clicked);
|
||||
|
||||
if (!user.TryGetComponent<IServerTransformComponent>(out var userTransform))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var distance = (userTransform.WorldPosition - attacked.GetComponent<IServerTransformComponent>().WorldPosition).LengthSquared;
|
||||
if (distance > INTERACTION_RANGE_SQUARED)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user.TryGetComponent<IHandsComponent>(out var hands))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var item = hands.GetHand(hands.ActiveIndex)?.Owner;
|
||||
|
||||
if (item != null && attacked != item)
|
||||
{
|
||||
Interaction(user, item, attacked);
|
||||
}
|
||||
else if(attacked == item)
|
||||
{
|
||||
UseInteraction(user, item);
|
||||
}
|
||||
else
|
||||
{
|
||||
Interaction(user, attacked);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses a weapon/object on an entity
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="weapon"></param>
|
||||
/// <param name="attacked"></param>
|
||||
public static void Interaction(IEntity user, IEntity weapon, IEntity attacked)
|
||||
{
|
||||
List<IAttackby> interactables = attacked.GetComponents<IAttackby>().ToList();
|
||||
|
||||
for(var i = 0; i < interactables.Count; i++)
|
||||
{
|
||||
if (interactables[i].Attackby(user, weapon)) //If an attackby returns a status completion we finish our attack
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//Else check damage component to see if we damage if not attackby, and if so can we attack object
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses an empty hand on an entity
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="attacked"></param>
|
||||
public static void Interaction(IEntity user, IEntity attacked)
|
||||
{
|
||||
List<IAttackHand> interactables = attacked.GetComponents<IAttackHand>().ToList();
|
||||
|
||||
for (var i = 0; i < interactables.Count; i++)
|
||||
{
|
||||
if (interactables[i].Attackhand(user)) //If an attackby returns a status completion we finish our attack
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//Else check damage component to see if we damage if not attackby, and if so can we attack object
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Activates/Uses an object in control/possession of a user
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="attacked"></param>
|
||||
public static void UseInteraction(IEntity user, IEntity used)
|
||||
{
|
||||
List<IUse> usables = used.GetComponents<IUse>().ToList();
|
||||
|
||||
for (var i = 0; i < usables.Count; i++)
|
||||
{
|
||||
if (usables[i].UseEntity(user)) //If an attackby returns a status completion we finish our attack
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user