Updated TurnBasedCombat to Link Enemy Health

This commit is contained in:
Philip W 2023-01-31 00:04:24 +00:00
parent 8b2b9d56cb
commit 6092c22a2b
2 changed files with 16 additions and 9 deletions

View File

@ -3,6 +3,7 @@
#include "TurnBaseCombatV2.h" #include "TurnBaseCombatV2.h"
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Blueprint/UserWidget.h" #include "Blueprint/UserWidget.h"
#include "Components/TextBlock.h" #include "Components/TextBlock.h"
#include "Components/ProgressBar.h" #include "Components/ProgressBar.h"
@ -12,7 +13,6 @@
// Sets default values // Sets default values
ATurnBaseCombatV2::ATurnBaseCombatV2() ATurnBaseCombatV2::ATurnBaseCombatV2()
{ {
if (HUDWidget == nullptr) if (HUDWidget == nullptr)
{ {
// Load the HUD widget from the specified path // Load the HUD widget from the specified path
@ -23,6 +23,14 @@ ATurnBaseCombatV2::ATurnBaseCombatV2()
void ATurnBaseCombatV2::StartCombat(AActor* Enemy) void ATurnBaseCombatV2::StartCombat(AActor* Enemy)
{ {
if (Enemy == nullptr) return;
UBlackboardComponent* EnemyBlackboard = Cast<UBlackboardComponent>(Enemy->GetComponentByClass(UBlackboardComponent::StaticClass()));
if (EnemyBlackboard->GetValueAsBool("IsInCombat")) return;
FProperty* HealthProperty = Enemy->GetClass()->FindPropertyByName(FName("Health"));
int32* EnemyHealthPtr = HealthProperty->ContainerPtrToValuePtr<int32>(Enemy);
EnemyHealth = EnemyHealthPtr;
if (HUD->IsInViewport()) return; if (HUD->IsInViewport()) return;
HUD->AddToViewport(); HUD->AddToViewport();
EnemyActor = Enemy; EnemyActor = Enemy;
@ -48,7 +56,7 @@ void ATurnBaseCombatV2::StartCombat(AActor* Enemy)
void ATurnBaseCombatV2::EndCombat() void ATurnBaseCombatV2::EndCombat()
{ {
//Remove the HUD from the viewport //Remove the HUD from the viewport
HUD->RemoveFromViewport(); HUD->RemoveFromParent();
APawn* PlayerPawn = Cast<APawn>(GetWorld()->GetFirstPlayerController()->GetPawn()); APawn* PlayerPawn = Cast<APawn>(GetWorld()->GetFirstPlayerController()->GetPawn());
PlayerPawn->bUseControllerRotationYaw = true; PlayerPawn->bUseControllerRotationYaw = true;
PlayerPawn->bUseControllerRotationPitch = true; PlayerPawn->bUseControllerRotationPitch = true;
@ -153,7 +161,7 @@ void ATurnBaseCombatV2::ExecuteCast(FString Combo)
break; break;
} }
//End Combat if either the player or enemy is dead //End Combat if either the player or enemy is dead
if (EnemyHealth <= 0) if (*EnemyHealth <= 0)
{ {
EndCombat(); EndCombat();
EnemyActor->Destroy(); EnemyActor->Destroy();
@ -195,7 +203,7 @@ void ATurnBaseCombatV2::DamagePlayer(int Damage)
void ATurnBaseCombatV2::DamageEnemy(int Damage) void ATurnBaseCombatV2::DamageEnemy(int Damage)
{ {
EnemyHealth -= FMath::Clamp(Damage, 0, 100); *EnemyHealth -= FMath::Clamp(Damage, 0, 100);
UpdateProgressBars(); UpdateProgressBars();
AddBattleLogMessage("Enemy was damaged for " + FString::FromInt(Damage) + " damage."); AddBattleLogMessage("Enemy was damaged for " + FString::FromInt(Damage) + " damage.");
} }
@ -203,7 +211,7 @@ void ATurnBaseCombatV2::DamageEnemy(int Damage)
void ATurnBaseCombatV2::UpdateProgressBars() const void ATurnBaseCombatV2::UpdateProgressBars() const
{ {
PlayerHealthBar->SetPercent(PlayerHealth / 100.0f); PlayerHealthBar->SetPercent(PlayerHealth / 100.0f);
EnemyHealthBar->SetPercent(EnemyHealth / 100.0f); EnemyHealthBar->SetPercent(*EnemyHealth / 100.0f);
} }
bool ATurnBaseCombatV2::IsValidCombo(FString Combo) const bool ATurnBaseCombatV2::IsValidCombo(FString Combo) const
@ -220,7 +228,7 @@ void ATurnBaseCombatV2::SwitchTurn()
//wait for 2 seconds //wait for 2 seconds
FTimerHandle UnusedHandle; FTimerHandle UnusedHandle;
GetWorldTimerManager().SetTimer(UnusedHandle, this, &ATurnBaseCombatV2::EnemyTurn, 2.0f, false); GetWorldTimerManager().SetTimer(UnusedHandle, this, &ATurnBaseCombatV2::EnemyTurn, 2.0f, false);
//activeActor = bIsPlayerTurn ? enemyActor : playerActor; //activeActor = bIsPlayerTurn ? enemyActor : playerActor;
} }
@ -338,4 +346,4 @@ void ATurnBaseCombatV2::EnemyTurn()
DamagePlayer(10); DamagePlayer(10);
TurnIndicatorTextBlock->SetText(FText::FromString("Player Turn")); TurnIndicatorTextBlock->SetText(FText::FromString("Player Turn"));
ToggleButtons(); ToggleButtons();
} }

View File

@ -21,11 +21,10 @@ class THE_TWILIGHT_ABYSS_API ATurnBaseCombatV2 : public AGameStateBase
public: public:
ATurnBaseCombatV2(); ATurnBaseCombatV2();
int* EnemyHealth = nullptr;
UPROPERTY(EditDefaultsOnly) UPROPERTY(EditDefaultsOnly)
int PlayerHealth = 100; int PlayerHealth = 100;
UPROPERTY(EditDefaultsOnly) UPROPERTY(EditDefaultsOnly)
int EnemyHealth = 100;
UPROPERTY(EditDefaultsOnly)
int DefaultActionPoints = 3; int DefaultActionPoints = 3;
UPROPERTY(EditDefaultsOnly) UPROPERTY(EditDefaultsOnly)
int ActiveActionPoints = 0; int ActiveActionPoints = 0;