refactor: solved duplicate code for shooting and fix raycast for interaction

This commit is contained in:
Kubson96 2025-03-23 22:27:10 +01:00
parent ae66d9790a
commit 4589f3efc7
3 changed files with 25 additions and 36 deletions

View File

@ -26,33 +26,16 @@ void UShootingComponent::BeginPlay()
void UShootingComponent::Shoot() void UShootingComponent::Shoot()
{ {
if (!IsValid(CurrentGun) || CurrentGun->CurrentAmmo == 0 || bIsReloading || !bCanShoot) return; if (!IsValid(CurrentGun) || CurrentGun->CurrentAmmo == 0 || bIsReloading || !bCanShoot) return;
APawn* PawnOwner = Cast<APawn>(PlayerCharacter);
if (!PawnOwner || !PawnOwner->GetController()) return;
bCanShoot = false; bCanShoot = false;
CurrentGun->CurrentAmmo--; CurrentGun->CurrentAmmo--;
FVector ViewLocation; AActor* HitActor = ExecuteLineTrace(CurrentGun->MaxRange);
FRotator ViewRotation; if (HitActor)
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<7A>
bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams);
if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements<UDamageable>())
{ {
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<EFBFBD>anie UI aktualizuj<75>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); PlayerCharacter->GetWorldTimerManager().SetTimer(ShootCooldownTimer, this, &UShootingComponent::ResetFireCooldown, CurrentGun->FireRateCooldown, false);
@ -63,35 +46,41 @@ void UShootingComponent::Shoot()
PlayerCharacter->AddControllerPitchInput(RecoilPitch * CurrentGun->RecoilForceMultiplier); PlayerCharacter->AddControllerPitchInput(RecoilPitch * CurrentGun->RecoilForceMultiplier);
PlayerCharacter->AddControllerYawInput(RecoilYaw * 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() void UShootingComponent::MeleAttack()
{
AActor* HitActor = ExecuteLineTrace(MeleRange);
if (HitActor)
{
IDamageable::Execute_TakeDamage(HitActor, MeleDamageValue);
}
}
AActor* UShootingComponent::ExecuteLineTrace(float LineRange)
{ {
FVector ForwardVector = PlayerCharacter->GetActorForwardVector(); FVector ForwardVector = PlayerCharacter->GetActorForwardVector();
FVector ViewLocation; FVector ViewLocation;
FRotator ViewRotation; FRotator ViewRotation;
PlayerCharacter->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation); PlayerCharacter->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation);
FVector EndVector = ViewLocation + ForwardVector * MeleRange; FVector EndVector = ViewLocation + ForwardVector * LineRange;
FHitResult Hit; FHitResult Hit;
FCollisionQueryParams QueryParams; FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(PlayerCharacter); QueryParams.AddIgnoredActor(PlayerCharacter);
// DEBUG
if (bShowDebugLine) if (bShowDebugLine)
DrawDebugLine(GetWorld(), ViewLocation, EndVector, FColor::Red, false, 1.0f, 0, 1.0f); 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<UDamageable>())
{ {
if (Hit.GetActor()->Implements<UDamageable>()) return Hit.GetActor();
{
IDamageable::Execute_TakeDamage(Hit.GetActor(), MeleDamageValue);
}
} }
return nullptr;
} }
void UShootingComponent::Reload() void UShootingComponent::Reload()

View File

@ -24,14 +24,13 @@ void UInteractionComponent::CheckForInteractable()
PawnOwner->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation); PawnOwner->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation);
FVector ForwardVector = ViewRotation.Vector(); FVector ForwardVector = ViewRotation.Vector();
FVector LineStart = Owner->GetActorLocation(); FVector LineEnd = ViewLocation + (ForwardVector * InteractionDistance);
FVector LineEnd = LineStart + (ForwardVector * InteractionDistance);
FHitResult HitResult; FHitResult HitResult;
FCollisionQueryParams QueryParams; FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(Owner); 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<UInteractable>()) if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements<UInteractable>())
{ {
@ -43,7 +42,7 @@ void UInteractionComponent::CheckForInteractable()
} }
if (bShowDebugLine) 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);
} }

View File

@ -66,6 +66,7 @@ public:
private: private:
void ResetFireCooldown(); void ResetFireCooldown();
void ReloadCompleted(); void ReloadCompleted();
AActor* ExecuteLineTrace(float LineRange);
FTimerHandle ShootCooldownTimer; FTimerHandle ShootCooldownTimer;
FTimerHandle ReloadTimer; FTimerHandle ReloadTimer;