Add Explosive Actor

This commit is contained in:
Louis Hobbs 2023-06-01 02:22:04 +01:00
parent 705d46820c
commit 62d6bc8dbd
2 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,74 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "ExplosiveActor.h"
#include <Kismet/GameplayStatics.h>
#include "NakatomiCharacter.h"
// Sets default values
AExplosiveActor::AExplosiveActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
HealthComponent = CreateDefaultSubobject<UHealthComponent>(TEXT("Health Component"));
HealthComponent->OnDeath.BindUFunction(this, "Explode");
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Static Mesh Component"));
StaticMeshComponent->SetSimulatePhysics(true);
}
// Called when the game starts or when spawned
void AExplosiveActor::BeginPlay()
{
Super::BeginPlay();
}
void AExplosiveActor::Explode()
{
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
if (ExplosionParticleSystem)
{
UGameplayStatics::SpawnEmitterAtLocation(this,
ExplosionParticleSystem,
this->ActorToWorld().GetLocation(),
FRotator::ZeroRotator,
true);
}
TArray<FOverlapResult> outOverlaps;
GetWorld()->OverlapMultiByObjectType(outOverlaps,
ActorToWorld().GetLocation(),
FQuat::Identity,
FCollisionObjectQueryParams::AllObjects,
FCollisionShape::MakeSphere(ExplosionRadius));
for (FOverlapResult Overlaps : outOverlaps)
{
if (auto healthComponent = Overlaps.GetActor()->GetComponentByClass<UHealthComponent>())
{
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<AFieldSystemActor>(FieldSystemActor, transform, SpawnParameters);
if (field)
{
field->Destroy();
}
}
this->Destroy();
}

View File

@ -0,0 +1,55 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "CollisionShape.h"
#include "GameFramework/Actor.h"
#include "HealthComponent.h"
#include "NakatomiFieldSystemActor.h"
#include "ExplosiveActor.generated.h"
UCLASS()
class NAKATOMI_API AExplosiveActor : public AActor
{
GENERATED_BODY()
protected:
UPROPERTY(EditDefaultsOnly)
UStaticMeshComponent* StaticMeshComponent;
UPROPERTY(EditDefaultsOnly)
USoundBase* ExplosionSound;
UPROPERTY(EditDefaultsOnly)
TSubclassOf<class ANakatomiFieldSystemActor> FieldSystemActor;
UPROPERTY(EditDefaultsOnly)
UParticleSystem* ExplosionParticleSystem;
UPROPERTY(EditDefaultsOnly)
float ExplosionRadius = 500.f;
UPROPERTY(EditDefaultsOnly)
float MaxDamage = 150.f;
private:
UPROPERTY(VisibleDefaultsOnly)
UHealthComponent* HealthComponent = nullptr;
public:
// Sets default values for this actor's properties
AExplosiveActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
UFUNCTION()
void Explode();
};