127 - Restart Game

This commit is contained in:
Kingsmedia 2022-05-10 19:05:04 +02:00
parent cdca489c56
commit 68ef1dd118
3 changed files with 17 additions and 26 deletions

View File

@ -49,7 +49,7 @@ void ABlasterGameMode::Tick(float DeltaSeconds)
} }
else if (MatchState == MatchState::Cooldown) else if (MatchState == MatchState::Cooldown)
{ {
CountDownTime = WarmupTime + MatchTime + CooldownTime - GetWorld()->GetTimeSeconds() + LevelStartingTime; CountDownTime = CooldownTime + WarmupTime + MatchTime - GetWorld()->GetTimeSeconds() + LevelStartingTime;
if (CountDownTime <= 0.f) if (CountDownTime <= 0.f)
{ {
RestartGame(); RestartGame();

View File

@ -73,7 +73,6 @@ void ABlasterPlayerController::CheckTimeSync(float DeltaSeconds)
float ABlasterPlayerController::GetServerTime() float ABlasterPlayerController::GetServerTime()
{ {
if (HasAuthority()) return GetWorld()->GetTimeSeconds(); if (HasAuthority()) return GetWorld()->GetTimeSeconds();
return GetWorld()->GetTimeSeconds() + ClientServerDelta; 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) void ABlasterPlayerController::ClientJoinMidGame_Implementation(FName StateOfMatch, float Warmup, float Match, float Cooldown, float StartingTime)
{ {
LevelStartingTime = StartingTime; LevelStartingTime = StartingTime;
@ -184,7 +182,6 @@ void ABlasterPlayerController::ClientReportServerTime_Implementation(float TimeO
{ {
const float RoundTripTime = GetWorld()->GetTimeSeconds() - TimeOfClientRequest; const float RoundTripTime = GetWorld()->GetTimeSeconds() - TimeOfClientRequest;
const float CurrentServerTime = TimeServerReceivedClientRequest + (0.5f * RoundTripTime); const float CurrentServerTime = TimeServerReceivedClientRequest + (0.5f * RoundTripTime);
ClientServerDelta = CurrentServerTime - GetWorld()->GetTimeSeconds(); ClientServerDelta = CurrentServerTime - GetWorld()->GetTimeSeconds();
} }
@ -193,22 +190,19 @@ void ABlasterPlayerController::SetHUDTime()
float TimeLeft = 0.f; float TimeLeft = 0.f;
if (MatchState == MatchState::WaitingToStart) TimeLeft = WarmupTime - GetServerTime() + LevelStartingTime; if (MatchState == MatchState::WaitingToStart) TimeLeft = WarmupTime - GetServerTime() + LevelStartingTime;
else if (MatchState == MatchState::InProgress) TimeLeft = WarmupTime + MatchTime - 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); uint32 SecondsLeft = FMath::CeilToInt(TimeLeft);
if (HasAuthority())
{
BlasterGameMode = BlasterGameMode == nullptr ? Cast<ABlasterGameMode>(UGameplayStatics::GetGameMode(this)) : BlasterGameMode;
if (BlasterGameMode)
{
SecondsLeft = FMath::CeilToInt(BlasterGameMode->GetCountdownTime() + LevelStartingTime);
}
}
if (CountdownInt != SecondsLeft) if (CountdownInt != SecondsLeft)
{ {
if (MatchState == MatchState::WaitingToStart || MatchState == MatchState::Cooldown) SetHUDAnnouncementCountdown(TimeLeft); if (MatchState == MatchState::WaitingToStart || MatchState == MatchState::Cooldown)
if (MatchState == MatchState::InProgress) SetHUDMatchCountdown(TimeLeft); {
SetHUDAnnouncementCountdown(TimeLeft);
}
if (MatchState == MatchState::InProgress)
{
SetHUDMatchCountdown(TimeLeft);
}
} }
CountdownInt = SecondsLeft; CountdownInt = SecondsLeft;

View File

@ -15,12 +15,11 @@ class BLASTER_API ABlasterPlayerController : public APlayerController
GENERATED_BODY() GENERATED_BODY()
public: public:
virtual void Tick(float DeltaSeconds) override; virtual void Tick(float DeltaSeconds) override;
virtual void OnPossess(APawn* InPawn) override; virtual void OnPossess(APawn* InPawn) override;
virtual void ReceivedPlayer() override; // Sync with server clock as soon as possible virtual void ReceivedPlayer() override; // Sync with server clock as soon as possible
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override; virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
void SetHUDHealth(float Health, float MaxHealth); void SetHUDHealth(float Health, float MaxHealth);
void SetHUDScore(float Score); void SetHUDScore(float Score);
void SetHUDDefeats(int32 Defeats); void SetHUDDefeats(int32 Defeats);
@ -34,9 +33,8 @@ public:
void OnMatchStateSet(FName State); void OnMatchStateSet(FName State);
void HandleCooldown(); void HandleCooldown();
protected:
protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
void CheckTimeSync(float DeltaSeconds); void CheckTimeSync(float DeltaSeconds);
void HandleMatchHasStarted(); void HandleMatchHasStarted();
@ -54,7 +52,7 @@ protected:
void ClientReportServerTime(float TimeOfClientRequest, float TimeServerReceivedClientRequest); void ClientReportServerTime(float TimeOfClientRequest, float TimeServerReceivedClientRequest);
// Difference between client and server time // Difference between client and server time
float ClientServerDelta = 0; float ClientServerDelta = 0.f;
UPROPERTY(EditAnywhere, Category = Time) UPROPERTY(EditAnywhere, Category = Time)
float TimeSyncFrequency = 5.f; float TimeSyncFrequency = 5.f;
@ -66,21 +64,21 @@ protected:
UFUNCTION(Client, Reliable) UFUNCTION(Client, Reliable)
void ClientJoinMidGame(FName StateOfMatch, float Warmup, float Match, float Cooldown, float StartingTime); void ClientJoinMidGame(FName StateOfMatch, float Warmup, float Match, float Cooldown, float StartingTime);
private:
private:
UPROPERTY() UPROPERTY()
class ABlasterHUD* BlasterHUD; class ABlasterHUD* BlasterHUD;
UPROPERTY() UPROPERTY()
class ABlasterGameMode* BlasterGameMode; class ABlasterGameMode* BlasterGameMode;
float LevelStartingTime = 0.f; float LevelStartingTime = 0.f;
float MatchTime = 0.f; float MatchTime = 0.f;
float WarmupTime = 0.f; float WarmupTime = 0.f;
float CooldownTime = 0.f; float CooldownTime = 0.f;
uint32 CountdownInt = 0; uint32 CountdownInt = 0;
UPROPERTY(ReplicatedUsing=OnRep_MatchState) UPROPERTY(ReplicatedUsing = OnRep_MatchState)
FName MatchState; FName MatchState;
UFUNCTION() UFUNCTION()
@ -94,5 +92,4 @@ private:
float HUDMaxHealth; float HUDMaxHealth;
float HUDScore; float HUDScore;
int32 HUDDefeats; int32 HUDDefeats;
}; };