Files
tbd-station-14/Content.Shared/GameObjects/Verbs/VerbUtility.cs
Visne 9b94d5c195 Added nullable to most Content.Shared files (#3238)
* Add nullable to some Content.Shared files.

* Use [NotNullWhen(true)]

* Undo adding now redundant !'s

* Forgot one

* Add a ton more nullable

* You can guess

* Fix some issues

* It actually compiles now

* Auto stash before merge of "null2" and "origin/master"

* I lied

* enable annotations -> enable

* Revert ActionBlockerSystem.cs to original

* Fix ActionBlockerSystem.cs

* More nullable

* Undo some added exclamation marks

* Fix issues

* Update Content.Shared/Maps/ContentTileDefinition.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Resolve some issues

* Remove unused method

* Fix more issues

* Fix more issues

* Fix more issues

* Fix more issues

* Fix issue, rollback SharedGhostComponent.cs

* Update submodule

* Fix issue, invert some if-statements to reduce nesting

* Revert RobustToolbox

* FIx things broken by merge

* Some fixes

- Replaced with string.Empty
- Remove some exclamation marks
- Revert file

* Some fixes

* Trivial #nullable enable

* Fix null ables

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-02-27 14:12:09 +11:00

101 lines
3.3 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.Reflection;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Utility;
namespace Content.Shared.GameObjects.Verbs
{
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)
{
var typeFactory = IoCManager.Resolve<IDynamicTypeFactory>();
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 = typeFactory.CreateInstance<Verb>(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)
{
var typeFactory = IoCManager.Resolve<IDynamicTypeFactory>();
foreach (Type type in assembly.GetTypes())
{
if (Attribute.IsDefined(type, typeof(GlobalVerbAttribute)))
{
if (!typeof(GlobalVerb).IsAssignableFrom(type) || type.IsAbstract)
{
continue;
}
yield return typeFactory.CreateInstance<GlobalVerb>(type);
}
}
}
public static bool VerbAccessChecks(IEntity user, IEntity target, VerbBase verb)
{
if (verb.RequireInteractionRange && !InVerbUseRange(user, target))
{
return false;
}
if (verb.BlockedByContainers && !VerbContainerCheck(user, target))
{
return false;
}
return true;
}
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 VerbContainerCheck(IEntity user, IEntity target)
{
if (!user.IsInSameOrNoContainer(target))
{
if (!target.TryGetContainer(out var container) ||
container.Owner != user)
{
return false;
}
}
return true;
}
}
}