From f15c43f00afa1ca0a3f8a2d31e75d7f22026c8a7 Mon Sep 17 00:00:00 2001 From: Lacyway <20912169+Lacyway@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:42:42 +0200 Subject: [PATCH] Improve health bar code --- Fika.Core/Coop/Custom/FikaHealthBar.cs | 76 +++++++++++++------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 08a6f1b1..84381e68 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -23,43 +23,19 @@ public class FikaHealthBar : MonoBehaviour private CoopPlayer mainPlayer; private PlayerPlateUI playerPlate; private float screenScale = 1f; - private int frameCounter = 0; - private readonly int throttleInterval = 60; // throttle to 1 update per 60 frames protected void Awake() { currentPlayer = GetComponent(); mainPlayer = (CoopPlayer)Singleton.Instance.MainPlayer; - CreateHealthBar(); + CreateHealthBar(); } protected void Update() { if (currentPlayer != null) { - bool throttleUpdate = IsThrottleUpdate(); - if (throttleUpdate) - { - // Handling the visibility of elements - if (!FikaPlugin.UseNamePlates.Value) - { - playerPlate.gameObject.SetActive(false); - return; - } - else if (playerPlate.gameObject.active == false) - { - playerPlate.gameObject.SetActive(true); - } - SetPlayerPlateFactionVisibility(FikaPlugin.UsePlateFactionSide.Value); - SetPlayerPlateHealthVisibility(FikaPlugin.HideHealthBar.Value); - } - // Finally, update the screen space position - UpdateScreenSpacePosition(throttleUpdate); - // Destroy if this player is dead - if (!currentPlayer.HealthController.IsAlive) - { - Destroy(this); - } + UpdateScreenSpacePosition(); } else { @@ -67,7 +43,7 @@ protected void Update() } } - private void UpdateScreenSpacePosition(bool throttleUpdate) + private void UpdateScreenSpacePosition() { // ADS opacity handling float opacityMultiplier = 1f; @@ -192,13 +168,27 @@ private void CreateHealthBar() playerPlate.bearPlateScreen.gameObject.SetActive(false); } + SetPlayerPlateFactionVisibility(FikaPlugin.UsePlateFactionSide.Value); + SetPlayerPlateHealthVisibility(FikaPlugin.HideHealthBar.Value); + playerPlate.gameObject.SetActive(FikaPlugin.UseNamePlates.Value); + + FikaPlugin.UsePlateFactionSide.SettingChanged += UsePlateFactionSide_SettingChanged; + FikaPlugin.HideHealthBar.SettingChanged += HideHealthBar_SettingChanged; + FikaPlugin.UseNamePlates.SettingChanged += UseNamePlates_SettingChanged; + currentPlayer.HealthController.HealthChangedEvent += HealthController_HealthChangedEvent; currentPlayer.HealthController.BodyPartDestroyedEvent += HealthController_BodyPartDestroyedEvent; currentPlayer.HealthController.BodyPartRestoredEvent += HealthController_BodyPartRestoredEvent; + currentPlayer.HealthController.DiedEvent += HealthController_DiedEvent; UpdateHealth(); } + private void HealthController_DiedEvent(EDamageType obj) + { + Destroy(this); + } + private void HealthController_BodyPartRestoredEvent(EBodyPart arg1, EFT.HealthSystem.ValueStruct arg2) { UpdateHealth(); @@ -214,6 +204,20 @@ private void HealthController_HealthChangedEvent(EBodyPart arg1, float arg2, Dam UpdateHealth(); } + private void UsePlateFactionSide_SettingChanged(object sender, EventArgs e) + { + SetPlayerPlateFactionVisibility(FikaPlugin.UsePlateFactionSide.Value); + } + private void HideHealthBar_SettingChanged(object sender, EventArgs e) + { + SetPlayerPlateHealthVisibility(FikaPlugin.HideHealthBar.Value); + } + + private void UseNamePlates_SettingChanged(object sender, EventArgs e) + { + playerPlate.gameObject.SetActive(FikaPlugin.UseNamePlates.Value); + } + /// /// Updates the health on the HealthBar, this is invoked from events on the healthcontroller /// @@ -293,23 +297,17 @@ private void SetPlayerPlateFactionVisibility(bool visible) } } - private bool IsThrottleUpdate() + protected void OnDestroy() { - // For throttling updates to various elements - frameCounter++; - bool throttleUpdate = frameCounter >= throttleInterval; - if (throttleUpdate) - { - frameCounter = 0; - } - return throttleUpdate; - } + FikaPlugin.UsePlateFactionSide.SettingChanged -= UsePlateFactionSide_SettingChanged; + FikaPlugin.HideHealthBar.SettingChanged -= HideHealthBar_SettingChanged; + FikaPlugin.UseNamePlates.SettingChanged -= UseNamePlates_SettingChanged; - private void OnDestroy() - { currentPlayer.HealthController.HealthChangedEvent -= HealthController_HealthChangedEvent; currentPlayer.HealthController.BodyPartDestroyedEvent -= HealthController_BodyPartDestroyedEvent; currentPlayer.HealthController.BodyPartRestoredEvent -= HealthController_BodyPartRestoredEvent; + currentPlayer.HealthController.DiedEvent -= HealthController_DiedEvent; + playerPlate.gameObject.SetActive(false); Destroy(this); }