From fe8ee2e86722da99d0045d2b7794f8d7d0ed7ed6 Mon Sep 17 00:00:00 2001 From: LongLy Date: Wed, 15 Jan 2025 11:00:34 -0700 Subject: [PATCH] Implemented CombatAttribute --- .../Attributes/OLSCombatAttributeSet.cpp | 30 ++++++++++++ .../Attributes/OLSHealthAttributeSet.cpp | 2 +- .../ols/Private/Characters/OLSCharacter.cpp | 2 +- Source/ols/Private/Player/OLSPlayerState.cpp | 13 +++++ .../Attributes/OLSCombatAttributeSet.h | 49 +++++++++++++++++++ Source/ols/Public/Player/OLSPlayerState.h | 10 ++++ 6 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 Source/ols/Private/AbilitySystem/Attributes/OLSCombatAttributeSet.cpp create mode 100644 Source/ols/Public/AbilitySystem/Attributes/OLSCombatAttributeSet.h diff --git a/Source/ols/Private/AbilitySystem/Attributes/OLSCombatAttributeSet.cpp b/Source/ols/Private/AbilitySystem/Attributes/OLSCombatAttributeSet.cpp new file mode 100644 index 0000000..14e575a --- /dev/null +++ b/Source/ols/Private/AbilitySystem/Attributes/OLSCombatAttributeSet.cpp @@ -0,0 +1,30 @@ +// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action. + + +#include "AbilitySystem/Attributes/OLSCombatAttributeSet.h" + +#include "Net/UnrealNetwork.h" + +UOLSCombatAttributeSet::UOLSCombatAttributeSet() + : BaseDamage(0.0f) + , BaseHeal(0.0f) +{ +} + +void UOLSCombatAttributeSet::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const +{ + Super::GetLifetimeReplicatedProps(OutLifetimeProps); + + DOREPLIFETIME_CONDITION_NOTIFY(ThisClass, BaseDamage, COND_OwnerOnly, REPNOTIFY_Always); + DOREPLIFETIME_CONDITION_NOTIFY(ThisClass, BaseHeal, COND_OwnerOnly, REPNOTIFY_Always); +} + +void UOLSCombatAttributeSet::OnRep_BaseDamage(const FGameplayAttributeData& oldValue) +{ + GAMEPLAYATTRIBUTE_REPNOTIFY(ThisClass, BaseDamage, oldValue); +} + +void UOLSCombatAttributeSet::OnRep_BaseHeal(const FGameplayAttributeData& oldValue) +{ + GAMEPLAYATTRIBUTE_REPNOTIFY(ThisClass, BaseHeal, oldValue); +} diff --git a/Source/ols/Private/AbilitySystem/Attributes/OLSHealthAttributeSet.cpp b/Source/ols/Private/AbilitySystem/Attributes/OLSHealthAttributeSet.cpp index 8d505cc..5b27a6c 100644 --- a/Source/ols/Private/AbilitySystem/Attributes/OLSHealthAttributeSet.cpp +++ b/Source/ols/Private/AbilitySystem/Attributes/OLSHealthAttributeSet.cpp @@ -229,7 +229,7 @@ void UOLSHealthAttributeSet::PostAttributeChange(const FGameplayAttribute& attri } void UOLSHealthAttributeSet::ClampAttribute(const FGameplayAttribute& attribute, float& outNewValue) const -{ +{ if (attribute == GetHealthAttribute()) { // Do not allow health to go negative or above max health. diff --git a/Source/ols/Private/Characters/OLSCharacter.cpp b/Source/ols/Private/Characters/OLSCharacter.cpp index 348d511..fa40841 100644 --- a/Source/ols/Private/Characters/OLSCharacter.cpp +++ b/Source/ols/Private/Characters/OLSCharacter.cpp @@ -18,7 +18,7 @@ AOLSCharacter::AOLSCharacter(const FObjectInitializer& objectInitializer) : Supe PrimaryActorTick.bCanEverTick = false; PrimaryActorTick.bStartWithTickEnabled = false; - NetCullDistanceSquared = 900000000.0f; + SetNetCullDistanceSquared(900000000.0f); LocomotionComponent = CreateDefaultSubobject(TEXT("Locomotion Component")); } diff --git a/Source/ols/Private/Player/OLSPlayerState.cpp b/Source/ols/Private/Player/OLSPlayerState.cpp index 01f078e..28080c2 100644 --- a/Source/ols/Private/Player/OLSPlayerState.cpp +++ b/Source/ols/Private/Player/OLSPlayerState.cpp @@ -4,6 +4,8 @@ #include "Player/OLSPlayerState.h" #include "AbilitySystem/OLSAbilitySystemComponent.h" +#include "AbilitySystem/Attributes/OLSCombatAttributeSet.h" +#include "AbilitySystem/Attributes/OLSHealthAttributeSet.h" #include "Components/GameFrameworkComponentManager.h" #include "DataAssets/OLSAbilitySetPrimaryDataAsset.h" #include "GameModes/OLSExperienceManagerComponent.h" @@ -18,6 +20,17 @@ const FName AOLSPlayerState::NAME_OLSAbilityReady("OLSAbilitiesReady"); AOLSPlayerState::AOLSPlayerState(const FObjectInitializer& objectInitializer) : Super(objectInitializer) { // Create attribute sets here. + + AbilitySystemComponent = objectInitializer.CreateDefaultSubobject(this, TEXT("AbilitySystemComponent")); + AbilitySystemComponent->SetIsReplicated(true); + AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed); + + // These attribute sets will be detected by AbilitySystemComponent::InitializeComponent. Keeping a reference so that the sets don't get garbage collected before that. + HealthSet = CreateDefaultSubobject(TEXT("HealthSet")); + CombatSet = CreateDefaultSubobject(TEXT("CombatSet")); + + // AbilitySystemComponent needs to be updated at a high frequency. + SetNetUpdateFrequency(100.0f); } void AOLSPlayerState::SetPawnData(const UOLSPawnDataAsset* pawnData) diff --git a/Source/ols/Public/AbilitySystem/Attributes/OLSCombatAttributeSet.h b/Source/ols/Public/AbilitySystem/Attributes/OLSCombatAttributeSet.h new file mode 100644 index 0000000..864f77d --- /dev/null +++ b/Source/ols/Public/AbilitySystem/Attributes/OLSCombatAttributeSet.h @@ -0,0 +1,49 @@ +// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action. + +#pragma once + +#include "CoreMinimal.h" +#include "AbilitySystemComponent.h" +#include "OLSAttributeSetBase.h" +#include "OLSCombatAttributeSet.generated.h" + +/** + * UOLSCombatAttributeSet + * + * Class that defines attributes that are necessary for applying damage or healing. + * Attribute examples include: damage, healing, attack power, and shield penetrations. + */ +UCLASS(BlueprintType) +class OLS_API UOLSCombatAttributeSet : public UOLSAttributeSetBase +{ + GENERATED_BODY() + +public: + + UOLSCombatAttributeSet(); + + //~ Begin UObject interface. + virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; + //~ End UObject interface. + + ATTRIBUTE_ACCESSORS(ThisClass, BaseDamage); + ATTRIBUTE_ACCESSORS(ThisClass, BaseHeal); + +protected: + + UFUNCTION() + void OnRep_BaseDamage(const FGameplayAttributeData& oldValue); + + UFUNCTION() + void OnRep_BaseHeal(const FGameplayAttributeData& oldValue); + +private: + + // The base amount of damage to apply in the damage execution. + UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_BaseDamage, Category = "OLS|Combat", Meta = (AllowPrivateAccess = true)) + FGameplayAttributeData BaseDamage; + + // The base amount of healing to apply in the heal execution. + UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_BaseHeal, Category = "OLS|Combat", Meta = (AllowPrivateAccess = true)) + FGameplayAttributeData BaseHeal; +}; diff --git a/Source/ols/Public/Player/OLSPlayerState.h b/Source/ols/Public/Player/OLSPlayerState.h index 72b0ef0..c1d7dd6 100644 --- a/Source/ols/Public/Player/OLSPlayerState.h +++ b/Source/ols/Public/Player/OLSPlayerState.h @@ -85,4 +85,14 @@ private: UPROPERTY(Replicated) FOLSGameplayTagStackContainer StatTags; + +private: + + // Health attribute set used by this actor. + UPROPERTY() + TObjectPtr HealthSet = nullptr; + + // Combat attribute set used by this actor. + UPROPERTY() + TObjectPtr CombatSet = nullptr; };