feat: add weapon change

This commit is contained in:
Kubson96 2025-03-23 21:47:10 +01:00
parent debe01abd2
commit ae66d9790a
11 changed files with 105 additions and 54 deletions

Binary file not shown.

View File

@ -3,8 +3,6 @@
#include "Characters/Components/ShootingComponent.h"
#include "KismetTraceUtils.h"
// Sets default values for this component's properties
UShootingComponent::UShootingComponent()
{
@ -27,13 +25,13 @@ void UShootingComponent::BeginPlay()
void UShootingComponent::Shoot()
{
if (!bIsHoldingGun || 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;
CurrentAmmo--;
CurrentGun->CurrentAmmo--;
FVector ViewLocation;
FRotator ViewRotation;
@ -41,7 +39,7 @@ void UShootingComponent::Shoot()
FVector ForwardVector = ViewRotation.Vector();
FVector LineStart = ViewLocation; //PlayerCharacter->GetActorLocation();
FVector LineEnd = LineStart + (ForwardVector * MaxRange);
FVector LineEnd = LineStart + (ForwardVector * CurrentGun->MaxRange);
FHitResult HitResult;
FCollisionQueryParams QueryParams;
@ -52,19 +50,19 @@ void UShootingComponent::Shoot()
if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements<UDamageable>())
{
IDamageable::Execute_TakeDamage(HitResult.GetActor(), FireDamageValue);
IDamageable::Execute_TakeDamage(HitResult.GetActor(), CurrentGun->DamageValue);
UE_LOG(LogTemp, Display, TEXT("Shoot. Ammo: %d/%d"), CurrentAmmo, MaxAmmo); // Docelowo tutaj wywo<77>anie UI aktualizuj<75>ce stan ammo
UE_LOG(LogTemp, Display, TEXT("Shoot. Ammo: %d/%d"), CurrentGun->CurrentAmmo, CurrentGun->MaxAmmo); // Docelowo tutaj wywo<77>anie UI aktualizuj<75>ce stan ammo
}
PlayerCharacter->GetWorldTimerManager().SetTimer(ShootCooldownTimer, this, &UShootingComponent::ResetFireCooldown, FireRateCooldown, false);
PlayerCharacter->GetWorldTimerManager().SetTimer(ShootCooldownTimer, this, &UShootingComponent::ResetFireCooldown, CurrentGun->FireRateCooldown, false);
// 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);
PlayerCharacter->AddControllerPitchInput(RecoilPitch * CurrentGun->RecoilForceMultiplier);
PlayerCharacter->AddControllerYawInput(RecoilYaw * CurrentGun->RecoilForceMultiplier);
// DEBUG
if (bShowDebugLine)
@ -73,7 +71,6 @@ void UShootingComponent::Shoot()
void UShootingComponent::MeleAttack()
{
FVector ForwardVector = PlayerCharacter->GetActorForwardVector();
FVector ViewLocation;
FRotator ViewRotation;
@ -85,7 +82,8 @@ void UShootingComponent::MeleAttack()
FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(PlayerCharacter);
//DrawDebugLine(GetWorld(), ViewLocation, EndVector, FColor::Red, false, 1.0f, 0, 1.0f);
if (bShowDebugLine)
DrawDebugLine(GetWorld(), ViewLocation, EndVector, FColor::Red, false, 1.0f, 0, 1.0f);
if (GetWorld()->LineTraceSingleByChannel(Hit, ViewLocation, EndVector, ECC_Visibility, QueryParams))
{
@ -94,43 +92,45 @@ void UShootingComponent::MeleAttack()
IDamageable::Execute_TakeDamage(Hit.GetActor(), MeleDamageValue);
}
}
}
void UShootingComponent::Reload()
{
if (!bIsHoldingGun || bIsReloading) return;
if (!IsValid(CurrentGun) || bIsReloading) return;
bIsReloading = true;
CurrentAmmo = MaxAmmo;
PlayerCharacter->GetWorldTimerManager().SetTimer(ReloadTimer, this, &UShootingComponent::ReloadCompleted, ReloadTime, false);
CurrentGun->CurrentAmmo = CurrentGun->MaxAmmo;
PlayerCharacter->GetWorldTimerManager().SetTimer(ReloadTimer, this, &UShootingComponent::ReloadCompleted, CurrentGun->ReloadTime, false);
UE_LOG(LogTemp, Display, TEXT("Reloaded. Ammo: %d/%d"), CurrentAmmo, MaxAmmo); // Docelowo tutaj wywo<77>anie UI aktualizuj<75>ce stan ammo
UE_LOG(LogTemp, Display, TEXT("Reloaded. Ammo: %d/%d"), CurrentGun->CurrentAmmo, CurrentGun->MaxAmmo); // Docelowo tutaj wywo<77>anie UI aktualizuj<75>ce stan ammo
}
void UShootingComponent::PickUpGun(AGunBase* gunItem)
{
if (bIsHoldingGun)
if (IsValid(CurrentGun) && IsValid(SecondaryGun))
DropGun();
bIsHoldingGun = true;
HoldingGunClass = gunItem->GetClass();
AGunBase* NewGun = NewObject<AGunBase>();
MaxRange = gunItem->MaxRange;
FireDamageValue = gunItem->DamageValue;
FireRateCooldown = gunItem->FireRateCooldown;
RecoilForceMultiplier = gunItem->RecoilForceMultiplier;
ReloadTime = gunItem->ReloadTime;
CurrentAmmo = gunItem->CurrentAmmo;
MaxAmmo = gunItem->MaxAmmo;
NewGun->MaxRange = gunItem->MaxRange;
NewGun->DamageValue = gunItem->DamageValue;
NewGun->FireRateCooldown = gunItem->FireRateCooldown;
NewGun->RecoilForceMultiplier = gunItem->RecoilForceMultiplier;
NewGun->ReloadTime = gunItem->ReloadTime;
NewGun->CurrentAmmo = gunItem->CurrentAmmo;
NewGun->MaxAmmo = gunItem->MaxAmmo;
NewGun->SceneItemClass = gunItem->GetClass();
if (IsValid(CurrentGun))
SecondaryGun = NewGun;
else
CurrentGun = NewGun;
}
void UShootingComponent::DropGun()
{
if (!bIsHoldingGun) return;
if (!IsValid(CurrentGun)) return;
FVector ForwardVector = PlayerCharacter->GetActorForwardVector();
FVector PlayerPos = PlayerCharacter->GetActorLocation();
@ -139,19 +139,20 @@ void UShootingComponent::DropGun()
FTransform DroppedGunTransform = PlayerCharacter->GetActorTransform();
DroppedGunTransform.SetLocation(DroppedGunPos);
AGunBase* DroppedGun = GetWorld()->SpawnActor<AGunBase>(HoldingGunClass, DroppedGunTransform);
AGunBase* DroppedGun = GetWorld()->SpawnActor<AGunBase>(CurrentGun->SceneItemClass, DroppedGunTransform);
if (DroppedGun)
{
DroppedGun->MaxRange = MaxRange;
DroppedGun->DamageValue = FireDamageValue;
DroppedGun->FireRateCooldown = FireRateCooldown;
DroppedGun->RecoilForceMultiplier = RecoilForceMultiplier;
DroppedGun->ReloadTime = ReloadTime;
DroppedGun->CurrentAmmo = CurrentAmmo;
DroppedGun->MaxAmmo = MaxAmmo;
bIsHoldingGun = false;
DroppedGun->MaxRange = CurrentGun->MaxRange;
DroppedGun->DamageValue = CurrentGun->DamageValue;
DroppedGun->FireRateCooldown = CurrentGun->FireRateCooldown;
DroppedGun->RecoilForceMultiplier = CurrentGun->RecoilForceMultiplier;
DroppedGun->ReloadTime = CurrentGun->ReloadTime;
DroppedGun->CurrentAmmo = CurrentGun->CurrentAmmo;
DroppedGun->MaxAmmo = CurrentGun->MaxAmmo;
CurrentGun->Destroy();
CurrentGun = nullptr;
}
}
@ -165,6 +166,31 @@ void UShootingComponent::ReloadCompleted()
bIsReloading = false;
}
void UShootingComponent::SelectGun(bool bSelectFirstSlot)
{
if (bSelectFirstSlot != bIsFirstGunSelected)
SwitchGun();
}
void UShootingComponent::SwitchGun()
{
AGunBase* temp = CurrentGun;
CurrentGun = SecondaryGun;
SecondaryGun = temp;
bIsFirstGunSelected = !bIsFirstGunSelected;
// Debug info
if (bIsFirstGunSelected)
{
UE_LOG(LogTemp, Display, TEXT("Zmiana broni na broń 1"));
}
else
{
UE_LOG(LogTemp, Display, TEXT("Zmiana broni na broń 2"));
}
}
// Called every frame
void UShootingComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{

View File

@ -60,6 +60,8 @@ void AExoPlayerController::SetupInputComponent()
EnhancedInputComponent->BindAction(AimAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerAim);
EnhancedInputComponent->BindAction(MeleAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerMeleAttack);
EnhancedInputComponent->BindAction(ChangeWeaponAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerChangeWeapon);
EnhancedInputComponent->BindAction(SelectFirstWeaponAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerSelectFirstWeapon);
EnhancedInputComponent->BindAction(SelectSecondWeaponAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerSelectSecondWeapon);
EnhancedInputComponent->BindAction(DropWeaponAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerDropWeapon);
EnhancedInputComponent->BindAction(ReloadAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerReload);
}
@ -169,7 +171,17 @@ void AExoPlayerController::PlayerMeleAttack()
void AExoPlayerController::PlayerChangeWeapon()
{
ShootingComponent->SwitchGun();
}
void AExoPlayerController::PlayerSelectFirstWeapon()
{
ShootingComponent->SelectGun(true);
}
void AExoPlayerController::PlayerSelectSecondWeapon()
{
ShootingComponent->SelectGun(false);
}
void AExoPlayerController::PlayerDropWeapon()

View File

@ -48,33 +48,32 @@ public:
UFUNCTION(Category = "Shooting")
void DropGun();
UFUNCTION(Category = "Shooting")
void SwitchGun();
UFUNCTION(Category = "Shooting")
void SelectGun(bool bSelectFirstSlot);
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shooting")
float DropGunRange = 20.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mele Attack")
float MeleRange = 75.f;
AGunBase* CurrentGun = nullptr;
AGunBase* SecondaryGun = nullptr;
private:
void ResetFireCooldown();
void ReloadCompleted();
FTimerHandle ShootCooldownTimer;
FTimerHandle ReloadTimer;
bool bIsHoldingGun = false;
TSubclassOf<AActor> HoldingGunClass;
bool bCanShoot = true;
bool bIsReloading = false;
bool bIsFirstGunSelected = true;
float MaxRange = 2000.0f;
float FireDamageValue = 100.0f;
float MeleDamageValue = 50.f;
float FireRateCooldown = 1.0f;
float RecoilForceMultiplier = 1.0f;
float ReloadTime = 3.0f;
int32 CurrentAmmo = 10;
int32 MaxAmmo = 10;
};

View File

@ -37,6 +37,8 @@ public:
UPROPERTY(EditAnywhere, Category = "Ammo")
int32 MaxAmmo = 10;
TSubclassOf<AActor> SceneItemClass;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

View File

@ -69,8 +69,14 @@ protected:
void PlayerMeleAttack(); // V - nieprzypisane
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerChangeWeapon(); // 1\2\MouseScroll - nieprzypisane
void PlayerChangeWeapon(); // MouseScroll
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerSelectFirstWeapon(); // 1
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerSelectSecondWeapon(); // 2
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerDropWeapon(); // F
@ -124,6 +130,12 @@ protected:
UPROPERTY(EditAnywhere, Category = "Input")
TObjectPtr<UInputAction> ChangeWeaponAction;
UPROPERTY(EditAnywhere, Category = "Input")
TObjectPtr<UInputAction> SelectFirstWeaponAction;
UPROPERTY(EditAnywhere, Category = "Input")
TObjectPtr<UInputAction> SelectSecondWeaponAction;
UPROPERTY(EditAnywhere, Category = "Input")
TObjectPtr<UInputAction> DropWeaponAction;