From 16869188b9ef65be1f59ec02afeb71e37846dcea Mon Sep 17 00:00:00 2001 From: Caleb Buhungiro Date: Sat, 13 Sep 2025 11:17:31 +0800 Subject: [PATCH] add respawn with health regen --- Content/Player/CPlayerCharacter_BP.uasset | 4 ++-- .../Crunch/Private/Character/CCharacter.cpp | 8 +++++++ .../Private/GAS/CAbilitySystemComponent.cpp | 22 ++++++++++++++----- .../Public/GAS/CAbilitySystemComponent.h | 5 +++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Content/Player/CPlayerCharacter_BP.uasset b/Content/Player/CPlayerCharacter_BP.uasset index a047ef4..78aa692 100644 --- a/Content/Player/CPlayerCharacter_BP.uasset +++ b/Content/Player/CPlayerCharacter_BP.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:173290a0f6c780f86f56a6809c009f915f3968d75364132f3c4914af17a69247 -size 82378 +oid sha256:84e458ba9629c1d92b00c9e939804842120a43af958a0906522da6c0bbc59cf1 +size 83596 diff --git a/Source/Crunch/Private/Character/CCharacter.cpp b/Source/Crunch/Private/Character/CCharacter.cpp index 27e010a..9d16e4f 100644 --- a/Source/Crunch/Private/Character/CCharacter.cpp +++ b/Source/Crunch/Private/Character/CCharacter.cpp @@ -162,6 +162,14 @@ void ACCharacter::StartDeathSequence() void ACCharacter::ReSpawn() { OnRespawn(); + GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); + GetCharacterMovement()->SetMovementMode(MOVE_Walking); + GetMesh()->GetAnimInstance()->StopAllMontages(0.f); + SetStatusGaugeEnabled(true); + if (CAbilitySystemComponent) + { + CAbilitySystemComponent->ApplyFullStatEffect(); + } } void ACCharacter::OnDead() diff --git a/Source/Crunch/Private/GAS/CAbilitySystemComponent.cpp b/Source/Crunch/Private/GAS/CAbilitySystemComponent.cpp index 978aff7..4ab2e75 100644 --- a/Source/Crunch/Private/GAS/CAbilitySystemComponent.cpp +++ b/Source/Crunch/Private/GAS/CAbilitySystemComponent.cpp @@ -24,8 +24,7 @@ void UCAbilitySystemComponent::ApplyInitialEffects() * const auto Effect{EffectClass->GetDefaultObject()}; * ApplyGameplayEffectToSelf(Effect, 0.f, MakeEffectContext()); **/ - auto GameplayEffectSpecHandle{MakeOutgoingSpec(EffectClass, 1, MakeEffectContext())}; - ApplyGameplayEffectSpecToSelf(*GameplayEffectSpecHandle.Data.Get()); + AuthApplyGameplayEffect(EffectClass); } } @@ -55,10 +54,23 @@ void UCAbilitySystemComponent::GiveInitialAbilities() void UCAbilitySystemComponent::HealthUpdated(const FOnAttributeChangeData& ChangeData) { - if (!GetOwner() && !GetOwner()->HasAuthority()) return; + // if (!GetOwner() && !GetOwner()->HasAuthority()) return; if (ChangeData.NewValue <= 0 && DeathEffectClass) { - const auto GameplayEffectSpecHandle{ MakeOutgoingSpec(DeathEffectClass, 1, MakeEffectContext() )}; - ApplyGameplayEffectSpecToSelf(*GameplayEffectSpecHandle.Data.Get()); + AuthApplyGameplayEffect(DeathEffectClass); } } + +void UCAbilitySystemComponent::ApplyFullStatEffect() +{ + if (!IsValid(FullStatEffectClass)) return; + AuthApplyGameplayEffect(FullStatEffectClass); +} + +void UCAbilitySystemComponent::AuthApplyGameplayEffect(const TSubclassOf EffectClass, const int Level) +{ + if (!GetOwner() || !GetOwner()->HasAuthority()) return; + + const auto FullStatEffectSpec { MakeOutgoingSpec(EffectClass, Level, MakeEffectContext())}; + ApplyGameplayEffectSpecToSelf(*FullStatEffectSpec.Data.Get()); +} diff --git a/Source/Crunch/Public/GAS/CAbilitySystemComponent.h b/Source/Crunch/Public/GAS/CAbilitySystemComponent.h index 8cc6451..6810e5d 100644 --- a/Source/Crunch/Public/GAS/CAbilitySystemComponent.h +++ b/Source/Crunch/Public/GAS/CAbilitySystemComponent.h @@ -20,10 +20,15 @@ public: void ApplyInitialEffects(); void GiveInitialAbilities(); void HealthUpdated(const FOnAttributeChangeData& ChangeData); + void ApplyFullStatEffect(); private: + void AuthApplyGameplayEffect(TSubclassOf EffectClass, int Level = 1); UPROPERTY(EditDefaultsOnly, Category="Gameplay Effects") TArray> InitialEffects; + + UPROPERTY(EditDefaultsOnly, Category="Gameplay Effects") + TSubclassOf FullStatEffectClass; UPROPERTY(EditDefaultsOnly, Category="Gameplay Effects") TSubclassOf DeathEffectClass;