ExoWest/Source/Exo/Private/Characters/ExoPlayerCharacter.cpp
2025-05-22 17:21:59 +02:00

100 lines
2.7 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Characters/ExoPlayerCharacter.h"
#include "Blueprint/UserWidget.h"
#include "Characters/Components/ShootingComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Items/AmmoBoxBase.h"
#include "Items/HealthBoxBase.h"
#include "Player/InteractionComponent.h"
#include "Widget/WBP_PlayerUI.h"
AExoPlayerCharacter::AExoPlayerCharacter()
{
GetCharacterMovement()->bSnapToPlaneAtStart = true;
InteractionComponent = CreateDefaultSubobject<UInteractionComponent>(TEXT("Interaction Component"));
ShootingComponent = CreateDefaultSubobject<UShootingComponent>(TEXT("Shooting Component"));
Weapon = CreateDefaultSubobject<USkeletalMeshComponent>("Weapon");
Weapon->SetupAttachment(GetMesh());
Weapon->SetCollisionEnabled(ECollisionEnabled::NoCollision);
Weapon->bCastDynamicShadow = false;
Weapon->CastShadow = false;
Weapon->SetRelativeLocation(FVector(-50.f, 0.f, -90.f));
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = true;
bUseControllerRotationRoll = false;
}
void AExoPlayerCharacter::BeginPlay()
{
Super::BeginPlay();
GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &AExoPlayerCharacter::OnActorBeginOverlap);
PlayerHud = CreateWidget<UWBP_PlayerUI>(GetWorld(),PlayerHudClass);
if (PlayerHud)
{
PlayerHud->AddToViewport();
PlayerHud->AddToPlayerScreen();
if (PlayerHud)
{
PlayerHud->SetHp(CurrentHealth,MaxHealth);
PlayerHud->SetAmmoNumber(10);
PlayerHud->SetAmmoType(EAmmoType::Revolver);
for (int i=0;i<4;i++)
PlayerHud->AddDebuf(NULL,0.25f*i);
}
}
}
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;
if (PlayerHud)
PlayerHud->SetHp(CurrentHealth,MaxHealth);
}