From c9a3013bb96f87d53fcd75bcc97f30d6373e53a7 Mon Sep 17 00:00:00 2001 From: Arnaud Jamin Date: Tue, 10 Oct 2023 01:12:59 -0400 Subject: [PATCH] save cheats state --- .../Private/CogAbilityWindow_Cheats.cpp | 67 ++++++++++++++++--- .../CogAbility/Public/CogAbilityReplicator.h | 5 +- .../Public/CogAbilityWindow_Cheats.h | 10 ++- Source/CogSample/CogSampleGameState.cpp | 3 +- 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/Plugins/CogAbility/Source/CogAbility/Private/CogAbilityWindow_Cheats.cpp b/Plugins/CogAbility/Source/CogAbility/Private/CogAbilityWindow_Cheats.cpp index 7ff400c..9718841 100644 --- a/Plugins/CogAbility/Source/CogAbility/Private/CogAbilityWindow_Cheats.cpp +++ b/Plugins/CogAbility/Source/CogAbility/Private/CogAbilityWindow_Cheats.cpp @@ -1,6 +1,8 @@ #include "CogAbilityWindow_Cheats.h" +#include "AbilitySystemGlobals.h" #include "CogAbilityDataAsset_Cheats.h" +#include "CogAbilityReplicator.h" #include "CogInterfaceAllegianceActor.h" #include "CogDebugDraw.h" #include "CogImguiHelper.h" @@ -23,6 +25,41 @@ void UCogAbilityWindow_Cheats::RenderHelp() ); } +//-------------------------------------------------------------------------------------------------------------------------- +void UCogAbilityWindow_Cheats::SetCheatsAsset(const UCogAbilityDataAsset_Cheats* Value) +{ + CheatsAsset = Value; + + if (CheatsAsset == nullptr) + { + return; + } + + APawn* LocalPawn = GetLocalPlayerPawn(); + if (LocalPawn == nullptr) + { + return; + } + + FCogAbilityModule& Module = FCogAbilityModule::Get(); + ACogAbilityReplicator* Replicator = Module.GetLocalReplicator(); + if (Replicator == nullptr) + { + return; + } + + TArray Targets{ LocalPawn }; + + for (const FString& AppliedCheatName : AppliedCheats) + { + if (const FCogAbilityCheat* Cheat = CheatsAsset->PersistentEffects.FindByPredicate( + [AppliedCheatName](const FCogAbilityCheat& Cheat) { return Cheat.Name == AppliedCheatName; })) + { + Replicator->ApplyCheat(LocalPawn, Targets, *Cheat); + } + } +} + //-------------------------------------------------------------------------------------------------------------------------- void UCogAbilityWindow_Cheats::RenderContent() { @@ -39,7 +76,12 @@ void UCogAbilityWindow_Cheats::RenderContent() return; } - APawn* LocalPawn = GetLocalPlayerPawn(); + APawn* ControlledActor = GetLocalPlayerPawn(); + + if (SelectedActor == ControlledActor) + { + AppliedCheats.Empty(); + } if (ImGui::BeginTable("Cheats", 2, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_NoBordersInBodyUntilResize)) { @@ -53,7 +95,7 @@ void UCogAbilityWindow_Cheats::RenderContent() for (const FCogAbilityCheat& CheatEffect : CheatsAsset->PersistentEffects) { ImGui::PushID(Index); - AddCheat(LocalPawn, SelectedActor, CheatEffect, true); + AddCheat(ControlledActor, SelectedActor, CheatEffect, true); ImGui::PopID(); Index++; } @@ -63,7 +105,7 @@ void UCogAbilityWindow_Cheats::RenderContent() for (const FCogAbilityCheat& CheatEffect : CheatsAsset->InstantEffects) { ImGui::PushID(Index); - AddCheat(LocalPawn, SelectedActor, CheatEffect, false); + AddCheat(ControlledActor, SelectedActor, CheatEffect, false); ImGui::PopID(); Index++; } @@ -73,7 +115,7 @@ void UCogAbilityWindow_Cheats::RenderContent() } //-------------------------------------------------------------------------------------------------------------------------- -void UCogAbilityWindow_Cheats::AddCheat(AActor* CheatInstigator, AActor* SelectedActor, const FCogAbilityCheat& Cheat, bool IsPersistent) +void UCogAbilityWindow_Cheats::AddCheat(AActor* ControlledActor, AActor* SelectedActor, const FCogAbilityCheat& Cheat, bool IsPersistent) { if (Cheat.Effect == nullptr) { @@ -103,14 +145,19 @@ void UCogAbilityWindow_Cheats::AddCheat(AActor* CheatInstigator, AActor* Selecte bool isEnabled = ACogAbilityReplicator::IsCheatActive(SelectedActor, Cheat); if (ImGui::Checkbox(TCHAR_TO_ANSI(*Cheat.Name), &isEnabled)) { - RequestCheat(CheatInstigator, SelectedActor, Cheat); + RequestCheat(ControlledActor, SelectedActor, Cheat); + } + + if (isEnabled && SelectedActor == ControlledActor) + { + AppliedCheats.Add(Cheat.Name); } } else { if (ImGui::Button(TCHAR_TO_ANSI(*Cheat.Name), ImVec2(-1, 0))) { - RequestCheat(CheatInstigator, SelectedActor, Cheat); + RequestCheat(ControlledActor, SelectedActor, Cheat); } } @@ -132,7 +179,7 @@ void UCogAbilityWindow_Cheats::AddCheat(AActor* CheatInstigator, AActor* Selecte } //-------------------------------------------------------------------------------------------------------------------------- -void UCogAbilityWindow_Cheats::RequestCheat(AActor* CheatInstigator, AActor* SelectedActor, const FCogAbilityCheat& Cheat) +void UCogAbilityWindow_Cheats::RequestCheat(AActor* ControlledActor, AActor* SelectedActor, const FCogAbilityCheat& Cheat) { const bool IsShiftDown = (ImGui::GetCurrentContext()->IO.KeyMods & ImGuiMod_Shift) != 0; const bool IsAltDown = (ImGui::GetCurrentContext()->IO.KeyMods & ImGuiMod_Alt) != 0; @@ -142,7 +189,7 @@ void UCogAbilityWindow_Cheats::RequestCheat(AActor* CheatInstigator, AActor* Sel if (IsControlDown) { - Actors.Add(CheatInstigator); + Actors.Add(ControlledActor); } if (IsShiftDown || IsAltDown) @@ -155,7 +202,7 @@ void UCogAbilityWindow_Cheats::RequestCheat(AActor* CheatInstigator, AActor* Sel if (ICogInterfacesAllegianceActor* AllegianceInterface = Cast(OtherActor)) { - AllegianceInterface->GetAllegianceWithOtherActor(CheatInstigator); + AllegianceInterface->GetAllegianceWithOtherActor(ControlledActor); } if ((IsShiftDown && (Allegiance == ECogInterfacesAllegiance::Enemy)) @@ -175,6 +222,6 @@ void UCogAbilityWindow_Cheats::RequestCheat(AActor* CheatInstigator, AActor* Sel FCogAbilityModule& Module = FCogAbilityModule::Get(); if (ACogAbilityReplicator* Replicator = Module.GetLocalReplicator()) { - Replicator->ApplyCheat(CheatInstigator, Actors, Cheat); + Replicator->ApplyCheat(ControlledActor, Actors, Cheat); } } diff --git a/Plugins/CogAbility/Source/CogAbility/Public/CogAbilityReplicator.h b/Plugins/CogAbility/Source/CogAbility/Public/CogAbilityReplicator.h index 2ccf037..3e44b5e 100644 --- a/Plugins/CogAbility/Source/CogAbility/Public/CogAbilityReplicator.h +++ b/Plugins/CogAbility/Source/CogAbility/Public/CogAbilityReplicator.h @@ -11,7 +11,7 @@ struct FCogAbilityCheat; struct FCogAbilityTweak; struct FGameplayTag; -UCLASS(NotBlueprintable, NotBlueprintType, notplaceable, noteditinlinenew, hidedropdown, Transient) +UCLASS(NotBlueprintable, NotBlueprintType, notplaceable, noteditinlinenew, hidedropdown, Transient, Config = Cog) class COGABILITY_API ACogAbilityReplicator : public AActor { GENERATED_UCLASS_BODY() @@ -71,4 +71,7 @@ private: UPROPERTY(Replicated) TArray TweakCurrentValues; + + UPROPERTY(Config) + TArray AppliedCheatsOnLocalPawn; }; diff --git a/Plugins/CogAbility/Source/CogAbility/Public/CogAbilityWindow_Cheats.h b/Plugins/CogAbility/Source/CogAbility/Public/CogAbilityWindow_Cheats.h index 786e85b..f7548f2 100644 --- a/Plugins/CogAbility/Source/CogAbility/Public/CogAbilityWindow_Cheats.h +++ b/Plugins/CogAbility/Source/CogAbility/Public/CogAbilityWindow_Cheats.h @@ -15,7 +15,9 @@ class COGABILITY_API UCogAbilityWindow_Cheats : public UCogWindow public: - TWeakObjectPtr CheatsAsset; + const UCogAbilityDataAsset_Cheats* GetCheatsAsset() const { return CheatsAsset.Get(); } + + void SetCheatsAsset(const UCogAbilityDataAsset_Cheats* Value); protected: @@ -25,10 +27,12 @@ protected: private: - void AddCheat(AActor* InstigatorActor, AActor* TargetActor, const FCogAbilityCheat& CheatEffect, bool IsPersistent); + void AddCheat(AActor* ControlledActor, AActor* TargetActor, const FCogAbilityCheat& CheatEffect, bool IsPersistent); - void RequestCheat(AActor* InstigatorActor, AActor* TargetActor, const FCogAbilityCheat& CheatEffect); + void RequestCheat(AActor* ControlledActor, AActor* TargetActor, const FCogAbilityCheat& CheatEffect); UPROPERTY(Config) TArray AppliedCheats; + + TWeakObjectPtr CheatsAsset; }; diff --git a/Source/CogSample/CogSampleGameState.cpp b/Source/CogSample/CogSampleGameState.cpp index 882fe9a..6250b25 100644 --- a/Source/CogSample/CogSampleGameState.cpp +++ b/Source/CogSample/CogSampleGameState.cpp @@ -188,7 +188,8 @@ void ACogSampleGameState::InitializeCog() CogWindowManager->CreateWindow("Gameplay.Attributes"); UCogAbilityWindow_Cheats* CheatsWindow = CogWindowManager->CreateWindow("Gameplay.Cheats"); - CheatsWindow->CheatsAsset = GetFirstAssetByClass(); + CheatsWindow->SetCheatsAsset(GetFirstAssetByClass()); + UCogAbilityWindow_Effects* EffectsWindow = CogWindowManager->CreateWindow("Gameplay.Effects"); EffectsWindow->NegativeEffectTag = Tag_Effect_Alignment_Negative; EffectsWindow->PositiveEffectTag = Tag_Effect_Alignment_Positive;