using Content.Server.Actions; using Content.Shared.Body; using Content.Shared.Cloning.Events; using Content.Shared.Humanoid.Markings; using Content.Shared.Mobs; using Content.Shared.Toggleable; using Content.Shared.Wagging; using Robust.Shared.Prototypes; using Robust.Shared.Utility; namespace Content.Server.Wagging; /// /// Adds an action to toggle wagging animation for tails markings that supporting this /// public sealed partial class WaggingSystem : EntitySystem { [Dependency] private ActionsSystem _actions = default!; [Dependency] private SharedVisualBodySystem _visualBody = default!; [Dependency] private IPrototypeManager _prototype = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnWaggingMapInit); SubscribeLocalEvent(OnWaggingShutdown); SubscribeLocalEvent(OnWaggingToggle); SubscribeLocalEvent(OnMobStateChanged); SubscribeLocalEvent(OnCloning); } private void OnCloning(Entity ent, ref CloningEvent args) { if (!args.Settings.EventComponents.Contains(Factory.GetRegistration(ent.Comp.GetType()).Name)) return; // Make sure to set the datafields before adding the component so that the correct action gets spawned on map init. var cloneComp = Factory.GetComponent(); cloneComp.Action = ent.Comp.Action; cloneComp.Layer = ent.Comp.Layer; cloneComp.Organ = ent.Comp.Organ; cloneComp.Suffix = ent.Comp.Suffix; AddComp(args.CloneUid, cloneComp, true); } private void OnWaggingMapInit(Entity ent, ref MapInitEvent args) { _actions.AddAction(ent, ref ent.Comp.ActionEntity, ent.Comp.Action, ent); } private void OnWaggingShutdown(Entity ent, ref ComponentShutdown args) { _actions.RemoveAction(ent.Owner, ent.Comp.ActionEntity); } private void OnWaggingToggle(Entity ent, ref ToggleActionEvent args) { if (args.Handled) return; TryToggleWagging(ent.AsNullable()); } private void OnMobStateChanged(Entity ent, ref MobStateChangedEvent args) { if (ent.Comp.Wagging) TryToggleWagging(ent.AsNullable()); } private bool TryToggleWagging(Entity ent) { if (!Resolve(ent, ref ent.Comp)) return false; if (!_visualBody.TryGatherMarkingsData(ent.Owner, [ent.Comp.Layer], out _, out _, out var applied)) { return false; } if (!applied.TryGetValue(ent.Comp.Organ, out var markingsSet)) return false; ent.Comp.Wagging = !ent.Comp.Wagging; markingsSet = markingsSet.ShallowClone(); foreach (var (layers, markings) in markingsSet) { markingsSet[layers] = markingsSet[layers].ShallowClone(); var layerMarkings = markingsSet[layers]; for (int i = 0; i < layerMarkings.Count; i++) { var currentMarkingId = layerMarkings[i].MarkingId; string newMarkingId; if (ent.Comp.Wagging) { newMarkingId = $"{currentMarkingId}{ent.Comp.Suffix}"; } else { if (currentMarkingId.Id.EndsWith(ent.Comp.Suffix)) { newMarkingId = currentMarkingId.Id[..^ent.Comp.Suffix.Length]; } else { newMarkingId = currentMarkingId; Log.Warning($"Unable to revert wagging for {currentMarkingId}"); } } if (!_prototype.HasIndex(newMarkingId)) { Log.Warning($"{ToPrettyString(ent):ent} tried toggling wagging but {newMarkingId} marking doesn't exist"); continue; } layerMarkings[i] = new Marking(newMarkingId, layerMarkings[i].MarkingColors); } } _visualBody.ApplyMarkings(ent, new() { [ent.Comp.Organ] = markingsSet }); return true; } }