2022-11-09 20:32:31 +00:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
#include "TurnBaseCombat.h"
|
2022-11-14 00:04:44 +00:00
|
|
|
#include "CoreMinimal.h"
|
2022-11-13 21:32:35 +00:00
|
|
|
#include "Blueprint/UserWidget.h"
|
2022-11-14 00:04:44 +00:00
|
|
|
#include "Components/TextBlock.h"
|
|
|
|
#include "Components/ProgressBar.h"
|
2022-11-09 21:24:36 +00:00
|
|
|
#include "Kismet/GameplayStatics.h"
|
2022-11-09 20:32:31 +00:00
|
|
|
|
2022-11-13 21:32:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 20:32:31 +00:00
|
|
|
void ATurnBaseCombat::BeginPlay()
|
|
|
|
{
|
|
|
|
Super::BeginPlay();
|
2022-11-13 21:32:35 +00:00
|
|
|
|
2022-11-09 21:24:36 +00:00
|
|
|
TArray<AActor*> AllCharacterActorsInScene;
|
2022-11-14 00:04:44 +00:00
|
|
|
UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), AActor::StaticClass(), FName("Character"), AllCharacterActorsInScene);
|
2022-11-13 21:32:35 +00:00
|
|
|
|
|
|
|
UUserWidget* HUD = CreateWidget<UUserWidget>(GetWorld(), HUDWidget);
|
|
|
|
HUD->AddToViewport();
|
2022-11-14 00:04:44 +00:00
|
|
|
|
2022-11-14 03:05:25 +00:00
|
|
|
TurnIndicatorTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("TurnIndicator"));
|
|
|
|
CurrentComboTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("CurrentCombo"));
|
|
|
|
ActionPointsTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("ActionPoints"));
|
2022-11-14 00:04:44 +00:00
|
|
|
PlayerHealthBar = Cast<UProgressBar>(HUD->GetWidgetFromName("PlayerHealthBar"));
|
|
|
|
EnemyHealthBar = Cast<UProgressBar>(HUD->GetWidgetFromName("EnemyHealthBar"));
|
2022-11-14 03:05:25 +00:00
|
|
|
CastButton = Cast<UButton>(HUD->GetWidgetFromName("CastButton"));
|
|
|
|
FButton = Cast<UButton>(HUD->GetWidgetFromName("FButton"));
|
|
|
|
WButton = Cast<UButton>(HUD->GetWidgetFromName("WButton"));
|
|
|
|
BackspaceButton = Cast<UButton>(HUD->GetWidgetFromName("BackspaceButton"));
|
|
|
|
CastButton->OnClicked.AddDynamic(this, &ATurnBaseCombat::CastButtonOnClick);
|
|
|
|
FButton->OnClicked.AddDynamic(this, &ATurnBaseCombat::FButtonOnClick);
|
|
|
|
WButton->OnClicked.AddDynamic(this, &ATurnBaseCombat::WButtonOnClick);
|
|
|
|
BackspaceButton->OnClicked.AddDynamic(this, &ATurnBaseCombat::BackspaceButtonOnClick);
|
2022-11-14 00:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::ExecuteCast(FString Combo)
|
|
|
|
{
|
|
|
|
if (!IsValidCombo(Combo))
|
|
|
|
{
|
|
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Invalid Combo"));
|
2022-11-14 03:05:25 +00:00
|
|
|
CurrentComboString = "";
|
|
|
|
UpdateComboString(CurrentComboString);
|
|
|
|
RevertActionPoints();
|
|
|
|
UpdateActionPoints();
|
2022-11-14 00:04:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-14 03:05:25 +00:00
|
|
|
CurrentComboString = "";
|
|
|
|
UpdateComboString(CurrentComboString);
|
|
|
|
RevertActionPoints();
|
|
|
|
UpdateActionPoints();
|
|
|
|
|
2022-11-14 00:04:44 +00:00
|
|
|
switch (bIsPlayerTurn)
|
|
|
|
{
|
|
|
|
case true:
|
|
|
|
// Player Turn
|
2022-11-14 01:11:37 +00:00
|
|
|
DamageEnemy(*ValidCombos.Find(Combo));
|
|
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Enemy Damaged %d"), *ValidCombos.Find(Combo)));
|
2022-11-14 00:04:44 +00:00
|
|
|
break;
|
|
|
|
case false:
|
|
|
|
// Enemy Turn
|
2022-11-14 01:11:37 +00:00
|
|
|
DamagePlayer(*ValidCombos.Find(Combo));
|
|
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Player Damaged %d"), *ValidCombos.Find(Combo)));
|
2022-11-14 00:04:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-14 03:15:26 +00:00
|
|
|
SwitchTurn();
|
2022-11-09 20:32:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 21:32:35 +00:00
|
|
|
void ATurnBaseCombat::UseActionPoint()
|
2022-11-09 20:32:31 +00:00
|
|
|
{
|
2022-11-14 03:05:25 +00:00
|
|
|
ActiveActionPoints += 1;
|
|
|
|
UpdateActionPoints();
|
2022-11-09 21:03:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 21:32:35 +00:00
|
|
|
void ATurnBaseCombat::ReuseActionPoint()
|
2022-11-09 21:03:47 +00:00
|
|
|
{
|
2022-11-14 03:05:25 +00:00
|
|
|
ActiveActionPoints -= 1;
|
|
|
|
UpdateActionPoints();
|
2022-11-09 20:32:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 21:32:35 +00:00
|
|
|
void ATurnBaseCombat::RevertActionPoints()
|
2022-11-09 20:32:31 +00:00
|
|
|
{
|
2022-11-14 03:05:25 +00:00
|
|
|
ActiveActionPoints = 0;
|
|
|
|
UpdateActionPoints();
|
2022-11-09 20:32:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 00:04:44 +00:00
|
|
|
void ATurnBaseCombat::DamagePlayer(int Damage)
|
|
|
|
{
|
|
|
|
PlayerHealth -= FMath::Clamp(Damage, 0, 100);
|
2022-11-14 03:05:25 +00:00
|
|
|
UpdateProgressBars();
|
2022-11-14 00:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::DamageEnemy(int Damage)
|
|
|
|
{
|
|
|
|
EnemyHealth -= FMath::Clamp(Damage, 0, 100);
|
2022-11-14 03:05:25 +00:00
|
|
|
UpdateProgressBars();
|
2022-11-14 00:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::UpdateProgressBars() const
|
|
|
|
{
|
2022-11-14 03:05:25 +00:00
|
|
|
PlayerHealthBar->SetPercent(PlayerHealth / 100.0f);
|
|
|
|
EnemyHealthBar->SetPercent(EnemyHealth / 100.0f);
|
2022-11-14 00:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ATurnBaseCombat::IsValidCombo(FString Combo) const
|
|
|
|
{
|
|
|
|
return ValidCombos.Contains(Combo);
|
|
|
|
}
|
|
|
|
|
2022-11-13 21:32:35 +00:00
|
|
|
void ATurnBaseCombat::SwitchTurn()
|
2022-11-09 21:03:47 +00:00
|
|
|
{
|
2022-11-14 03:15:26 +00:00
|
|
|
//TurnIndicatorTextBlock->SetText(FText::FromString(bIsPlayerTurn ? "Enemy Turn" : "Player Turn"));
|
|
|
|
//bIsPlayerTurn = !bIsPlayerTurn;
|
|
|
|
TurnIndicatorTextBlock->SetText(FText::FromString("Enemy Turn"));
|
|
|
|
DamagePlayer(10);
|
|
|
|
TurnIndicatorTextBlock->SetText(FText::FromString("Player Turn"));
|
2022-11-14 03:05:25 +00:00
|
|
|
//activeActor = bIsPlayerTurn ? enemyActor : playerActor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::CastButtonOnClick()
|
|
|
|
{
|
|
|
|
ExecuteCast(CurrentComboString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::FButtonOnClick()
|
|
|
|
{
|
|
|
|
if (ActiveActionPoints >= DefaultActionPoints)
|
|
|
|
{
|
|
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("No More Action Points"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UseActionPoint();
|
|
|
|
CurrentComboString.AppendChar('F');
|
|
|
|
UpdateComboString(CurrentComboString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::WButtonOnClick()
|
|
|
|
{
|
|
|
|
if (ActiveActionPoints >= DefaultActionPoints)
|
|
|
|
{
|
|
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("No More Action Points"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UseActionPoint();
|
|
|
|
CurrentComboString.AppendChar('W');
|
|
|
|
UpdateComboString(CurrentComboString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::BackspaceButtonOnClick()
|
|
|
|
{
|
|
|
|
if (CurrentComboString.Len() <= 0)
|
|
|
|
{
|
|
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Blank Combo"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ReuseActionPoint();
|
|
|
|
CurrentComboString.RemoveAt(CurrentComboString.Len() - 1);
|
|
|
|
UpdateComboString(CurrentComboString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::UpdateComboString(FString NewCombo)
|
|
|
|
{
|
|
|
|
CurrentComboTextBlock->SetText(FText::FromString(NewCombo));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ATurnBaseCombat::UpdateActionPoints()
|
|
|
|
{
|
|
|
|
ActionPointsTextBlock->SetText(FText::FromString(FString::FromInt(ActiveActionPoints)));
|
2022-11-09 21:03:47 +00:00
|
|
|
}
|