Action stuff (#31305)

* Action stuff

- Cleanup some event stuff
- Avoid dirtying entity unnecessarily
- Add ActionGrant as an easy way to apply / remove actions via compregistry.

* Fix merge
This commit is contained in:
metalgearsloth
2024-08-25 22:43:31 +10:00
committed by GitHub
parent f96dddd9fb
commit 2e029a7d5b
10 changed files with 168 additions and 40 deletions

View File

@@ -0,0 +1,48 @@
namespace Content.Shared.Actions;
/// <summary>
/// <see cref="ActionGrantComponent"/>
/// </summary>
public sealed class ActionGrantSystem : EntitySystem
{
[Dependency] private readonly SharedActionsSystem _actions = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ActionGrantComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<ActionGrantComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<ItemActionGrantComponent, GetItemActionsEvent>(OnItemGet);
}
private void OnItemGet(Entity<ItemActionGrantComponent> ent, ref GetItemActionsEvent args)
{
if (!TryComp(ent.Owner, out ActionGrantComponent? grant))
return;
foreach (var action in grant.ActionEntities)
{
args.AddAction(action);
}
}
private void OnMapInit(Entity<ActionGrantComponent> ent, ref MapInitEvent args)
{
foreach (var action in ent.Comp.Actions)
{
EntityUid? actionEnt = null;
_actions.AddAction(ent.Owner, ref actionEnt, action);
if (actionEnt != null)
ent.Comp.ActionEntities.Add(actionEnt.Value);
}
}
private void OnShutdown(Entity<ActionGrantComponent> ent, ref ComponentShutdown args)
{
foreach (var actionEnt in ent.Comp.ActionEntities)
{
_actions.RemoveAction(ent.Owner, actionEnt);
}
}
}