@ -0,0 +1,76 @@
|
||||
using UnrealSharp.Engine;
|
||||
using UnrealSharp.Attributes;
|
||||
using UnrealSharp.BusyRabbit;
|
||||
using UnrealSharp.GameplayAbilities;
|
||||
using UnrealSharp;
|
||||
using UnrealSharp.CoreUObject;
|
||||
|
||||
namespace UI.Level.Controllers;
|
||||
|
||||
[UMultiDelegate]
|
||||
public delegate void OnUIAttributeChange(FName AttributeName, float NewValue, float OldValue);
|
||||
|
||||
|
||||
|
||||
[UClass]
|
||||
public class UBusyRoleStateController : UPW_UIController
|
||||
{
|
||||
[UProperty()]
|
||||
public TMulticastDelegate<OnUIAttributeChange> OnAttributeChangeDelegate { get; set; }
|
||||
|
||||
|
||||
protected override void OnUIControllerInitialized()
|
||||
{
|
||||
if (UGameplayStatics.GetPlayerController(0) is ALevelPlayerController PC)
|
||||
{
|
||||
ABusyPlayerRole Role = PC.ControlledRole;
|
||||
if (AbilitySystemLibrary.GetAbilitySystemComponent(Role) is UBusyAbilitySystemComponent ASC)
|
||||
{
|
||||
RegisterAttributeListener(ASC, "Health");
|
||||
RegisterAttributeListener(ASC, "Hunger");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float GetControlledAttribute(FName AttributeName)
|
||||
{
|
||||
if (UGameplayStatics.GetPlayerController(0) is ALevelPlayerController PC)
|
||||
{
|
||||
ABusyPlayerRole Role = PC.ControlledRole;
|
||||
if (AbilitySystemLibrary.GetAbilitySystemComponent(Role) is UBusyAbilitySystemComponent ASC)
|
||||
{
|
||||
FGameplayAttribute Attribute = UBusyAscLibrary.GetAttribute(Role.Attributes, AttributeName);
|
||||
return ASC.GetGameplayAttributeValue(Attribute, out bool IsFound);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public float GetRoleAttribute(ABusyPawnBase Pawn, FName Attribute)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected void RegisterAttributeListener(UBusyAbilitySystemComponent ASC, FName AttributeName)
|
||||
{
|
||||
TDelegate<OnBusyAttributeChange> Delegate = new TDelegate<OnBusyAttributeChange>();
|
||||
Delegate.BindUFunction(this, "OnAttributeChange");
|
||||
ASC.BindEventToAttributeChange(typeof(UBusyPlayerRoleAttributeSet), AttributeName, Delegate);
|
||||
}
|
||||
|
||||
|
||||
[UFunction()]
|
||||
protected void OnAttributeChange(FName AttributeName, float NewValue, float OldValue)
|
||||
{
|
||||
OnAttributeChangeDelegate.Invoke(AttributeName, NewValue, OldValue);
|
||||
}
|
||||
|
||||
|
||||
protected override void OnUIControllerDestroy()
|
||||
{
|
||||
base.OnUIControllerDestroy();
|
||||
}
|
||||
}
|
||||
71
Script/ManagedBusyRabbit/UI/Level/StateBar/BusyHealthBar.cs
Normal file
71
Script/ManagedBusyRabbit/UI/Level/StateBar/BusyHealthBar.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using UnrealSharp.Attributes;
|
||||
using UnrealSharp.BusyRabbit;
|
||||
using UnrealSharp.UMG;
|
||||
namespace UI.Level.StateBar;
|
||||
|
||||
|
||||
[UClass]
|
||||
public class UBusyWidgetHealthBar : UPW_MinimalWidget
|
||||
{
|
||||
[UProperty(PropertyFlags.EditAnywhere)]
|
||||
public float Percent { get; set; }
|
||||
|
||||
[UProperty(PropertyFlags.EditAnywhere)]
|
||||
public float Test { get; set; }
|
||||
|
||||
|
||||
protected UProgressBar? HpBarHurt { get { return GetWidget("HpBarHurt") as UProgressBar; } }
|
||||
protected UProgressBar? HpBarHealth { get { return GetWidget("HpBarHealth") as UProgressBar; } }
|
||||
protected UProgressBar? HpBarNormal { get { return GetWidget("HpBarNormal") as UProgressBar; } }
|
||||
protected USizeBox ? SizeboxMain { get { return GetWidget("SizeboxMain") as USizeBox; } }
|
||||
|
||||
[UProperty(PropertyFlags.EditAnywhere)]
|
||||
public float MaxHp { get; set; }
|
||||
|
||||
[UProperty(PropertyFlags.EditAnywhere)]
|
||||
public float CurHp { get; set; }
|
||||
|
||||
[UProperty(PropertyFlags.EditAnywhere)]
|
||||
public float Width { get; set; }
|
||||
|
||||
[UProperty(PropertyFlags.EditAnywhere)]
|
||||
public float Height { get; set; }
|
||||
|
||||
protected float LastHp = 200;
|
||||
|
||||
|
||||
public override void PreConstruct(bool isDesignTime)
|
||||
{
|
||||
SetHpConfig(CurHp, MaxHp);
|
||||
RefreshHealthBarDisplay();
|
||||
}
|
||||
|
||||
public override void Construct()
|
||||
{
|
||||
base.Construct();
|
||||
SetHpConfig(180, 200);
|
||||
}
|
||||
|
||||
public void SetHpConfig(float CurHp, float MaxHp)
|
||||
{
|
||||
LastHp = CurHp;
|
||||
this.CurHp = CurHp;
|
||||
this.MaxHp = MaxHp;
|
||||
RefreshHealthBarDisplay();
|
||||
}
|
||||
|
||||
|
||||
protected void RefreshHealthBarDisplay()
|
||||
{
|
||||
if (HpBarHealth == null || HpBarHurt == null || HpBarNormal == null || SizeboxMain == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
HpBarHurt.Visibility = ESlateVisibility.Collapsed;
|
||||
HpBarHealth.Visibility = ESlateVisibility.Collapsed;
|
||||
HpBarNormal.Percent = MaxHp != 0 ? (CurHp / MaxHp) : 0;
|
||||
SizeboxMain.WidthOverride = Width;
|
||||
SizeboxMain.HeightOverride = Height;
|
||||
}
|
||||
|
||||
}
|
||||
36
Script/ManagedBusyRabbit/UI/Level/StateBar/BusySatietyBar.cs
Normal file
36
Script/ManagedBusyRabbit/UI/Level/StateBar/BusySatietyBar.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using UnrealSharp.Engine;
|
||||
using UnrealSharp.Attributes;
|
||||
using UnrealSharp.BusyRabbit;
|
||||
using UnrealSharp.UMG;
|
||||
|
||||
namespace UI.Level.StateBar;
|
||||
|
||||
[UClass]
|
||||
public class UBusyWidgetSatietyBar : UPW_MinimalWidget
|
||||
{
|
||||
protected UProgressBar? SatietyBar { get { return GetWidget("SatietyBar") as UProgressBar; } }
|
||||
|
||||
protected float CurrSatiety;
|
||||
protected float MaxSatiety;
|
||||
public void SetSatietyConfig(float CurrSatiety, float MaxSatiety)
|
||||
{
|
||||
this.MaxSatiety = MaxSatiety;
|
||||
this.CurrSatiety = CurrSatiety;
|
||||
RefreshDisplay();
|
||||
}
|
||||
|
||||
public void OnSatietyChanged(float CurSatiety)
|
||||
{
|
||||
this.CurrSatiety = CurSatiety;
|
||||
RefreshDisplay();
|
||||
}
|
||||
|
||||
public void RefreshDisplay()
|
||||
{
|
||||
float Percent = MaxSatiety > 0 ? CurrSatiety / MaxSatiety : 0;
|
||||
if (SatietyBar != null)
|
||||
{
|
||||
SatietyBar.Percent = Percent;
|
||||
}
|
||||
}
|
||||
}
|
||||
55
Script/ManagedBusyRabbit/UI/Level/StateBar/BusyStateBar.cs
Normal file
55
Script/ManagedBusyRabbit/UI/Level/StateBar/BusyStateBar.cs
Normal file
@ -0,0 +1,55 @@
|
||||
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 UBusyRoleStateController? UIController;
|
||||
|
||||
|
||||
public override void Construct()
|
||||
{
|
||||
base.Construct();
|
||||
WBP_SatietyBar.SetSatietyConfig(100, 200);
|
||||
|
||||
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);
|
||||
WBP_SatietyBar.SetSatietyConfig(CurSatiety, MaxSatiety);
|
||||
}
|
||||
|
||||
public override void Destruct()
|
||||
{
|
||||
base.Destruct();
|
||||
UIController.OnAttributeChangeDelegate -= OnAttributeChanged;
|
||||
}
|
||||
|
||||
|
||||
[UFunction()]
|
||||
protected void OnAttributeChanged(FName AttributeName, float NewValue, float OldValue)
|
||||
{
|
||||
if (AttributeName == "Hunger")
|
||||
{
|
||||
WBP_SatietyBar.OnSatietyChanged(NewValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user