Added Interfacing to Blueprint UI via Game State

This commit is contained in:
Philip W 2022-11-14 00:04:44 +00:00
parent df51750a5e
commit 2a92b5577f
2 changed files with 77 additions and 8 deletions

View File

@ -2,8 +2,10 @@
#include "TurnBaseCombat.h"
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/TextBlock.h"
#include "Components/ProgressBar.h"
#include "Kismet/GameplayStatics.h"
ATurnBaseCombat::ATurnBaseCombat()
@ -21,12 +23,38 @@ void ATurnBaseCombat::BeginPlay()
Super::BeginPlay();
TArray<AActor*> AllCharacterActorsInScene;
UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), AActor::StaticClass(), FName("Character"),
AllCharacterActorsInScene);
UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), AActor::StaticClass(), FName("Character"), AllCharacterActorsInScene);
ActiveActionPoints = DefaultActionPoints;
UUserWidget* HUD = CreateWidget<UUserWidget>(GetWorld(), HUDWidget);
HUD->AddToViewport();
TurnIndicator = Cast<UTextBlock>(HUD->GetWidgetFromName("TurnIndicator"));
PlayerHealthBar = Cast<UProgressBar>(HUD->GetWidgetFromName("PlayerHealthBar"));
EnemyHealthBar = Cast<UProgressBar>(HUD->GetWidgetFromName("EnemyHealthBar"));
}
void ATurnBaseCombat::ExecuteCast(FString Combo)
{
if (!IsValidCombo(Combo))
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Invalid Combo"));
return;
}
switch (bIsPlayerTurn)
{
case true:
// Player Turn
DamageEnemy(ValidCombos[Combo]);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Enemy Damaged %f"), ValidCombos[Combo]);
break;
case false:
// Enemy Turn
DamagePlayer(ValidCombos[Combo]);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Player Damaged %f"), ValidCombos[Combo]);
break;
}
}
void ATurnBaseCombat::UseActionPoint()
@ -46,6 +74,27 @@ void ATurnBaseCombat::RevertActionPoints()
ActiveActionPoints = DefaultActionPoints;
}
void ATurnBaseCombat::DamagePlayer(int Damage)
{
PlayerHealth -= FMath::Clamp(Damage, 0, 100);
}
void ATurnBaseCombat::DamageEnemy(int Damage)
{
EnemyHealth -= FMath::Clamp(Damage, 0, 100);
}
void ATurnBaseCombat::UpdateProgressBars() const
{
PlayerHealthBar->SetPercent(PlayerHealth / 100);
EnemyHealthBar->SetPercent(EnemyHealth / 100);
}
bool ATurnBaseCombat::IsValidCombo(FString Combo) const
{
return ValidCombos.Contains(Combo);
}
void ATurnBaseCombat::SwitchTurn()
{
activeActor = bIsPlayerTurn ? enemyActor : playerActor;

View File

@ -3,6 +3,8 @@
#pragma once
#include "CoreMinimal.h"
#include "Components/ProgressBar.h"
#include "Components/TextBlock.h"
#include "GameFramework/GameStateBase.h"
#include "TurnBaseCombat.generated.h"
@ -18,11 +20,11 @@ public:
ATurnBaseCombat();
UPROPERTY(EditDefaultsOnly)
int PlayerHealth = 100;
int PlayerHealth = 100;
UPROPERTY(EditDefaultsOnly)
int EnemyHealth = 100;
int EnemyHealth = 100;
UPROPERTY(EditDefaultsOnly)
int DefaultActionPoints = 3;
int DefaultActionPoints = 3;
int ActiveActionPoints = 0;
AActor* playerActor;
@ -37,15 +39,33 @@ public:
*/
UPROPERTY(EditAnywhere)
TSubclassOf<UUserWidget> HUDWidget;
UPROPERTY(EditAnywhere)
TMap<FString, int32> ValidCombos =
{
{"F", 10},
{"W", 10},
{"FW", 20}
};
protected:
virtual void BeginPlay() override;
void ExecuteCast(FString Combo);
void UseActionPoint();
void ReuseActionPoint();
void RevertActionPoints();
void DamagePlayer(int Damage);
void DamageEnemy(int Damage);
void UpdateProgressBars() const;
private:
UPROPERTY(EditDefaultsOnly)
bool bIsPlayerTurn = true;
bool IsValidCombo(FString Combo) const;
UPROPERTY(VisibleAnywhere)
bool bIsPlayerTurn = true;
void SwitchTurn();
UPROPERTY(VisibleAnywhere)
UTextBlock* TurnIndicator;
UPROPERTY(VisibleAnywhere)
UProgressBar* PlayerHealthBar;
UPROPERTY(VisibleAnywhere)
UProgressBar* EnemyHealthBar;
};