From 9d03081fdabddd5a27511e7dce5289090142041e Mon Sep 17 00:00:00 2001
From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Date: Tue, 15 Apr 2025 05:10:09 +1000
Subject: [PATCH] Predict DamagePopup (#36547)
* Predict DamagePopup
Pretty easy one.
* cleanup and localize
* oops
---------
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
---
.../Damage/Components/DamagePopupComponent.cs | 25 ---------
.../Damage/Systems/DamagePopupSystem.cs | 54 -------------------
.../Damage/Components/DamagePopupComponent.cs | 33 ++++++++++++
.../Damage/Systems/DamagePopupSystem.cs | 48 +++++++++++++++++
.../en-US/damage/damage-popup-component.ftl | 10 ++++
5 files changed, 91 insertions(+), 79 deletions(-)
delete mode 100644 Content.Server/Damage/Components/DamagePopupComponent.cs
delete mode 100644 Content.Server/Damage/Systems/DamagePopupSystem.cs
create mode 100644 Content.Shared/Damage/Components/DamagePopupComponent.cs
create mode 100644 Content.Shared/Damage/Systems/DamagePopupSystem.cs
create mode 100644 Resources/Locale/en-US/damage/damage-popup-component.ftl
diff --git a/Content.Server/Damage/Components/DamagePopupComponent.cs b/Content.Server/Damage/Components/DamagePopupComponent.cs
deleted file mode 100644
index 37c5d57cf4..0000000000
--- a/Content.Server/Damage/Components/DamagePopupComponent.cs
+++ /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
-{
- ///
- /// Bool that will be used to determine if the popup type can be changed with a left click.
- ///
- [DataField("allowTypeChange")] [ViewVariables(VVAccess.ReadWrite)]
- public bool AllowTypeChange = false;
- ///
- /// Enum that will be used to determine the type of damage popup displayed.
- ///
- [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
index 3386c92129..0000000000
--- a/Content.Server/Damage/Systems/DamagePopupSystem.cs
+++ /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(OnDamageChange);
- SubscribeLocalEvent(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().Last())
- {
- component.Type = Enum.GetValues(typeof(DamagePopupType)).Cast().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
index 0000000000..917763fb7d
--- /dev/null
+++ b/Content.Shared/Damage/Components/DamagePopupComponent.cs
@@ -0,0 +1,33 @@
+using Content.Shared.Damage.Systems;
+using Robust.Shared.GameStates;
+using Robust.Shared.Serialization;
+
+namespace Content.Shared.Damage.Components;
+
+///
+/// An entity with this component will show a popup indicating the amount of damage taken.
+///
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, Access(typeof(DamagePopupSystem))]
+public sealed partial class DamagePopupComponent : Component
+{
+ ///
+ /// Bool that will be used to determine if the popup type can be changed with a left click.
+ ///
+ [DataField, AutoNetworkedField]
+ public bool AllowTypeChange;
+
+ ///
+ /// Enum that will be used to determine the type of damage popup displayed.
+ ///
+ [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
index 0000000000..83cd551279
--- /dev/null
+++ b/Content.Shared/Damage/Systems/DamagePopupSystem.cs
@@ -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(OnDamageChange);
+ SubscribeLocalEvent(OnInteractHand);
+ }
+
+ private void OnDamageChange(Entity 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 ent, ref InteractHandEvent args)
+ {
+ if (ent.Comp.AllowTypeChange)
+ {
+ var next = (DamagePopupType)(((int)ent.Comp.Type + 1) % Enum.GetValues().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
index 0000000000..3ab9c37c02
--- /dev/null
+++ b/Resources/Locale/en-US/damage/damage-popup-component.ftl
@@ -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
--
2.51.2