]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Predict DamagePopup (#36547)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Mon, 14 Apr 2025 19:10:09 +0000 (05:10 +1000)
committerGitHub <noreply@github.com>
Mon, 14 Apr 2025 19:10:09 +0000 (21:10 +0200)
* Predict DamagePopup

Pretty easy one.

* cleanup and localize

* oops

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Content.Server/Damage/Components/DamagePopupComponent.cs [deleted file]
Content.Server/Damage/Systems/DamagePopupSystem.cs [deleted file]
Content.Shared/Damage/Components/DamagePopupComponent.cs [new file with mode: 0644]
Content.Shared/Damage/Systems/DamagePopupSystem.cs [new file with mode: 0644]
Resources/Locale/en-US/damage/damage-popup-component.ftl [new file with mode: 0644]

diff --git a/Content.Server/Damage/Components/DamagePopupComponent.cs b/Content.Server/Damage/Components/DamagePopupComponent.cs
deleted file mode 100644 (file)
index 37c5d57..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-using Content.Server.Damage.Systems;
-
-namespace Content.Server.Damage.Components;
-
-[RegisterComponent, Access(typeof(DamagePopupSystem))]
-public sealed partial class DamagePopupComponent : Component
-{
-    /// <summary>
-    /// Bool that will be used to determine if the popup type can be changed with a left click.
-    /// </summary>
-    [DataField("allowTypeChange")] [ViewVariables(VVAccess.ReadWrite)]
-    public bool AllowTypeChange = false;
-    /// <summary>
-    /// Enum that will be used to determine the type of damage popup displayed.
-    /// </summary>
-    [DataField("damagePopupType")] [ViewVariables(VVAccess.ReadWrite)]
-    public DamagePopupType Type = DamagePopupType.Combined;
-}
-public enum DamagePopupType
-{
-    Combined,
-    Total,
-    Delta,
-    Hit,
-};
diff --git a/Content.Server/Damage/Systems/DamagePopupSystem.cs b/Content.Server/Damage/Systems/DamagePopupSystem.cs
deleted file mode 100644 (file)
index 3386c92..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-using System.Linq;
-using Content.Server.Damage.Components;
-using Content.Server.Popups;
-using Content.Shared.Damage;
-using Content.Shared.Interaction;
-
-namespace Content.Server.Damage.Systems;
-
-public sealed class DamagePopupSystem : EntitySystem
-{
-    [Dependency] private readonly PopupSystem _popupSystem = default!;
-
-    public override void Initialize()
-    {
-        base.Initialize();
-        SubscribeLocalEvent<DamagePopupComponent, DamageChangedEvent>(OnDamageChange);
-        SubscribeLocalEvent<DamagePopupComponent, InteractHandEvent>(OnInteractHand);
-    }
-
-    private void OnDamageChange(EntityUid uid, DamagePopupComponent component, DamageChangedEvent args)
-    {
-        if (args.DamageDelta != null)
-        {
-            var damageTotal = args.Damageable.TotalDamage;
-            var damageDelta = args.DamageDelta.GetTotal();
-
-            var msg = component.Type switch
-            {
-                DamagePopupType.Delta => damageDelta.ToString(),
-                DamagePopupType.Total => damageTotal.ToString(),
-                DamagePopupType.Combined => damageDelta + " | " + damageTotal,
-                DamagePopupType.Hit => "!",
-                _ => "Invalid type",
-            };
-            _popupSystem.PopupEntity(msg, uid);
-        }
-    }
-
-    private void OnInteractHand(EntityUid uid, DamagePopupComponent component, InteractHandEvent args)
-    {
-        if (component.AllowTypeChange)
-        {
-            if (component.Type == Enum.GetValues(typeof(DamagePopupType)).Cast<DamagePopupType>().Last())
-            {
-                component.Type = Enum.GetValues(typeof(DamagePopupType)).Cast<DamagePopupType>().First();
-            }
-            else
-            {
-                component.Type = (DamagePopupType) (int) component.Type + 1;
-            }
-            _popupSystem.PopupEntity("Target set to type: " + component.Type.ToString(), uid);
-        }
-    }
-}
diff --git a/Content.Shared/Damage/Components/DamagePopupComponent.cs b/Content.Shared/Damage/Components/DamagePopupComponent.cs
new file mode 100644 (file)
index 0000000..917763f
--- /dev/null
@@ -0,0 +1,33 @@
+using Content.Shared.Damage.Systems;
+using Robust.Shared.GameStates;
+using Robust.Shared.Serialization;
+
+namespace Content.Shared.Damage.Components;
+
+/// <summary>
+/// An entity with this component will show a popup indicating the amount of damage taken.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, Access(typeof(DamagePopupSystem))]
+public sealed partial class DamagePopupComponent : Component
+{
+    /// <summary>
+    /// Bool that will be used to determine if the popup type can be changed with a left click.
+    /// </summary>
+    [DataField, AutoNetworkedField]
+    public bool AllowTypeChange;
+
+    /// <summary>
+    /// Enum that will be used to determine the type of damage popup displayed.
+    /// </summary>
+    [DataField("damagePopupType"), AutoNetworkedField]
+    public DamagePopupType Type = DamagePopupType.Combined;
+}
+
+[Serializable, NetSerializable]
+public enum DamagePopupType : byte
+{
+    Combined,
+    Total,
+    Delta,
+    Hit,
+};
diff --git a/Content.Shared/Damage/Systems/DamagePopupSystem.cs b/Content.Shared/Damage/Systems/DamagePopupSystem.cs
new file mode 100644 (file)
index 0000000..83cd551
--- /dev/null
@@ -0,0 +1,48 @@
+using Content.Shared.Damage.Components;
+using Content.Shared.Interaction;
+using Content.Shared.Popups;
+
+namespace Content.Shared.Damage.Systems;
+
+public sealed class DamagePopupSystem : EntitySystem
+{
+    [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
+
+    public override void Initialize()
+    {
+        base.Initialize();
+        SubscribeLocalEvent<DamagePopupComponent, DamageChangedEvent>(OnDamageChange);
+        SubscribeLocalEvent<DamagePopupComponent, InteractHandEvent>(OnInteractHand);
+    }
+
+    private void OnDamageChange(Entity<DamagePopupComponent> ent, ref DamageChangedEvent args)
+    {
+        if (args.DamageDelta != null)
+        {
+            var damageTotal = args.Damageable.TotalDamage;
+            var damageDelta = args.DamageDelta.GetTotal();
+
+            var msg = ent.Comp.Type switch
+            {
+                DamagePopupType.Delta => damageDelta.ToString(),
+                DamagePopupType.Total => damageTotal.ToString(),
+                DamagePopupType.Combined => damageDelta + " | " + damageTotal,
+                DamagePopupType.Hit => "!",
+                _ => "Invalid type",
+            };
+
+            _popupSystem.PopupPredicted(msg, ent.Owner, args.Origin);
+        }
+    }
+
+    private void OnInteractHand(Entity<DamagePopupComponent> ent, ref InteractHandEvent args)
+    {
+        if (ent.Comp.AllowTypeChange)
+        {
+            var next = (DamagePopupType)(((int)ent.Comp.Type + 1) % Enum.GetValues<DamagePopupType>().Length);
+            ent.Comp.Type = next;
+            Dirty(ent);
+            _popupSystem.PopupPredicted(Loc.GetString("damage-popup-component-switched", ("setting", ent.Comp.Type)), ent.Owner, args.User);
+        }
+    }
+}
diff --git a/Resources/Locale/en-US/damage/damage-popup-component.ftl b/Resources/Locale/en-US/damage/damage-popup-component.ftl
new file mode 100644 (file)
index 0000000..3ab9c37
--- /dev/null
@@ -0,0 +1,10 @@
+-damage-popup-component-type =
+    { $setting ->
+        [combined] Combined
+        [total] Total
+        [delta] Delta
+        [hit] Hit
+       *[other] Unknown
+    }
+
+damage-popup-component-switched = Target set to type: { -damage-popup-component-type(setting: $setting) }
\ No newline at end of file