From 329cf183b9125069e46d450ab82c05f3d19d38ad Mon Sep 17 00:00:00 2001 From: PHILIP White Date: Mon, 14 Nov 2022 03:05:25 +0000 Subject: [PATCH] Updated TurnBaseCombat for Button Functionality --- Config/DefaultEngine.ini | 3 + .../TurnBasedCombat/TurnBaseCombat.cpp | 93 +++++++++++++++++-- .../TurnBasedCombat/TurnBaseCombat.h | 50 +++++++++- 3 files changed, 132 insertions(+), 14 deletions(-) diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index a4873a5..a4ad8a7 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -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") diff --git a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp index 2d5d103..a01235b 100644 --- a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp +++ b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.cpp @@ -24,14 +24,23 @@ void ATurnBaseCombat::BeginPlay() TArray AllCharacterActorsInScene; UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), AActor::StaticClass(), FName("Character"), AllCharacterActorsInScene); - ActiveActionPoints = DefaultActionPoints; UUserWidget* HUD = CreateWidget(GetWorld(), HUDWidget); HUD->AddToViewport(); - TurnIndicator = Cast(HUD->GetWidgetFromName("TurnIndicator")); + TurnIndicatorTextBlock = Cast(HUD->GetWidgetFromName("TurnIndicator")); + CurrentComboTextBlock = Cast(HUD->GetWidgetFromName("CurrentCombo")); + ActionPointsTextBlock = Cast(HUD->GetWidgetFromName("ActionPoints")); PlayerHealthBar = Cast(HUD->GetWidgetFromName("PlayerHealthBar")); EnemyHealthBar = Cast(HUD->GetWidgetFromName("EnemyHealthBar")); + CastButton = Cast(HUD->GetWidgetFromName("CastButton")); + FButton = Cast(HUD->GetWidgetFromName("FButton")); + WButton = Cast(HUD->GetWidgetFromName("WButton")); + BackspaceButton = Cast(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))); } diff --git a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h index 45e7311..5c6fa1c 100644 --- a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h +++ b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h @@ -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(); +}; \ No newline at end of file