diff --git a/Source/Blaster/GameMode/BlasterGameMode.cpp b/Source/Blaster/GameMode/BlasterGameMode.cpp index e8616f4..304d8e7 100644 --- a/Source/Blaster/GameMode/BlasterGameMode.cpp +++ b/Source/Blaster/GameMode/BlasterGameMode.cpp @@ -35,6 +35,20 @@ void ABlasterGameMode::Tick(float DeltaSeconds) } } +void ABlasterGameMode::OnMatchStateSet() +{ + Super::OnMatchStateSet(); + + for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator) + { + ABlasterPlayerController* PlayerController = Cast(*Iterator); + if (PlayerController) + { + PlayerController->OnMatchStateSet(MatchState); + } + } +} + void ABlasterGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController, ABlasterPlayerController* AttackerController) { diff --git a/Source/Blaster/GameMode/BlasterGameMode.h b/Source/Blaster/GameMode/BlasterGameMode.h index d8efe3c..c8b1d5d 100644 --- a/Source/Blaster/GameMode/BlasterGameMode.h +++ b/Source/Blaster/GameMode/BlasterGameMode.h @@ -28,6 +28,7 @@ public: protected: virtual void BeginPlay() override; + virtual void OnMatchStateSet() override; private: float CountDownTime = 0.f; diff --git a/Source/Blaster/HUD/BlasterHUD.cpp b/Source/Blaster/HUD/BlasterHUD.cpp index 055d3ad..3b0848c 100644 --- a/Source/Blaster/HUD/BlasterHUD.cpp +++ b/Source/Blaster/HUD/BlasterHUD.cpp @@ -9,8 +9,6 @@ void ABlasterHUD::BeginPlay() { Super::BeginPlay(); - - AddCharacterOverlay(); } void ABlasterHUD::AddCharacterOverlay() diff --git a/Source/Blaster/HUD/BlasterHUD.h b/Source/Blaster/HUD/BlasterHUD.h index d211aae..4c2637f 100644 --- a/Source/Blaster/HUD/BlasterHUD.h +++ b/Source/Blaster/HUD/BlasterHUD.h @@ -31,7 +31,8 @@ class BLASTER_API ABlasterHUD : public AHUD public: virtual void DrawHUD() override; - + void AddCharacterOverlay(); + UPROPERTY(EditAnywhere, Category = "Player Stats") TSubclassOf CharacterOverlayClass; @@ -41,7 +42,6 @@ public: protected: virtual void BeginPlay() override; - void AddCharacterOverlay(); private: FHUDPackage HUDPackage; diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.cpp b/Source/Blaster/PlayerController/BlasterPlayerController.cpp index ee231cd..2f77881 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.cpp +++ b/Source/Blaster/PlayerController/BlasterPlayerController.cpp @@ -8,6 +8,8 @@ #include "Blaster/HUD/CharacterOverlay.h" #include "Components/ProgressBar.h" #include "Components/TextBlock.h" +#include "GameFramework/GameMode.h" +#include "Net/UnrealNetwork.h" void ABlasterPlayerController::BeginPlay() @@ -17,12 +19,19 @@ void ABlasterPlayerController::BeginPlay() BlasterHUD = Cast(GetHUD()); } +void ABlasterPlayerController::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const +{ + Super::GetLifetimeReplicatedProps(OutLifetimeProps); + + DOREPLIFETIME(ABlasterPlayerController, MatchState); +} + void ABlasterPlayerController::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); + PollInit(); SetHUDTime(); - CheckTimeSync(DeltaSeconds); } @@ -59,10 +68,36 @@ void ABlasterPlayerController::CheckTimeSync(float DeltaSeconds) float ABlasterPlayerController::GetServerTime() { if (HasAuthority()) return GetWorld()->GetTimeSeconds(); - + return GetWorld()->GetTimeSeconds() + ClientServerDelta; } +void ABlasterPlayerController::OnMatchStateSet(FName State) +{ + MatchState = State; + + if (MatchState == MatchState::InProgress) + { + BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; + if (BlasterHUD) + { + BlasterHUD->AddCharacterOverlay(); + } + } +} + +void ABlasterPlayerController::OnRep_MatchState() +{ + if (MatchState == MatchState::InProgress) + { + BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; + if (BlasterHUD) + { + BlasterHUD->AddCharacterOverlay(); + } + } +} + void ABlasterPlayerController::ServerRequestServerTime_Implementation(float TimeOfClientRequest) { const float ServerTimeOfReceipt = GetWorld()->GetTimeSeconds(); @@ -89,14 +124,27 @@ void ABlasterPlayerController::SetHUDTime() CountdownInt = SecondsLeft; } +void ABlasterPlayerController::PollInit() +{ + if (CharacterOverlay == nullptr) + { + if (BlasterHUD && BlasterHUD->CharacterOverlay) + { + CharacterOverlay = BlasterHUD->CharacterOverlay; + if (CharacterOverlay) + { + SetHUDHealth(HUDHealth, HUDMaxHealth); + SetHUDScore(HUDScore); + SetHUDDefeats(HUDDefeats); + } + } + } +} + void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; - bool bHUDValid = - BlasterHUD && - BlasterHUD->CharacterOverlay && - BlasterHUD->CharacterOverlay->HealthBar && - BlasterHUD->CharacterOverlay->HealthText; + bool bHUDValid = CharacterOverlay && CharacterOverlay->HealthBar && CharacterOverlay->HealthText; if (bHUDValid) { @@ -105,45 +153,52 @@ void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth) const FString HealthText = FString::Printf(TEXT("%d/%d"), FMath::CeilToInt(Health), FMath::CeilToInt(MaxHealth)); BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText)); } + else + { + bInitializeCharacterOverlay = true; + HUDHealth = Health; + HUDMaxHealth = MaxHealth; + } } void ABlasterPlayerController::SetHUDScore(float Score) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; - bool bHUDValid = - BlasterHUD && - BlasterHUD->CharacterOverlay && - BlasterHUD->CharacterOverlay->ScoreValue; + bool bHUDValid =CharacterOverlay && CharacterOverlay->ScoreValue; if (bHUDValid) { const FString ScoreAmount = FString::Printf(TEXT("%d"), FMath::FloorToInt(Score)); BlasterHUD->CharacterOverlay->ScoreValue->SetText(FText::FromString(ScoreAmount)); } + else + { + bInitializeCharacterOverlay = true; + HUDScore = Score; + } } void ABlasterPlayerController::SetHUDDefeats(int32 Defeats) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; - bool bHUDValid = - BlasterHUD && - BlasterHUD->CharacterOverlay && - BlasterHUD->CharacterOverlay->DefeatsValue; + bool bHUDValid = CharacterOverlay && CharacterOverlay->DefeatsValue; if (bHUDValid) { const FString DefeatsAmount = FString::Printf(TEXT("%d"), Defeats); BlasterHUD->CharacterOverlay->DefeatsValue->SetText(FText::FromString(DefeatsAmount)); } + else + { + bInitializeCharacterOverlay = true; + HUDDefeats = Defeats; + } } void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; - bool bHUDValid = - BlasterHUD && - BlasterHUD->CharacterOverlay && - BlasterHUD->CharacterOverlay->WeaponAmmoValue; + bool bHUDValid = CharacterOverlay && CharacterOverlay->WeaponAmmoValue; if (bHUDValid) { @@ -155,10 +210,7 @@ void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo) void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; - bool bHUDValid = - BlasterHUD && - BlasterHUD->CharacterOverlay && - BlasterHUD->CharacterOverlay->CarriedAmmoValue; + bool bHUDValid = CharacterOverlay && CharacterOverlay->CarriedAmmoValue; if (bHUDValid) { @@ -170,15 +222,12 @@ void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo) void ABlasterPlayerController::SetHUDMatchCountdown(float CountdownTime) { BlasterHUD = BlasterHUD == nullptr ? Cast(GetHUD()) : BlasterHUD; - bool bHUDValid = - BlasterHUD && - BlasterHUD->CharacterOverlay && - BlasterHUD->CharacterOverlay->MatchCountdownText; + bool bHUDValid = CharacterOverlay && CharacterOverlay->MatchCountdownText; if (bHUDValid) { - int32 Minutes = FMath::FloorToInt(CountdownTime / 60); - int32 Seconds = CountdownTime - Minutes * 60; + const int32 Minutes = FMath::FloorToInt(CountdownTime / 60); + const int32 Seconds = CountdownTime - Minutes * 60; const FString CountdownText = FString::Printf(TEXT("%02d:%02d"), Minutes, Seconds); BlasterHUD->CharacterOverlay->MatchCountdownText->SetText(FText::FromString(CountdownText)); diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.h b/Source/Blaster/PlayerController/BlasterPlayerController.h index 308c3dd..a1dfa13 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.h +++ b/Source/Blaster/PlayerController/BlasterPlayerController.h @@ -19,6 +19,7 @@ public: virtual void Tick(float DeltaSeconds) override; virtual void OnPossess(APawn* InPawn) override; virtual void ReceivedPlayer() override; // Sync with server clock as soon as possible + virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; void SetHUDHealth(float Health, float MaxHealth); void SetHUDScore(float Score); @@ -29,12 +30,15 @@ public: // Synced with server world clock virtual float GetServerTime(); + + void OnMatchStateSet(FName State); protected: virtual void BeginPlay() override; void CheckTimeSync(float DeltaSeconds); void SetHUDTime(); + void PollInit(); // Sync time between client and server @@ -60,4 +64,20 @@ private: float MatchTime = 120.f; uint32 CountdownInt = 0; + + UPROPERTY(ReplicatedUsing=OnRep_MatchState) + FName MatchState; + + UFUNCTION() + void OnRep_MatchState(); + + UPROPERTY() + class UCharacterOverlay* CharacterOverlay; + bool bInitializeCharacterOverlay = false; + + float HUDHealth; + float HUDMaxHealth; + float HUDScore; + int32 HUDDefeats; + };