2025-02-21 20:42:52 +01:00
|
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "Characters/Components/ShootingComponent.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Sets default values for this component's properties
|
|
|
|
|
|
UShootingComponent::UShootingComponent()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
|
|
|
|
|
|
// off to improve performance if you don't need them.
|
|
|
|
|
|
PrimaryComponentTick.bCanEverTick = true;
|
|
|
|
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Called when the game starts
|
|
|
|
|
|
void UShootingComponent::BeginPlay()
|
|
|
|
|
|
{
|
|
|
|
|
|
Super::BeginPlay();
|
|
|
|
|
|
|
2025-02-22 01:28:07 +01:00
|
|
|
|
PlayerCharacter = Cast<AExoPlayerCharacter>(GetOwner());
|
2025-02-21 20:42:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UShootingComponent::Shoot()
|
|
|
|
|
|
{
|
2025-03-15 20:17:03 +01:00
|
|
|
|
if (!bIsHoldingGun || CurrentAmmo == 0 || bIsReloading || !bCanShoot) return;
|
2025-03-15 13:48:39 +01:00
|
|
|
|
|
|
|
|
|
|
APawn* PawnOwner = Cast<APawn>(PlayerCharacter);
|
|
|
|
|
|
if (!PawnOwner || !PawnOwner->GetController()) return;
|
2025-02-22 01:28:07 +01:00
|
|
|
|
|
|
|
|
|
|
bCanShoot = false;
|
|
|
|
|
|
CurrentAmmo--;
|
2025-02-21 20:42:52 +01:00
|
|
|
|
|
2025-03-15 13:48:39 +01:00
|
|
|
|
FVector ViewLocation;
|
|
|
|
|
|
FRotator ViewRotation;
|
|
|
|
|
|
PawnOwner->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation);
|
|
|
|
|
|
|
|
|
|
|
|
FVector ForwardVector = ViewRotation.Vector();
|
2025-02-22 01:28:07 +01:00
|
|
|
|
FVector LineStart = PlayerCharacter->GetActorLocation();
|
2025-02-21 20:42:52 +01:00
|
|
|
|
FVector LineEnd = LineStart + (ForwardVector * MaxRange);
|
|
|
|
|
|
|
|
|
|
|
|
FHitResult HitResult;
|
|
|
|
|
|
FCollisionQueryParams QueryParams;
|
2025-02-22 01:28:07 +01:00
|
|
|
|
QueryParams.AddIgnoredActor(PlayerCharacter);
|
2025-02-21 20:42:52 +01:00
|
|
|
|
|
2025-03-15 13:48:39 +01:00
|
|
|
|
// Strza<7A>
|
2025-02-21 20:42:52 +01:00
|
|
|
|
bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams);
|
|
|
|
|
|
|
|
|
|
|
|
if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements<UDamageable>())
|
|
|
|
|
|
{
|
|
|
|
|
|
IDamageable::Execute_TakeDamage(HitResult.GetActor(), DamageValue);
|
|
|
|
|
|
|
2025-03-15 13:48:39 +01:00
|
|
|
|
UE_LOG(LogTemp, Display, TEXT("Shoot. Ammo: %d/%d"), CurrentAmmo, MaxAmmo); // Docelowo tutaj wywo<77>anie UI aktualizuj<75>ce stan ammo
|
2025-02-21 20:42:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-22 01:28:07 +01:00
|
|
|
|
PlayerCharacter->GetWorldTimerManager().SetTimer(ShootCooldownTimer, this, &UShootingComponent::ResetFireCooldown, FireRateCooldown, false);
|
|
|
|
|
|
|
|
|
|
|
|
// Odrzut
|
2025-02-22 20:33:07 +01:00
|
|
|
|
float RecoilPitch = FMath::RandRange(-1.0f, -0.5f);
|
|
|
|
|
|
float RecoilYaw = FMath::RandRange(-0.5f, 0.5f);
|
|
|
|
|
|
|
|
|
|
|
|
PlayerCharacter->AddControllerPitchInput(RecoilPitch * RecoilForceMultiplier);
|
|
|
|
|
|
PlayerCharacter->AddControllerYawInput(RecoilYaw * RecoilForceMultiplier);
|
2025-02-22 01:28:07 +01:00
|
|
|
|
|
|
|
|
|
|
// DEBUG
|
2025-02-21 20:42:52 +01:00
|
|
|
|
if (bShowDebugLine)
|
|
|
|
|
|
DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-22 01:28:07 +01:00
|
|
|
|
void UShootingComponent::Reload()
|
|
|
|
|
|
{
|
2025-03-15 20:17:03 +01:00
|
|
|
|
if (!bIsHoldingGun || bIsReloading) return;
|
2025-02-22 01:28:07 +01:00
|
|
|
|
|
|
|
|
|
|
bIsReloading = true;
|
|
|
|
|
|
|
|
|
|
|
|
CurrentAmmo = MaxAmmo;
|
|
|
|
|
|
PlayerCharacter->GetWorldTimerManager().SetTimer(ReloadTimer, this, &UShootingComponent::ReloadCompleted, ReloadTime, false);
|
|
|
|
|
|
|
2025-03-15 13:48:39 +01:00
|
|
|
|
UE_LOG(LogTemp, Display, TEXT("Reloaded. Ammo: %d/%d"), CurrentAmmo, MaxAmmo); // Docelowo tutaj wywo<77>anie UI aktualizuj<75>ce stan ammo
|
2025-02-22 01:28:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-15 20:17:03 +01:00
|
|
|
|
void UShootingComponent::PickUpGun(AGunBase* gunItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bIsHoldingGun)
|
|
|
|
|
|
DropGun();
|
|
|
|
|
|
|
|
|
|
|
|
bIsHoldingGun = true;
|
|
|
|
|
|
|
|
|
|
|
|
MaxRange = gunItem->MaxRange;
|
|
|
|
|
|
DamageValue = gunItem->DamageValue;
|
|
|
|
|
|
FireRateCooldown = gunItem->FireRateCooldown;
|
|
|
|
|
|
RecoilForceMultiplier = gunItem->RecoilForceMultiplier;
|
|
|
|
|
|
ReloadTime = gunItem->ReloadTime;
|
|
|
|
|
|
CurrentAmmo = gunItem->CurrentAmmo;
|
|
|
|
|
|
MaxAmmo = gunItem->MaxAmmo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UShootingComponent::DropGun()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!bIsHoldingGun) return;
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Spawn gun actor
|
|
|
|
|
|
|
|
|
|
|
|
bIsHoldingGun = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-22 01:28:07 +01:00
|
|
|
|
void UShootingComponent::ResetFireCooldown()
|
|
|
|
|
|
{
|
|
|
|
|
|
bCanShoot = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UShootingComponent::ReloadCompleted()
|
|
|
|
|
|
{
|
|
|
|
|
|
bIsReloading = false;
|
|
|
|
|
|
}
|
2025-02-21 20:42:52 +01:00
|
|
|
|
|
|
|
|
|
|
// Called every frame
|
|
|
|
|
|
void UShootingComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
|
|
|
|
|
{
|
|
|
|
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
|
|
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
}
|
|
|
|
|
|
|