feat: add collecting HP and ammo

Player automatically collect HP and ammo when standing on crates. Ammo is collected only for owned gun.
This commit is contained in:
Kubson96 2025-04-18 01:53:08 +02:00
parent c4320db5e0
commit b7263615c0
18 changed files with 207 additions and 3 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -101,6 +101,26 @@ void UShootingComponent::Reload()
UE_LOG(LogTemp, Display, TEXT("Reloaded. Ammo: %d/%d"), CurrentGun->CurrentAmmo, CurrentGun->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
} }
bool UShootingComponent::AddAmmo(EAmmoType CheckingAmmo, int AddValue)
{
if (IsValid(CurrentGun))
{
// Adding ammo only if type is compatible
if (CurrentGun->AmmoType == CheckingAmmo)
{
CurrentGun->AddAmmo(AddValue);
return true;
}
if (SecondaryGun->AmmoType == CheckingAmmo)
{
SecondaryGun->AddAmmo(AddValue);
return true;
}
}
return false;
}
void UShootingComponent::PickUpGun(AGunBase* gunItem) void UShootingComponent::PickUpGun(AGunBase* gunItem)
{ {
if (IsValid(CurrentGun) && IsValid(SecondaryGun)) if (IsValid(CurrentGun) && IsValid(SecondaryGun))
@ -113,6 +133,7 @@ void UShootingComponent::PickUpGun(AGunBase* gunItem)
NewGun->FireRateCooldown = gunItem->FireRateCooldown; NewGun->FireRateCooldown = gunItem->FireRateCooldown;
NewGun->RecoilForceMultiplier = gunItem->RecoilForceMultiplier; NewGun->RecoilForceMultiplier = gunItem->RecoilForceMultiplier;
NewGun->ReloadTime = gunItem->ReloadTime; NewGun->ReloadTime = gunItem->ReloadTime;
NewGun->AmmoType = gunItem->AmmoType;
NewGun->CurrentAmmo = gunItem->CurrentAmmo; NewGun->CurrentAmmo = gunItem->CurrentAmmo;
NewGun->MaxAmmo = gunItem->MaxAmmo; NewGun->MaxAmmo = gunItem->MaxAmmo;
NewGun->SceneItemClass = gunItem->GetClass(); NewGun->SceneItemClass = gunItem->GetClass();
@ -177,6 +198,7 @@ void UShootingComponent::DropGun()
DroppedGun->FireRateCooldown = CurrentGun->FireRateCooldown; DroppedGun->FireRateCooldown = CurrentGun->FireRateCooldown;
DroppedGun->RecoilForceMultiplier = CurrentGun->RecoilForceMultiplier; DroppedGun->RecoilForceMultiplier = CurrentGun->RecoilForceMultiplier;
DroppedGun->ReloadTime = CurrentGun->ReloadTime; DroppedGun->ReloadTime = CurrentGun->ReloadTime;
DroppedGun->AmmoType = CurrentGun->AmmoType;
DroppedGun->CurrentAmmo = CurrentGun->CurrentAmmo; DroppedGun->CurrentAmmo = CurrentGun->CurrentAmmo;
DroppedGun->MaxAmmo = CurrentGun->MaxAmmo; DroppedGun->MaxAmmo = CurrentGun->MaxAmmo;

View File

@ -4,6 +4,8 @@
#include "Characters/ExoPlayerCharacter.h" #include "Characters/ExoPlayerCharacter.h"
#include "Characters/Components/ShootingComponent.h" #include "Characters/Components/ShootingComponent.h"
#include "GameFramework/CharacterMovementComponent.h" #include "GameFramework/CharacterMovementComponent.h"
#include "Items/AmmoBoxBase.h"
#include "Items/HealthBoxBase.h"
#include "Player/InteractionComponent.h" #include "Player/InteractionComponent.h"
AExoPlayerCharacter::AExoPlayerCharacter() AExoPlayerCharacter::AExoPlayerCharacter()
@ -22,5 +24,46 @@ AExoPlayerCharacter::AExoPlayerCharacter()
void AExoPlayerCharacter::BeginPlay() void AExoPlayerCharacter::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &AExoPlayerCharacter::OnActorBeginOverlap);
} }
void AExoPlayerCharacter::OnActorBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && OtherActor != this)
{
if (OtherActor->IsA(AHealthBoxBase::StaticClass()))
{
AHealthBoxBase* HealthBox = Cast<AHealthBoxBase>(OtherActor);
if (HealthBox)
{
AddHealthPoints(HealthBox->HealthValue);
HealthBox->Destroy();
UE_LOG(LogTemp, Warning, TEXT("Zebrano apteczkę"));
}
}
else if (OtherActor->IsA(AAmmoBoxBase::StaticClass()))
{
AAmmoBoxBase* AmmoBox = Cast<AAmmoBoxBase>(OtherActor);
if (AmmoBox)
{
if (ShootingComponent->AddAmmo(AmmoBox->AmmoType, AmmoBox->AmmoValue))
{
AmmoBox->Destroy();
UE_LOG(LogTemp, Warning, TEXT("Zebrano amunicję"));
}
}
}
}
}
void AExoPlayerCharacter::AddHealthPoints(float addValue)
{
CurrentHealth += addValue;
if (CurrentHealth > MaxHealth)
CurrentHealth = MaxHealth;
}

View File

@ -0,0 +1,23 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Items/AmmoBoxBase.h"
AAmmoBoxBase::AAmmoBoxBase()
{
PrimaryActorTick.bCanEverTick = true;
}
void AAmmoBoxBase::BeginPlay()
{
Super::BeginPlay();
}
void AAmmoBoxBase::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

View File

@ -32,4 +32,11 @@ void AGunBase::Interact_Implementation(AExoPlayerCharacter* playerCharacter)
playerCharacter->ShootingComponent->PickUpGun(this); playerCharacter->ShootingComponent->PickUpGun(this);
Destroy(); Destroy();
}
void AGunBase::AddAmmo(int AddValue)
{
CurrentAmmo += AddValue;
if (CurrentAmmo > MaxAmmo)
CurrentAmmo = MaxAmmo;
} }

View File

@ -0,0 +1,24 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Items/HealthBoxBase.h"
// Sets default values
AHealthBoxBase::AHealthBoxBase()
{
PrimaryActorTick.bCanEverTick = true;
}
void AHealthBoxBase::BeginPlay()
{
Super::BeginPlay();
}
void AHealthBoxBase::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

View File

@ -45,9 +45,12 @@ public:
UFUNCTION(Category = "Mele Attack") UFUNCTION(Category = "Mele Attack")
void MeleAttack(); void MeleAttack();
UFUNCTION(Category = "Shooting") UFUNCTION(Category = "Ammo")
void Reload(); void Reload();
UFUNCTION(Category = "Ammo")
bool AddAmmo(EAmmoType CheckingAmmo, int AddValue);
UFUNCTION(BlueprintCallable, Category = "Shooting") UFUNCTION(BlueprintCallable, Category = "Shooting")
void PickUpGun(AGunBase* gunItem); void PickUpGun(AGunBase* gunItem);

View File

@ -4,6 +4,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Characters/ExoCharacterBase.h" #include "Characters/ExoCharacterBase.h"
#include "Components/CapsuleComponent.h"
#include "ExoPlayerCharacter.generated.h" #include "ExoPlayerCharacter.generated.h"
class UInteractionComponent; class UInteractionComponent;
@ -38,8 +39,19 @@ public:
UPROPERTY(EditAnywhere, Category = "Aiming") UPROPERTY(EditAnywhere, Category = "Aiming")
float bIsAimingMode = false; float bIsAimingMode = false;
UPROPERTY(EditAnywhere, Category = "Health")
float CurrentHealth = 50.0f;
UFUNCTION()
void OnActorBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
);
UFUNCTION()
void AddHealthPoints(float addValue);
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
float MaxHealth = 100.0f;
}; };

View File

@ -0,0 +1,37 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AmmoBoxBase.generated.h"
UENUM()
enum class EAmmoType : uint8
{
Revolver UMETA(DisplayName = "Revolver"),
Shotgun UMETA(DisplayName = "Shotgun"),
Rifle UMETA(DisplayName = "Rifle")
};
UCLASS()
class EXO_API AAmmoBoxBase : public AActor
{
GENERATED_BODY()
public:
AAmmoBoxBase();
UPROPERTY(EditAnywhere, BlueprintReadWrite)
EAmmoType AmmoType = EAmmoType::Revolver;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int AmmoValue = 30;
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
};

View File

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "AmmoBoxBase.h"
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "Interfaces/Interactable.h" #include "Interfaces/Interactable.h"
#include "GunBase.generated.h" #include "GunBase.generated.h"
@ -40,8 +41,14 @@ public:
UPROPERTY(EditAnywhere, Category = "Ammo") UPROPERTY(EditAnywhere, Category = "Ammo")
int32 MaxAmmo = 10; int32 MaxAmmo = 10;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Ammo")
EAmmoType AmmoType = EAmmoType::Revolver;
TSubclassOf<AActor> SceneItemClass; TSubclassOf<AActor> SceneItemClass;
UFUNCTION(Category = "Ammo")
void AddAmmo(int AddValue);
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;

View File

@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "HealthBoxBase.generated.h"
UCLASS()
class EXO_API AHealthBoxBase : public AActor
{
GENERATED_BODY()
public:
AHealthBoxBase();
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float HealthValue = 75.0f;
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
};