Add global verbs (#400)

* Add support for global verbs

These are verbs that are visible for all entities, regardless of their components. This works by adding a new class `GlobalVerb` and a new attribute `GlobalVerbAttribute`. It works in the same way as current verbs, except you can put the verbs class definition anywhere instead of inside a component. Also moved VerbUtility into it's own file since it now has functions for both verbs and global verbs.

* Add view variables verb as an example of global verbs

* Implement suggested changes

Implemented some suggested changes from code review:
- Remove unneeded attribute from `GlobalVerb`
- Added some useful attributes to `GlobalVerbAttribute`
- Moved constants used by both `Verb` and `GlobalVerb` into `VerbUtility`

* Reduce duplicate code in VerbSystem (client & server)

Greatly reduced the amount of duplicate code for handling component verbs and global verbs separately.

* Update engine submodule

Need this so client side permissions checks are available.
This commit is contained in:
moneyl
2019-10-30 11:31:35 -04:00
committed by Pieter-Jan Briers
parent e4f3ea7798
commit 6497cdf8ff
6 changed files with 228 additions and 69 deletions

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Shared.GameObjects
{
/// <summary>
/// A verb is an action in the right click menu of an entity.
/// Global verbs are visible on all entities, regardless of their components.
/// </summary>
/// <remarks>
/// To add a global verb to all entities,
/// define it and mark it with <see cref="GlobalVerbAttribute"/>
/// </remarks>
public abstract class GlobalVerb
{
/// <summary>
/// If true, this verb requires the user to be within
/// <see cref="VerbUtility.InteractionRange"/> meters from the entity on which this verb resides.
/// </summary>
public virtual bool RequireInteractionRange => true;
/// <summary>
/// Gets the text string that will be shown to <paramref name="user"/> in the right click menu.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <returns>The text string that is shown in the right click menu for this verb.</returns>
public abstract string GetText(IEntity user, IEntity target);
/// <summary>
/// Gets the visibility level of this verb in the right click menu.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <returns>The visibility level of the verb in the client's right click menu.</returns>
public abstract VerbVisibility GetVisibility(IEntity user, IEntity target);
/// <summary>
/// Invoked when this verb is activated from the right click menu.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="target">The entity that is being acted upon.</param>
public abstract void Activate(IEntity user, IEntity target);
}
/// <summary>
/// This attribute should be used on <see cref="GlobalVerb"/>. These are verbs which are on visible for all entities,
/// regardless of the components they contain.
/// </summary>
[MeansImplicitUse]
[BaseTypeRequired(typeof(GlobalVerb))]
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class GlobalVerbAttribute : Attribute
{
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using JetBrains.Annotations;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Utility;
@@ -22,9 +23,6 @@ namespace Content.Shared.GameObjects
/// </summary>
public virtual bool RequireInteractionRange => true;
public const float InteractionRange = 2;
public const float InteractionRangeSquared = InteractionRange * InteractionRange;
/// <summary>
/// Gets the text string that will be shown to <paramref name="user"/> in the right click menu.
/// </summary>
@@ -109,29 +107,6 @@ namespace Content.Shared.GameObjects
{
}
public static class VerbUtility
{
// TODO: This is a quick hack. Verb objects should absolutely be cached properly.
// This works for now though.
public static IEnumerable<(IComponent, Verb)> GetVerbs(IEntity entity)
{
foreach (var component in entity.GetAllComponents())
{
var type = component.GetType();
foreach (var nestedType in type.GetAllNestedTypes())
{
if (!typeof(Verb).IsAssignableFrom(nestedType) || nestedType.IsAbstract)
{
continue;
}
var verb = (Verb) Activator.CreateInstance(nestedType);
yield return (component, verb);
}
}
}
}
/// <summary>
/// Possible states of visibility for the verb in the right click menu.
/// </summary>

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Utility;
namespace Content.Shared.GameObjects
{
public static class VerbUtility
{
public const float InteractionRange = 2;
public const float InteractionRangeSquared = InteractionRange * InteractionRange;
// TODO: This is a quick hack. Verb objects should absolutely be cached properly.
// This works for now though.
public static IEnumerable<(IComponent, Verb)> GetVerbs(IEntity entity)
{
foreach (var component in entity.GetAllComponents())
{
var type = component.GetType();
foreach (var nestedType in type.GetAllNestedTypes())
{
if (!typeof(Verb).IsAssignableFrom(nestedType) || nestedType.IsAbstract)
{
continue;
}
var verb = (Verb)Activator.CreateInstance(nestedType);
yield return (component, verb);
}
}
}
/// <summary>
/// Returns an IEnumerable of all classes inheriting <see cref="GlobalVerb"/> with the <see cref="GlobalVerbAttribute"/> attribute.
/// </summary>
/// <param name="assembly">The assembly to search for global verbs in.</param>
public static IEnumerable<GlobalVerb> GetGlobalVerbs(Assembly assembly)
{
foreach (Type type in assembly.GetTypes())
{
if (Attribute.IsDefined(type, typeof(GlobalVerbAttribute)))
{
if (!typeof(GlobalVerb).IsAssignableFrom(type) || type.IsAbstract)
{
continue;
}
yield return (GlobalVerb)Activator.CreateInstance(type);
}
}
}
public static bool InVerbUseRange(IEntity user, IEntity target)
{
var distanceSquared = (user.Transform.WorldPosition - target.Transform.WorldPosition)
.LengthSquared;
if (distanceSquared > InteractionRangeSquared)
{
return false;
}
return true;
}
public static bool IsVerbInvisible(Verb verb, IEntity user, IComponent target, out VerbVisibility visibility)
{
visibility = verb.GetVisibility(user, target);
return visibility == VerbVisibility.Invisible;
}
public static bool IsVerbInvisible(GlobalVerb verb, IEntity user, IEntity target, out VerbVisibility visibility)
{
visibility = verb.GetVisibility(user, target);
return visibility == VerbVisibility.Invisible;
}
}
}