From d8ca7f001ce9f4fae577f0de3f3fd1c341b4f588 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Wed, 14 Jun 2023 22:57:44 +0100 Subject: [PATCH] Add logic to Explode in Demolition Character --- Source/Nakatomi/DemolitionCharacter.cpp | 44 ++++++++++++++++++++++++- Source/Nakatomi/DemolitionCharacter.h | 16 +++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Source/Nakatomi/DemolitionCharacter.cpp b/Source/Nakatomi/DemolitionCharacter.cpp index 96c141a..50bd389 100644 --- a/Source/Nakatomi/DemolitionCharacter.cpp +++ b/Source/Nakatomi/DemolitionCharacter.cpp @@ -2,8 +2,50 @@ #include "DemolitionCharacter.h" +#include void ADemolitionCharacter::Explode() { - UE_LOG(LogTemp, Warning, TEXT("BANG!")); + GetHealthComponent()->TakeDamage(this, this->GetHealthComponent()->GetMaxHealth(), nullptr, nullptr, nullptr); + + FActorSpawnParameters SpawnParameters; + SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; + + if (ExplosionParticleSystem) + { + UGameplayStatics::SpawnEmitterAtLocation(this, + ExplosionParticleSystem, + this->ActorToWorld().GetLocation(), + FRotator::ZeroRotator, + true); + } + + TArray outOverlaps; + GetWorld()->OverlapMultiByObjectType(outOverlaps, + ActorToWorld().GetLocation(), + FQuat::Identity, + FCollisionObjectQueryParams::AllObjects, + FCollisionShape::MakeSphere(ExplosionRadius)); + + for (FOverlapResult Overlaps : outOverlaps) + { + if (auto healthComponent = Overlaps.GetActor()->GetComponentByClass()) + { + float distance = FVector::Distance(ActorToWorld().GetLocation(), Overlaps.GetActor()->ActorToWorld().GetLocation()); + float scale = 1.f - (distance / ExplosionRadius); + healthComponent->TakeDamage(Overlaps.GetActor(), scale * MaxDamage, nullptr, nullptr, this); + } + } + + if (FieldSystemActor) + { + FTransform transform; + transform.SetLocation(this->ActorToWorld().GetLocation()); + auto field = GetWorld()->SpawnActor(FieldSystemActor, transform, SpawnParameters); + + if (field) + { + field->Destroy(); + } + } } diff --git a/Source/Nakatomi/DemolitionCharacter.h b/Source/Nakatomi/DemolitionCharacter.h index ed023da..b7e305d 100644 --- a/Source/Nakatomi/DemolitionCharacter.h +++ b/Source/Nakatomi/DemolitionCharacter.h @@ -14,6 +14,22 @@ class NAKATOMI_API ADemolitionCharacter : public AEnemyCharacter { GENERATED_BODY() +protected: + UPROPERTY(EditDefaultsOnly) + USoundBase* ExplosionSound; + + UPROPERTY(EditDefaultsOnly) + TSubclassOf FieldSystemActor; + + UPROPERTY(EditDefaultsOnly) + UParticleSystem* ExplosionParticleSystem; + + UPROPERTY(EditDefaultsOnly) + float ExplosionRadius = 500.f; + + UPROPERTY(EditDefaultsOnly) + float MaxDamage = 150.f; + public: UFUNCTION()