GameSave for Selected Troopers

pull/8/head
m4xxx1m 2 years ago
parent 26b49e2e9d
commit 1eb65de5e6

@ -2,10 +2,48 @@
#include "ManageSquadGameState.h"
AManageSquadGameState::AManageSquadGameState() {
#include "ManageSquadTrooper.h"
#include "SelectedTrooperSaveGame.h"
#include "Kismet/GameplayStatics.h"
void AManageSquadGameState::BeginPlay() {
// USelectedTrooperSaveGame *SaveGameInstance = Cast<USelectedTrooperSaveGame>(
// UGameplayStatics::CreateSaveGameObject(
// USelectedTrooperSaveGame::StaticClass()));
if (UGameplayStatics::DoesSaveGameExist("Selected troopers", 0)) {
const USelectedTrooperSaveGame *SaveGameInstance = Cast<
USelectedTrooperSaveGame>(
UGameplayStatics::LoadGameFromSlot(TEXT("Selected troopers"), 0));
TroopersKinds = SaveGameInstance->SelectedTroopers;
} else {
TroopersKinds = {0, 0, 0, 0, 0};
}
InitializeTroopers();
}
void AManageSquadGameState::ChangeSquad(int TrooperIndex, int TrooperKind) {
TroopersKinds[TrooperIndex] = TrooperKind;
}
TArray<uint8> AManageSquadGameState::GetSquad() const {
return TroopersKinds;
}
void AManageSquadGameState::InitializeTroopers() const {
FVector Location(-1150.0f, -2850.0f, 3000.0f);
const FRotator Rotation(0.0f, 90.0f, 0.0f);
for (int index = 0; index < 5; ++index) {
constexpr float DeltaX = 500.0f;
FTransform SpawnLocationAndRotation(Rotation);
SpawnLocationAndRotation.SetLocation(Location);
if (TroopersBpAssets.Num() > TroopersKinds[index]) {
AActor *Spawned = GetWorld()->SpawnActorDeferred<AManageSquadTrooper>(
TroopersBpAssets[TroopersKinds[index]], SpawnLocationAndRotation);
Cast<AManageSquadTrooper>(Spawned)->Initialize(index);
Spawned->SetActorRelativeScale3D({1.5f, 1.5f, 1.5f});
Spawned->FinishSpawning(SpawnLocationAndRotation);
Spawned->SetActorLocation(Location);
}
Location += {DeltaX, 0.f, 0.0f};
}
}

@ -14,12 +14,21 @@ class TURNBASEDTUTORIAL_API AManageSquadGameState : public AGameState {
GENERATED_BODY()
public:
AManageSquadGameState();
virtual void BeginPlay() override;
UFUNCTION()
void ChangeSquad(int TrooperIndex, int TrooperKind);
private:
UFUNCTION()
TArray<uint8> GetSquad() const;
protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray<UClass *> TroopersBpAssets;
UFUNCTION()
void InitializeTroopers() const;
UPROPERTY()
TArray<int> TroopersKinds;
TArray<uint8> TroopersKinds;
};

@ -29,3 +29,8 @@ ETrooperType AManageSquadTrooper::GetType() const {
int AManageSquadTrooper::GetIndex() const {
return Index;
}
void AManageSquadTrooper::Initialize(int TrooperIndex) {
Index = TrooperIndex;
Type = ETrooperType::TROOPER_IN_SQUAD;
}

@ -40,4 +40,7 @@ public:
UFUNCTION()
int GetIndex() const;
UFUNCTION()
void Initialize(int TrooperIndex);
};

@ -1,6 +1,9 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "ManageSquadWidget.h"
#include "ManageSquadGameState.h"
#include "SelectedTrooperSaveGame.h"
#include "Components/Button.h"
#include "Kismet/GameplayStatics.h"
@ -12,6 +15,13 @@ void UManageSquadWidget::NativeConstruct() {
}
void UManageSquadWidget::OnBackButtonClicked() {
USelectedTrooperSaveGame *SaveGameInstance = Cast<USelectedTrooperSaveGame>(
UGameplayStatics::CreateSaveGameObject(
USelectedTrooperSaveGame::StaticClass()));
SaveGameInstance->SelectedTroopers = GetWorld()->GetGameState<
AManageSquadGameState>()->GetSquad();
UGameplayStatics::SaveGameToSlot(
SaveGameInstance,TEXT("Selected troopers"), 0);
UGameplayStatics::OpenLevel(GetWorld(), "MainMenuLevel");
RemoveFromParent();
}

@ -3,8 +3,6 @@
#include "SelectedTrooperSaveGame.h"
USelectedTrooperSaveGame::USelectedTrooperSaveGame(): Super()
{
USelectedTrooperSaveGame::USelectedTrooperSaveGame()
: Super() {
}

@ -10,8 +10,7 @@
*
*/
UCLASS()
class TURNBASEDTUTORIAL_API USelectedTrooperSaveGame : public USaveGame
{
class TURNBASEDTUTORIAL_API USelectedTrooperSaveGame : public USaveGame {
GENERATED_BODY()
public:

Loading…
Cancel
Save