diff --git a/Content/Player/CPlayerController_BP.uasset b/Content/Player/CPlayerController_BP.uasset index d4c6d8f..45b5b5c 100644 --- a/Content/Player/CPlayerController_BP.uasset +++ b/Content/Player/CPlayerController_BP.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2a746677bfcd5e74d0ddccdfdbb31c17187bc1964cbac40acdf833fb94444d23 -size 19955 +oid sha256:0db44a67b2b3d5caa090645a115025eb1a245a6e3bdab0f6e989a7b336dc11ad +size 20848 diff --git a/Content/Widgets/Gameplay/GameplayWidget_WBP.uasset b/Content/Widgets/Gameplay/GameplayWidget_WBP.uasset new file mode 100644 index 0000000..5590627 --- /dev/null +++ b/Content/Widgets/Gameplay/GameplayWidget_WBP.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cfdedd193788a44ba5fb55d71b3de2db58d31a0ba038b907d4433437602f4af3 +size 26973 diff --git a/Content/Widgets/Gameplay/ValueGauge/ValueGuage_WBP.uasset b/Content/Widgets/Gameplay/ValueGauge/ValueGuage_WBP.uasset index 0891acc..651ba94 100644 --- a/Content/Widgets/Gameplay/ValueGauge/ValueGuage_WBP.uasset +++ b/Content/Widgets/Gameplay/ValueGauge/ValueGuage_WBP.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2ce43b8288419329a4cfef7f787aab2145506922f4c790583045a9a5b4c162ff -size 26430 +oid sha256:405ca78ef8d34a8352af6e4a72ec947de04b5ca87ef32bbd84f9b54cd1472c44 +size 28989 diff --git a/Source/Crunch/Private/Player/CPlayerController.cpp b/Source/Crunch/Private/Player/CPlayerController.cpp index 21bdc34..edaec40 100644 --- a/Source/Crunch/Private/Player/CPlayerController.cpp +++ b/Source/Crunch/Private/Player/CPlayerController.cpp @@ -3,7 +3,9 @@ #include "Crunch/Public/Player/CPlayerController.h" +#include "Blueprint/UserWidget.h" #include "Character/CPlayerCharacter.h" +#include "Widgets/GameplayWidget.h" void ACPlayerController::OnPossess(APawn* InPawn) { @@ -20,5 +22,13 @@ void ACPlayerController::AcknowledgePossession(APawn* P) if (CPlayerCharacter = Cast(P); IsValid(CPlayerCharacter)) { CPlayerCharacter->ClientSideInit(); + SpawnGameplayWidget(); } } + +void ACPlayerController::SpawnGameplayWidget() +{ + if (!IsLocalPlayerController()) return; + GameplayWidget = CreateWidget(this, GameplayWidgetClass); + if (GameplayWidget) GameplayWidget->AddToViewport(); +} diff --git a/Source/Crunch/Private/Widgets/GameplayWidget.cpp b/Source/Crunch/Private/Widgets/GameplayWidget.cpp new file mode 100644 index 0000000..4955e3a --- /dev/null +++ b/Source/Crunch/Private/Widgets/GameplayWidget.cpp @@ -0,0 +1,27 @@ +// Multiplayer By Caleb + + +#include "Widgets/GameplayWidget.h" + +#include "AbilitySystemBlueprintLibrary.h" +#include "GAS/CAttributeSet.h" +#include "Widgets/ValueGauge.h" + +void UGameplayWidget::NativeConstruct() +{ + Super::NativeConstruct(); + OwnerAbilitySystemComponent = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetOwningPlayerPawn()); + if (OwnerAbilitySystemComponent) + { + HealthBar->SetAndBoundToGameplayAttribute( + OwnerAbilitySystemComponent, + UCAttributeSet::GetHealthAttribute(), + UCAttributeSet::GetMaxHealthAttribute() + ); + ManaBar->SetAndBoundToGameplayAttribute( + OwnerAbilitySystemComponent, + UCAttributeSet::GetManaAttribute(), + UCAttributeSet::GetMaxManaAttribute() + ); + } +} diff --git a/Source/Crunch/Private/Widgets/ValueGauge.cpp b/Source/Crunch/Private/Widgets/ValueGauge.cpp index 4a7393d..df6bdf2 100644 --- a/Source/Crunch/Private/Widgets/ValueGauge.cpp +++ b/Source/Crunch/Private/Widgets/ValueGauge.cpp @@ -3,6 +3,7 @@ #include "Widgets/ValueGauge.h" +#include "AbilitySystemComponent.h" #include "Components/ProgressBar.h" #include "Components/TextBlock.h" @@ -12,8 +13,27 @@ void UValueGauge::NativePreConstruct() ProgressBar->SetFillColorAndOpacity(BarColor); } +void UValueGauge::SetAndBoundToGameplayAttribute(UAbilitySystemComponent* AbilitySystemComponent, + const FGameplayAttribute& Attribute, + const FGameplayAttribute& MaxAttribute) +{ + if (AbilitySystemComponent) + { + bool bFound{false}; + const float Value{AbilitySystemComponent->GetGameplayAttributeValue(Attribute, bFound)}; + const float MaxValue{AbilitySystemComponent->GetGameplayAttributeValue(MaxAttribute, bFound)}; + if (bFound) SetValue(Value, MaxValue); + AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(Attribute).AddUObject( + this, &ThisClass::ValueChanged); + AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(MaxAttribute).AddUObject( + this, &ThisClass::MaxValueChanged); + } +} + void UValueGauge::SetValue(float NewValue, float NewMaxValue) { + CachedValue = NewValue; + CachedMaxValue = NewMaxValue; if (NewMaxValue == 0) { UE_LOG(LogTemp, Warning, TEXT("Value Gauge: %s, NewMaxValue can't be 0"), *GetName()); @@ -29,3 +49,13 @@ void UValueGauge::SetValue(float NewValue, float NewMaxValue) FText::AsNumber(NewMaxValue, &FormatOps) )); } + +void UValueGauge::ValueChanged(const FOnAttributeChangeData& ChangedData) +{ + SetValue(ChangedData.NewValue, CachedMaxValue); +} + +void UValueGauge::MaxValueChanged(const FOnAttributeChangeData& ChangedData) +{ + SetValue(CachedValue, ChangedData.NewValue); +} diff --git a/Source/Crunch/Public/GAS/CAttributeSet.h b/Source/Crunch/Public/GAS/CAttributeSet.h index e4886aa..f4ad025 100644 --- a/Source/Crunch/Public/GAS/CAttributeSet.h +++ b/Source/Crunch/Public/GAS/CAttributeSet.h @@ -39,23 +39,24 @@ class CRUNCH_API UCAttributeSet : public UAttributeSet public: virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; + + ATTRIBUTE_ACCESSORS(UCAttributeSet, Health); + ATTRIBUTE_ACCESSORS(UCAttributeSet, MaxHealth); + ATTRIBUTE_ACCESSORS(UCAttributeSet, Mana); + ATTRIBUTE_ACCESSORS(UCAttributeSet, MaxMana); private: UPROPERTY(ReplicatedUsing=OnRep_Health) FGameplayAttributeData Health; - ATTRIBUTE_ACCESSORS(UCAttributeSet, Health); UPROPERTY(ReplicatedUsing=OnRep_MaxHealth) FGameplayAttributeData MaxHealth; - ATTRIBUTE_ACCESSORS(UCAttributeSet, MaxHealth); UPROPERTY(ReplicatedUsing=OnRep_Mana) FGameplayAttributeData Mana; - ATTRIBUTE_ACCESSORS(UCAttributeSet, Mana); UPROPERTY(ReplicatedUsing=OnRep_MaxMana) FGameplayAttributeData MaxMana; - ATTRIBUTE_ACCESSORS(UCAttributeSet, MaxMana); UFUNCTION() void OnRep_Health(const FGameplayAttributeData& OldValue); diff --git a/Source/Crunch/Public/Player/CPlayerController.h b/Source/Crunch/Public/Player/CPlayerController.h index 9d17b89..97144ff 100644 --- a/Source/Crunch/Public/Player/CPlayerController.h +++ b/Source/Crunch/Public/Player/CPlayerController.h @@ -6,6 +6,7 @@ #include "GameFramework/PlayerController.h" #include "CPlayerController.generated.h" +class UGameplayWidget; class ACPlayerCharacter; UCLASS() @@ -18,6 +19,12 @@ public: virtual void AcknowledgePossession(APawn* P) override; private: + void SpawnGameplayWidget(); + UPROPERTY() TObjectPtr CPlayerCharacter; + UPROPERTY(EditDefaultsOnly, Category= "UI") + TSubclassOf GameplayWidgetClass; + UPROPERTY() + TObjectPtr GameplayWidget; }; diff --git a/Source/Crunch/Public/Widgets/GameplayWidget.h b/Source/Crunch/Public/Widgets/GameplayWidget.h new file mode 100644 index 0000000..62fbd74 --- /dev/null +++ b/Source/Crunch/Public/Widgets/GameplayWidget.h @@ -0,0 +1,29 @@ +// Multiplayer By Caleb + +#pragma once + +#include "CoreMinimal.h" +#include "Blueprint/UserWidget.h" +#include "GameplayWidget.generated.h" + +class UAbilitySystemComponent; +class UValueGauge; +/** + * + */ +UCLASS() +class CRUNCH_API UGameplayWidget : public UUserWidget +{ + GENERATED_BODY() + +public: + virtual void NativeConstruct() override; + +private: + UPROPERTY(meta=(BindWidget)) + TObjectPtr HealthBar; + UPROPERTY(meta=(BindWidget)) + TObjectPtr ManaBar; + UPROPERTY() + TObjectPtr OwnerAbilitySystemComponent; +}; diff --git a/Source/Crunch/Public/Widgets/ValueGauge.h b/Source/Crunch/Public/Widgets/ValueGauge.h index 5c95475..160c1da 100644 --- a/Source/Crunch/Public/Widgets/ValueGauge.h +++ b/Source/Crunch/Public/Widgets/ValueGauge.h @@ -6,6 +6,9 @@ #include "Blueprint/UserWidget.h" #include "ValueGauge.generated.h" +class UAbilitySystemComponent; +struct FOnAttributeChangeData; +struct FGameplayAttribute; class UTextBlock; class UProgressBar; /** @@ -17,14 +20,22 @@ class CRUNCH_API UValueGauge : public UUserWidget GENERATED_BODY() public: virtual void NativePreConstruct() override; + void SetAndBoundToGameplayAttribute(UAbilitySystemComponent* AbilitySystemComponent, const FGameplayAttribute& Attribute, const FGameplayAttribute& MaxAttribute); void SetValue(float NewValue, float NewMaxValue); private: + + void ValueChanged(const FOnAttributeChangeData& ChangedData); + void MaxValueChanged(const FOnAttributeChangeData& ChangedData); + + float CachedValue; + float CachedMaxValue; + UPROPERTY(EditAnywhere, Category="Visual") FLinearColor BarColor; - UPROPERTY(EditAnywhere, meta=(BindWidget)) + UPROPERTY(VisibleAnywhere, meta=(BindWidget)) TObjectPtr ProgressBar; - UPROPERTY(EditAnywhere, meta=(BindWidget)) + UPROPERTY(VisibleAnywhere, meta=(BindWidget)) TObjectPtr ValueText;