Nakatomi/Source/Nakatomi/ExplosiveActor.cpp

74 lines
2.4 KiB
C++
Raw Normal View History

2023-06-01 03:22:04 +02:00
// 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()
{
2023-06-23 22:06:18 +02:00
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
2023-06-01 03:22:04 +02:00
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)
{
2023-06-23 22:06:18 +02:00
UGameplayStatics::SpawnEmitterAtLocation(this,
ExplosionParticleSystem,
this->ActorToWorld().GetLocation(),
FRotator::ZeroRotator,
true);
2023-06-01 03:22:04 +02:00
}
2023-06-23 22:06:18 +02:00
2023-06-01 03:22:04 +02:00
TArray<FOverlapResult> outOverlaps;
2023-06-23 22:06:18 +02:00
GetWorld()->OverlapMultiByObjectType(outOverlaps,
ActorToWorld().GetLocation(),
FQuat::Identity,
FCollisionObjectQueryParams::AllObjects,
FCollisionShape::MakeSphere(ExplosionRadius));
2023-06-01 03:22:04 +02:00
for (FOverlapResult Overlaps : outOverlaps)
{
if (auto healthComponent = Overlaps.GetActor()->GetComponentByClass<UHealthComponent>())
{
2023-06-23 22:06:18 +02:00
float distance = FVector::Distance(ActorToWorld().GetLocation(),
Overlaps.GetActor()->ActorToWorld().GetLocation());
2023-06-01 03:22:04 +02:00
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();
}