142 - Projectile Grenades

This commit is contained in:
Kingsmedia 2022-05-20 18:12:50 +02:00
parent d2c9cce7a3
commit 8f3a46b63d
10 changed files with 194 additions and 69 deletions

View File

@ -137,7 +137,7 @@ void ABlasterCharacter::MulticastEliminated_Implementation()
GetActorLocation() GetActorLocation()
); );
} }
if (IsLocallyControlled() && GetEquippedWeapon()->GetWeaponType() == EWeaponType::EWT_SniperRifle && IsAiming()) if (IsLocallyControlled() && GetEquippedWeapon() && GetEquippedWeapon()->GetWeaponType() == EWeaponType::EWT_SniperRifle && IsAiming())
{ {
ShowSniperScopeWidget(false); ShowSniperScopeWidget(false);
} }

View File

@ -3,6 +3,7 @@
#include "Projectile.h" #include "Projectile.h"
#include "NiagaraFunctionLibrary.h"
#include "Blaster/Blaster.h" #include "Blaster/Blaster.h"
#include "Components/BoxComponent.h" #include "Components/BoxComponent.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
@ -50,6 +51,27 @@ void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimi
Destroy(); Destroy();
} }
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AProjectile::StartDestroyTimer()
{
GetWorldTimerManager().SetTimer(
DestroyTimer,
this,
&AProjectile::DestroyTimerFinished,
DestroyTime
);
}
void AProjectile::DestroyTimerFinished()
{
Destroy();
}
void AProjectile::Destroyed() void AProjectile::Destroyed()
{ {
Super::Destroyed(); Super::Destroyed();
@ -65,7 +87,43 @@ void AProjectile::Destroyed()
} }
} }
void AProjectile::Tick(float DeltaTime) void AProjectile::SpawnTrailSystem()
{ {
Super::Tick(DeltaTime); if (TrailSystem)
{
TrailSystemComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(
TrailSystem,
GetRootComponent(),
FName(),
GetActorLocation(),
GetActorRotation(),
EAttachLocation::KeepWorldPosition,
false
);
}
}
void AProjectile::ExplodeDamage()
{
APawn* FiringPawn = GetInstigator();
if (FiringPawn && HasAuthority())
{
AController* FiringController = FiringPawn->GetController();
if (FiringController)
{
UGameplayStatics::ApplyRadialDamageWithFalloff(
this,
Damage,
10.f,
GetActorLocation(),
DamageInnerRadius,
DamageOuterRadius,
1.f,
UDamageType::StaticClass(),
TArray<AActor*>(),
this,
FiringController
);
}
}
} }

View File

@ -18,6 +18,10 @@ public:
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
void StartDestroyTimer();
void DestroyTimerFinished();
void SpawnTrailSystem();
void ExplodeDamage();
UFUNCTION() UFUNCTION()
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit); virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
@ -37,6 +41,21 @@ protected:
UPROPERTY(VisibleAnywhere) UPROPERTY(VisibleAnywhere)
class UProjectileMovementComponent* ProjectileMovementComponent; class UProjectileMovementComponent* ProjectileMovementComponent;
UPROPERTY(EditAnywhere)
class UNiagaraSystem* TrailSystem;
UPROPERTY()
class UNiagaraComponent* TrailSystemComponent;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* ProjectileMesh;
UPROPERTY(EditAnywhere)
float DamageInnerRadius = 200.f;
UPROPERTY(EditAnywhere)
float DamageOuterRadius = 500.f;
private: private:
@ -45,4 +64,9 @@ private:
UPROPERTY() UPROPERTY()
class UParticleSystemComponent* TracerComponent; class UParticleSystemComponent* TracerComponent;
FTimerHandle DestroyTimer;
UPROPERTY(EditAnywhere)
float DestroyTime = 3.f;
}; };

View File

@ -0,0 +1,67 @@
// 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(
BlasterCharacter,
Damage / 4,
InstigatorController,
this,
UDamageType::StaticClass()
);
}
}
}
if (BounceSound)
{
UGameplayStatics::PlaySoundAtLocation(
this,
BounceSound,
GetActorLocation()
);
}
}
void AProjectileGrenade::Destroyed()
{
ExplodeDamage();
Super::Destroyed();
}

View File

@ -0,0 +1,33 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Projectile.h"
#include "ProjectileGrenade.generated.h"
/**
*
*/
UCLASS()
class BLASTER_API AProjectileGrenade : public AProjectile
{
GENERATED_BODY()
public:
AProjectileGrenade();
virtual void Destroyed() override;
protected:
virtual void BeginPlay() override;
UFUNCTION()
void OnBounce(const FHitResult& ImpactResult, const FVector& ImpactVelocity);
private:
UPROPERTY(EditAnywhere)
USoundCue* BounceSound;
};

View File

@ -4,7 +4,6 @@
#include "ProjectileRocket.h" #include "ProjectileRocket.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h" #include "NiagaraComponent.h"
#include "RocketMovementComponent.h" #include "RocketMovementComponent.h"
#include "Components/AudioComponent.h" #include "Components/AudioComponent.h"
@ -13,9 +12,9 @@
AProjectileRocket::AProjectileRocket() AProjectileRocket::AProjectileRocket()
{ {
RocketMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Rocket Mesh")); ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Rocket Mesh"));
RocketMesh->SetupAttachment(RootComponent); ProjectileMesh->SetupAttachment(RootComponent);
RocketMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision); ProjectileMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
RocketMovementComponent = CreateDefaultSubobject<URocketMovementComponent>("RocketMovementComponent"); RocketMovementComponent = CreateDefaultSubobject<URocketMovementComponent>("RocketMovementComponent");
RocketMovementComponent->bRotationFollowsVelocity = true; RocketMovementComponent->bRotationFollowsVelocity = true;
@ -35,18 +34,8 @@ void AProjectileRocket::BeginPlay()
CollisionBox->OnComponentHit.AddDynamic(this, &AProjectileRocket::OnHit); CollisionBox->OnComponentHit.AddDynamic(this, &AProjectileRocket::OnHit);
} }
if (TrailSystem) SpawnTrailSystem();
{
TrailSystemComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(
TrailSystem,
GetRootComponent(),
FName(),
GetActorLocation(),
GetActorRotation(),
EAttachLocation::KeepWorldPosition,
false
);
}
if (ProjectileLoop && LoopingSoundAttenuation) if (ProjectileLoop && LoopingSoundAttenuation)
{ {
ProjectileLoopComponent = UGameplayStatics::SpawnSoundAttached( ProjectileLoopComponent = UGameplayStatics::SpawnSoundAttached(
@ -66,45 +55,15 @@ void AProjectileRocket::BeginPlay()
} }
} }
void AProjectileRocket::DestroyTimerFinished()
{
Destroy();
}
void AProjectileRocket::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) void AProjectileRocket::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{ {
if (OtherActor == GetOwner()) if (OtherActor == GetOwner())
{ {
return; return;
} }
APawn* FiringPawn = GetInstigator(); ExplodeDamage();
if (FiringPawn && HasAuthority())
{
AController* FiringController = FiringPawn->GetController();
if (FiringController)
{
UGameplayStatics::ApplyRadialDamageWithFalloff(
this,
Damage,
10.f,
GetActorLocation(),
200.f,
500.f,
1.f,
UDamageType::StaticClass(),
TArray<AActor*>(),
this,
FiringController
);
}
}
GetWorldTimerManager().SetTimer( StartDestroyTimer();
DestroyTimer,
this,
&AProjectileRocket::DestroyTimerFinished,
DestroyTime
);
if (ImpactParticles) if (ImpactParticles)
{ {
@ -114,9 +73,9 @@ void AProjectileRocket::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor,
{ {
UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation()); UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());
} }
if (RocketMesh) if (ProjectileMesh)
{ {
RocketMesh->SetVisibility(false); ProjectileMesh->SetVisibility(false);
} }
if (CollisionBox) if (CollisionBox)
{ {

View File

@ -20,14 +20,7 @@ protected:
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) override; virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) override;
virtual void BeginPlay() override; virtual void BeginPlay() override;
void DestroyTimerFinished();
UPROPERTY(EditAnywhere)
class UNiagaraSystem* TrailSystem;
UPROPERTY()
class UNiagaraComponent* TrailSystemComponent;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
USoundCue* ProjectileLoop; USoundCue* ProjectileLoop;
@ -40,13 +33,4 @@ protected:
UPROPERTY(VisibleAnywhere) UPROPERTY(VisibleAnywhere)
class URocketMovementComponent* RocketMovementComponent; class URocketMovementComponent* RocketMovementComponent;
private:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* RocketMesh;
FTimerHandle DestroyTimer;
UPROPERTY(EditAnywhere)
float DestroyTime = 3.f;
}; };