AzureAbyss/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp

280 lines
7.3 KiB
C++
Raw Normal View History

2022-11-09 20:32:31 +00:00
// 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"
2022-11-09 20:32:31 +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;
}
}
void ATurnBaseCombat::StartCombat(AActor* Enemy)
2022-11-09 20:32:31 +00:00
{
if (HUD->IsInViewport()) return;
HUD->AddToViewport();
EnemyActor = Enemy;
if (APlayerController* PC = Cast<APlayerController>(GetWorld()->GetFirstPlayerController()))
2022-11-14 03:18:50 +00:00
{
2022-11-14 22:30:31 +00:00
PC->bShowMouseCursor = true;
PC->bEnableClickEvents = true;
2022-11-14 03:18:50 +00:00
PC->bEnableMouseOverEvents = true;
}
}
void ATurnBaseCombat::EndCombat()
{
//Remove the HUD from the viewport
HUD->RemoveFromViewport();
}
void ATurnBaseCombat::BeginPlay()
{
Super::BeginPlay();
TArray<AActor*> AllCharacterActorsInScene;
UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), AActor::StaticClass(), FName("Character"), AllCharacterActorsInScene);
HUD = CreateWidget<UUserWidget>(GetWorld(), HUDWidget);
2022-11-14 03:18:50 +00:00
TurnIndicatorTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("TurnIndicator"));
CurrentComboTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("CurrentCombo"));
ActionPointsTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("ActionPoints"));
2022-11-14 22:30:31 +00:00
BattleLogTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("BattleLog"));
PlayerHealthBar = Cast<UProgressBar>(HUD->GetWidgetFromName("PlayerHealthBar"));
EnemyHealthBar = Cast<UProgressBar>(HUD->GetWidgetFromName("EnemyHealthBar"));
IronResourceBar = Cast<UProgressBar>(HUD->GetWidgetFromName("IronResourceBar"));
SulfurResourceBar = Cast<UProgressBar>(HUD->GetWidgetFromName("SulfurResourceBar"));
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);
}
void ATurnBaseCombat::ExecuteCast(FString Combo)
{
if (!IsValidCombo(Combo))
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Invalid Combo"));
//For each character in the current combo add back the resource
for (int i = 0; i < Combo.Len(); i++)
{
if (Combo[i] == 'F')
{
IronResource += 1;
}
else if (Combo[i] == 'W')
{
SulfurResource += 1;
}
}
CurrentComboString = "";
UpdateComboString(CurrentComboString);
RevertActionPoints();
UpdateActionPoints();
UpdateResourceBars();
return;
}
CurrentComboString = "";
UpdateComboString(CurrentComboString);
RevertActionPoints();
UpdateActionPoints();
switch (bIsPlayerTurn)
{
case true:
// Player Turn
DamageEnemy(*ValidCombos.Find(Combo));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Enemy Damaged %d"), *ValidCombos.Find(Combo)));
break;
case false:
// Enemy Turn
DamagePlayer(*ValidCombos.Find(Combo));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Player Damaged %d"), *ValidCombos.Find(Combo)));
break;
}
//End Combat if either the player or enemy is dead
if (EnemyHealth <= 0)
{
EndCombat();
EnemyActor->Destroy();
return;
}
if (PlayerHealth <= 0)
{
EndCombat();
return;
}
SwitchTurn();
2022-11-09 20:32:31 +00:00
}
void ATurnBaseCombat::UseActionPoint()
2022-11-09 20:32:31 +00:00
{
ActiveActionPoints += 1;
UpdateActionPoints();
}
void ATurnBaseCombat::ReuseActionPoint()
{
ActiveActionPoints -= 1;
UpdateActionPoints();
2022-11-09 20:32:31 +00:00
}
void ATurnBaseCombat::RevertActionPoints()
2022-11-09 20:32:31 +00:00
{
ActiveActionPoints = 0;
UpdateActionPoints();
2022-11-09 20:32:31 +00:00
}
void ATurnBaseCombat::DamagePlayer(int Damage)
{
PlayerHealth -= FMath::Clamp(Damage, 0, 100);
UpdateProgressBars();
2022-11-14 22:30:31 +00:00
AddBattleLogMessage("Player was damaged for " + FString::FromInt(Damage) + " damage.");
}
void ATurnBaseCombat::DamageEnemy(int Damage)
{
EnemyHealth -= FMath::Clamp(Damage, 0, 100);
UpdateProgressBars();
2022-11-14 22:30:31 +00:00
AddBattleLogMessage("Enemy was damaged for " + FString::FromInt(Damage) + " damage.");
}
void ATurnBaseCombat::UpdateProgressBars() const
{
PlayerHealthBar->SetPercent(PlayerHealth / 100.0f);
EnemyHealthBar->SetPercent(EnemyHealth / 100.0f);
}
bool ATurnBaseCombat::IsValidCombo(FString Combo) const
{
return ValidCombos.Contains(Combo);
}
void ATurnBaseCombat::SwitchTurn()
{
//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"));
//activeActor = bIsPlayerTurn ? enemyActor : playerActor;
}
void ATurnBaseCombat::CastButtonOnClick()
{
ExecuteCast(CurrentComboString);
}
void ATurnBaseCombat::FButtonOnClick()
{
if (ActiveActionPoints >= DefaultActionPoints)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("No More Action Points"));
return;
}
if (IronResource <= 0)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("No More Iron"));
return;
}
UseActionPoint();
CurrentComboString.AppendChar('F');
UpdateComboString(CurrentComboString);
IronResource -= 1;
UpdateResourceBars();
}
void ATurnBaseCombat::WButtonOnClick()
{
if (ActiveActionPoints >= DefaultActionPoints)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("No More Action Points"));
return;
}
if (SulfurResource <= 0)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("No More Sulfur"));
return;
}
UseActionPoint();
CurrentComboString.AppendChar('W');
UpdateComboString(CurrentComboString);
SulfurResource -= 1;
UpdateResourceBars();
}
void ATurnBaseCombat::BackspaceButtonOnClick()
{
if (CurrentComboString.Len() <= 0)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Blank Combo"));
return;
}
ReuseActionPoint();
if (CurrentComboString.Right(1) == "F")
{
IronResource += 1;
}
else if (CurrentComboString.Right(1) == "W")
{
SulfurResource += 1;
}
CurrentComboString.RemoveAt(CurrentComboString.Len() - 1);
UpdateComboString(CurrentComboString);
UpdateResourceBars();
}
void ATurnBaseCombat::UpdateComboString(FString NewCombo) const
{
CurrentComboTextBlock->SetText(FText::FromString(NewCombo));
}
void ATurnBaseCombat::UpdateActionPoints() const
{
ActionPointsTextBlock->SetText(FText::FromString(FString::FromInt(ActiveActionPoints)));
}
2022-11-14 11:31:51 +00:00
void ATurnBaseCombat::AddBattleLogMessage(FString Message)
{
2022-11-14 22:30:31 +00:00
BattleLog.Append(Message + "\n");
UpdateBattleLog();
}
void ATurnBaseCombat::ClearBattleLog()
{
BattleLog = "";
}
2022-11-14 11:31:51 +00:00
2022-11-14 22:30:31 +00:00
void ATurnBaseCombat::UpdateBattleLog()
{
TArray<FString> TempArray;
if (const int32 LineCount = BattleLog.ParseIntoArray(TempArray, TEXT("\n"), true); LineCount > 10)
2022-11-14 22:30:31 +00:00
{
ClearBattleLog();
}
BattleLogTextBlock->SetText(FText::FromString(BattleLog));
2022-11-14 11:31:51 +00:00
}
void ATurnBaseCombat::UpdateResourceBars() const
{
IronResourceBar->SetPercent(IronResource / 10.0f);
SulfurResourceBar->SetPercent(SulfurResource / 10.0f);
}