CatStation/Content.Shared/Weapons/Ranged/Systems/BatteryWeaponFireModesSystem.cs
Whatstone a88d2c5164
Cleanup: Resolve remaining PopupSystem warnings in Content.Shared (#44685)
* PopupClient to PopupEntity/Coordinates

* PopupPredictedX to PopupEntity/Cursor/Coordinates

* The rest of the damn owl

* nullability fixes

* Buckle cleanup

* cleanup, address PR comments

* ExecutionSystem: one popup method

* correct my filthy lies

* Apply most of ajcm's suggestions

* return user to SharedItemRecallSystem.cs

* Fix lingering Ensnarable broadcast popups

* targeted drain popups

* fix lingering untargeted Guardian popups

I read the comment on GuardianHostRecall.  Oops.

* target stethoscope popups

* fix the PDA vibration popup

* Fix target for encryption key removal popup

* anomaly scanner: args.User, not uid

* disposal unit comment, bcast pda vibration popup
2026-07-16 21:54:33 +00:00

137 lines
4.6 KiB
C#

using Content.Shared.Access.Systems;
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.Interaction.Events;
using Content.Shared.Popups;
using Content.Shared.Verbs;
using Content.Shared.Weapons.Ranged.Components;
using Robust.Shared.Prototypes;
namespace Content.Shared.Weapons.Ranged.Systems;
public sealed partial class BatteryWeaponFireModesSystem : EntitySystem
{
[Dependency] private AccessReaderSystem _accessReaderSystem = default!;
[Dependency] private SharedAppearanceSystem _appearanceSystem = default!;
[Dependency] private SharedGunSystem _gun = default!;
[Dependency] private SharedPopupSystem _popupSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BatteryWeaponFireModesComponent, UseInHandEvent>(OnUseInHandEvent);
SubscribeLocalEvent<BatteryWeaponFireModesComponent, GetVerbsEvent<Verb>>(OnGetVerb);
SubscribeLocalEvent<BatteryWeaponFireModesComponent, ExaminedEvent>(OnExamined);
}
private void OnExamined(Entity<BatteryWeaponFireModesComponent> ent, ref ExaminedEvent args)
{
if (ent.Comp.FireModes.Count < 2)
return;
var fireMode = GetMode(ent.Comp);
if (!ProtoMan.TryIndex<EntityPrototype>(fireMode.Prototype, out var proto))
return;
args.PushMarkup(Loc.GetString("gun-set-fire-mode-examine", ("mode", proto.Name)));
}
private BatteryWeaponFireMode GetMode(BatteryWeaponFireModesComponent component)
{
return component.FireModes[component.CurrentFireMode];
}
private void OnGetVerb(EntityUid uid, BatteryWeaponFireModesComponent component, GetVerbsEvent<Verb> args)
{
if (!args.CanAccess || !args.CanInteract || !args.CanComplexInteract)
return;
if (component.FireModes.Count < 2)
return;
if (!_accessReaderSystem.IsAllowed(args.User, uid))
return;
for (var i = 0; i < component.FireModes.Count; i++)
{
var fireMode = component.FireModes[i];
var entProto = ProtoMan.Index<EntityPrototype>(fireMode.Prototype);
var index = i;
var v = new Verb
{
Priority = 1,
Category = VerbCategory.SelectType,
Text = entProto.Name,
Disabled = i == component.CurrentFireMode,
Impact = LogImpact.Medium,
DoContactInteraction = true,
Act = () =>
{
TrySetFireMode((uid, component), index, args.User);
}
};
args.Verbs.Add(v);
}
}
private void OnUseInHandEvent(Entity<BatteryWeaponFireModesComponent> ent, ref UseInHandEvent args)
{
if (args.Handled)
return;
args.Handled = true;
TryCycleFireMode(ent, args.User);
}
public void TryCycleFireMode(Entity<BatteryWeaponFireModesComponent> ent, EntityUid? user = null)
{
if (ent.Comp.FireModes.Count < 2)
return;
var index = (ent.Comp.CurrentFireMode + 1) % ent.Comp.FireModes.Count;
TrySetFireMode(ent, index, user);
}
public bool TrySetFireMode(Entity<BatteryWeaponFireModesComponent> ent, int index, EntityUid? user = null)
{
if (index < 0 || index >= ent.Comp.FireModes.Count)
return false;
if (user != null && !_accessReaderSystem.IsAllowed(user.Value, ent))
return false;
SetFireMode(ent, index, user);
return true;
}
private void SetFireMode(Entity<BatteryWeaponFireModesComponent> ent, int index, EntityUid? user = null)
{
var fireMode = ent.Comp.FireModes[index];
ent.Comp.CurrentFireMode = index;
Dirty(ent);
if (ProtoMan.TryIndex<EntityPrototype>(fireMode.Prototype, out var prototype))
{
if (TryComp<AppearanceComponent>(ent, out var appearance))
_appearanceSystem.SetData(ent, BatteryWeaponFireModeVisuals.State, prototype.ID, appearance);
if (user != null)
_popupSystem.PopupEntity(Loc.GetString("gun-set-fire-mode-popup", ("mode", prototype.Name)), ent, user.Value);
}
if (TryComp(ent, out BatteryAmmoProviderComponent? batteryAmmoProviderComponent))
{
batteryAmmoProviderComponent.Prototype = fireMode.Prototype;
batteryAmmoProviderComponent.FireCost = fireMode.FireCost;
Dirty(ent, batteryAmmoProviderComponent);
_gun.UpdateShots((ent, batteryAmmoProviderComponent));
}
}
}