diff --git a/Content/Blueprints/Characters/Player/BP_ExoPlayerCharacter.uasset b/Content/Blueprints/Characters/Player/BP_ExoPlayerCharacter.uasset index 4ba0aa7..3b8aa07 100644 Binary files a/Content/Blueprints/Characters/Player/BP_ExoPlayerCharacter.uasset and b/Content/Blueprints/Characters/Player/BP_ExoPlayerCharacter.uasset differ diff --git a/Source/Exo/Private/Characters/Components/ShootingComponent.cpp b/Source/Exo/Private/Characters/Components/ShootingComponent.cpp index 31dc989..713da6d 100644 --- a/Source/Exo/Private/Characters/Components/ShootingComponent.cpp +++ b/Source/Exo/Private/Characters/Components/ShootingComponent.cpp @@ -19,36 +19,67 @@ void UShootingComponent::BeginPlay() { Super::BeginPlay(); - // ... - + PlayerCharacter = Cast(GetOwner()); } void UShootingComponent::Shoot() { - AActor* Owner = GetOwner(); + if (CurrentAmmo == 0 || bIsReloading || !bCanShoot) return; - FVector LineStart = Owner->GetActorLocation(); - FVector ForwardVector = Owner->GetActorForwardVector(); + bCanShoot = false; + CurrentAmmo--; + + FVector LineStart = PlayerCharacter->GetActorLocation(); + FVector ForwardVector = PlayerCharacter->GetActorForwardVector(); FVector LineEnd = LineStart + (ForwardVector * MaxRange); FHitResult HitResult; FCollisionQueryParams QueryParams; - QueryParams.AddIgnoredActor(Owner); + QueryParams.AddIgnoredActor(PlayerCharacter); + // Strzał bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams); if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements()) { IDamageable::Execute_TakeDamage(HitResult.GetActor(), DamageValue); - UE_LOG(LogTemp, Warning, TEXT("Shoot")); + UE_LOG(LogTemp, Display, TEXT("Shoot. Ammo: %d/%d"), CurrentAmmo, MaxAmmo); // Docelowo tutaj wywołanie UI aktualizujące stan ammo } + PlayerCharacter->GetWorldTimerManager().SetTimer(ShootCooldownTimer, this, &UShootingComponent::ResetFireCooldown, FireRateCooldown, false); + + // Odrzut + FVector RecoilDirection = -PlayerCharacter->GetActorForwardVector(); + PlayerCharacter->LaunchCharacter(RecoilDirection * RecoilForce, true, false); + + // DEBUG if (bShowDebugLine) DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f); } +void UShootingComponent::Reload() +{ + if (bIsReloading) return; + + bIsReloading = true; + + CurrentAmmo = MaxAmmo; + PlayerCharacter->GetWorldTimerManager().SetTimer(ReloadTimer, this, &UShootingComponent::ReloadCompleted, ReloadTime, false); + + UE_LOG(LogTemp, Display, TEXT("Reloaded. Ammo: %d/%d"), CurrentAmmo, MaxAmmo); // Docelowo tutaj wywołanie UI aktualizujące stan ammo +} + +void UShootingComponent::ResetFireCooldown() +{ + bCanShoot = true; +} + +void UShootingComponent::ReloadCompleted() +{ + bIsReloading = false; +} // Called every frame void UShootingComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) diff --git a/Source/Exo/Private/Player/ExoPlayerController.cpp b/Source/Exo/Private/Player/ExoPlayerController.cpp index 68803ed..f8822d0 100644 --- a/Source/Exo/Private/Player/ExoPlayerController.cpp +++ b/Source/Exo/Private/Player/ExoPlayerController.cpp @@ -48,7 +48,6 @@ void AExoPlayerController::SetupInputComponent() EnhancedInputComponent->BindAction(InteractAction, ETriggerEvent::Triggered, this, &AExoPlayerController::Interact); EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerJump); EnhancedInputComponent->BindAction(DodgeAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerDodge); - //EnhancedInputComponent->BindAction(, ETriggerEvent::Started, this, &AExoPlayerController::ResetDodge); EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerCrouch); EnhancedInputComponent->BindAction(SprintAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerSprint); EnhancedInputComponent->BindAction(ShootAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerShoot); @@ -152,5 +151,5 @@ void AExoPlayerController::PlayerChangeWeapon() void AExoPlayerController::PlayerReload() { - + ShootingComponent->Reload(); } diff --git a/Source/Exo/Public/Characters/Components/ShootingComponent.h b/Source/Exo/Public/Characters/Components/ShootingComponent.h index 6730a8d..b83de10 100644 --- a/Source/Exo/Public/Characters/Components/ShootingComponent.h +++ b/Source/Exo/Public/Characters/Components/ShootingComponent.h @@ -4,6 +4,7 @@ #include "CoreMinimal.h" #include "Components/ActorComponent.h" +#include #include "Interfaces/Damageable.h" #include "ShootingComponent.generated.h" @@ -23,9 +24,27 @@ public: UPROPERTY(EditAnywhere, Category = "Shooting") float DamageValue = 100.0f; + UPROPERTY(EditAnywhere, Category = "Shooting") + float FireRateCooldown = 1.0f; + + UPROPERTY(EditAnywhere, Category = "Shooting") + float RecoilForce = 50.0f; + + UPROPERTY(EditAnywhere, Category = "Shooting") + float ReloadTime = 3.0f; + + UPROPERTY(EditAnywhere, Category = "Ammo") + int32 CurrentAmmo = 10; + + UPROPERTY(EditAnywhere, Category = "Ammo") + int32 MaxAmmo = 10; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Debug") bool bShowDebugLine = true; + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Character") + TObjectPtr PlayerCharacter; + protected: // Called when the game starts virtual void BeginPlay() override; @@ -37,6 +56,21 @@ public: UFUNCTION(Category = "Shooting") void Shoot(); - //UFUNCTION(Category = "Shooting") - //void Reload(); + UFUNCTION(Category = "Shooting") + void Reload(); + +private: + void ResetFireCooldown(); + + void ReloadCompleted(); + + FTimerHandle ShootCooldownTimer; + + FTimerHandle ReloadTimer; + + UPROPERTY(EditAnywhere, Category = "Shooting") + bool bCanShoot = true; + + UPROPERTY(EditAnywhere, Category = "Reloading") + bool bIsReloading = false; };