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,
ABlasterPlayerController* AttackerController)
{

View File

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

View File

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

View File

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

View File

@ -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<ABlasterHUD>(GetHUD());
}
void ABlasterPlayerController::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& 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<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)
{
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<ABlasterHUD>(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<ABlasterHUD>(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<ABlasterHUD>(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<ABlasterHUD>(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<ABlasterHUD>(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<ABlasterHUD>(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));

View File

@ -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<FLifetimeProperty>& 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;
};