diff --git a/Source/Blaster/GameMode/BlasterGameMode.cpp b/Source/Blaster/GameMode/BlasterGameMode.cpp index bc78cf4..a1075f4 100644 --- a/Source/Blaster/GameMode/BlasterGameMode.cpp +++ b/Source/Blaster/GameMode/BlasterGameMode.cpp @@ -49,7 +49,7 @@ void ABlasterGameMode::Tick(float DeltaSeconds) } else if (MatchState == MatchState::Cooldown) { - CountDownTime = WarmupTime + MatchTime + CooldownTime - GetWorld()->GetTimeSeconds() + LevelStartingTime; + CountDownTime = CooldownTime + WarmupTime + MatchTime - GetWorld()->GetTimeSeconds() + LevelStartingTime; if (CountDownTime <= 0.f) { RestartGame(); diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.cpp b/Source/Blaster/PlayerController/BlasterPlayerController.cpp index a3d292d..b3ac849 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.cpp +++ b/Source/Blaster/PlayerController/BlasterPlayerController.cpp @@ -73,7 +73,6 @@ void ABlasterPlayerController::CheckTimeSync(float DeltaSeconds) float ABlasterPlayerController::GetServerTime() { if (HasAuthority()) return GetWorld()->GetTimeSeconds(); - return GetWorld()->GetTimeSeconds() + ClientServerDelta; } @@ -158,7 +157,6 @@ void ABlasterPlayerController::ServerCheckMatchState_Implementation() } } - void ABlasterPlayerController::ClientJoinMidGame_Implementation(FName StateOfMatch, float Warmup, float Match, float Cooldown, float StartingTime) { LevelStartingTime = StartingTime; @@ -184,7 +182,6 @@ void ABlasterPlayerController::ClientReportServerTime_Implementation(float TimeO { const float RoundTripTime = GetWorld()->GetTimeSeconds() - TimeOfClientRequest; const float CurrentServerTime = TimeServerReceivedClientRequest + (0.5f * RoundTripTime); - ClientServerDelta = CurrentServerTime - GetWorld()->GetTimeSeconds(); } @@ -193,22 +190,19 @@ void ABlasterPlayerController::SetHUDTime() float TimeLeft = 0.f; if (MatchState == MatchState::WaitingToStart) TimeLeft = WarmupTime - GetServerTime() + LevelStartingTime; else if (MatchState == MatchState::InProgress) TimeLeft = WarmupTime + MatchTime - GetServerTime() + LevelStartingTime; - else if (MatchState == MatchState::Cooldown) TimeLeft = WarmupTime + MatchTime + CooldownTime - GetServerTime() + LevelStartingTime; + else if (MatchState == MatchState::Cooldown) TimeLeft = CooldownTime + WarmupTime + MatchTime - GetServerTime() + LevelStartingTime; + uint32 SecondsLeft = FMath::CeilToInt(TimeLeft); - - if (HasAuthority()) - { - BlasterGameMode = BlasterGameMode == nullptr ? Cast(UGameplayStatics::GetGameMode(this)) : BlasterGameMode; - if (BlasterGameMode) - { - SecondsLeft = FMath::CeilToInt(BlasterGameMode->GetCountdownTime() + LevelStartingTime); - } - } - if (CountdownInt != SecondsLeft) { - if (MatchState == MatchState::WaitingToStart || MatchState == MatchState::Cooldown) SetHUDAnnouncementCountdown(TimeLeft); - if (MatchState == MatchState::InProgress) SetHUDMatchCountdown(TimeLeft); + if (MatchState == MatchState::WaitingToStart || MatchState == MatchState::Cooldown) + { + SetHUDAnnouncementCountdown(TimeLeft); + } + if (MatchState == MatchState::InProgress) + { + SetHUDMatchCountdown(TimeLeft); + } } CountdownInt = SecondsLeft; diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.h b/Source/Blaster/PlayerController/BlasterPlayerController.h index 245955a..7d00814 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.h +++ b/Source/Blaster/PlayerController/BlasterPlayerController.h @@ -15,12 +15,11 @@ class BLASTER_API ABlasterPlayerController : public APlayerController GENERATED_BODY() 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); void SetHUDDefeats(int32 Defeats); @@ -34,9 +33,8 @@ public: void OnMatchStateSet(FName State); void HandleCooldown(); - -protected: +protected: virtual void BeginPlay() override; void CheckTimeSync(float DeltaSeconds); void HandleMatchHasStarted(); @@ -54,7 +52,7 @@ protected: void ClientReportServerTime(float TimeOfClientRequest, float TimeServerReceivedClientRequest); // Difference between client and server time - float ClientServerDelta = 0; + float ClientServerDelta = 0.f; UPROPERTY(EditAnywhere, Category = Time) float TimeSyncFrequency = 5.f; @@ -66,21 +64,21 @@ protected: UFUNCTION(Client, Reliable) void ClientJoinMidGame(FName StateOfMatch, float Warmup, float Match, float Cooldown, float StartingTime); -private: +private: UPROPERTY() class ABlasterHUD* BlasterHUD; UPROPERTY() class ABlasterGameMode* BlasterGameMode; - + float LevelStartingTime = 0.f; float MatchTime = 0.f; float WarmupTime = 0.f; float CooldownTime = 0.f; uint32 CountdownInt = 0; - UPROPERTY(ReplicatedUsing=OnRep_MatchState) + UPROPERTY(ReplicatedUsing = OnRep_MatchState) FName MatchState; UFUNCTION() @@ -94,5 +92,4 @@ private: float HUDMaxHealth; float HUDScore; int32 HUDDefeats; - };