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()
{
if (!IsValid(CurrentGun) || CurrentGun->CurrentAmmo == 0 || bIsReloading || !bCanShoot) return;
APawn* PawnOwner = Cast<APawn>(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<7A>
bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams);
if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements<UDamageable>())
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<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);
@ -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<UDamageable>())
{
if (Hit.GetActor()->Implements<UDamageable>())
{
IDamageable::Execute_TakeDamage(Hit.GetActor(), MeleDamageValue);
}
return Hit.GetActor();
}
return nullptr;
}
void UShootingComponent::Reload()

View File

@ -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<UInteractable>())
{
@ -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);
}

View File

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