95 lines
2.7 KiB
C++
95 lines
2.7 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Explosive.h"
|
|
|
|
#include "Components/StaticMeshComponent.h"
|
|
#include "Components/SphereComponent.h"
|
|
#include "GameFramework/Character.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "Sound/SoundCue.h"
|
|
#include "Particles/ParticleSystemComponent.h"
|
|
#include "Engine/World.h"
|
|
#include "Engine/HitResult.h"
|
|
#include "GameFramework/DamageType.h"
|
|
|
|
// Sets default values
|
|
AExplosive::AExplosive() :
|
|
Damage(40.f),
|
|
Exploded(false)
|
|
{
|
|
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
|
|
ExplosiveMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ExplosiveMesh"));
|
|
SetRootComponent(ExplosiveMesh);
|
|
|
|
OverlapSphere = CreateDefaultSubobject<USphereComponent>(TEXT("OverlapSphere"));
|
|
OverlapSphere->SetupAttachment(GetRootComponent());
|
|
}
|
|
|
|
// Called when the game starts or when spawned
|
|
void AExplosive::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
}
|
|
|
|
// Called every frame
|
|
void AExplosive::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
}
|
|
|
|
void AExplosive::Explode(AActor* Shooter, AController* ShooterController)
|
|
{
|
|
Exploded = true;
|
|
|
|
if (ExplosionSound)
|
|
{
|
|
UGameplayStatics::PlaySoundAtLocation(this, ExplosionSound, GetActorLocation());
|
|
}
|
|
|
|
if (ExplosionParticles)
|
|
{
|
|
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionParticles, GetActorLocation(), FRotator(0.0), true);
|
|
}
|
|
|
|
// Apply explosive damage
|
|
TArray<AActor*> OverlappingActors;
|
|
GetOverlappingActors(OverlappingActors, ACharacter::StaticClass());
|
|
|
|
for (auto Actor : OverlappingActors)
|
|
{
|
|
UGameplayStatics::ApplyDamage(Actor, Damage, ShooterController, Shooter, UDamageType::StaticClass());
|
|
}
|
|
|
|
// Other explosives
|
|
OverlappingActors.Empty();
|
|
GetOverlappingActors(OverlappingActors, AExplosive::StaticClass());
|
|
for (auto Actor : OverlappingActors)
|
|
{
|
|
AExplosive* Explosive = Cast<AExplosive>(Actor);
|
|
if (Explosive && Explosive != this && !Explosive->Exploded)
|
|
UGameplayStatics::ApplyDamage(Actor, Damage, ShooterController, Shooter, UDamageType::StaticClass());
|
|
}
|
|
|
|
Destroy();
|
|
}
|
|
|
|
void AExplosive::BulletHit_Implementation(FHitResult HitResult, AActor* Shooter, AController* ShooterController)
|
|
{
|
|
Explode(Shooter, ShooterController);
|
|
}
|
|
|
|
float AExplosive::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator,
|
|
AActor* DamageCauser)
|
|
{
|
|
Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
|
|
|
|
Explode(DamageCauser, EventInstigator);
|
|
return DamageAmount;
|
|
}
|
|
|