diff --git a/Source/Nakatomi/EnemyCharacter.cpp b/Source/Nakatomi/EnemyCharacter.cpp index 9042fe5..2fc41ef 100644 --- a/Source/Nakatomi/EnemyCharacter.cpp +++ b/Source/Nakatomi/EnemyCharacter.cpp @@ -4,6 +4,7 @@ #include "EnemyAIController.h" #include "EnemyHealthComponent.h" #include "InteractableComponent.h" +#include "NiagaraFunctionLibrary.h" #include "BehaviorTree/BehaviorTree.h" #include "BehaviorTree/BlackboardComponent.h" #include "BehaviorTree/BlackboardData.h" @@ -33,8 +34,9 @@ void AEnemyCharacter::OnFire() CurrentWeapon->SetCurrentWeaponStatus(Firing); TArray Hits = TArray(); - CalculateHits(&Hits); - ProcessHits(Hits); + FVector direction = FVector::ZeroVector; + CalculateHits(&Hits, &direction); + ProcessHits(Hits, direction); CurrentWeapon->PlayFireSoundAtLocation(GetActorLocation()); @@ -68,7 +70,7 @@ void AEnemyCharacter::PlayOnFireAnimations() Super::PlayOnFireAnimations(); } -void AEnemyCharacter::CalculateHits(TArray* hits) +void AEnemyCharacter::CalculateHits(TArray* hits, FVector* dir) { // Set up randomness const int32 RandomSeed = FMath::Rand(); @@ -108,7 +110,7 @@ void AEnemyCharacter::CalculateHits(TArray* hits) } } -void AEnemyCharacter::ProcessHits(TArray hits) +void AEnemyCharacter::ProcessHits(TArray hits, FVector dir) { for (FHitResult Hit : hits) { @@ -117,10 +119,18 @@ void AEnemyCharacter::ProcessHits(TArray hits) SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; // Spawn field actor - FTransform transform; - transform.SetLocation(Hit.ImpactPoint); - auto field = GetWorld()->SpawnActor(CurrentWeapon->GetFieldSystemActor(), transform, - SpawnParameters); + if (CurrentWeapon->GetFieldSystemActor()) + { + FTransform transform; + transform.SetLocation(Hit.ImpactPoint); + auto field = GetWorld()->SpawnActor(CurrentWeapon->GetFieldSystemActor(), transform, + SpawnParameters); + + if (field) + { + field->Destroy(); + } + } if (Hit.GetActor()) { @@ -135,6 +145,36 @@ void AEnemyCharacter::ProcessHits(TArray hits) GetController(), this); } } + + auto staticMeshComponent = Hit.GetActor()->GetComponentByClass(); + + if (staticMeshComponent && !staticMeshComponent->IsSimulatingPhysics() && CurrentWeapon->GetDecalActor()) + { + FTransform transform; + transform.SetLocation(Hit.ImpactPoint); + + auto decalActor = GetWorld()->SpawnActor(CurrentWeapon->GetDecalActor(), transform, + SpawnParameters); + auto rot = Hit.ImpactNormal.Rotation(); + rot.Roll += 90.0f; + rot.Yaw += 180.0f; + decalActor->SetActorRotation(rot); + } + + if (staticMeshComponent && !staticMeshComponent->IsSimulatingPhysics() && + CurrentWeapon->GetImpactParticleSystem()) + { + FTransform transform; + transform.SetLocation(Hit.ImpactPoint); + + UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, + CurrentWeapon->GetImpactParticleSystem(), + transform.GetLocation(), + dir.MirrorByVector(Hit.ImpactNormal).Rotation(), + FVector(1), + true); + + } } } diff --git a/Source/Nakatomi/EnemyCharacter.h b/Source/Nakatomi/EnemyCharacter.h index 3e24291..c86f489 100644 --- a/Source/Nakatomi/EnemyCharacter.h +++ b/Source/Nakatomi/EnemyCharacter.h @@ -52,9 +52,9 @@ protected: private: virtual void PlayOnFireAnimations() override; - virtual void CalculateHits(TArray* hits) override; + virtual void CalculateHits(TArray* hits, FVector* dir) override; - virtual void ProcessHits(TArray hits) override; + virtual void ProcessHits(TArray hits, FVector dir) override; protected: virtual void OnDamaged() override; diff --git a/Source/Nakatomi/NakatomiCharacter.cpp b/Source/Nakatomi/NakatomiCharacter.cpp index 6bc791a..8bd590b 100644 --- a/Source/Nakatomi/NakatomiCharacter.cpp +++ b/Source/Nakatomi/NakatomiCharacter.cpp @@ -257,11 +257,11 @@ bool ANakatomiCharacter::GetCrouched() return false; } -void ANakatomiCharacter::CalculateHits(TArray* hits) +void ANakatomiCharacter::CalculateHits(TArray* hits, FVector* dir) { } -void ANakatomiCharacter::ProcessHits(TArray hits) +void ANakatomiCharacter::ProcessHits(TArray hits, FVector dir) { } diff --git a/Source/Nakatomi/NakatomiCharacter.h b/Source/Nakatomi/NakatomiCharacter.h index dc47905..2e700d7 100644 --- a/Source/Nakatomi/NakatomiCharacter.h +++ b/Source/Nakatomi/NakatomiCharacter.h @@ -116,9 +116,9 @@ public: bool GetCrouched(); protected: - virtual void CalculateHits(TArray* hits); + virtual void CalculateHits(TArray* hits, FVector* dir); - virtual void ProcessHits(TArray hits); + virtual void ProcessHits(TArray hits, FVector dir); virtual void PlayOnFireAnimations(); diff --git a/Source/Nakatomi/PlayerCharacter.cpp b/Source/Nakatomi/PlayerCharacter.cpp index 2351d2d..47e389c 100644 --- a/Source/Nakatomi/PlayerCharacter.cpp +++ b/Source/Nakatomi/PlayerCharacter.cpp @@ -14,6 +14,7 @@ #include "InteractableComponent.h" #include "NakatomiCMC.h" #include "WeaponThrowable.h" +#include "NiagaraFunctionLibrary.h" #include "GameFramework/CharacterMovementComponent.h" #define COLLISION_WEAPON ECC_GameTraceChannel1 @@ -318,7 +319,7 @@ void APlayerCharacter::SetWalkingCallback(const FInputActionInstance& Instance) } } -void APlayerCharacter::CalculateHits(TArray* hits) +void APlayerCharacter::CalculateHits(TArray* hits, FVector* dir) { // Set up the collision query params, use the Weapon trace settings, Ignore the actor firing this trace FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(WeaponTrace), true, GetInstigator()); @@ -349,6 +350,7 @@ void APlayerCharacter::CalculateHits(TArray* hits) TraceStart = GetRootComponent()->GetComponentLocation(); FVector AimDir = CamHit.ImpactPoint - TraceStart; AimDir.Normalize(); + *dir = AimDir; TraceStart = TraceStart + AimDir * ((GetInstigator()->GetActorLocation() - TraceStart) | AimDir); // Calculate the hit results from the trace @@ -371,7 +373,7 @@ void APlayerCharacter::CalculateHits(TArray* hits) } } -void APlayerCharacter::ProcessHits(TArray hits) +void APlayerCharacter::ProcessHits(TArray hits, FVector dir) { for (FHitResult Hit : hits) { @@ -436,12 +438,14 @@ void APlayerCharacter::ProcessHits(TArray hits) { FTransform transform; transform.SetLocation(Hit.ImpactPoint); - - UGameplayStatics::SpawnEmitterAtLocation(this, - CurrentWeapon->GetImpactParticleSystem(), - transform.GetLocation(), - FRotator::ZeroRotator, - true); + + UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, + CurrentWeapon->GetImpactParticleSystem(), + transform.GetLocation(), + dir.MirrorByVector(Hit.ImpactNormal).Rotation(), + FVector(1), + true); + } } @@ -643,8 +647,9 @@ void APlayerCharacter::OnFire() CurrentWeapon->SetCurrentWeaponStatus(Firing); TArray Hits = TArray(); - CalculateHits(&Hits); - ProcessHits(Hits); + FVector direction = FVector::ZeroVector; + CalculateHits(&Hits, &direction); + ProcessHits(Hits, direction); CurrentWeapon->DecrementAmmoCount(1); diff --git a/Source/Nakatomi/PlayerCharacter.h b/Source/Nakatomi/PlayerCharacter.h index b4e9ace..f291d00 100644 --- a/Source/Nakatomi/PlayerCharacter.h +++ b/Source/Nakatomi/PlayerCharacter.h @@ -243,9 +243,9 @@ public: void SetIsThrowing(bool bIsThrowing); protected: - virtual void CalculateHits(TArray* hits) override; + virtual void CalculateHits(TArray* hits, FVector* dir) override; - virtual void ProcessHits(TArray hits) override; + virtual void ProcessHits(TArray hits, FVector dir) override; virtual void PlayOnFireAnimations() override;