diff --git a/Content/BattleField/BP_EndOfGameWidget.uasset b/Content/BattleField/BP_EndOfGameWidget.uasset new file mode 100644 index 0000000..91d6502 Binary files /dev/null and b/Content/BattleField/BP_EndOfGameWidget.uasset differ diff --git a/Content/BattleField/BP_MyPlayerState.uasset b/Content/BattleField/BP_MyPlayerState.uasset index 6aa028c..31d3e7a 100644 Binary files a/Content/BattleField/BP_MyPlayerState.uasset and b/Content/BattleField/BP_MyPlayerState.uasset differ diff --git a/Content/BattleField/Multiplayer/BP_MyGameState.uasset b/Content/BattleField/Multiplayer/BP_MyGameState.uasset index b40b693..954272f 100644 Binary files a/Content/BattleField/Multiplayer/BP_MyGameState.uasset and b/Content/BattleField/Multiplayer/BP_MyGameState.uasset differ diff --git a/Content/BattleField/SinglePlayer/BP_SinglePlayerGS.uasset b/Content/BattleField/SinglePlayer/BP_SinglePlayerGS.uasset index 649a0e6..31f2b76 100644 Binary files a/Content/BattleField/SinglePlayer/BP_SinglePlayerGS.uasset and b/Content/BattleField/SinglePlayer/BP_SinglePlayerGS.uasset differ diff --git a/Source/TurnBasedTutorial/EnemyAIController.cpp b/Source/TurnBasedTutorial/EnemyAIController.cpp index 2456825..68fcde5 100644 --- a/Source/TurnBasedTutorial/EnemyAIController.cpp +++ b/Source/TurnBasedTutorial/EnemyAIController.cpp @@ -117,6 +117,9 @@ void AEnemyAIController::MakeMove() { return; } const int Index = GetClosestTrooper(); + if (Index == -1) { + return; + } bool failed; if (!IsCloseEnough(Index)) { failed = MoveTo(Index); @@ -185,7 +188,7 @@ void AEnemyAIController::InitializeSpawnPoints() { int AEnemyAIController::GetClosestTrooper() const { float minDistance = 1000000.0f; - int minIndex = 0; + int minIndex = -1; const ATrooper *CurrentTrooper = PossessedTroopers[TroopersCursor]; for (int index = 0; index < PlayerTroopers.Num(); ++index) { const ATrooper *OtherTrooper = PlayerTroopers[index]; diff --git a/Source/TurnBasedTutorial/GameOverWidget.cpp b/Source/TurnBasedTutorial/GameOverWidget.cpp new file mode 100644 index 0000000..92afb95 --- /dev/null +++ b/Source/TurnBasedTutorial/GameOverWidget.cpp @@ -0,0 +1,13 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#include "GameOverWidget.h" +#include "Components/TextBlock.h" + + +void UGameOverWidget::SetWidgetText_Implementation(bool HasWon) { + if (HasWon) { + GameOverText->SetText(FText::FromString("You won!")); + } else { + GameOverText->SetText(FText::FromString("You lose!")); + } +} diff --git a/Source/TurnBasedTutorial/GameOverWidget.h b/Source/TurnBasedTutorial/GameOverWidget.h new file mode 100644 index 0000000..5bee377 --- /dev/null +++ b/Source/TurnBasedTutorial/GameOverWidget.h @@ -0,0 +1,26 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Blueprint/UserWidget.h" +#include "GameOverWidget.generated.h" + +/** + * + */ +UCLASS() +class TURNBASEDTUTORIAL_API UGameOverWidget : public UUserWidget { + GENERATED_BODY() + +public: + UFUNCTION(Client, Reliable) + void SetWidgetText(bool HasWon); + +protected: + UPROPERTY(meta = (BindWidget)) + class UButton *ButtonToMenu; + + UPROPERTY(meta = (BindWidget)) + class UTextBlock *GameOverText; +}; diff --git a/Source/TurnBasedTutorial/MyGameState.cpp b/Source/TurnBasedTutorial/MyGameState.cpp index ee2bed3..45ed445 100644 --- a/Source/TurnBasedTutorial/MyGameState.cpp +++ b/Source/TurnBasedTutorial/MyGameState.cpp @@ -2,8 +2,10 @@ #include "MyGameState.h" +// #include "GameOverWidget.h" #include "MyPlayerState.h" #include "Trooper.h" +#include "Blueprint/UserWidget.h" #include "Net/UnrealNetwork.h" auto AMyGameState::GetMyPlayerState(uint8 PlayerIndex) const { @@ -90,9 +92,24 @@ void AMyGameState::DecreaseLivingTroopers(int PlayerIndex) { if (LivingTroopersCount[PlayerIndex] <= 0) { UE_LOG(LogTemp, Warning, TEXT("Player %d lose!"), PlayerIndex); bGameIsOver = true; + GameOver(PlayerIndex); } } +void AMyGameState::GameOver(int PlayerIndexLose) const { + Cast(PlayerArray[0])->GameOver(PlayerIndexLose); + if (bIsMultiplayer) { + Cast(PlayerArray[1])->GameOver(PlayerIndexLose); + } +} + +// void AMyGameState::GameOver_Implementation(int PlayerIndexLose) { +// UGameOverWidget *CreatedWidget = CreateWidget( +// GetWorld(), GameOverWidgetClass); +// CreatedWidget->AddToViewport(); +// CreatedWidget->SetWidgetText(PlayerIndexLose != ); +// } + void AMyGameState::GetLifetimeReplicatedProps( TArray &OutLifetimeProps) const { diff --git a/Source/TurnBasedTutorial/MyGameState.h b/Source/TurnBasedTutorial/MyGameState.h index 00817bb..1a30b60 100644 --- a/Source/TurnBasedTutorial/MyGameState.h +++ b/Source/TurnBasedTutorial/MyGameState.h @@ -45,8 +45,14 @@ public: UFUNCTION() void DecreaseLivingTroopers(int PlayerIndex); + + UFUNCTION() + void GameOver(int PlayerIndexLose) const; protected: + // UPROPERTY(EditAnywhere, BlueprintReadWrite) + // TSubclassOf GameOverWidgetClass; + UPROPERTY() bool bIsMultiplayer = true; diff --git a/Source/TurnBasedTutorial/MyPlayerState.cpp b/Source/TurnBasedTutorial/MyPlayerState.cpp index ca50779..240e267 100644 --- a/Source/TurnBasedTutorial/MyPlayerState.cpp +++ b/Source/TurnBasedTutorial/MyPlayerState.cpp @@ -1,6 +1,8 @@ // Fill out your copyright notice in the Description page of Project Settings. #include "MyPlayerState.h" + +#include "GameOverWidget.h" #include "MyGameState.h" #include "Kismet/GameplayStatics.h" #include "Net/UnrealNetwork.h" @@ -37,6 +39,13 @@ void AMyPlayerState::SetPlayerIndex(uint8 NewPlayerIndex) { PlayerIndex = NewPlayerIndex; } +void AMyPlayerState::GameOver_Implementation(int PlayerLoseIndex) { + UGameOverWidget *CreatedWidget = CreateWidget( + GetWorld(), GameOverWidgetClass); + CreatedWidget->AddToViewport(); + CreatedWidget->SetWidgetText(PlayerLoseIndex != PlayerIndex); +} + void AMyPlayerState::SetEnemySelection_Implementation( /*const TArray &Troopers*/) const { TArray Troopers; diff --git a/Source/TurnBasedTutorial/MyPlayerState.h b/Source/TurnBasedTutorial/MyPlayerState.h index 62109fa..84c4aa2 100644 --- a/Source/TurnBasedTutorial/MyPlayerState.h +++ b/Source/TurnBasedTutorial/MyPlayerState.h @@ -56,7 +56,13 @@ public: UFUNCTION(Client, Reliable) void SetEnemySelection(/*const TArray &Troopers*/) const; -private: + UFUNCTION(Client, Reliable) + void GameOver(int PlayerLoseIndex); + +protected: + UPROPERTY(EditAnywhere, BlueprintReadWrite) + TSubclassOf GameOverWidgetClass; + UPROPERTY(Replicated) bool bIsSelectionInitialized = false;