game over widget

pull/6/head
m4xxx1m 2 years ago
parent 3151f79474
commit 33cae564e9

@ -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];

@ -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!"));
}
}

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

@ -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<AMyPlayerState>(PlayerArray[0])->GameOver(PlayerIndexLose);
if (bIsMultiplayer) {
Cast<AMyPlayerState>(PlayerArray[1])->GameOver(PlayerIndexLose);
}
}
// void AMyGameState::GameOver_Implementation(int PlayerIndexLose) {
// UGameOverWidget *CreatedWidget = CreateWidget<UGameOverWidget>(
// GetWorld(), GameOverWidgetClass);
// CreatedWidget->AddToViewport();
// CreatedWidget->SetWidgetText(PlayerIndexLose != );
// }
void AMyGameState::GetLifetimeReplicatedProps(
TArray<FLifetimeProperty> &OutLifetimeProps) const {

@ -45,8 +45,14 @@ public:
UFUNCTION()
void DecreaseLivingTroopers(int PlayerIndex);
UFUNCTION()
void GameOver(int PlayerIndexLose) const;
protected:
// UPROPERTY(EditAnywhere, BlueprintReadWrite)
// TSubclassOf<UUserWidget> GameOverWidgetClass;
UPROPERTY()
bool bIsMultiplayer = true;

@ -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<UGameOverWidget>(
GetWorld(), GameOverWidgetClass);
CreatedWidget->AddToViewport();
CreatedWidget->SetWidgetText(PlayerLoseIndex != PlayerIndex);
}
void AMyPlayerState::SetEnemySelection_Implementation(
/*const TArray<AActor *> &Troopers*/) const {
TArray<AActor *> Troopers;

@ -56,7 +56,13 @@ public:
UFUNCTION(Client, Reliable)
void SetEnemySelection(/*const TArray<AActor *> &Troopers*/) const;
private:
UFUNCTION(Client, Reliable)
void GameOver(int PlayerLoseIndex);
protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<UUserWidget> GameOverWidgetClass;
UPROPERTY(Replicated)
bool bIsSelectionInitialized = false;

Loading…
Cancel
Save