feat: add melee attack and fix raycast for shooting

This commit is contained in:
Foxim 2025-03-22 13:46:05 +01:00
parent 56c1825cc4
commit debe01abd2
3 changed files with 45 additions and 8 deletions

View File

@ -3,6 +3,8 @@
#include "Characters/Components/ShootingComponent.h"
#include "KismetTraceUtils.h"
// Sets default values for this component's properties
UShootingComponent::UShootingComponent()
{
@ -38,7 +40,7 @@ void UShootingComponent::Shoot()
PawnOwner->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation);
FVector ForwardVector = ViewRotation.Vector();
FVector LineStart = PlayerCharacter->GetActorLocation();
FVector LineStart = ViewLocation; //PlayerCharacter->GetActorLocation();
FVector LineEnd = LineStart + (ForwardVector * MaxRange);
FHitResult HitResult;
@ -50,7 +52,7 @@ void UShootingComponent::Shoot()
if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements<UDamageable>())
{
IDamageable::Execute_TakeDamage(HitResult.GetActor(), DamageValue);
IDamageable::Execute_TakeDamage(HitResult.GetActor(), FireDamageValue);
UE_LOG(LogTemp, Display, TEXT("Shoot. Ammo: %d/%d"), CurrentAmmo, MaxAmmo); // Docelowo tutaj wywo<77>anie UI aktualizuj<75>ce stan ammo
}
@ -60,7 +62,7 @@ void UShootingComponent::Shoot()
// Odrzut
float RecoilPitch = FMath::RandRange(-1.0f, -0.5f);
float RecoilYaw = FMath::RandRange(-0.5f, 0.5f);
PlayerCharacter->AddControllerPitchInput(RecoilPitch * RecoilForceMultiplier);
PlayerCharacter->AddControllerYawInput(RecoilYaw * RecoilForceMultiplier);
@ -69,6 +71,33 @@ void UShootingComponent::Shoot()
DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f);
}
void UShootingComponent::MeleAttack()
{
FVector ForwardVector = PlayerCharacter->GetActorForwardVector();
FVector ViewLocation;
FRotator ViewRotation;
PlayerCharacter->GetController()->GetPlayerViewPoint(ViewLocation, ViewRotation);
FVector EndVector = ViewLocation + ForwardVector * MeleRange;
FHitResult Hit;
FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(PlayerCharacter);
//DrawDebugLine(GetWorld(), ViewLocation, EndVector, FColor::Red, false, 1.0f, 0, 1.0f);
if (GetWorld()->LineTraceSingleByChannel(Hit, ViewLocation, EndVector, ECC_Visibility, QueryParams))
{
if (Hit.GetActor()->Implements<UDamageable>())
{
IDamageable::Execute_TakeDamage(Hit.GetActor(), MeleDamageValue);
}
}
}
void UShootingComponent::Reload()
{
if (!bIsHoldingGun || bIsReloading) return;
@ -91,7 +120,7 @@ void UShootingComponent::PickUpGun(AGunBase* gunItem)
HoldingGunClass = gunItem->GetClass();
MaxRange = gunItem->MaxRange;
DamageValue = gunItem->DamageValue;
FireDamageValue = gunItem->DamageValue;
FireRateCooldown = gunItem->FireRateCooldown;
RecoilForceMultiplier = gunItem->RecoilForceMultiplier;
ReloadTime = gunItem->ReloadTime;
@ -115,7 +144,7 @@ void UShootingComponent::DropGun()
if (DroppedGun)
{
DroppedGun->MaxRange = MaxRange;
DroppedGun->DamageValue = DamageValue;
DroppedGun->DamageValue = FireDamageValue;
DroppedGun->FireRateCooldown = FireRateCooldown;
DroppedGun->RecoilForceMultiplier = RecoilForceMultiplier;
DroppedGun->ReloadTime = ReloadTime;

View File

@ -38,6 +38,7 @@ void AExoPlayerController::BeginPlay()
void AExoPlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);
}
void AExoPlayerController::SetupInputComponent()
@ -163,7 +164,7 @@ void AExoPlayerController::PlayerAim()
void AExoPlayerController::PlayerMeleAttack()
{
ShootingComponent->MeleAttack();
}
void AExoPlayerController::PlayerChangeWeapon()

View File

@ -32,10 +32,13 @@ protected:
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
UFUNCTION(Category = "Shooting")
void Shoot();
UFUNCTION(Category = "Mele Attack")
void MeleAttack();
UFUNCTION(Category = "Shooting")
void Reload();
@ -48,6 +51,9 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shooting")
float DropGunRange = 20.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mele Attack")
float MeleRange = 75.f;
private:
void ResetFireCooldown();
@ -64,7 +70,8 @@ private:
bool bIsReloading = false;
float MaxRange = 2000.0f;
float DamageValue = 100.0f;
float FireDamageValue = 100.0f;
float MeleDamageValue = 50.f;
float FireRateCooldown = 1.0f;
float RecoilForceMultiplier = 1.0f;
float ReloadTime = 3.0f;