blaster/Source/Blaster/Weapon/ProjectileGrenade.cpp

67 lines
1.7 KiB
C++
Raw Normal View History

2022-05-20 16:12:50 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileGrenade.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
AProjectileGrenade::AProjectileGrenade()
{
ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Grenade Mesh"));
ProjectileMesh->SetupAttachment(RootComponent);
ProjectileMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->bRotationFollowsVelocity = true;
ProjectileMovementComponent->SetIsReplicated(true);
ProjectileMovementComponent->bShouldBounce = true;
}
void AProjectileGrenade::BeginPlay()
{
AActor::BeginPlay();
SpawnTrailSystem();
StartDestroyTimer();
ProjectileMovementComponent->OnProjectileBounce.AddDynamic(this, &AProjectileGrenade::OnBounce);
}
void AProjectileGrenade::OnBounce(const FHitResult& ImpactResult, const FVector& ImpactVelocity)
{
if (ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(ImpactResult.GetActor()))
{
const APawn* FiringPawn = GetInstigator();
if (FiringPawn && HasAuthority())
{
AController* InstigatorController = FiringPawn->GetController();
if (InstigatorController)
{
UGameplayStatics::ApplyDamage(
2022-05-22 12:11:35 +00:00
BlasterCharacter,
Damage / 4,
InstigatorController,
this,
UDamageType::StaticClass()
);
2022-05-20 16:12:50 +00:00
}
}
}
if (BounceSound)
{
UGameplayStatics::PlaySoundAtLocation(
this,
BounceSound,
GetActorLocation()
);
}
}
void AProjectileGrenade::Destroyed()
{
ExplodeDamage();
Super::Destroyed();
}