Optimize code to unreal style

This commit is contained in:
martinluckyrobots
2025-04-07 11:32:45 +08:00
parent 5d8e225db5
commit 4d4026d9ad
19 changed files with 306 additions and 315 deletions

View File

@ -9,7 +9,6 @@
void UMainScreenUserWidget::NativeConstruct()
{
Super::NativeConstruct();
InitData();
}
@ -18,132 +17,121 @@ void UMainScreenUserWidget::InitData()
InitRobotData();
InitLevelData();
ULuckyRobotsGameInstance* LuckyRobotsGameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (LuckyRobotsGameInstance)
if (ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance()))
{
iCurrentSelectQuality = int(LuckyRobotsGameInstance->CurrentSelectQuality);
CurrentQualityIndex = static_cast<int32>(GameInstance->CurrentSelectQuality);
}
}
void UMainScreenUserWidget::InitRobotData()
{
RobotDataList = ULuckyRobotsFunctionLibrary::GetActiveRobotDataList(this);
iCurrentSelectRobot = 0;
CurrentRobotIndex = 0;
UpdateSelectRobot();
}
void UMainScreenUserWidget::InitLevelData()
{
LevelDataList = ULuckyRobotsFunctionLibrary::GetActiveLevelDataList(this);
FRobotData CurrentRobotData = GetCurrentRobotData();
if (CurrentRobotData.Name != ERobotsName::None)
{
TArray<FLevelData> ActiveLevelDataList = LevelDataList;
LevelDataList.Empty();
for (auto ActiveLevelData : ActiveLevelDataList)
TArray<FLevelData> FilteredLevels;
for (const FLevelData& LevelData : LevelDataList)
{
if (ActiveLevelData.RobotTypeList.Find(CurrentRobotData.RobotType) >= 0)
if (LevelData.RobotTypeList.Contains(CurrentRobotData.RobotType))
{
LevelDataList.Add(ActiveLevelData);
FilteredLevels.Add(LevelData);
}
}
LevelDataList = FilteredLevels;
}
iCurrentSelectLevel = 0;
CurrentLevelIndex = 0;
UpdateSelectLevel();
}
FRobotData UMainScreenUserWidget::GetCurrentRobotData()
FRobotData UMainScreenUserWidget::GetCurrentRobotData() const
{
FRobotData CurrentRobotData;
if (RobotDataList.IsValidIndex(iCurrentSelectRobot))
if (RobotDataList.IsValidIndex(CurrentRobotIndex))
{
CurrentRobotData = RobotDataList[iCurrentSelectRobot];
return RobotDataList[CurrentRobotIndex];
}
return CurrentRobotData;
return FRobotData();
}
FLevelData UMainScreenUserWidget::GetCurrentLevelData()
FLevelData UMainScreenUserWidget::GetCurrentLevelData() const
{
FLevelData CurrentLevelData;
if (LevelDataList.IsValidIndex(iCurrentSelectLevel))
if (LevelDataList.IsValidIndex(CurrentLevelIndex))
{
CurrentLevelData = LevelDataList[iCurrentSelectLevel];
return LevelDataList[CurrentLevelIndex];
}
return CurrentLevelData;
return FLevelData();
}
void UMainScreenUserWidget::SelectNextRobot()
{
iCurrentSelectRobot = FMath::Clamp(iCurrentSelectRobot + 1, 0, RobotDataList.Num() - 1);
CurrentRobotIndex = FMath::Clamp(CurrentRobotIndex + 1, 0, RobotDataList.Num() - 1);
UpdateSelectRobot();
}
void UMainScreenUserWidget::SelectPreviousRobot()
{
iCurrentSelectRobot = FMath::Clamp(iCurrentSelectRobot - 1, 0, RobotDataList.Num() - 1);
CurrentRobotIndex = FMath::Clamp(CurrentRobotIndex - 1, 0, RobotDataList.Num() - 1);
UpdateSelectRobot();
}
void UMainScreenUserWidget::SelectNextLevel()
{
iCurrentSelectLevel = FMath::Clamp(iCurrentSelectLevel + 1, 0, LevelDataList.Num() - 1);
CurrentLevelIndex = FMath::Clamp(CurrentLevelIndex + 1, 0, LevelDataList.Num() - 1);
UpdateSelectLevel();
}
void UMainScreenUserWidget::SelectPreviousLevel()
{
iCurrentSelectLevel = FMath::Clamp(iCurrentSelectLevel - 1, 0, LevelDataList.Num() - 1);
CurrentLevelIndex = FMath::Clamp(CurrentLevelIndex - 1, 0, LevelDataList.Num() - 1);
UpdateSelectLevel();
}
void UMainScreenUserWidget::SelectNextQuality()
{
UEnum* QualityEnum = StaticEnum<EQualityEnum>();
int QualityEnumNum = QualityEnum->NumEnums() - 1;
iCurrentSelectQuality = FMath::Clamp(iCurrentSelectQuality - 1, 0, QualityEnumNum - 1);
int32 EnumCount = QualityEnum->NumEnums() - 1;
CurrentQualityIndex = FMath::Clamp(CurrentQualityIndex - 1, 0, EnumCount - 1);
UpdateSelectQuality();
}
void UMainScreenUserWidget::SelectPreviousQuality()
{
UEnum* QualityEnum = StaticEnum<EQualityEnum>();
int QualityEnumNum = QualityEnum->NumEnums() - 1;
iCurrentSelectQuality = FMath::Clamp(iCurrentSelectQuality + 1, 0, QualityEnumNum - 1);
int32 EnumCount = QualityEnum->NumEnums() - 1;
CurrentQualityIndex = FMath::Clamp(CurrentQualityIndex + 1, 0, EnumCount - 1);
UpdateSelectQuality();
}
void UMainScreenUserWidget::UpdateSelectRobot()
{
ULuckyRobotsGameInstance* LuckyRobotsGameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (LuckyRobotsGameInstance)
if (ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance()))
{
LuckyRobotsGameInstance->CurrentSelectRobot = GetCurrentRobotData().Name;
GameInstance->CurrentSelectRobot = GetCurrentRobotData().Name;
}
BPUpdateSelectRobot();
InitLevelData();
}
void UMainScreenUserWidget::UpdateSelectLevel()
{
ULuckyRobotsGameInstance* LuckyRobotsGameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (LuckyRobotsGameInstance)
if (ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance()))
{
LuckyRobotsGameInstance->CurrentSelectLevel = GetCurrentLevelData().LevelEnum;
GameInstance->CurrentSelectLevel = GetCurrentLevelData().LevelEnum;
}
BPUpdateSelectLevel();
}
void UMainScreenUserWidget::UpdateSelectQuality()
{
ULuckyRobotsGameInstance* LuckyRobotsGameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance());
if (LuckyRobotsGameInstance)
if (ULuckyRobotsGameInstance* GameInstance = Cast<ULuckyRobotsGameInstance>(GetGameInstance()))
{
LuckyRobotsGameInstance->CurrentSelectQuality = static_cast<EQualityEnum>(iCurrentSelectQuality);
GameInstance->CurrentSelectQuality = static_cast<EQualityEnum>(CurrentQualityIndex);
}
BPUpdateSelectQuality();
}
}