****** Header *****
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Kismet/GameplayStatics.h"
#include <vector>
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/PostProcessComponent.h"
#include "Components/TimelineComponent.h"
#include "G_nadeProjectile.generated.h"
UCLASS(config=Game)
class AG_nadeProjectile : public AActor
{
GENERATED_BODY()
/** Sphere collision component */
UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
class USphereComponent* CollisionComp;
/** Projectile movement component */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
class UProjectileMovementComponent* ProjectileMovement;
UPROPERTY(EditAnywhere, Category = "FX")
class UParticleSystem* ExplosionParticies;
UPROPERTY(EditAnywhere, Category = "SOUND_FX")
class USoundCue* ExplosionSound;
UPROPERTY(EditAnywhere, Category = "Projectile")
float Radius = 500.f;
public:
AG_nadeProjectile();
virtual void BeginPlay() override;
UFUNCTION()
void OnDetonate();
/** called when projectile hits something */
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
/** Returns CollisionComp subobject **/
FORCEINLINE class USphereComponent* GetCollisionComp() const { return CollisionComp; }
/** Returns ProjectileMovement subobject **/
FORCEINLINE class UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; }
};
****** CPP *******
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "G_nadeProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Components/SphereComponent.h"
#include "DestructibleActor.h"
#include "DestructibleComponent.h"
#include "DrawDebugHelpers.h"
AG_nadeProjectile::AG_nadeProjectile()
{
// Use a sphere as a simple collision representation
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
CollisionComp->InitSphereRadius(5.0f);
CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
CollisionComp->OnComponentHit.AddDynamic(this, &AG_nadeProjectile::OnHit); // set up a notification for when this component hits something blocking
// Players can't walk on it
CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));
CollisionComp->CanCharacterStepUpOn = ECB_No;
// Set as root component
RootComponent = CollisionComp;
// Use a ProjectileMovementComponent to govern this projectile's movement
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
ProjectileMovement->UpdatedComponent = CollisionComp;
ProjectileMovement->InitialSpeed = 3000.f;
ProjectileMovement->MaxSpeed = 3000.f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->bShouldBounce = true;
// Die after 3 seconds by default
InitialLifeSpan = 3.0f;
}
void AG_nadeProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
// Only add impulse and destroy projectile if we hit a physics
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics())
{
OnDetonate();
}
}
void AG_nadeProjectile::BeginPlay()
{
Super::BeginPlay();
FTimerHandle T_Handle;
//GetWorld()->GetTimerManager().SetTimer(T_Handle, this, &AG_nadeProjectile::OnDetonate, 5.f, false);
}
void AG_nadeProjectile::OnDetonate()
{
UParticleSystemComponent* Explosion = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionParticies, GetActorTransform());
//Explosion->SetRelativeScale3D(FVector(4.f));
//Explosion
//UGameplayStatics::PlaySoundAtLocation(GetWorld(), ExplosionSound, GetActorLocation());
//UGameplayStatics::PlaySoundAtLocation();
TArray<FHitResult> HitActors;
FVector StartTrace = GetActorLocation();
FVector EndTrace = StartTrace;
EndTrace.Z += 300.f;
FCollisionShape CollisionShape;
CollisionShape.ShapeType = ECollisionShape::Sphere;
CollisionShape.SetSphere(Radius);
if (GetWorld()->SweepMultiByChannel(HitActors, StartTrace, EndTrace, FQuat::FQuat(), ECC_WorldStatic, CollisionShape))
{
for (auto Actors = HitActors.CreateIterator(); Actors; Actors++)
{
UStaticMeshComponent* SM = Cast<UStaticMeshComponent>((*Actors).Actor->GetRootComponent());
ADestructibleActor* DA = Cast<ADestructibleActor>((*Actors).GetActor());
if (SM)
{
SM->ApplyRadialDamage(GetActorLocation(), 140, 10, ERadialImpulseFalloff::RIF_Linear, false);
}
else if(DA)
{
DA->GetDestructibleComponent()->ApplyRadialDamage(10.f, Actors->ImpactPoint, 500.f, 3000.f, false);
}
}
}
Destroy();
}