vampires/Source/vampires/VampirePlayerController.cpp

81 lines
2.1 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "VampirePlayerController.h"
#include "EXPComponent.h"
#include "VampireGameMode.h"
#include "Blueprint/UserWidget.h"
#include "Kismet/GameplayStatics.h"
#include "Widgets/HUDWidget.h"
void AVampirePlayerController::OnPossess(APawn* aPawn)
{
Super::OnPossess(aPawn);
if (PlayerHUD)
{
currentPlayerHUD = CreateWidget<UHUDWidget, AVampirePlayerController*>(this, PlayerHUD.Get());
if (UEXPComponent* expComponent = aPawn->GetComponentByClass<UEXPComponent>())
{
expComponent->OnEXPGained.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerEXPHUD);
expComponent->OnEXPLevelUp.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerLevelHUD);
UpdatePlayerEXPHUD(expComponent->GetCurrentEXP(), expComponent->GetCurrentLevelPercent());
UpdatePlayerLevelHUD(expComponent->GetCurrentLevel());
}
if (AVampireGameMode* gamemode = Cast<AVampireGameMode>(UGameplayStatics::GetGameMode(GetWorld())))
{
gamemode->OnEnemyDeathCountIncrementDelegate.AddDynamic(this, &AVampirePlayerController::UpdateKillCountHUD);
UpdateKillCountHUD(gamemode->GetEnemyDeathCount());
}
GetWorld()->GetTimerManager().SetTimer(pawnLifeTimeHandle, this, &AVampirePlayerController::UpdateTimerHUD, 1.0f, true,0.f);
if (currentPlayerHUD)
{
currentPlayerHUD->AddToViewport();
}
}
}
void AVampirePlayerController::OnUnPossess()
{
Super::OnUnPossess();
GetWorld()->GetTimerManager().ClearTimer(pawnLifeTimeHandle);
}
void AVampirePlayerController::UpdatePlayerEXPHUD(int exp, float currentLevelPercent)
{
if (currentPlayerHUD)
{
currentPlayerHUD->UpdateEXPBar(currentLevelPercent);
}
}
void AVampirePlayerController::UpdatePlayerLevelHUD(int level)
{
if (currentPlayerHUD)
{
currentPlayerHUD->UpdateLevelBlock(level);
}
}
void AVampirePlayerController::UpdateTimerHUD()
{
if (currentPlayerHUD)
{
currentPlayerHUD->UpdateTimerBlock(GetPawn());
}
}
void AVampirePlayerController::UpdateKillCountHUD(int killCount)
{
if (currentPlayerHUD)
{
currentPlayerHUD->UpdateKillBlock(killCount);
}
}