From babbfda3cf28dfd98a792cb37e9d45131eb23f1d Mon Sep 17 00:00:00 2001 From: Kingsmedia Date: Mon, 23 May 2022 00:16:27 +0200 Subject: [PATCH] 157 - Healing the Character --- Source/Blaster/Character/BlasterCharacter.cpp | 4 +-- Source/Blaster/Character/BlasterCharacter.h | 9 ++++--- Source/Blaster/Components/BuffComponent.cpp | 25 +++++++++++++++++++ Source/Blaster/Components/BuffComponent.h | 7 ++++++ Source/Blaster/Pickups/HealthPickup.cpp | 7 +++++- 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/Source/Blaster/Character/BlasterCharacter.cpp b/Source/Blaster/Character/BlasterCharacter.cpp index 5887297..f21ae5e 100644 --- a/Source/Blaster/Character/BlasterCharacter.cpp +++ b/Source/Blaster/Character/BlasterCharacter.cpp @@ -628,10 +628,10 @@ void ABlasterCharacter::HideCameraIfCharacterClose() } } -void ABlasterCharacter::OnRep_Health() +void ABlasterCharacter::OnRep_Health(float LastHealth) { UpdateHUDHealth(); - if (Health > 0.f) + if (Health < LastHealth) { PlayHitReactMontage(); } diff --git a/Source/Blaster/Character/BlasterCharacter.h b/Source/Blaster/Character/BlasterCharacter.h index be4b787..4f8a8b3 100644 --- a/Source/Blaster/Character/BlasterCharacter.h +++ b/Source/Blaster/Character/BlasterCharacter.h @@ -40,6 +40,8 @@ public: UFUNCTION(BlueprintImplementableEvent) void ShowSniperScopeWidget(bool bShowScope); + + void UpdateHUDHealth(); protected: virtual void BeginPlay() override; @@ -64,8 +66,7 @@ protected: UFUNCTION() void ReceiveDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* InstigatorController, AActor* DamageCauser); - - void UpdateHUDHealth(); + // Poll for any relevant classes and initialize them void PollInit(); @@ -145,7 +146,7 @@ private: float Health = 100.f; UFUNCTION() - void OnRep_Health(); + void OnRep_Health(float LastHealth); UPROPERTY() class ABlasterPlayerController* BlasterPlayerController; @@ -206,9 +207,11 @@ public: FORCEINLINE bool ShouldRotateRootBone() const { return bRotateRootBone; } FORCEINLINE bool IsEliminated() const { return bEliminated; } FORCEINLINE float GetHealth() const { return Health; } + FORCEINLINE void SetHealth(float Amount) { Health = Amount; } FORCEINLINE float GetMaxHealth() const { return MaxHealth; } ECombatState GetCombatState() const; FORCEINLINE UCombatComponent* GetCombat() const { return Combat; } + FORCEINLINE UBuffComponent* GetBuff() const { return Buff; } FORCEINLINE bool GetDisableGameplay() const { return bDisableGameplay; } FORCEINLINE UAnimMontage* GetReloadMontage() const { return ReloadMontage; } FORCEINLINE UStaticMeshComponent* GetAttachedGrenade() const { return AttachedGrenade; } diff --git a/Source/Blaster/Components/BuffComponent.cpp b/Source/Blaster/Components/BuffComponent.cpp index 861888b..6c4e98a 100644 --- a/Source/Blaster/Components/BuffComponent.cpp +++ b/Source/Blaster/Components/BuffComponent.cpp @@ -3,12 +3,21 @@ #include "BuffComponent.h" +#include "Blaster/Character/BlasterCharacter.h" + UBuffComponent::UBuffComponent() { PrimaryComponentTick.bCanEverTick = true; } +void UBuffComponent::Heal(float HealAmount, float HealingTime) +{ + bHealing = true; + HealingRate = HealAmount / HealingTime; + AmountToHeal += HealAmount; +} + void UBuffComponent::BeginPlay() { @@ -16,10 +25,26 @@ void UBuffComponent::BeginPlay() } +void UBuffComponent::HealRampUp(float DeltaTime) +{ + if (!bHealing || Character == nullptr || Character->IsEliminated()) return; + + const float HealThisFrame = HealingRate * DeltaTime; + Character->SetHealth(FMath::Clamp(Character->GetHealth() + HealThisFrame, 0, Character->GetMaxHealth())); + Character->UpdateHUDHealth(); + AmountToHeal -= HealThisFrame; + + if (AmountToHeal <= 0 || Character->GetHealth() >= Character->GetMaxHealth()) + { + bHealing = false; + AmountToHeal = 0.f; + } +} void UBuffComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + HealRampUp(DeltaTime); } diff --git a/Source/Blaster/Components/BuffComponent.h b/Source/Blaster/Components/BuffComponent.h index b4ec368..ff7d82d 100644 --- a/Source/Blaster/Components/BuffComponent.h +++ b/Source/Blaster/Components/BuffComponent.h @@ -16,12 +16,19 @@ public: UBuffComponent(); friend class ABlasterCharacter; + void Heal(float HealAmount, float HealingTime); + protected: virtual void BeginPlay() override; + void HealRampUp(float DeltaTime); private: UPROPERTY() class ABlasterCharacter* Character; + + bool bHealing = false; + float HealingRate = 0; + float AmountToHeal = 0.f; public: virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; diff --git a/Source/Blaster/Pickups/HealthPickup.cpp b/Source/Blaster/Pickups/HealthPickup.cpp index 0290717..dd8f4b3 100644 --- a/Source/Blaster/Pickups/HealthPickup.cpp +++ b/Source/Blaster/Pickups/HealthPickup.cpp @@ -6,6 +6,7 @@ #include "NiagaraComponent.h" #include "NiagaraFunctionLibrary.h" #include "Blaster/Character/BlasterCharacter.h" +#include "Blaster/Components/BuffComponent.h" AHealthPickup::AHealthPickup() { @@ -22,7 +23,11 @@ void AHealthPickup::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AA ABlasterCharacter* BlasterCharacter = Cast(OtherActor); if (BlasterCharacter) { - + UBuffComponent* Buff = BlasterCharacter->GetBuff(); + if (Buff) + { + Buff->Heal(HealAmount, HealingTime); + } } Destroy();