122 - On Match State Set

This commit is contained in:
Kingsmedia 2022-05-10 12:42:09 +02:00
parent cca4fc3e83
commit b9ac8954e7
6 changed files with 115 additions and 33 deletions

View File

@ -35,6 +35,20 @@ void ABlasterGameMode::Tick(float DeltaSeconds)
} }
} }
void ABlasterGameMode::OnMatchStateSet()
{
Super::OnMatchStateSet();
for (FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
ABlasterPlayerController* PlayerController = Cast<ABlasterPlayerController>(*Iterator);
if (PlayerController)
{
PlayerController->OnMatchStateSet(MatchState);
}
}
}
void ABlasterGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController, void ABlasterGameMode::PlayerEliminated(ABlasterCharacter* EliminatedCharacter, ABlasterPlayerController* VictimController,
ABlasterPlayerController* AttackerController) ABlasterPlayerController* AttackerController)
{ {

View File

@ -28,6 +28,7 @@ public:
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void OnMatchStateSet() override;
private: private:
float CountDownTime = 0.f; float CountDownTime = 0.f;

View File

@ -9,8 +9,6 @@
void ABlasterHUD::BeginPlay() void ABlasterHUD::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
AddCharacterOverlay();
} }
void ABlasterHUD::AddCharacterOverlay() void ABlasterHUD::AddCharacterOverlay()

View File

@ -31,6 +31,7 @@ class BLASTER_API ABlasterHUD : public AHUD
public: public:
virtual void DrawHUD() override; virtual void DrawHUD() override;
void AddCharacterOverlay();
UPROPERTY(EditAnywhere, Category = "Player Stats") UPROPERTY(EditAnywhere, Category = "Player Stats")
TSubclassOf<class UUserWidget> CharacterOverlayClass; TSubclassOf<class UUserWidget> CharacterOverlayClass;
@ -41,7 +42,6 @@ public:
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
void AddCharacterOverlay();
private: private:
FHUDPackage HUDPackage; FHUDPackage HUDPackage;

View File

@ -8,6 +8,8 @@
#include "Blaster/HUD/CharacterOverlay.h" #include "Blaster/HUD/CharacterOverlay.h"
#include "Components/ProgressBar.h" #include "Components/ProgressBar.h"
#include "Components/TextBlock.h" #include "Components/TextBlock.h"
#include "GameFramework/GameMode.h"
#include "Net/UnrealNetwork.h"
void ABlasterPlayerController::BeginPlay() void ABlasterPlayerController::BeginPlay()
@ -17,12 +19,19 @@ void ABlasterPlayerController::BeginPlay()
BlasterHUD = Cast<ABlasterHUD>(GetHUD()); BlasterHUD = Cast<ABlasterHUD>(GetHUD());
} }
void ABlasterPlayerController::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ABlasterPlayerController, MatchState);
}
void ABlasterPlayerController::Tick(float DeltaSeconds) void ABlasterPlayerController::Tick(float DeltaSeconds)
{ {
Super::Tick(DeltaSeconds); Super::Tick(DeltaSeconds);
PollInit();
SetHUDTime(); SetHUDTime();
CheckTimeSync(DeltaSeconds); CheckTimeSync(DeltaSeconds);
} }
@ -63,6 +72,32 @@ float ABlasterPlayerController::GetServerTime()
return GetWorld()->GetTimeSeconds() + ClientServerDelta; return GetWorld()->GetTimeSeconds() + ClientServerDelta;
} }
void ABlasterPlayerController::OnMatchStateSet(FName State)
{
MatchState = State;
if (MatchState == MatchState::InProgress)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
if (BlasterHUD)
{
BlasterHUD->AddCharacterOverlay();
}
}
}
void ABlasterPlayerController::OnRep_MatchState()
{
if (MatchState == MatchState::InProgress)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
if (BlasterHUD)
{
BlasterHUD->AddCharacterOverlay();
}
}
}
void ABlasterPlayerController::ServerRequestServerTime_Implementation(float TimeOfClientRequest) void ABlasterPlayerController::ServerRequestServerTime_Implementation(float TimeOfClientRequest)
{ {
const float ServerTimeOfReceipt = GetWorld()->GetTimeSeconds(); const float ServerTimeOfReceipt = GetWorld()->GetTimeSeconds();
@ -89,14 +124,27 @@ void ABlasterPlayerController::SetHUDTime()
CountdownInt = SecondsLeft; 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) void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth)
{ {
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD; BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid = bool bHUDValid = CharacterOverlay && CharacterOverlay->HealthBar && CharacterOverlay->HealthText;
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->HealthBar &&
BlasterHUD->CharacterOverlay->HealthText;
if (bHUDValid) 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)); const FString HealthText = FString::Printf(TEXT("%d/%d"), FMath::CeilToInt(Health), FMath::CeilToInt(MaxHealth));
BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText)); BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText));
} }
else
{
bInitializeCharacterOverlay = true;
HUDHealth = Health;
HUDMaxHealth = MaxHealth;
}
} }
void ABlasterPlayerController::SetHUDScore(float Score) void ABlasterPlayerController::SetHUDScore(float Score)
{ {
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD; BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid = bool bHUDValid =CharacterOverlay && CharacterOverlay->ScoreValue;
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->ScoreValue;
if (bHUDValid) if (bHUDValid)
{ {
const FString ScoreAmount = FString::Printf(TEXT("%d"), FMath::FloorToInt(Score)); const FString ScoreAmount = FString::Printf(TEXT("%d"), FMath::FloorToInt(Score));
BlasterHUD->CharacterOverlay->ScoreValue->SetText(FText::FromString(ScoreAmount)); BlasterHUD->CharacterOverlay->ScoreValue->SetText(FText::FromString(ScoreAmount));
} }
else
{
bInitializeCharacterOverlay = true;
HUDScore = Score;
}
} }
void ABlasterPlayerController::SetHUDDefeats(int32 Defeats) void ABlasterPlayerController::SetHUDDefeats(int32 Defeats)
{ {
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD; BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid = bool bHUDValid = CharacterOverlay && CharacterOverlay->DefeatsValue;
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->DefeatsValue;
if (bHUDValid) if (bHUDValid)
{ {
const FString DefeatsAmount = FString::Printf(TEXT("%d"), Defeats); const FString DefeatsAmount = FString::Printf(TEXT("%d"), Defeats);
BlasterHUD->CharacterOverlay->DefeatsValue->SetText(FText::FromString(DefeatsAmount)); BlasterHUD->CharacterOverlay->DefeatsValue->SetText(FText::FromString(DefeatsAmount));
} }
else
{
bInitializeCharacterOverlay = true;
HUDDefeats = Defeats;
}
} }
void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo) void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo)
{ {
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD; BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid = bool bHUDValid = CharacterOverlay && CharacterOverlay->WeaponAmmoValue;
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->WeaponAmmoValue;
if (bHUDValid) if (bHUDValid)
{ {
@ -155,10 +210,7 @@ void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo)
void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo) void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo)
{ {
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD; BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid = bool bHUDValid = CharacterOverlay && CharacterOverlay->CarriedAmmoValue;
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->CarriedAmmoValue;
if (bHUDValid) if (bHUDValid)
{ {
@ -170,15 +222,12 @@ void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo)
void ABlasterPlayerController::SetHUDMatchCountdown(float CountdownTime) void ABlasterPlayerController::SetHUDMatchCountdown(float CountdownTime)
{ {
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD; BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid = bool bHUDValid = CharacterOverlay && CharacterOverlay->MatchCountdownText;
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->MatchCountdownText;
if (bHUDValid) if (bHUDValid)
{ {
int32 Minutes = FMath::FloorToInt(CountdownTime / 60); const int32 Minutes = FMath::FloorToInt(CountdownTime / 60);
int32 Seconds = CountdownTime - Minutes * 60; const int32 Seconds = CountdownTime - Minutes * 60;
const FString CountdownText = FString::Printf(TEXT("%02d:%02d"), Minutes, Seconds); const FString CountdownText = FString::Printf(TEXT("%02d:%02d"), Minutes, Seconds);
BlasterHUD->CharacterOverlay->MatchCountdownText->SetText(FText::FromString(CountdownText)); BlasterHUD->CharacterOverlay->MatchCountdownText->SetText(FText::FromString(CountdownText));

View File

@ -19,6 +19,7 @@ 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;
void SetHUDHealth(float Health, float MaxHealth); void SetHUDHealth(float Health, float MaxHealth);
void SetHUDScore(float Score); void SetHUDScore(float Score);
@ -30,11 +31,14 @@ public:
// Synced with server world clock // Synced with server world clock
virtual float GetServerTime(); virtual float GetServerTime();
void OnMatchStateSet(FName State);
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
void CheckTimeSync(float DeltaSeconds); void CheckTimeSync(float DeltaSeconds);
void SetHUDTime(); void SetHUDTime();
void PollInit();
// Sync time between client and server // Sync time between client and server
@ -60,4 +64,20 @@ private:
float MatchTime = 120.f; float MatchTime = 120.f;
uint32 CountdownInt = 0; 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;
}; };