From d73a0fbd8349bc89e9469252cf20cbbb3eda9826 Mon Sep 17 00:00:00 2001 From: PHILIP White Date: Thu, 17 Nov 2022 04:16:44 +0000 Subject: [PATCH] Updated RealTimeCombat to Map Functions to Keys --- .../RealTimeCombat/RealTimeCombat.cpp | 182 +++++++----------- .../RealTimeCombat/RealTimeCombat.h | 43 ++--- .../TurnBasedCombat/TurnBaseCombat.h | 2 + .../TurnBasedCombatV2/TurnBaseCombatV2.h | 2 + 4 files changed, 88 insertions(+), 141 deletions(-) diff --git a/Source/the_twilight_abyss/RealTimeCombat/RealTimeCombat.cpp b/Source/the_twilight_abyss/RealTimeCombat/RealTimeCombat.cpp index 6719dd1..7565ad5 100644 --- a/Source/the_twilight_abyss/RealTimeCombat/RealTimeCombat.cpp +++ b/Source/the_twilight_abyss/RealTimeCombat/RealTimeCombat.cpp @@ -11,11 +11,10 @@ ARealTimeCombat::ARealTimeCombat() { - if (HUDWidget == nullptr) { // Load the HUD widget from the specified path - static ConstructorHelpers::FClassFinder HUDWidgetClass(TEXT("/Game/Blueprints/Combat_UI/Combat_UI")); + static ConstructorHelpers::FClassFinder HUDWidgetClass(TEXT("/Game/Blueprints/Combat_UI/Combat_UI_RT")); HUDWidget = HUDWidgetClass.Class; } } @@ -40,6 +39,49 @@ void ARealTimeCombat::EndCombat() HUD->RemoveFromViewport(); } +void ARealTimeCombat::OnMouseClick() +{ + ExecuteCast(CurrentComboString); +} + +void ARealTimeCombat::OnQPress() +{ + if (ActiveActionPoints >= HeldActionPoints) + { + 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 ARealTimeCombat::OnEPress() +{ + if (ActiveActionPoints >= HeldActionPoints) + { + 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 Sulfur")); + return; + } + UseActionPoint(); + CurrentComboString.AppendChar('W'); + UpdateComboString(CurrentComboString); + SulfurResource -= 1; + UpdateResourceBars(); +} + void ARealTimeCombat::BeginPlay() { Super::BeginPlay(); @@ -49,7 +91,6 @@ void ARealTimeCombat::BeginPlay() HUD = CreateWidget(GetWorld(), HUDWidget); - TurnIndicatorTextBlock = Cast(HUD->GetWidgetFromName("TurnIndicator")); CurrentComboTextBlock = Cast(HUD->GetWidgetFromName("CurrentCombo")); ActionPointsTextBlock = Cast(HUD->GetWidgetFromName("ActionPoints")); BattleLogTextBlock = Cast(HUD->GetWidgetFromName("BattleLog")); @@ -57,14 +98,7 @@ void ARealTimeCombat::BeginPlay() EnemyHealthBar = Cast(HUD->GetWidgetFromName("EnemyHealthBar")); IronResourceBar = Cast(HUD->GetWidgetFromName("IronResourceBar")); SulfurResourceBar = Cast(HUD->GetWidgetFromName("SulfurResourceBar")); - CastButton = Cast(HUD->GetWidgetFromName("CastButton")); - FButton = Cast(HUD->GetWidgetFromName("FButton")); - WButton = Cast(HUD->GetWidgetFromName("WButton")); - BackspaceButton = Cast(HUD->GetWidgetFromName("BackspaceButton")); - CastButton->OnClicked.AddDynamic(this, &ARealTimeCombat::CastButtonOnClick); - FButton->OnClicked.AddDynamic(this, &ARealTimeCombat::FButtonOnClick); - WButton->OnClicked.AddDynamic(this, &ARealTimeCombat::WButtonOnClick); - BackspaceButton->OnClicked.AddDynamic(this, &ARealTimeCombat::BackspaceButtonOnClick); + ActionPointsBar = Cast(HUD->GetWidgetFromName("ActionPointsBar")); } void ARealTimeCombat::ExecuteCast(FString Combo) @@ -123,24 +157,27 @@ void ARealTimeCombat::ExecuteCast(FString Combo) return; } - SwitchTurn(); + // SwitchTurn(); } void ARealTimeCombat::UseActionPoint() +{ + if (HeldActionPoints > 0 && ActiveActionPoints < MaxActionPoints) + { + ActiveActionPoints -= 1; + UpdateActionPoints(); + } +} + +void ARealTimeCombat::ReuseActionPoint() { ActiveActionPoints += 1; UpdateActionPoints(); } -void ARealTimeCombat::ReuseActionPoint() -{ - ActiveActionPoints -= 1; - UpdateActionPoints(); -} - void ARealTimeCombat::RevertActionPoints() { - ActiveActionPoints = 0; + ActiveActionPoints = HeldActionPoints; UpdateActionPoints(); } @@ -164,88 +201,26 @@ void ARealTimeCombat::UpdateProgressBars() const EnemyHealthBar->SetPercent(EnemyHealth / 100.0f); } +void ARealTimeCombat::Tick(float DeltaSeconds) +{ + Super::Tick(DeltaSeconds); + //Every second add 1 action point + if (ActionPointsTimer >= 1.0f && ActiveActionPoints < MaxActionPoints) + { + HeldActionPoints += 1; + ActionPointsTimer = 0.0f; + } + else + { + ActionPointsTimer += DeltaSeconds; + } +} + bool ARealTimeCombat::IsValidCombo(FString Combo) const { return ValidCombos.Contains(Combo); } -void ARealTimeCombat::SwitchTurn() -{ - //TurnIndicatorTextBlock->SetText(FText::FromString(bIsPlayerTurn ? "Enemy Turn" : "Player Turn")); - //bIsPlayerTurn = !bIsPlayerTurn; - TurnIndicatorTextBlock->SetText(FText::FromString("Enemy Turn")); - ToggleButtons(); - //wait for 2 seconds - FTimerHandle UnusedHandle; - GetWorldTimerManager().SetTimer(UnusedHandle, this, &ARealTimeCombat::EnemyTurn, 2.0f, false); - - //activeActor = bIsPlayerTurn ? enemyActor : playerActor; -} - -void ARealTimeCombat::CastButtonOnClick() -{ - ExecuteCast(CurrentComboString); -} - -void ARealTimeCombat::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 ARealTimeCombat::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 ARealTimeCombat::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 ARealTimeCombat::UpdateComboString(FString NewCombo) const { CurrentComboTextBlock->SetText(FText::FromString(NewCombo)); @@ -282,18 +257,3 @@ void ARealTimeCombat::UpdateResourceBars() const IronResourceBar->SetPercent(IronResource / 10.0f); SulfurResourceBar->SetPercent(SulfurResource / 10.0f); } - -void ARealTimeCombat::ToggleButtons() const -{ - FButton->SetIsEnabled(!FButton->bIsEnabled); - WButton->SetIsEnabled(!WButton->bIsEnabled); - BackspaceButton->SetIsEnabled(!BackspaceButton->bIsEnabled); - CastButton->SetIsEnabled(!CastButton->bIsEnabled); -} - -void ARealTimeCombat::EnemyTurn() -{ - DamagePlayer(10); - TurnIndicatorTextBlock->SetText(FText::FromString("Player Turn")); - ToggleButtons(); -} \ No newline at end of file diff --git a/Source/the_twilight_abyss/RealTimeCombat/RealTimeCombat.h b/Source/the_twilight_abyss/RealTimeCombat/RealTimeCombat.h index 7b75b4c..db47e6e 100644 --- a/Source/the_twilight_abyss/RealTimeCombat/RealTimeCombat.h +++ b/Source/the_twilight_abyss/RealTimeCombat/RealTimeCombat.h @@ -20,7 +20,7 @@ class THE_TWILIGHT_ABYSS_API ARealTimeCombat : public AActor UPROPERTY(EditDefaultsOnly) int EnemyHealth = 100; UPROPERTY(EditDefaultsOnly) - int DefaultActionPoints = 3; + int MaxActionPoints = 3; UPROPERTY(EditDefaultsOnly) int ActiveActionPoints = 0; UPROPERTY(EditDefaultsOnly) @@ -55,6 +55,12 @@ class THE_TWILIGHT_ABYSS_API ARealTimeCombat : public AActor void StartCombat(AActor* Enemy); UFUNCTION(BlueprintCallable) void EndCombat(); + UFUNCTION(BlueprintCallable) + void OnMouseClick(); + UFUNCTION(BlueprintCallable) + void OnQPress(); + UFUNCTION(BlueprintCallable) + void OnEPress(); protected: virtual void BeginPlay() override; @@ -65,9 +71,14 @@ protected: void DamagePlayer(int Damage); void DamageEnemy(int Damage); void UpdateProgressBars() const; + virtual void Tick(float DeltaSeconds) override; private: bool IsValidCombo(FString Combo) const; + float ActionPointsTimer = 0.0f; + int HeldActionPoints = 0; + + UPROPERTY() UUserWidget* HUD; UPROPERTY(VisibleAnywhere) @@ -76,11 +87,6 @@ private: UPROPERTY(VisibleAnywhere) FString CurrentComboString = ""; - void SwitchTurn(); - - UPROPERTY(VisibleAnywhere) - UTextBlock* TurnIndicatorTextBlock; - UPROPERTY(VisibleAnywhere) UTextBlock* CurrentComboTextBlock; @@ -103,28 +109,7 @@ private: UProgressBar* SulfurResourceBar; 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(); + UProgressBar* ActionPointsBar; void UpdateComboString(FString NewCombo) const; void UpdateActionPoints() const; @@ -133,6 +118,4 @@ private: void ClearBattleLog(); void UpdateBattleLog(); void UpdateResourceBars() const; - void ToggleButtons() const; - void EnemyTurn(); }; diff --git a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h index bb755c9..8b8409a 100644 --- a/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h +++ b/Source/the_twilight_abyss/TurnBasedCombat/TurnBaseCombat.h @@ -73,6 +73,8 @@ protected: private: bool IsValidCombo(FString Combo) const; + + UPROPERTY() UUserWidget* HUD; UPROPERTY(VisibleAnywhere) diff --git a/Source/the_twilight_abyss/TurnBasedCombatV2/TurnBaseCombatV2.h b/Source/the_twilight_abyss/TurnBasedCombatV2/TurnBaseCombatV2.h index b7224ae..dbdc8c1 100644 --- a/Source/the_twilight_abyss/TurnBasedCombatV2/TurnBaseCombatV2.h +++ b/Source/the_twilight_abyss/TurnBasedCombatV2/TurnBaseCombatV2.h @@ -70,6 +70,8 @@ protected: private: bool IsValidCombo(FString Combo) const; + + UPROPERTY() UUserWidget* HUD; UPROPERTY(VisibleAnywhere)