119 - Game Timer

This commit is contained in:
Kingsmedia 2022-05-10 10:40:23 +02:00
parent 6960a94be5
commit f1558e3a8a
4 changed files with 48 additions and 2 deletions

View File

@ -34,4 +34,7 @@ public:
UPROPERTY(meta = (BindWidget))
UTextBlock* CarriedAmmoValue;
UPROPERTY(meta = (BindWidget))
UTextBlock* MatchCountdownText;
};

View File

@ -17,6 +17,13 @@ void ABlasterPlayerController::BeginPlay()
BlasterHUD = Cast<ABlasterHUD>(GetHUD());
}
void ABlasterPlayerController::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
SetHUDTime();
}
void ABlasterPlayerController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
@ -27,6 +34,18 @@ void ABlasterPlayerController::OnPossess(APawn* InPawn)
}
}
void ABlasterPlayerController::SetHUDTime()
{
const uint32 SecondsLeft = FMath::CeilToInt(MatchTime - GetWorld()->GetTimeSeconds());
if (CountdownInt != SecondsLeft)
{
SetHUDMatchCountdown(MatchTime - GetWorld()->GetTimeSeconds());
}
CountdownInt = SecondsLeft;
}
void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
@ -104,3 +123,21 @@ void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo)
BlasterHUD->CharacterOverlay->CarriedAmmoValue->SetText(FText::FromString(CarriedAmmoAmount));
}
}
void ABlasterPlayerController::SetHUDMatchCountdown(float CountdownTime)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->MatchCountdownText;
if (bHUDValid)
{
int32 Minutes = FMath::FloorToInt(CountdownTime / 60);
int32 Seconds = CountdownTime - Minutes * 60;
const FString CountdownText = FString::Printf(TEXT("%02d:%02d"), Minutes, Seconds);
BlasterHUD->CharacterOverlay->MatchCountdownText->SetText(FText::FromString(CountdownText));
}
}

View File

@ -16,20 +16,26 @@ class BLASTER_API ABlasterPlayerController : public APlayerController
public:
virtual void Tick(float DeltaSeconds) override;
virtual void OnPossess(APawn* InPawn) override;
void SetHUDHealth(float Health, float MaxHealth);
void SetHUDScore(float Score);
void SetHUDDefeats(int32 Defeats);
void SetHUDWeaponAmmo(int32 Ammo);
void SetHUDCarriedAmmo(int32 Ammo);
virtual void OnPossess(APawn* InPawn) override;
void SetHUDMatchCountdown(float CountdownTime);
protected:
virtual void BeginPlay() override;
void SetHUDTime();
private:
UPROPERTY()
class ABlasterHUD* BlasterHUD;
float MatchTime = 120.f;
uint32 CountdownInt = 0;
};