Updated TurnBaseCombat for Button Functionality

This commit is contained in:
Philip W 2022-11-14 03:05:25 +00:00
parent 3b627518ca
commit 329cf183b9
3 changed files with 132 additions and 14 deletions

View File

@ -94,4 +94,7 @@ GlobalDefaultServerGameMode=None
+PropertyRedirects=(OldName="/Script/the_twilight_abyss.TurnBaseCombat.playerHealth",NewName="/Script/the_twilight_abyss.TurnBaseCombat.PlayerHealth")
+PropertyRedirects=(OldName="/Script/the_twilight_abyss.TurnBaseCombat.enemyHealth",NewName="/Script/the_twilight_abyss.TurnBaseCombat.EnemyHealth")
+PropertyRedirects=(OldName="/Script/the_twilight_abyss.TurnBaseCombat.defaultActionPoints",NewName="/Script/the_twilight_abyss.TurnBaseCombat.DefaultActionPoints")
+PropertyRedirects=(OldName="/Script/the_twilight_abyss.TurnBaseCombat.TestButton",NewName="/Script/the_twilight_abyss.TurnBaseCombat.CastButton")
+PropertyRedirects=(OldName="/Script/the_twilight_abyss.TurnBaseCombat.CurrentCombo",NewName="/Script/the_twilight_abyss.TurnBaseCombat.CurrentComboTextBlock")
+PropertyRedirects=(OldName="/Script/the_twilight_abyss.TurnBaseCombat.TurnIndicator",NewName="/Script/the_twilight_abyss.TurnBaseCombat.TurnIndicatorTextBlock")

View File

@ -24,14 +24,23 @@ void ATurnBaseCombat::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"));
TurnIndicatorTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("TurnIndicator"));
CurrentComboTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("CurrentCombo"));
ActionPointsTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("ActionPoints"));
PlayerHealthBar = Cast<UProgressBar>(HUD->GetWidgetFromName("PlayerHealthBar"));
EnemyHealthBar = Cast<UProgressBar>(HUD->GetWidgetFromName("EnemyHealthBar"));
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)
@ -39,9 +48,18 @@ void ATurnBaseCombat::ExecuteCast(FString Combo)
if (!IsValidCombo(Combo))
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Invalid Combo"));
CurrentComboString = "";
UpdateComboString(CurrentComboString);
RevertActionPoints();
UpdateActionPoints();
return;
}
CurrentComboString = "";
UpdateComboString(CurrentComboString);
RevertActionPoints();
UpdateActionPoints();
switch (bIsPlayerTurn)
{
case true:
@ -59,35 +77,38 @@ void ATurnBaseCombat::ExecuteCast(FString Combo)
void ATurnBaseCombat::UseActionPoint()
{
ActiveActionPoints -= 1;
ActiveActionPoints = FMath::Clamp(ActiveActionPoints, 0, DefaultActionPoints);
ActiveActionPoints += 1;
UpdateActionPoints();
}
void ATurnBaseCombat::ReuseActionPoint()
{
ActiveActionPoints += 1;
ActiveActionPoints = FMath::Clamp(ActiveActionPoints, 0, DefaultActionPoints);
ActiveActionPoints -= 1;
UpdateActionPoints();
}
void ATurnBaseCombat::RevertActionPoints()
{
ActiveActionPoints = DefaultActionPoints;
ActiveActionPoints = 0;
UpdateActionPoints();
}
void ATurnBaseCombat::DamagePlayer(int Damage)
{
PlayerHealth -= FMath::Clamp(Damage, 0, 100);
UpdateProgressBars();
}
void ATurnBaseCombat::DamageEnemy(int Damage)
{
EnemyHealth -= FMath::Clamp(Damage, 0, 100);
UpdateProgressBars();
}
void ATurnBaseCombat::UpdateProgressBars() const
{
PlayerHealthBar->SetPercent(PlayerHealth / 100);
EnemyHealthBar->SetPercent(EnemyHealth / 100);
PlayerHealthBar->SetPercent(PlayerHealth / 100.0f);
EnemyHealthBar->SetPercent(EnemyHealth / 100.0f);
}
bool ATurnBaseCombat::IsValidCombo(FString Combo) const
@ -97,5 +118,57 @@ bool ATurnBaseCombat::IsValidCombo(FString Combo) const
void ATurnBaseCombat::SwitchTurn()
{
activeActor = bIsPlayerTurn ? enemyActor : playerActor;
TurnIndicatorTextBlock->SetText(FText::FromString(bIsPlayerTurn ? "Enemy Turn" : "Player Turn"));
//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)));
}

View File

@ -3,6 +3,7 @@
#pragma once
#include "CoreMinimal.h"
#include "Components/Button.h"
#include "Components/ProgressBar.h"
#include "Components/TextBlock.h"
#include "GameFramework/GameStateBase.h"
@ -25,6 +26,7 @@ public:
int EnemyHealth = 100;
UPROPERTY(EditDefaultsOnly)
int DefaultActionPoints = 3;
UPROPERTY(EditDefaultsOnly)
int ActiveActionPoints = 0;
AActor* playerActor;
@ -33,7 +35,6 @@ public:
/*
TODO:
Reference Action Library
Reference Player Inventory
Reference Combat Logging System
*/
@ -59,13 +60,54 @@ protected:
private:
bool IsValidCombo(FString Combo) const;
UPROPERTY(VisibleAnywhere)
bool bIsPlayerTurn = true;
void SwitchTurn();
UPROPERTY(VisibleAnywhere)
UTextBlock* TurnIndicator;
FString CurrentComboString = "";
void SwitchTurn();
UPROPERTY(VisibleAnywhere)
UTextBlock* TurnIndicatorTextBlock;
UPROPERTY(VisibleAnywhere)
UTextBlock* CurrentComboTextBlock;
UPROPERTY(VisibleAnywhere)
UTextBlock* ActionPointsTextBlock;
UPROPERTY(VisibleAnywhere)
UProgressBar* PlayerHealthBar;
UPROPERTY(VisibleAnywhere)
UProgressBar* EnemyHealthBar;
UPROPERTY(VisibleAnywhere)
UButton* CastButton;
UPROPERTY(VisibleAnywhere)
UButton* FButton;
UPROPERTY(VisibleAnywhere)
UButton* WButton;
UPROPERTY(VisibleAnywhere)
UButton* BackspaceButton;
UFUNCTION()
void CastButtonOnClick();
UFUNCTION()
void FButtonOnClick();
UFUNCTION()
void WButtonOnClick();
UFUNCTION()
void BackspaceButtonOnClick();
void UpdateComboString(FString NewCombo);
void UpdateActionPoints();
};