diff --git a/Source/Blaster/GameMode/BlasterGameMode.h b/Source/Blaster/GameMode/BlasterGameMode.h index c8b1d5d..862148b 100644 --- a/Source/Blaster/GameMode/BlasterGameMode.h +++ b/Source/Blaster/GameMode/BlasterGameMode.h @@ -23,6 +23,8 @@ public: UPROPERTY(EditDefaultsOnly) float WarmupTime = 10.f; + UPROPERTY(EditDefaultsOnly) + float MatchTime = 120.f; float LevelStartingTime = 0.f; diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.cpp b/Source/Blaster/PlayerController/BlasterPlayerController.cpp index 75e9f49..3d9c1c8 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.cpp +++ b/Source/Blaster/PlayerController/BlasterPlayerController.cpp @@ -4,24 +4,23 @@ #include "BlasterPlayerController.h" #include "Blaster/Character/BlasterCharacter.h" +#include "Blaster/GameMode/BlasterGameMode.h" #include "Blaster/HUD/Announcement.h" #include "Blaster/HUD/BlasterHUD.h" #include "Blaster/HUD/CharacterOverlay.h" #include "Components/ProgressBar.h" #include "Components/TextBlock.h" #include "GameFramework/GameMode.h" +#include "Kismet/GameplayStatics.h" #include "Net/UnrealNetwork.h" void ABlasterPlayerController::BeginPlay() { Super::BeginPlay(); - + BlasterHUD = Cast(GetHUD()); - if (BlasterHUD) - { - BlasterHUD->AddAnnouncementOverlay(); - } + ServerCheckMatchState(); } void ABlasterPlayerController::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const @@ -34,10 +33,10 @@ void ABlasterPlayerController::GetLifetimeReplicatedProps(TArray(UGameplayStatics::GetGameMode(this)); + if (GameMode) + { + LevelStartingTime = GameMode->LevelStartingTime; + WarmupTime = GameMode->WarmupTime; + MatchTime = GameMode->MatchTime; + MatchState = GameMode->GetMatchState(); + ClientJoinMidGame(MatchState, WarmupTime, MatchTime, LevelStartingTime); + } +} + + +void ABlasterPlayerController::ClientJoinMidGame_Implementation(FName StateOfMatch, float Warmup, float Match, float StartingTime) +{ + LevelStartingTime = StartingTime; + WarmupTime = Warmup; + MatchTime = Match; + MatchState = StateOfMatch; + OnMatchStateSet(MatchState); + + if (BlasterHUD && MatchState == MatchState::WaitingToStart) + { + BlasterHUD->AddAnnouncementOverlay(); + } +} + void ABlasterPlayerController::ServerRequestServerTime_Implementation(float TimeOfClientRequest) { const float ServerTimeOfReceipt = GetWorld()->GetTimeSeconds(); @@ -124,11 +151,15 @@ void ABlasterPlayerController::ClientReportServerTime_Implementation(float TimeO void ABlasterPlayerController::SetHUDTime() { - const uint32 SecondsLeft = FMath::CeilToInt(MatchTime - GetServerTime()); + float TimeLeft = 0.f; + if (MatchState == MatchState::WaitingToStart) TimeLeft = WarmupTime - GetServerTime() + LevelStartingTime; + else if (MatchState == MatchState::InProgress) TimeLeft = WarmupTime + MatchTime - GetServerTime() + LevelStartingTime; + const uint32 SecondsLeft = FMath::CeilToInt(TimeLeft); if (CountdownInt != SecondsLeft) { - SetHUDMatchCountdown(MatchTime - GetServerTime()); + if (MatchState == MatchState::WaitingToStart) SetHUDAnnouncementCountdown(TimeLeft); + if (MatchState == MatchState::InProgress) SetHUDMatchCountdown(TimeLeft); } CountdownInt = SecondsLeft; @@ -243,3 +274,22 @@ void ABlasterPlayerController::SetHUDMatchCountdown(float CountdownTime) BlasterHUD->CharacterOverlay->MatchCountdownText->SetText(FText::FromString(CountdownText)); } } + +void ABlasterPlayerController::SetHUDAnnouncementCountdown(float CountdownTime) +{ + BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; + bool bHUDValid = + BlasterHUD && + BlasterHUD->Announcement && + BlasterHUD->Announcement->AnnouncementText && + BlasterHUD->Announcement->WarmupTime; + + if (bHUDValid) + { + const int32 Minutes = FMath::FloorToInt(CountdownTime / 60); + const int32 Seconds = CountdownTime - Minutes * 60; + + const FString CountdownText = FString::Printf(TEXT("%02d:%02d"), Minutes, Seconds); + BlasterHUD->Announcement->WarmupTime->SetText(FText::FromString(CountdownText)); + } +} diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.h b/Source/Blaster/PlayerController/BlasterPlayerController.h index 800fe5f..2ddc676 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.h +++ b/Source/Blaster/PlayerController/BlasterPlayerController.h @@ -27,6 +27,7 @@ public: void SetHUDWeaponAmmo(int32 Ammo); void SetHUDCarriedAmmo(int32 Ammo); void SetHUDMatchCountdown(float CountdownTime); + void SetHUDAnnouncementCountdown(float CountdownTime); // Synced with server world clock virtual float GetServerTime(); @@ -58,12 +59,20 @@ protected: float TimeSyncFrequency = 5.f; float TimeSyncRunningTime = 0.f; + + UFUNCTION(Server, Reliable) + void ServerCheckMatchState(); + + UFUNCTION(Client, Reliable) + void ClientJoinMidGame(FName StateOfMatch, float Warmup, float Match, float StartingTime); private: UPROPERTY() class ABlasterHUD* BlasterHUD; - float MatchTime = 120.f; + float LevelStartingTime = 0.f; + float MatchTime = 0.f; + float WarmupTime = 0.f; uint32 CountdownInt = 0; UPROPERTY(ReplicatedUsing=OnRep_MatchState)