Updated RealTimeCombat to Map Functions to Keys

This commit is contained in:
Philip W 2022-11-17 04:16:44 +00:00
parent 09ec1cb181
commit d73a0fbd83
4 changed files with 88 additions and 141 deletions

View File

@ -11,11 +11,10 @@
ARealTimeCombat::ARealTimeCombat()
{
if (HUDWidget == nullptr)
{
// Load the HUD widget from the specified path
static ConstructorHelpers::FClassFinder<UUserWidget> HUDWidgetClass(TEXT("/Game/Blueprints/Combat_UI/Combat_UI"));
static ConstructorHelpers::FClassFinder<UUserWidget> 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<UUserWidget>(GetWorld(), HUDWidget);
TurnIndicatorTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("TurnIndicator"));
CurrentComboTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("CurrentCombo"));
ActionPointsTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("ActionPoints"));
BattleLogTextBlock = Cast<UTextBlock>(HUD->GetWidgetFromName("BattleLog"));
@ -57,14 +98,7 @@ void ARealTimeCombat::BeginPlay()
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, &ARealTimeCombat::CastButtonOnClick);
FButton->OnClicked.AddDynamic(this, &ARealTimeCombat::FButtonOnClick);
WButton->OnClicked.AddDynamic(this, &ARealTimeCombat::WButtonOnClick);
BackspaceButton->OnClicked.AddDynamic(this, &ARealTimeCombat::BackspaceButtonOnClick);
ActionPointsBar = Cast<UProgressBar>(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();
}

View File

@ -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();
};

View File

@ -73,6 +73,8 @@ protected:
private:
bool IsValidCombo(FString Combo) const;
UPROPERTY()
UUserWidget* HUD;
UPROPERTY(VisibleAnywhere)

View File

@ -70,6 +70,8 @@ protected:
private:
bool IsValidCombo(FString Combo) const;
UPROPERTY()
UUserWidget* HUD;
UPROPERTY(VisibleAnywhere)