// Fill out your copyright notice in the Description page of Project Settings. #include "BlasterPlayerController.h" #include "Blaster/Character/BlasterCharacter.h" #include "Blaster/HUD/BlasterHUD.h" #include "Blaster/HUD/CharacterOverlay.h" #include "Components/ProgressBar.h" #include "Components/TextBlock.h" void ABlasterPlayerController::BeginPlay() { Super::BeginPlay(); BlasterHUD = Cast(GetHUD()); } void ABlasterPlayerController::OnPossess(APawn* InPawn) { Super::OnPossess(InPawn); if (const ABlasterCharacter* BlasterCharacter = Cast(InPawn)) { SetHUDHealth(BlasterCharacter->GetHealth(), BlasterCharacter->GetMaxHealth()); } } void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; bool bHUDValid = BlasterHUD && BlasterHUD->CharacterOverlay && BlasterHUD->CharacterOverlay->HealthBar && BlasterHUD->CharacterOverlay->HealthText; if (bHUDValid) { const float HealthPercent = Health / MaxHealth; BlasterHUD->CharacterOverlay->HealthBar->SetPercent(HealthPercent); const FString HealthText = FString::Printf(TEXT("%d/%d"), FMath::CeilToInt(Health), FMath::CeilToInt(MaxHealth)); BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText)); } } void ABlasterPlayerController::SetHUDScore(float Score) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; bool bHUDValid = BlasterHUD && BlasterHUD->CharacterOverlay && BlasterHUD->CharacterOverlay->ScoreValue; if (bHUDValid) { const FString ScoreAmount = FString::Printf(TEXT("%d"), FMath::FloorToInt(Score)); BlasterHUD->CharacterOverlay->ScoreValue->SetText(FText::FromString(ScoreAmount)); } } void ABlasterPlayerController::SetHUDDefeats(int32 Defeats) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; bool bHUDValid = BlasterHUD && BlasterHUD->CharacterOverlay && BlasterHUD->CharacterOverlay->DefeatsValue; if (bHUDValid) { const FString DefeatsAmount = FString::Printf(TEXT("%d"), Defeats); BlasterHUD->CharacterOverlay->DefeatsValue->SetText(FText::FromString(DefeatsAmount)); } }