| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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);
- SubscribeLocalEvent<MobStateComponent, ComponentInit>(OnMobStateComponentInit);
- }
- private void OnMobStateChanged(EntityUid uid, MobStateActionsComponent component, MobStateChangedEvent args)
- {
- ComposeActions(uid, component, args.NewMobState);
- }
- private void OnMobStateComponentInit(EntityUid uid, MobStateComponent component, ComponentInit args)
- {
- if (!TryComp<MobStateActionsComponent>(uid, out var mobStateActionsComp))
- return;
- ComposeActions(uid, mobStateActionsComp, component.CurrentState);
- }
- /// <summary>
- /// Adds or removes actions from a mob based on mobstate.
- /// </summary>
- private void ComposeActions(EntityUid uid, MobStateActionsComponent component, MobState newMobState)
- {
- if (!TryComp<ActionsComponent>(uid, out var action))
- return;
- foreach (var act in component.GrantedActions)
- {
- Del(act);
- }
- component.GrantedActions.Clear();
- if (!component.Actions.TryGetValue(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);
- }
- }
- }
|