Files
tbd-station-14/Content.Shared/Mobs/Systems/MobStateActionsSystem.cs
2023-12-28 10:57:48 +11:00

41 lines
1.2 KiB
C#

using Content.Shared.Actions;
using Content.Shared.Mobs.Components;
namespace Content.Shared.Mobs.Systems;
/// <summary>
/// Adds and removes defined actions when a mob's <see cref="MobState"/> changes.
/// </summary>
public sealed class MobStateActionsSystem : EntitySystem
{
[Dependency] private readonly SharedActionsSystem _actions = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<MobStateActionsComponent, MobStateChangedEvent>(OnMobStateChanged);
}
private void OnMobStateChanged(EntityUid uid, MobStateActionsComponent component, MobStateChangedEvent args)
{
if (!TryComp<ActionsComponent>(uid, out var action))
return;
foreach (var act in component.GrantedActions)
{
Del(act);
}
component.GrantedActions.Clear();
if (!component.Actions.TryGetValue(args.NewMobState, out var toGrant))
return;
foreach (var id in toGrant)
{
EntityUid? act = null;
if (_actions.AddAction(uid, ref act, id, uid, action))
component.GrantedActions.Add(act.Value);
}
}
}