Large map and tiles maps generated based on noise render target

This commit is contained in:
aollero 2022-02-07 14:58:56 +01:00 committed by bernat
parent 41d93b4a65
commit a237d5bcf1
7 changed files with 126 additions and 22 deletions

View File

@ -20,41 +20,52 @@
FString UMapGeneratorWidget::GenerateMapFiles(const FString& destinationPath, const FString& mapName)
FString UMapGeneratorWidget::GenerateMapFiles(const FMapGeneratorMetaInfo& metaInfo)
{
DEBUG_MSG("Generating new map...");
FString errorMsg = "";
FAssetData worldAssetData;
// 1. Creating Levels
bool bLoaded = LoadWorld(worldAssetData);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, bLoaded ? "Loaded CORRECT" : "NOT loaded");
CreateMainLargeMap(metaInfo);
CreateTilesMaps(metaInfo);
// bool bLoaded = LoadWorld(worldAssetData);
// GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, bLoaded ? "Loaded CORRECT" : "NOT loaded");
// 2. Applying heightmap
UWorld* world = CastChecked<UWorld>(worldAssetData.GetAsset());
ALandscape* landscape = (ALandscape*) UGameplayStatics::GetActorOfClass(world, ALandscape::StaticClass());
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, landscape!=nullptr ? "L TRUE" : "L FALSE");
AssignLandscapeHeightMap(landscape);
// UWorld* world = CastChecked<UWorld>(worldAssetData.GetAsset());
// ALandscape* landscape = (ALandscape*) UGameplayStatics::GetActorOfClass(world, ALandscape::StaticClass());
// GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, landscape!=nullptr ? "L TRUE" : "L FALSE");
// AssignLandscapeHeightMap(landscape);
// 3. Saving world
bool bSaved = SaveWorld(worldAssetData, destinationPath, mapName);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, bSaved ? "Saved CORRECT" : "NOT saved");
// bool bSaved = SaveWorld(worldAssetData, metaInfo.destinationPath, metaInfo.mapName);
// GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, bSaved ? "Saved CORRECT" : "NOT saved");
return errorMsg;
}
bool UMapGeneratorWidget::LoadWorld(FAssetData& WorldAssetData)
bool UMapGeneratorWidget::LoadBaseTileWorld(FAssetData& WorldAssetData)
{
const FString baseMapPath= TEXT("/CarlaTools/MapGenerator/BaseMap/Tiles");
return LoadWorld(WorldAssetData, baseMapPath);
}
bool UMapGeneratorWidget::LoadBaseLargeMapWorld(FAssetData& WorldAssetData)
{
const FString baseMapPath= TEXT("/CarlaTools/MapGenerator/BaseMap/MainLargeMap");
return LoadWorld(WorldAssetData, baseMapPath);
}
bool UMapGeneratorWidget::LoadWorld(FAssetData& WorldAssetData, const FString& baseMapPath)
{
TArray<FAssetData> AssetDatas;
UObjectLibrary *MapObjectLibrary;
const FString BaseMap = TEXT("/CarlaTools/MapGenerator/BaseMap");
// Loading Map from folder using object library
MapObjectLibrary = UObjectLibrary::CreateLibrary(UWorld::StaticClass(), false, GIsEditor);
MapObjectLibrary->AddToRoot();
MapObjectLibrary->LoadAssetDataFromPath(*BaseMap);
MapObjectLibrary->LoadAssetDataFromPath(*baseMapPath);
MapObjectLibrary->LoadAssetsFromAssetData();
MapObjectLibrary->GetAssetDataList(AssetDatas);
if (AssetDatas.Num() > 0)
@ -103,4 +114,51 @@ bool UMapGeneratorWidget::SaveWorld(FAssetData& WorldToBeSaved, const FString& D
*PackageFileName, GError, nullptr, true, true, SAVE_NoError);
}
bool UMapGeneratorWidget::CreateMainLargeMap(const FMapGeneratorMetaInfo& metaInfo)
{
FAssetData worldAssetData;
bool bLoaded = LoadBaseLargeMapWorld(worldAssetData);
bool bSaved = SaveWorld(worldAssetData, metaInfo.destinationPath, metaInfo.mapName);
return true;
}
bool UMapGeneratorWidget::CreateTilesMaps(const FMapGeneratorMetaInfo& metaInfo)
{
FAssetData worldAssetData;
int numberOfTiles = metaInfo.sizeX * metaInfo.sizeY;
for(int i = 0; i < numberOfTiles; i++)
{
bool bLoaded = LoadBaseTileWorld(worldAssetData);
ApplyHeightMapToLandscape(worldAssetData);
const FString mapName = metaInfo.mapName + "_" + FString::FromInt(i);
bool bSaved = SaveWorld(worldAssetData, metaInfo.destinationPath, mapName);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, bSaved ? "Saved CORRECT" : "NOT saved");
}
return true;
}
bool UMapGeneratorWidget::ApplyHeightMapToLandscape(FAssetData& worldAssetData)
{
UWorld* world;
UObjectRedirector* BaseMapRedirector = Cast<UObjectRedirector>(worldAssetData.GetAsset());
if(BaseMapRedirector != nullptr)
{
world = CastChecked<UWorld>(BaseMapRedirector->DestinationObject);
}
else
{
world = CastChecked<UWorld>(worldAssetData.GetAsset());
}
ALandscape* landscape = (ALandscape*) UGameplayStatics::GetActorOfClass(world, ALandscape::StaticClass());
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, landscape!=nullptr ? "L TRUE" : "L FALSE");
AssignLandscapeHeightMap(landscape);
return true;
}
// #endif

View File

@ -17,6 +17,24 @@
#include "MapGeneratorWidget.generated.h"
USTRUCT(BlueprintType)
struct CARLATOOLS_API FMapGeneratorMetaInfo
{
GENERATED_USTRUCT_BODY();
UPROPERTY(BlueprintReadWrite)
FString destinationPath;
UPROPERTY(BlueprintReadWrite)
FString mapName;
UPROPERTY(BlueprintReadWrite)
int sizeX;
UPROPERTY(BlueprintReadWrite)
int sizeY;
};
/// Class UMapGeneratorWidget extends the functionality of UEditorUtilityWidget
/// to be able to generate and manage maps and largemaps tiles for procedural
/// map generation
@ -25,6 +43,11 @@ class CARLATOOLS_API UMapGeneratorWidget : public UEditorUtilityWidget
{
GENERATED_BODY()
private:
// UPROPERTY()
// UObjectLibrary *MapObjectLibrary;
public:
/// This function invokes a blueprint event defined in widget blueprint
/// event graph, which sets a heightmap to the @a landscape using
@ -33,26 +56,49 @@ public:
UFUNCTION(BlueprintImplementableEvent)
void AssignLandscapeHeightMap(ALandscape* landscape);
//private:
// UPROPERTY()
// UObjectLibrary *MapObjectLibrary;
public:
/// Function called by Widget Blueprint which generates all tiles of map
/// @a mapName, and saves them in @a destinationPath
/// Returns a void string is success and an error message if the process failed
UFUNCTION(Category="Map Generator",BlueprintCallable)
FString GenerateMapFiles(const FString& destinationPath, const FString& mapName);
FString GenerateMapFiles(const FMapGeneratorMetaInfo& metaInfo);
private:
/// Loads the base template UWorld object and is return in @a WorldAssetData
/// Loads the base tile map and stores it in @a WorldAssetData
/// The funtions return true is success, otherwise false
UFUNCTION()
bool LoadWorld(FAssetData& WorldAssetData);
bool LoadBaseTileWorld(FAssetData& WorldAssetData);
/// Loads the base large map and stores it in @a WorldAssetData
/// The funtions return true is success, otherwise false
UFUNCTION()
bool LoadBaseLargeMapWorld(FAssetData& WorldAssetData);
/// Loads the base template UWorld object from @a baseMapPath and returns
/// it in @a WorldAssetData
/// The funtions return true is success, otherwise false
UFUNCTION()
bool LoadWorld(FAssetData& WorldAssetData, const FString& baseMapPath);
/// Saves a world contained in @a WorldToBeSaved, in the path defined in @a DestinationPath
/// named as @a WorldName, as a package .umap
UFUNCTION()
bool SaveWorld(FAssetData& WorldToBeSaved, const FString& DestinationPath, const FString& WorldName);
/// Takes the name of the map from @a metaInfo and created the main map
/// including all the actors needed by large map system
UFUNCTION()
bool CreateMainLargeMap(const FMapGeneratorMetaInfo& metaInfo);
/// Takes @a metaInfo as input and generates all tiles based on the
/// dimensions specified for the map
/// The funtions return true is success, otherwise false
UFUNCTION()
bool CreateTilesMaps(const FMapGeneratorMetaInfo& metaInfo);
/// Gets the landscape from the input world @a worldAssetData and
/// applies the heightmap to it
/// The funtions return true is success, otherwise false
UFUNCTION()
bool ApplyHeightMapToLandscape(FAssetData& worldAssetData);
};
// #endif