From c964d7bd455dfb70665f5a00487898beaf94b45b Mon Sep 17 00:00:00 2001 From: Kingsmedia Date: Mon, 30 May 2022 22:35:28 +0200 Subject: [PATCH] 221 - Teams Cooldown Announcement --- .../BlasterPlayerController.cpp | 97 ++++++++++++++----- .../BlasterPlayerController.h | 3 + Source/Blaster/Types/Announcement.h | 14 +++ 3 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 Source/Blaster/Types/Announcement.h diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.cpp b/Source/Blaster/PlayerController/BlasterPlayerController.cpp index 6e6259a..206d33f 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.cpp +++ b/Source/Blaster/PlayerController/BlasterPlayerController.cpp @@ -13,6 +13,7 @@ #include "Blaster/HUD/DebugWidget.h" #include "Blaster/HUD/ReturnToMainMenu.h" #include "Blaster/PlayerState/BlasterPlayerState.h" +#include "Blaster/Types/Announcement.h" #include "Components/ProgressBar.h" #include "Components/TextBlock.h" #include "GameFramework/GameMode.h" @@ -697,7 +698,7 @@ void ABlasterPlayerController::HandleCooldown() if (bHUDValid) { BlasterHUD->Announcement->SetVisibility(ESlateVisibility::Visible); - FString AnnouncementText("New match starts in:"); + FString AnnouncementText = Announcement::NewMatchStartsIn; BlasterHUD->Announcement->AnnouncementText->SetText(FText::FromString(AnnouncementText)); ABlasterGameState* BlasterGameState = Cast(UGameplayStatics::GetGameState(this)); @@ -705,27 +706,7 @@ void ABlasterPlayerController::HandleCooldown() if (BlasterGameState && BlasterPlayerState) { TArray TopPlayers = BlasterGameState->TopScoringPlayers; - FString InfoTextString; - if (TopPlayers.Num() == 0) - { - InfoTextString = FString("There is no winner."); - } - else if (TopPlayers.Num() == 1 && TopPlayers[0] == BlasterPlayerState) - { - InfoTextString = FString("You are the winner!"); - } - else if (TopPlayers.Num() == 1) - { - InfoTextString = FString::Printf(TEXT("%s won!"), *TopPlayers[0]->GetPlayerName()); - } - else if (TopPlayers.Num() > 1) - { - InfoTextString = FString("Players tied for the win:\n"); - for (auto TiedPlayer : TopPlayers) - { - InfoTextString.Append(FString::Printf(TEXT("%s\n"), *TiedPlayer->GetPlayerName())); - } - } + FString InfoTextString = bShowTeamScores ? GetTeamsInfoText(BlasterGameState) : GetInfoText(TopPlayers); BlasterHUD->Announcement->AnnouncementMessage->SetText(FText::FromString(InfoTextString)); } @@ -738,3 +719,75 @@ void ABlasterPlayerController::HandleCooldown() BlasterCharacter->GetCombat()->FireButtonPressed(false); } } + +FString ABlasterPlayerController::GetInfoText(const TArray& Players) +{ + const ABlasterPlayerState* BlasterPlayerState = GetPlayerState(); + if (BlasterPlayerState == nullptr) return FString(); + + FString InfoTextString; + if (Players.Num() == 0) + { + InfoTextString = Announcement::ThereIsNoWinner; + } + else if (Players.Num() == 1 && Players[0] == BlasterPlayerState) + { + InfoTextString = Announcement::YouAreTheWinner; + } + else if (Players.Num() == 1) + { + InfoTextString = FString::Printf(TEXT("%s won!"), *Players[0]->GetPlayerName()); + } + else if (Players.Num() > 1) + { + InfoTextString = Announcement::PlayersTiedForTheWin; + InfoTextString.Append(FString("\n")); + for (const auto TiedPlayer : Players) + { + InfoTextString.Append(FString::Printf(TEXT("%s\n"), *TiedPlayer->GetPlayerName())); + } + } + + return InfoTextString; +} + +FString ABlasterPlayerController::GetTeamsInfoText(ABlasterGameState* BlasterGameState) +{ + if (BlasterGameState == nullptr) return FString(); + + FString InfoTextString; + + const int32 RedTeamScore = BlasterGameState->RedTeamScore; + const int32 BlueTeamScore = BlasterGameState->BlueTeamScore; + + if (RedTeamScore == 0 && BlueTeamScore == 0) + { + InfoTextString = Announcement::ThereIsNoWinner; + } + else if (RedTeamScore == BlueTeamScore) + { + InfoTextString = FString::Printf(TEXT("%s\n"), *Announcement::TeamsTiedForTheWin); + InfoTextString.Append(Announcement::RedTeam); + InfoTextString.Append(TEXT("\n")); + InfoTextString.Append(Announcement::BlueTeam); + InfoTextString.Append(TEXT("\n")); + } + else if (RedTeamScore > BlueTeamScore) + { + InfoTextString = Announcement::RedTeamWins; + InfoTextString.Append(TEXT("\n")); + InfoTextString.Append(FString::Printf(TEXT("%s: %d"), *Announcement::RedTeam, RedTeamScore)); + InfoTextString.Append(TEXT("\n")); + InfoTextString.Append(FString::Printf(TEXT("%s: %d"), *Announcement::BlueTeam, BlueTeamScore)); + } + else if (BlueTeamScore > RedTeamScore) + { + InfoTextString = Announcement::BlueTeamWins; + InfoTextString.Append(TEXT("\n")); + InfoTextString.Append(FString::Printf(TEXT("%s: %d"), *Announcement::BlueTeam, BlueTeamScore)); + InfoTextString.Append(TEXT("\n")); + InfoTextString.Append(FString::Printf(TEXT("%s: %d"), *Announcement::RedTeam, RedTeamScore)); + } + + return InfoTextString; +} diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.h b/Source/Blaster/PlayerController/BlasterPlayerController.h index 7bf1b12..53901cc 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.h +++ b/Source/Blaster/PlayerController/BlasterPlayerController.h @@ -103,6 +103,9 @@ protected: UFUNCTION() void OnRep_ShowTeamScores(); + + FString GetInfoText(const TArray& Players); + FString GetTeamsInfoText(class ABlasterGameState* BlasterGameState); private: UPROPERTY() diff --git a/Source/Blaster/Types/Announcement.h b/Source/Blaster/Types/Announcement.h new file mode 100644 index 0000000..5712dda --- /dev/null +++ b/Source/Blaster/Types/Announcement.h @@ -0,0 +1,14 @@ +#pragma once + +namespace Announcement +{ + const FString NewMatchStartsIn(TEXT("New match starts in:")); + const FString ThereIsNoWinner(TEXT("There is no winner.")); + const FString YouAreTheWinner(TEXT("You are the winner!")); + const FString PlayersTiedForTheWin(TEXT("Players tied for the win:")); + const FString TeamsTiedForTheWin(TEXT("Teams tied for the win:")); + const FString RedTeam(TEXT("Red team")); + const FString BlueTeam(TEXT("Blue team")); + const FString RedTeamWins(TEXT("Red team wins!")); + const FString BlueTeamWins(TEXT("Blue team wins!")); +}