using UnrealSharp.Engine; using UI.Level.Controllers; using UnrealSharp.Attributes; using UnrealSharp.BusyRabbit; using UnrealSharp; namespace UI.Level.StateBar; [UClass] public class UBusyWidgetStateBar : UPW_MinimalWidget { protected UBusyWidgetHealthBar? WBP_HealthBar { get { return GetWidget("WBP_HealthBar") as UBusyWidgetHealthBar; } } // protected UBusyWidgetSatietyBar? WBP_SatietyBar { get { return GetWidget("WBP_SatietyBar") as UBusyWidgetSatietyBar; } } protected List SatietyBars = new(); protected List HealthBars = new(); protected UBusyRoleStateController? UIController; public override void Construct() { base.Construct(); for (int i = 0; i < 5; ++i) { if (GetWidget($"WBP_SatietyBar_{i}") is UBusyWidgetSatietyBar Bar) { SatietyBars.Add(Bar); } if (GetWidget("WBP_HealthBar") is UBusyWidgetHealthBar HpBar) { HealthBars.Add(HpBar); } } UIController = UUIFunctionLibrary.GetUIController(this, "RoleState") as UBusyRoleStateController; InitRoleAttribute(); UIController.OnAttributeChangeDelegate += OnAttributeChanged; } public void InitRoleAttribute() { float CurHealth = UIController.GetControlledAttribute("Health"); float MaxHealth = UIController.GetControlledAttribute("MaxHealth"); float CurSatiety = UIController.GetControlledAttribute("Hunger"); float MaxSatiety = UIController.GetControlledAttribute("MaxHunger"); WBP_HealthBar.SetHpConfig(CurHealth, MaxHealth); foreach (UBusyWidgetSatietyBar Bar in SatietyBars) { Bar.SetSatietyConfig(CurSatiety, MaxSatiety); } } public override void Destruct() { base.Destruct(); UIController.OnAttributeChangeDelegate -= OnAttributeChanged; } public void SetSatiety(float Satiety) { foreach(UBusyWidgetSatietyBar Bar in SatietyBars) { Bar.OnSatietyChanged(Satiety); } } [UFunction()] protected void OnAttributeChanged(FName AttributeName, float NewValue, float OldValue) { if (AttributeName == "Hunger") { SetSatiety(NewValue); } } }