ExoWest/Source/Exo/Private/Characters/ExoPlayerCharacter.cpp
Kubson96 b7263615c0 feat: add collecting HP and ammo
Player automatically collect HP and ammo when standing on crates. Ammo is collected only for owned gun.
2025-04-18 01:53:08 +02:00

69 lines
1.9 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Characters/ExoPlayerCharacter.h"
#include "Characters/Components/ShootingComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Items/AmmoBoxBase.h"
#include "Items/HealthBoxBase.h"
#include "Player/InteractionComponent.h"
AExoPlayerCharacter::AExoPlayerCharacter()
{
GetCharacterMovement()->bSnapToPlaneAtStart = true;
InteractionComponent = CreateDefaultSubobject<UInteractionComponent>(TEXT("Interaction Component"));
ShootingComponent = CreateDefaultSubobject<UShootingComponent>(TEXT("Shooting Component"));
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = true;
bUseControllerRotationRoll = false;
}
void AExoPlayerCharacter::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;
}