Predict DamagePopup (#36547)
* Predict DamagePopup Pretty easy one. * cleanup and localize * oops --------- Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
@@ -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,
|
||||
};
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Content.Shared/Damage/Components/DamagePopupComponent.cs
Normal file
33
Content.Shared/Damage/Components/DamagePopupComponent.cs
Normal file
@@ -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,
|
||||
};
|
||||
48
Content.Shared/Damage/Systems/DamagePopupSystem.cs
Normal file
48
Content.Shared/Damage/Systems/DamagePopupSystem.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Resources/Locale/en-US/damage/damage-popup-component.ftl
Normal file
10
Resources/Locale/en-US/damage/damage-popup-component.ftl
Normal file
@@ -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) }
|
||||
Reference in New Issue
Block a user