This commit is contained in:
2023-07-30 16:30:14 +02:00
commit d3b4f002d3
23 changed files with 510 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.Collections.Generic;
public class UE5_GridToolTarget : TargetRules
{
public UE5_GridToolTarget( TargetInfo Target) : base(Target)
{
Type = TargetType.Game;
DefaultBuildSettings = BuildSettingsVersion.V2;
IncludeOrderVersion = EngineIncludeOrderVersion.Unreal5_1;
ExtraModuleNames.Add("UE5_GridTool");
}
}

View File

@@ -0,0 +1,84 @@
#include "LevelChunk.h"
ALevelChunk::ALevelChunk()
{
PrimaryActorTick.bCanEverTick = false;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
SetupGrid();
}
// Called when the game starts or when spawned
void ALevelChunk::BeginPlay()
{
Super::BeginPlay();
CleanupComponents();
}
void ALevelChunk::SetupGrid() {
// Set XYZ base points. Next we will offset each next slot from these points
float ZBase = -(100.f * (SizeZ - 1) / 2);
float XBase = -(100.f * (SizeX - 1) / 2);
float YBase = -(100.f * (SizeY - 1) / 2);
int32 Count = 0;
// Z axis loop
for (int32 z = 0; z < SizeZ; ++z) {
float ZOffset = ZBase + (100.f * z);
// X axis loop
for (int32 x = 0; x < SizeX; ++x) {
float XOffset = XBase + (100.f * x);
// Y axis loop
for (int32 y = 0; y < SizeY; ++y) {
float YOffset = YBase + (100.f * y);
ULevelGridSlot* ThisSlot = CreateDefaultSubobject<ULevelGridSlot>(*FString::Printf(TEXT("GridSlot %i"), Count));
UStaticMeshComponent* PreviewMesh = CreateDefaultSubobject<UStaticMeshComponent>(*FString::Printf(TEXT("PreviewMesh %i"), Count));
ThisSlot->Index = Count;
ThisSlot->SetupAttachment(RootComponent);
ThisSlot->EditorScale = 0.25f;
ThisSlot->SetRelativeLocation(FVector(XOffset, YOffset, ZOffset));
PreviewMesh->SetRelativeLocation(FVector(XOffset, YOffset, ZOffset));
Slots.Add(ThisSlot);
PreviewMeshes.Add(PreviewMesh);
Count++;
}
}
}
}
// Preview elements --- Go through all of the slots and if given slot has any class chosen, and that class has any static mesh, set corresponding PreviewMesh to display that mesh.
void ALevelChunk::PreviewElements() {
for (ULevelGridSlot* Slot : Slots) {
if (IsValid(Slot)) {
UClass* Class = Slot->ResolveClass();
if (IsValid(Class)) {
ASpawnableElement* DefaultObject = Cast<ASpawnableElement>(Class->GetDefaultObject());
if (IsValid(DefaultObject->StaticMesh)) {
PreviewMeshes[Slot->Index]->SetStaticMesh(DefaultObject->StaticMesh->GetStaticMesh());
}
}
}
}
}
// Set all PreviewMeshes to nullptr
void ALevelChunk::HidePreview() {
int32 Count = 0;
for (UStaticMeshComponent* PreviewMesh : PreviewMeshes) {
if (IsValid(PreviewMesh)) {
PreviewMesh->SetStaticMesh(nullptr);
}
Count++;
}
}
void ALevelChunk::CleanupComponents() {
for (UStaticMeshComponent* PreviewMesh : PreviewMeshes) {
PreviewMesh->UnregisterComponent();
}
PreviewMeshes.Empty();
}

View File

@@ -0,0 +1,55 @@
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LevelGridSlot.h"
#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "SpawnableElement.h"
#include "LevelChunk.generated.h"
UCLASS()
class UE5_GRIDTOOL_API ALevelChunk : public AActor
{
GENERATED_BODY()
public:
ALevelChunk();
// --- Variables
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Actor Setup")
int32 SizeX = 6;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Actor Setup")
int32 SizeY = 6;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Actor Setup")
int32 SizeZ = 4;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly)
TArray<ULevelGridSlot*> Slots;
UPROPERTY()
TArray<UStaticMeshComponent*> PreviewMeshes;
// --- Functions
// Remove preview elements
UFUNCTION(BlueprintCallable, CallInEditor, Category = "Actor Methods")
void HidePreview();
// Temporarily spawn each element in this chunk
UFUNCTION(BlueprintCallable, CallInEditor, Category = "Actor Methods")
void PreviewElements();
private:
void BeginPlay() override;
UFUNCTION()
void SetupGrid();
UFUNCTION()
void CleanupComponents();
};

View File

@@ -0,0 +1,37 @@
#include "LevelGridSlot.h"
// Sets default values for this component's properties
ULevelGridSlot::ULevelGridSlot()
{
PrimaryComponentTick.bCanEverTick = false;
}
// Called when the game starts
void ULevelGridSlot::BeginPlay()
{
Super::BeginPlay();
Spawn();
}
UClass* ULevelGridSlot::ResolveClass() {
UClass* Class = nullptr;
if (!Object.IsNull()) {
if (!Object.IsValid()) {
Class = Object.LoadSynchronous();
}
else {
Class = Object.Get();
}
}
return Class;
}
void ULevelGridSlot::Spawn() {
UClass* Class = ResolveClass();
if (IsValid(Class)) {
FActorSpawnParameters SpawnParams;
GetWorld()->SpawnActor<ASpawnableElement>(Class, this->GetComponentLocation(), this->GetComponentRotation(), SpawnParams);
}
this->DestroyComponent();
}

View File

@@ -0,0 +1,36 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "SpawnableElement.h"
#include "Components/BillboardComponent.h"
#include "LevelGridSlot.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class UE5_GRIDTOOL_API ULevelGridSlot : public UBillboardComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
ULevelGridSlot();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Actor Setup")
TSoftClassPtr<ASpawnableElement> Object;
UPROPERTY()
int32 Index;
UFUNCTION()
UClass* ResolveClass();
protected:
// Called when the game starts
virtual void BeginPlay() override;
private:
UFUNCTION()
void Spawn();
};

View File

@@ -0,0 +1,7 @@
#include "SpawnableElement.h"
ASpawnableElement::ASpawnableElement()
{
PrimaryActorTick.bCanEverTick = false;
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
}

View File

@@ -0,0 +1,21 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "SpawnableElement.generated.h"
UCLASS()
class UE5_GRIDTOOL_API ASpawnableElement : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpawnableElement();
UPROPERTY(EditAnywhere, BlueprintReadOnly)
UStaticMeshComponent* StaticMesh;
};

View File

@@ -0,0 +1,23 @@
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class UE5_GridTool : ModuleRules
{
public UE5_GridTool(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}

View File

@@ -0,0 +1,6 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include "UE5_GridTool.h"
#include "Modules/ModuleManager.h"
IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, UE5_GridTool, "UE5_GridTool" );

View File

@@ -0,0 +1,6 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"

View File

@@ -0,0 +1,5 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include "UE5_GridToolGameModeBase.h"

View File

@@ -0,0 +1,17 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "UE5_GridToolGameModeBase.generated.h"
/**
*
*/
UCLASS()
class UE5_GRIDTOOL_API AUE5_GridToolGameModeBase : public AGameModeBase
{
GENERATED_BODY()
};

View File

@@ -0,0 +1,15 @@
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.Collections.Generic;
public class UE5_GridToolEditorTarget : TargetRules
{
public UE5_GridToolEditorTarget( TargetInfo Target) : base(Target)
{
Type = TargetType.Editor;
DefaultBuildSettings = BuildSettingsVersion.V2;
IncludeOrderVersion = EngineIncludeOrderVersion.Unreal5_1;
ExtraModuleNames.Add("UE5_GridTool");
}
}