vampires/Source/vampires/PlayerCharacter.cpp

132 lines
4.5 KiB
C++
Raw Normal View History

2025-02-05 23:12:03 +00:00
// Louis Hobbs | 2024-2025
2024-06-12 00:34:11 +01:00
#include "PlayerCharacter.h"
2024-11-14 18:39:20 +00:00
#include "EXPComponent.h"
#include "GoldComponent.h"
2025-04-07 23:40:01 +01:00
#include "HealthComponent.h"
2024-11-14 18:39:20 +00:00
#include "Components/WidgetComponent.h"
2025-04-04 22:57:43 +01:00
#include "Kismet/GameplayStatics.h"
2024-06-12 00:34:24 +01:00
APlayerCharacter::APlayerCharacter()
{
2024-06-12 19:06:51 +01:00
// Create EXP Component
EXPComponent = CreateDefaultSubobject<UEXPComponent>(TEXT("EXP Component"));
2024-06-19 02:29:19 +01:00
2024-06-19 16:55:26 +01:00
// Create Gold Component
GoldComponent = CreateDefaultSubobject<UGoldComponent>(TEXT("Gold Component"));
2024-08-28 02:38:31 +01:00
// Create HealthBar Widget Component
HealthBarWidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("Healthbar"));
HealthBarWidgetComponent->SetupAttachment(RootComponent);
2024-11-14 18:39:20 +00:00
HealthBarWidgetComponent->SetRelativeLocation(FVector(0, 0, 90));
2024-08-28 02:38:31 +01:00
HealthBarWidgetComponent->SetTwoSided(true);
2024-11-14 18:39:20 +00:00
HealthBarWidgetComponent->SetBackgroundColor(FLinearColor(1, 1, 1, 0));
HealthBarWidgetComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
CameraShakeTimelineComponent = CreateDefaultSubobject<UTimelineComponent>(TEXT("Camera Shake Timeline Component"));
CameraShakeTimelineComponent->SetDirectionPropertyName(FName("TimelineDirection"));
CameraShakeTimelineComponent->SetLooping(false);
CameraShakeTimelineComponent->SetTimelineLength(0.5f);
CameraShakeTimelineComponent->SetTimelineLengthMode(TL_TimelineLength);
CameraShakeTimelineComponent->SetPlaybackPosition(0.0f, false);
2025-07-29 22:06:42 +01:00
OnTimelineCallback.BindUFunction(this, FName(TEXT("CameraShakeTimelineCallback")));
OnTimelineFinishedCallback.BindUFunction(this, FName(TEXT("CameraShakeTimelineFinishedCallback")));
2024-06-12 00:34:24 +01:00
}
void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();
2025-04-07 23:40:01 +01:00
GetHealthComponent()->OnDamaged.AddDynamic(this, &APlayerCharacter::OnDamaged);
GetHealthComponent()->OnDeath.AddDynamic(this, &APlayerCharacter::OnDeath);
if (CameraShakeCurve != nullptr)
{
2025-07-29 22:06:42 +01:00
CameraShakeTimelineComponent->AddInterpFloat(CameraShakeCurve, OnTimelineCallback);
CameraShakeTimelineComponent->SetTimelineFinishedFunc(OnTimelineFinishedCallback);
}
PlayerCameraManager = GetWorld()->GetFirstPlayerController()->PlayerCameraManager;
// For some reason, we need a slight delay here. I need to investigate the exact reason, but this is a quick workaround
GEngine->GameViewport->Viewport->ViewportResizedEvent.AddUObject(this, &APlayerCharacter::OnViewportResized);
GetWorldTimerManager().SetTimer(ResizeViewportTimerDelegate, this, &APlayerCharacter::ResizeViewportTimerCallback,
0.01f, false);
2024-06-12 00:34:24 +01:00
}
2025-04-04 22:57:43 +01:00
void APlayerCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector Location = GetActorLocation();
if (Location.X < BottomRight.X || Location.X > TopLeft.X || Location.Y > BottomRight.Y || Location.Y < TopLeft.Y)
{
Location.X = FMath::Clamp(Location.X, BottomRight.X, TopLeft.X);
Location.Y = FMath::Clamp(Location.Y, TopLeft.Y, BottomRight.Y);
2025-04-04 22:57:43 +01:00
SetActorLocation(Location);
}
2025-04-04 22:57:43 +01:00
}
2024-06-24 21:41:49 +01:00
UEXPComponent* APlayerCharacter::GetEXPComponent()
{
return EXPComponent;
}
2024-06-24 21:42:03 +01:00
UGoldComponent* APlayerCharacter::GetGoldComponent()
{
return GoldComponent;
}
2025-04-07 23:40:01 +01:00
void APlayerCharacter::OnDamaged(FDamageInfo DamageInfo)
2025-04-07 23:40:01 +01:00
{
if (OnDamagedSound)
{
UGameplayStatics::PlaySoundAtLocation(GetWorld(), OnDamagedSound, GetActorLocation());
}
CameraShakeTimelineComponent->PlayFromStart();
2025-04-07 23:40:01 +01:00
}
void APlayerCharacter::OnDeath(FDamageInfo DamageInfo)
2025-04-07 23:40:01 +01:00
{
if (OnDeathSound)
{
UGameplayStatics::PlaySoundAtLocation(GetWorld(), OnDeathSound, GetActorLocation());
}
}
void APlayerCharacter::CameraShakeTimelineCallback(float Val)
{
auto PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
auto cameraActor = PlayerController->GetViewTarget();
cameraActor->SetActorLocation(FVector(FMath::RandRange(0.0f, CameraShakeStrength) * Val,
FMath::RandRange(0.0f, CameraShakeStrength) * Val, 0.0f));
}
void APlayerCharacter::CameraShakeTimelineFinishedCallback()
{
auto PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
auto cameraActor = PlayerController->GetViewTarget();
cameraActor->SetActorLocation(FVector::ZeroVector);
}
void APlayerCharacter::ResizeViewportTimerCallback()
{
OnViewportResized(GEngine->GameViewport->Viewport, 0);
}
void APlayerCharacter::OnViewportResized(FViewport* ViewPort, uint32 val)
{
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
FVector2d ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
PlayerController->DeprojectScreenPositionToWorld(0, 0, TopLeft, TopLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, ViewportSize.Y, BottomRight, BottomRightDir);
}