243 lines
6.5 KiB
C++
243 lines
6.5 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Menus/MainScreenUserWidget.h"
|
|
#include "Engine/DataTable.h"
|
|
#include "Core/LuckyRobotsGameInstance.h"
|
|
#include "FunctionLibraries/LuckyRobotsFunctionLibrary.h"
|
|
#include "Subsystems/SubsystemBlueprintLibrary.h"
|
|
#include "VaRestSubsystem.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
|
|
void UMainScreenUserWidget::NativeConstruct()
|
|
{
|
|
Super::NativeConstruct();
|
|
InitData();
|
|
|
|
DoSendReadyJson();
|
|
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
GameInstance->bIsFirstOpenGame = false;
|
|
GameInstance->DoResolutionChange(bIsFullScreen);
|
|
|
|
GameInstance->OnMessageDispatched.AddDynamic(this, &UMainScreenUserWidget::OnMessageDispatchedHandler);
|
|
}
|
|
}
|
|
|
|
void UMainScreenUserWidget::InitData()
|
|
{
|
|
InitRobotData();
|
|
InitLevelData();
|
|
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
CurrentQualityIndex = static_cast<int32>(GameInstance->CurrentSelectQuality);
|
|
}
|
|
}
|
|
|
|
void UMainScreenUserWidget::InitRobotData()
|
|
{
|
|
RobotDataList = ULuckyRobotsFunctionLibrary::GetActiveRobotDataList(this);
|
|
CurrentRobotIndex = 0;
|
|
UpdateSelectRobot();
|
|
}
|
|
|
|
void UMainScreenUserWidget::InitLevelData()
|
|
{
|
|
LevelDataList = ULuckyRobotsFunctionLibrary::GetActiveLevelDataList(this);
|
|
FRobotData CurrentRobotData = GetCurrentRobotData();
|
|
if (CurrentRobotData.Name != ERobotsName::None)
|
|
{
|
|
TArray<FLevelData> FilteredLevels;
|
|
for (const FLevelData& LevelData : LevelDataList)
|
|
{
|
|
if (LevelData.RobotTypeList.Contains(CurrentRobotData.RobotType))
|
|
{
|
|
FilteredLevels.Add(LevelData);
|
|
}
|
|
}
|
|
LevelDataList = FilteredLevels;
|
|
}
|
|
CurrentLevelIndex = 0;
|
|
UpdateSelectLevel();
|
|
}
|
|
|
|
FRobotData UMainScreenUserWidget::GetCurrentRobotData() const
|
|
{
|
|
if (RobotDataList.IsValidIndex(CurrentRobotIndex))
|
|
{
|
|
return RobotDataList[CurrentRobotIndex];
|
|
}
|
|
return FRobotData();
|
|
}
|
|
|
|
FLevelData UMainScreenUserWidget::GetCurrentLevelData() const
|
|
{
|
|
if (LevelDataList.IsValidIndex(CurrentLevelIndex))
|
|
{
|
|
return LevelDataList[CurrentLevelIndex];
|
|
}
|
|
return FLevelData();
|
|
}
|
|
|
|
void UMainScreenUserWidget::SelectNextRobot()
|
|
{
|
|
CurrentRobotIndex = FMath::Clamp(CurrentRobotIndex + 1, 0, RobotDataList.Num() - 1);
|
|
UpdateSelectRobot();
|
|
}
|
|
|
|
void UMainScreenUserWidget::SelectPreviousRobot()
|
|
{
|
|
CurrentRobotIndex = FMath::Clamp(CurrentRobotIndex - 1, 0, RobotDataList.Num() - 1);
|
|
UpdateSelectRobot();
|
|
}
|
|
|
|
void UMainScreenUserWidget::SelectNextLevel()
|
|
{
|
|
CurrentLevelIndex = FMath::Clamp(CurrentLevelIndex + 1, 0, LevelDataList.Num() - 1);
|
|
UpdateSelectLevel();
|
|
}
|
|
|
|
void UMainScreenUserWidget::SelectPreviousLevel()
|
|
{
|
|
CurrentLevelIndex = FMath::Clamp(CurrentLevelIndex - 1, 0, LevelDataList.Num() - 1);
|
|
UpdateSelectLevel();
|
|
}
|
|
|
|
void UMainScreenUserWidget::SelectNextQuality()
|
|
{
|
|
UEnum* QualityEnum = StaticEnum<EQualityEnum>();
|
|
int32 EnumCount = QualityEnum->NumEnums() - 1;
|
|
CurrentQualityIndex = FMath::Clamp(CurrentQualityIndex - 1, 0, EnumCount - 1);
|
|
UpdateSelectQuality();
|
|
}
|
|
|
|
void UMainScreenUserWidget::SelectPreviousQuality()
|
|
{
|
|
UEnum* QualityEnum = StaticEnum<EQualityEnum>();
|
|
int32 EnumCount = QualityEnum->NumEnums() - 1;
|
|
CurrentQualityIndex = FMath::Clamp(CurrentQualityIndex + 1, 0, EnumCount - 1);
|
|
UpdateSelectQuality();
|
|
}
|
|
|
|
void UMainScreenUserWidget::DoResolutionChange()
|
|
{
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
bIsFullScreen = !bIsFullScreen;
|
|
GameInstance->DoResolutionChange(bIsFullScreen);
|
|
}
|
|
}
|
|
|
|
void UMainScreenUserWidget::GameStart()
|
|
{
|
|
if (UKismetSystemLibrary::IsValidSoftObjectReference(CurrentSelectLevelObject))
|
|
{
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
GameInstance->bIsFirstOpenGame = true;
|
|
UGameplayStatics::OpenLevelBySoftObjectPtr(this, CurrentSelectLevelObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
void UMainScreenUserWidget::UpdateSelectRobot()
|
|
{
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
GameInstance->CurrentSelectRobot = GetCurrentRobotData().Name;
|
|
}
|
|
BPUpdateSelectRobot();
|
|
InitLevelData();
|
|
}
|
|
|
|
void UMainScreenUserWidget::UpdateSelectLevel()
|
|
{
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
GameInstance->CurrentSelectLevel = GetCurrentLevelData().LevelEnum;
|
|
}
|
|
BPUpdateSelectLevel();
|
|
}
|
|
|
|
void UMainScreenUserWidget::UpdateSelectQuality()
|
|
{
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
GameInstance->CurrentSelectQuality = static_cast<EQualityEnum>(CurrentQualityIndex);
|
|
}
|
|
BPUpdateSelectQuality();
|
|
}
|
|
|
|
void UMainScreenUserWidget::OnMessageDispatchedHandler(const FString& Message)
|
|
{
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
FParsedData ParsedData = GameInstance->DoJsonParse(Message);
|
|
if (ParsedData.CharacterName == "DRONE")
|
|
{
|
|
GameInstance->CurrentSelectRobot = ERobotsName::LuckyDrone;
|
|
}else if(ParsedData.CharacterName == "Stretch Robot V1")
|
|
{
|
|
GameInstance->CurrentSelectRobot = ERobotsName::StretchRobotV1;
|
|
}
|
|
|
|
GameInstance->bIsFirstOpenGame = true;
|
|
|
|
ELevelEnum TempLevelEnum = ELevelEnum::None;
|
|
if (ParsedData.LevelName == "LOFT")
|
|
{
|
|
TempLevelEnum = ELevelEnum::Loft;
|
|
}else if(ParsedData.LevelName == "ISTANBUL")
|
|
{
|
|
TempLevelEnum = ELevelEnum::Istanbul;
|
|
}
|
|
|
|
if (TempLevelEnum != ELevelEnum::None)
|
|
{
|
|
TArray<FLevelData> TempLevelDataList = ULuckyRobotsFunctionLibrary::GetActiveLevelDataList(this);
|
|
for (const FLevelData& LevelData : TempLevelDataList)
|
|
{
|
|
if (LevelData.LevelEnum == TempLevelEnum && LevelData.LevelObject)
|
|
{
|
|
UGameplayStatics::OpenLevelBySoftObjectPtr(this, LevelData.LevelObject);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void UMainScreenUserWidget::DoSendReadyJson()
|
|
{
|
|
auto VaRestSubsystem = CastChecked<UVaRestSubsystem>(USubsystemBlueprintLibrary::GetEngineSubsystem(UVaRestSubsystem::StaticClass()), ECastCheckedType::NullChecked);
|
|
|
|
if (!VaRestSubsystem)
|
|
{
|
|
return;
|
|
}
|
|
|
|
UVaRestJsonObject* VaRestJsonObject = VaRestSubsystem->ConstructVaRestJsonObject();
|
|
if (!VaRestJsonObject)
|
|
{
|
|
return;
|
|
}
|
|
|
|
VaRestJsonObject->SetStringField("name", "game_is_loaded");
|
|
ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
|
|
if (GameInstance)
|
|
{
|
|
FString SendedString = VaRestJsonObject->EncodeJsonToSingleString();
|
|
GameInstance->DoSendMessage(SendedString);
|
|
UE_LOG(LogTemp, Log, TEXT("Sended: %s"), *SendedString);
|
|
}
|
|
} |