]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
BatteryWeaponFireModes refactor (#24502)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Mon, 29 Jan 2024 00:09:56 +0000 (11:09 +1100)
committerGitHub <noreply@github.com>
Mon, 29 Jan 2024 00:09:56 +0000 (11:09 +1100)
* BatteryWeaponFireModes refactor

Made the code a bit better but still needs integrating into attachments.

* murder

* Fix serialization

* weh

* weh

Content.Shared/Weapons/Ranged/Components/BatteryWeaponFireModesComponent.cs [moved from Content.Server/Weapons/Ranged/Components/BatteryWeaponFireModesComponent.cs with 62% similarity]
Content.Shared/Weapons/Ranged/Systems/BatteryWeaponFireModesSystem.cs [moved from Content.Server/Weapons/Ranged/Systems/BatteryWeaponFireModesSystem.cs with 59% similarity]
Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs

similarity index 62%
rename from Content.Server/Weapons/Ranged/Components/BatteryWeaponFireModesComponent.cs
rename to Content.Shared/Weapons/Ranged/Components/BatteryWeaponFireModesComponent.cs
index 0d2b05d36c57c61cb6ec01db164048e24fa9c1b0..b0ca1f215cc1a5645dffb6386b3c325364b53291 100644 (file)
@@ -1,13 +1,14 @@
+using Content.Shared.Weapons.Ranged.Systems;
+using Robust.Shared.GameStates;
 using Robust.Shared.Prototypes;
-using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
-using Content.Server.Weapons.Ranged.Systems;
+using Robust.Shared.Serialization;
 
-namespace Content.Server.Weapons.Ranged.Components;
+namespace Content.Shared.Weapons.Ranged.Components;
 
 /// <summary>
 /// Allows battery weapons to fire different types of projectiles
 /// </summary>
-[RegisterComponent]
+[RegisterComponent, NetworkedComponent]
 [Access(typeof(BatteryWeaponFireModesSystem))]
 [AutoGenerateComponentState]
 public sealed partial class BatteryWeaponFireModesComponent : Component
@@ -15,30 +16,30 @@ public sealed partial class BatteryWeaponFireModesComponent : Component
     /// <summary>
     /// A list of the different firing modes the weapon can switch between
     /// </summary>
-    [DataField("fireModes", required: true)]
+    [DataField(required: true)]
     [AutoNetworkedField]
     public List<BatteryWeaponFireMode> FireModes = new();
 
     /// <summary>
     /// The currently selected firing mode
     /// </summary>
-    [DataField("currentFireMode")]
+    [DataField]
     [AutoNetworkedField]
-    public BatteryWeaponFireMode? CurrentFireMode = default!;
+    public int CurrentFireMode;
 }
 
-[DataDefinition]
+[DataDefinition, Serializable, NetSerializable]
 public sealed partial class BatteryWeaponFireMode
 {
     /// <summary>
     /// The projectile prototype associated with this firing mode
     /// </summary>
-    [DataField("proto", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
-    public string Prototype = default!;
+    [DataField("proto", required: true)]
+    public EntProtoId Prototype = default!;
 
     /// <summary>
     /// The battery cost to fire the projectile associated with this firing mode
     /// </summary>
-    [DataField("fireCost")]
+    [DataField]
     public float FireCost = 100;
 }
similarity index 59%
rename from Content.Server/Weapons/Ranged/Systems/BatteryWeaponFireModesSystem.cs
rename to Content.Shared/Weapons/Ranged/Systems/BatteryWeaponFireModesSystem.cs
index 90859d4068212d4f7ab07cc09a5f7ffdcf82cc1d..68fb2f27c98e1a3c954efefa865b1e7cfae3c1e3 100644 (file)
@@ -1,19 +1,18 @@
-using Content.Server.Popups;
-using Content.Server.Weapons.Ranged.Components;
+using System.Linq;
 using Content.Shared.Database;
 using Content.Shared.Examine;
 using Content.Shared.Interaction;
+using Content.Shared.Popups;
 using Content.Shared.Verbs;
 using Content.Shared.Weapons.Ranged.Components;
 using Robust.Shared.Prototypes;
-using System.Linq;
 
-namespace Content.Server.Weapons.Ranged.Systems;
+namespace Content.Shared.Weapons.Ranged.Systems;
 
 public sealed class BatteryWeaponFireModesSystem : EntitySystem
 {
     [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
-    [Dependency] private readonly PopupSystem _popupSystem = default!;
+    [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
 
     public override void Initialize()
     {
@@ -26,51 +25,47 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
 
     private void OnExamined(EntityUid uid, BatteryWeaponFireModesComponent component, ExaminedEvent args)
     {
-        if (component.FireModes == null || component.FireModes.Count < 2)
+        if (component.FireModes.Count < 2)
             return;
 
-        if (component.CurrentFireMode == null)
-        {
-            SetFireMode(uid, component, component.FireModes.First());
-        }
-
-        if (component.CurrentFireMode?.Prototype == null)
-            return;
+        var fireMode = GetMode(component);
 
-        if (!_prototypeManager.TryIndex<EntityPrototype>(component.CurrentFireMode.Prototype, out var proto))
+        if (!_prototypeManager.TryIndex<EntityPrototype>(fireMode.Prototype, out var proto))
             return;
 
         args.PushMarkup(Loc.GetString("gun-set-fire-mode", ("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.Hands == null)
             return;
 
-        if (component.FireModes == null || component.FireModes.Count < 2)
+        if (component.FireModes.Count < 2)
             return;
 
-        if (component.CurrentFireMode == null)
-        {
-            SetFireMode(uid, component, component.FireModes.First());
-        }
-
-        foreach (var fireMode in component.FireModes)
+        for (var i = 0; i < component.FireModes.Count; i++)
         {
+            var fireMode = component.FireModes[i];
             var entProto = _prototypeManager.Index<EntityPrototype>(fireMode.Prototype);
+            var index = i;
 
             var v = new Verb
             {
                 Priority = 1,
                 Category = VerbCategory.SelectType,
                 Text = entProto.Name,
-                Disabled = fireMode == component.CurrentFireMode,
+                Disabled = i == component.CurrentFireMode,
                 Impact = LogImpact.Low,
                 DoContactInteraction = true,
                 Act = () =>
                 {
-                    SetFireMode(uid, component, fireMode, args.User);
+                    SetFireMode(uid, component, index, args.User);
                 }
             };
 
@@ -80,7 +75,7 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
 
     private void OnInteractHandEvent(EntityUid uid, BatteryWeaponFireModesComponent component, ActivateInWorldEvent args)
     {
-        if (component.FireModes == null || component.FireModes.Count < 2)
+        if (component.FireModes.Count < 2)
             return;
 
         CycleFireMode(uid, component, args.User);
@@ -88,30 +83,18 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
 
     private void CycleFireMode(EntityUid uid, BatteryWeaponFireModesComponent component, EntityUid user)
     {
-        int index = (component.CurrentFireMode != null) ?
-            Math.Max(component.FireModes.IndexOf(component.CurrentFireMode), 0) + 1 : 1;
-
-        BatteryWeaponFireMode? fireMode;
-
-        if (index >= component.FireModes.Count)
-        {
-            fireMode = component.FireModes.FirstOrDefault();
-        }
-
-        else
-        {
-            fireMode = component.FireModes[index];
-        }
+        if (component.FireModes.Count < 2)
+            return;
 
-        SetFireMode(uid, component, fireMode, user);
+        var index = (component.CurrentFireMode + 1) % component.FireModes.Count;
+        SetFireMode(uid, component, index, user);
     }
 
-    private void SetFireMode(EntityUid uid, BatteryWeaponFireModesComponent component, BatteryWeaponFireMode? fireMode, EntityUid? user = null)
+    private void SetFireMode(EntityUid uid, BatteryWeaponFireModesComponent component, int index, EntityUid? user = null)
     {
-        if (fireMode?.Prototype == null)
-            return;
-
-        component.CurrentFireMode = fireMode;
+        var fireMode = component.FireModes[index];
+        component.CurrentFireMode = index;
+        Dirty(uid, component);
 
         if (TryComp(uid, out ProjectileBatteryAmmoProviderComponent? projectileBatteryAmmoProvider))
         {
@@ -123,7 +106,7 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
 
             if (user != null)
             {
-                _popupSystem.PopupEntity(Loc.GetString("gun-set-fire-mode", ("mode", prototype.Name)), uid, user.Value);
+                _popupSystem.PopupClient(Loc.GetString("gun-set-fire-mode", ("mode", prototype.Name)), uid, user.Value);
             }
         }
     }
index 07af02e509a7335e54cdfd86076f8d1c2fed48f8..ba22ba2cdc8fe0cd5e172d346bee2c9a0b65a60e 100644 (file)
@@ -69,7 +69,7 @@ public abstract partial class SharedGunSystem : EntitySystem
     private const float EjectOffset = 0.4f;
     protected const string AmmoExamineColor = "yellow";
     protected const string FireRateExamineColor = "yellow";
-    protected const string ModeExamineColor = "cyan";
+    public const string ModeExamineColor = "cyan";
 
     public override void Initialize()
     {