Some basic boilerplate for win/lose states

This commit is contained in:
charnet3d 2024-01-29 03:46:47 +01:00
parent 549d4e590a
commit 1d964b289c
14 changed files with 207 additions and 13 deletions

View File

@ -9,7 +9,9 @@
"Type": "Runtime",
"LoadingPhase": "Default",
"AdditionalDependencies": [
"SST"
"SST",
"AIModule",
"Engine"
]
}
],

View File

@ -0,0 +1,36 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "FighterAIController.h"
#include "FunnyPrinceCharacter.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Kismet/GameplayStatics.h"
void AFighterAIController::BeginPlay()
{
Super::BeginPlay();
if (AIBehavior)
{
RunBehaviorTree(AIBehavior);
GetBlackboardComponent()->SetValueAsVector(TEXT("StartLocation"), GetPawn()->GetActorLocation());
}
}
void AFighterAIController::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
}
bool AFighterAIController::IsDead() const
{
AFunnyPrinceCharacter* ControlledCharacter = Cast<AFunnyPrinceCharacter>(GetPawn());
if (ControlledCharacter)
{
return ControlledCharacter->IsDead();
}
return true;
}

View File

@ -0,0 +1,28 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AIController.h"
#include "FighterAIController.generated.h"
/**
*
*/
UCLASS()
class FUNNYPRINCE_API AFighterAIController : public AAIController
{
GENERATED_BODY()
public:
virtual void Tick(float DeltaSeconds) override;
bool IsDead() const;
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(EditAnywhere)
class UBehaviorTree* AIBehavior;
};

View File

@ -8,6 +8,6 @@ public class FunnyPrince : ModuleRules
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", "UMG" });
}
}

View File

@ -8,6 +8,8 @@
#include "EnhancedInputComponent.h"
#include "Engine/DamageEvents.h"
#include "EnemyCharacter.h"
#include "FunnyPrinceGameMode.h"
#include "Components/CapsuleComponent.h"
DEFINE_LOG_CATEGORY(LogTemplateFunnyPrinceCharacter);
@ -46,6 +48,8 @@ void AFunnyPrinceCharacter::BeginPlay()
{
// Call the base class
Super::BeginPlay();
Health = MaxHealth;
}
@ -84,14 +88,15 @@ float AFunnyPrinceCharacter::TakeDamage(float DamageAmount, FDamageEvent const&
{
Health = 0;
/*ASimpleShooterGameModeBase* GameMode = GetWorld()->GetAuthGameMode<ASimpleShooterGameModeBase>();
AFunnyPrinceGameMode* GameMode = GetWorld()->GetAuthGameMode<AFunnyPrinceGameMode>();
if (GameMode)
{
GameMode->PawnKilled(this);
}
DetachFromControllerPendingDestroy();
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);*/
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
SetLifeSpan(0.f);
}
UE_LOG(LogTemp, Warning, TEXT("Actor hit for %f damage. Current HP: %f"), DamageToApply, Health);

View File

@ -29,6 +29,9 @@ public:
// To add mapping context
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintPure)
bool IsDead() const;
protected:
// To add mapping context
@ -36,9 +39,6 @@ protected:
virtual float TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser) override;
UFUNCTION(BlueprintPure)
bool IsDead() const;
UFUNCTION(BlueprintPure)
float GetHealthPercent() const;

View File

@ -0,0 +1,41 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "FunnyPrinceController.h"
#include "Blueprint/UserWidget.h"
void AFunnyPrinceController::BeginPlay()
{
Super::BeginPlay();
HUD = CreateWidget(this, HUDClass);
if (HUD != nullptr)
{
HUD->AddToViewport();
}
}
void AFunnyPrinceController::GameHasEnded(AActor* EndGameFocus, bool bIsWinner)
{
Super::GameHasEnded(EndGameFocus, bIsWinner);
HUD->RemoveFromParent();
if (bIsWinner)
{
UUserWidget* WinScreen = CreateWidget(this, WinScreenClass);
if (WinScreen != nullptr)
{
WinScreen->AddToViewport();
}
}
else
{
UUserWidget* LoseScreen = CreateWidget(this, LoseScreenClass);
if (LoseScreen != nullptr)
{
LoseScreen->AddToViewport();
}
}
GetWorldTimerManager().SetTimer(RestartTimer, this, &APlayerController::RestartLevel, RestartDelay);
}

View File

@ -0,0 +1,42 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "Blueprint/UserWidget.h"
#include "FunnyPrinceController.generated.h"
class UUserWidget;
/**
*
*/
UCLASS()
class FUNNYPRINCE_API AFunnyPrinceController : public APlayerController
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
virtual void GameHasEnded(class AActor* EndGameFocus = nullptr, bool bIsWinner = false) override;
private:
UPROPERTY(EditAnywhere)
TSubclassOf<UUserWidget> HUDClass;
UPROPERTY(EditAnywhere)
TSubclassOf<UUserWidget> WinScreenClass;
UPROPERTY(EditAnywhere)
TSubclassOf<UUserWidget> LoseScreenClass;
UPROPERTY(EditAnywhere)
float RestartDelay = 5;
FTimerHandle RestartTimer;
UPROPERTY()
UUserWidget* HUD;
};

View File

@ -2,8 +2,45 @@
#include "FunnyPrinceGameMode.h"
#include "UObject/ConstructorHelpers.h"
#include "EngineUtils.h"
#include "FighterAIController.h"
AFunnyPrinceGameMode::AFunnyPrinceGameMode()
{
}
void AFunnyPrinceGameMode::PawnKilled(APawn* PawnKilled)
{
if (!PawnKilled)
{
return;
}
APlayerController* PlayerController = Cast<APlayerController>(PawnKilled->GetController());
if (PlayerController)
{
PlayerController->GameHasEnded(nullptr, false);
}
// Check for win condition
for (AFighterAIController* Controller : TActorRange<AFighterAIController>(GetWorld()))
{
if (!Controller->IsDead())
{
return;
}
}
// All AI is dead, game is won
EndGame(true);
}
void AFunnyPrinceGameMode::EndGame(bool bIsPlayerWinner)
{
for (AController* Controller : TActorRange<AController>(GetWorld()))
{
bool bWon = (bIsPlayerWinner == Controller->IsPlayerController());
Controller->GameHasEnded(Controller->GetPawn(), bWon);
}
}

View File

@ -13,6 +13,9 @@ class AFunnyPrinceGameMode : public AGameModeBase
public:
AFunnyPrinceGameMode();
virtual void PawnKilled(APawn* PawnKilled);
void EndGame(bool bIsPlayerWinner);
};