From 2a92b5577ffbbdd5757d508f617a109803f12828 Mon Sep 17 00:00:00 2001 From: PHILIP White Date: Mon, 14 Nov 2022 00:04:44 +0000 Subject: [PATCH] Added Interfacing to Blueprint UI via Game State --- .../TurnBasedCombat/TurnBaseCombat.cpp | 55 ++++++++++++++++++- .../TurnBasedCombat/TurnBaseCombat.h | 30 ++++++++-- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp index c67a361..2c62004 100644 --- a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp +++ b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp @@ -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 AllCharacterActorsInScene; - UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), AActor::StaticClass(), FName("Character"), - AllCharacterActorsInScene); + UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), AActor::StaticClass(), FName("Character"), AllCharacterActorsInScene); ActiveActionPoints = DefaultActionPoints; UUserWidget* HUD = CreateWidget(GetWorld(), HUDWidget); HUD->AddToViewport(); + + TurnIndicator = Cast(HUD->GetWidgetFromName("TurnIndicator")); + PlayerHealthBar = Cast(HUD->GetWidgetFromName("PlayerHealthBar")); + EnemyHealthBar = Cast(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; diff --git a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h index 798c76c..45e7311 100644 --- a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h +++ b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h @@ -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 HUDWidget; + UPROPERTY(EditAnywhere) + TMap 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; };