From 4589f3efc741986c33f62906573c58d30bedff2e Mon Sep 17 00:00:00 2001 From: Kubson96 <42842162+Kubson96@users.noreply.github.com> Date: Sun, 23 Mar 2025 22:27:10 +0100 Subject: [PATCH] refactor: solved duplicate code for shooting and fix raycast for interaction --- .../Components/ShootingComponent.cpp | 53 ++++++++----------- .../Private/Player/InteractionComponent.cpp | 7 ++- .../Characters/Components/ShootingComponent.h | 1 + 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/Source/Exo/Private/Characters/Components/ShootingComponent.cpp b/Source/Exo/Private/Characters/Components/ShootingComponent.cpp index b5e96da..1cc0d29 100644 --- a/Source/Exo/Private/Characters/Components/ShootingComponent.cpp +++ b/Source/Exo/Private/Characters/Components/ShootingComponent.cpp @@ -26,33 +26,16 @@ void UShootingComponent::BeginPlay() void UShootingComponent::Shoot() { if (!IsValid(CurrentGun) || CurrentGun->CurrentAmmo == 0 || bIsReloading || !bCanShoot) return; - - APawn* PawnOwner = Cast(PlayerCharacter); - if (!PawnOwner || !PawnOwner->GetController()) return; bCanShoot = false; CurrentGun->CurrentAmmo--; - - FVector ViewLocation; - FRotator ViewRotation; - PawnOwner->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation); - - FVector ForwardVector = ViewRotation.Vector(); - FVector LineStart = ViewLocation; //PlayerCharacter->GetActorLocation(); - FVector LineEnd = LineStart + (ForwardVector * CurrentGun->MaxRange); - - FHitResult HitResult; - FCollisionQueryParams QueryParams; - QueryParams.AddIgnoredActor(PlayerCharacter); - - // Strza� - bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams); - - if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements()) + + AActor* HitActor = ExecuteLineTrace(CurrentGun->MaxRange); + if (HitActor) { - IDamageable::Execute_TakeDamage(HitResult.GetActor(), CurrentGun->DamageValue); + IDamageable::Execute_TakeDamage(HitActor, CurrentGun->DamageValue); - UE_LOG(LogTemp, Display, TEXT("Shoot. Ammo: %d/%d"), CurrentGun->CurrentAmmo, CurrentGun->MaxAmmo); // Docelowo tutaj wywo�anie UI aktualizuj�ce stan ammo + UE_LOG(LogTemp, Display, TEXT("Shoot. Ammo: %d/%d"), CurrentGun->CurrentAmmo, CurrentGun->MaxAmmo); // Docelowo tutaj wywołanie UI aktualizujące stan ammo } PlayerCharacter->GetWorldTimerManager().SetTimer(ShootCooldownTimer, this, &UShootingComponent::ResetFireCooldown, CurrentGun->FireRateCooldown, false); @@ -63,35 +46,41 @@ void UShootingComponent::Shoot() PlayerCharacter->AddControllerPitchInput(RecoilPitch * CurrentGun->RecoilForceMultiplier); PlayerCharacter->AddControllerYawInput(RecoilYaw * CurrentGun->RecoilForceMultiplier); - - // DEBUG - if (bShowDebugLine) - DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f); } void UShootingComponent::MeleAttack() +{ + AActor* HitActor = ExecuteLineTrace(MeleRange); + if (HitActor) + { + IDamageable::Execute_TakeDamage(HitActor, MeleDamageValue); + } +} + +AActor* UShootingComponent::ExecuteLineTrace(float LineRange) { FVector ForwardVector = PlayerCharacter->GetActorForwardVector(); FVector ViewLocation; FRotator ViewRotation; PlayerCharacter->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation); - FVector EndVector = ViewLocation + ForwardVector * MeleRange; + FVector EndVector = ViewLocation + ForwardVector * LineRange; FHitResult Hit; FCollisionQueryParams QueryParams; QueryParams.AddIgnoredActor(PlayerCharacter); + // DEBUG if (bShowDebugLine) DrawDebugLine(GetWorld(), ViewLocation, EndVector, FColor::Red, false, 1.0f, 0, 1.0f); - if (GetWorld()->LineTraceSingleByChannel(Hit, ViewLocation, EndVector, ECC_Visibility, QueryParams)) + if (GetWorld()->LineTraceSingleByChannel(Hit, ViewLocation, EndVector, ECC_Visibility, QueryParams) && + IsValid(Hit.GetActor()) && Hit.GetActor()->Implements()) { - if (Hit.GetActor()->Implements()) - { - IDamageable::Execute_TakeDamage(Hit.GetActor(), MeleDamageValue); - } + return Hit.GetActor(); } + + return nullptr; } void UShootingComponent::Reload() diff --git a/Source/Exo/Private/Player/InteractionComponent.cpp b/Source/Exo/Private/Player/InteractionComponent.cpp index 0b01ae0..2574595 100644 --- a/Source/Exo/Private/Player/InteractionComponent.cpp +++ b/Source/Exo/Private/Player/InteractionComponent.cpp @@ -24,14 +24,13 @@ void UInteractionComponent::CheckForInteractable() PawnOwner->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation); FVector ForwardVector = ViewRotation.Vector(); - FVector LineStart = Owner->GetActorLocation(); - FVector LineEnd = LineStart + (ForwardVector * InteractionDistance); + FVector LineEnd = ViewLocation + (ForwardVector * InteractionDistance); FHitResult HitResult; FCollisionQueryParams QueryParams; QueryParams.AddIgnoredActor(Owner); - bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams); + bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, ViewLocation, LineEnd, ECC_Visibility, QueryParams); if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements()) { @@ -43,7 +42,7 @@ void UInteractionComponent::CheckForInteractable() } if (bShowDebugLine) - DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f); + DrawDebugLine(GetWorld(), ViewLocation, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f); } diff --git a/Source/Exo/Public/Characters/Components/ShootingComponent.h b/Source/Exo/Public/Characters/Components/ShootingComponent.h index 94730eb..f70a72e 100644 --- a/Source/Exo/Public/Characters/Components/ShootingComponent.h +++ b/Source/Exo/Public/Characters/Components/ShootingComponent.h @@ -66,6 +66,7 @@ public: private: void ResetFireCooldown(); void ReloadCompleted(); + AActor* ExecuteLineTrace(float LineRange); FTimerHandle ShootCooldownTimer; FTimerHandle ReloadTimer;