2023-08-11 22:56:34 -07:00
|
|
|
|
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);
|
2024-11-11 09:54:23 +03:00
|
|
|
|
SubscribeLocalEvent<MobStateComponent, ComponentInit>(OnMobStateComponentInit); // WD ahead of wizden
|
2023-08-11 22:56:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnMobStateChanged(EntityUid uid, MobStateActionsComponent component, MobStateChangedEvent args)
|
2024-11-11 09:54:23 +03:00
|
|
|
|
{
|
|
|
|
|
|
ComposeActions(uid, component, args.NewMobState); // WD ahead of wizden
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnMobStateComponentInit(EntityUid uid, MobStateComponent component, ComponentInit args) // WD ahead of wizden
|
|
|
|
|
|
{
|
|
|
|
|
|
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) // WD ahead of wizden
|
2023-08-11 22:56:34 -07:00
|
|
|
|
{
|
2023-09-23 04:49:39 -04:00
|
|
|
|
if (!TryComp<ActionsComponent>(uid, out var action))
|
2023-09-09 16:14:17 -07:00
|
|
|
|
return;
|
|
|
|
|
|
|
2023-09-23 04:49:39 -04:00
|
|
|
|
foreach (var act in component.GrantedActions)
|
|
|
|
|
|
{
|
|
|
|
|
|
Del(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
component.GrantedActions.Clear();
|
|
|
|
|
|
|
2024-11-11 09:54:23 +03:00
|
|
|
|
if (!component.Actions.TryGetValue(newMobState, out var toGrant))
|
2023-08-11 22:56:34 -07:00
|
|
|
|
return;
|
|
|
|
|
|
|
2023-09-23 04:49:39 -04:00
|
|
|
|
foreach (var id in toGrant)
|
2023-08-11 22:56:34 -07:00
|
|
|
|
{
|
2023-09-23 04:49:39 -04:00
|
|
|
|
EntityUid? act = null;
|
|
|
|
|
|
if (_actions.AddAction(uid, ref act, id, uid, action))
|
|
|
|
|
|
component.GrantedActions.Add(act.Value);
|
2023-08-11 22:56:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|