AzureAbyss/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp

102 lines
2.7 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "TurnBaseCombat.h"
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/TextBlock.h"
#include "Components/ProgressBar.h"
#include "Kismet/GameplayStatics.h"
ATurnBaseCombat::ATurnBaseCombat()
{
if (HUDWidget == nullptr)
{
// Load the HUD widget from the specified path
static ConstructorHelpers::FClassFinder<UUserWidget> HUDWidgetClass(TEXT("/Game/Blueprints/Combat_UI/Combat_UI"));
HUDWidget = HUDWidgetClass.Class;
}
}
void ATurnBaseCombat::BeginPlay()
{
Super::BeginPlay();
TArray<AActor*> 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.Find(Combo));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Enemy Damaged %d"), *ValidCombos.Find(Combo)));
break;
case false:
// Enemy Turn
DamagePlayer(*ValidCombos.Find(Combo));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Player Damaged %d"), *ValidCombos.Find(Combo)));
break;
}
}
void ATurnBaseCombat::UseActionPoint()
{
ActiveActionPoints -= 1;
ActiveActionPoints = FMath::Clamp(ActiveActionPoints, 0, DefaultActionPoints);
}
void ATurnBaseCombat::ReuseActionPoint()
{
ActiveActionPoints += 1;
ActiveActionPoints = FMath::Clamp(ActiveActionPoints, 0, DefaultActionPoints);
}
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;
}