From ed2b879889ba51983c07fce10ec6d15e7cd987e0 Mon Sep 17 00:00:00 2001 From: yuichipanda Date: Sun, 1 Nov 2026 18:06:25 +0900 Subject: [PATCH] Add GridManager to spawn grid cells --- Source/Tactica/Private/GridManager.cpp | 66 ++++++++++++++++++++++++++ Source/Tactica/Public/GridManager.h | 43 +++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 Source/Tactica/Private/GridManager.cpp create mode 100644 Source/Tactica/Public/GridManager.h diff --git a/Source/Tactica/Private/GridManager.cpp b/Source/Tactica/Private/GridManager.cpp new file mode 100644 index 0000000..69a34b6 --- /dev/null +++ b/Source/Tactica/Private/GridManager.cpp @@ -0,0 +1,66 @@ +#include "GridManager.h" +#include "GridCell.h" +#include "Engine/World.h" + +// このファイル専用のログカテゴリ +DEFINE_LOG_CATEGORY_STATIC(LogTacticaGrid, Log, All); + +AGridManager::AGridManager() +{ + PrimaryActorTick.bCanEverTick = false; +} + +void AGridManager::BeginPlay() +{ + Super::BeginPlay(); + + UE_LOG(LogTacticaGrid, Log, TEXT("GridManager BeginPlay: %d x %d, CellSize=%.1f"), + GridSizeX, GridSizeY, CellSize); + + GenerateGrid(); +} + +void AGridManager::GenerateGrid() +{ + UWorld* World = GetWorld(); + if (!World) + { + UE_LOG(LogTacticaGrid, Error, TEXT("GetWorld() が null です")); + return; + } + + if (!CellClass) + { + CellClass = AGridCell::StaticClass(); + } + + Cells.Reset(); + const FVector Origin = GetActorLocation(); + + for (int32 Y = 0; Y < GridSizeY; ++Y) + { + for (int32 X = 0; X < GridSizeX; ++X) + { + const FVector Location = Origin + FVector(X * CellSize, Y * CellSize, 0.0f); + + FActorSpawnParameters Params; + Params.SpawnCollisionHandlingOverride = + ESpawnActorCollisionHandlingMethod::AlwaysSpawn; + + AGridCell* Cell = World->SpawnActor( + CellClass, Location, FRotator::ZeroRotator, Params); + + if (Cell) + { + Cell->GridCoord = FIntPoint(X, Y); + Cells.Add(Cell); + } + else + { + UE_LOG(LogTacticaGrid, Warning, TEXT("マス (%d, %d) の生成に失敗"), X, Y); + } + } + } + + UE_LOG(LogTacticaGrid, Log, TEXT("%d 個のマスを生成しました"), Cells.Num()); +} \ No newline at end of file diff --git a/Source/Tactica/Public/GridManager.h b/Source/Tactica/Public/GridManager.h new file mode 100644 index 0000000..0526dec --- /dev/null +++ b/Source/Tactica/Public/GridManager.h @@ -0,0 +1,43 @@ +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "GridManager.generated.h" + +class AGridCell; + +UCLASS() +class TACTICA_API AGridManager : public AActor +{ + GENERATED_BODY() + +public: + AGridManager(); + +protected: + virtual void BeginPlay() override; + +public: + // グリッドの横のマス数 + UPROPERTY(EditAnywhere, Category = "Grid") + int32 GridSizeX = 3; + + // グリッドの縦のマス数 + UPROPERTY(EditAnywhere, Category = "Grid") + int32 GridSizeY = 3; + + // 1マスの大きさ(cm) + UPROPERTY(EditAnywhere, Category = "Grid") + float CellSize = 100.0f; + + // スポーンするマスのクラス(未設定ならAGridCell) + UPROPERTY(EditAnywhere, Category = "Grid") + TSubclassOf CellClass; + + // 生成したマスの一覧 + UPROPERTY(VisibleAnywhere, Category = "Grid") + TArray> Cells; + +private: + void GenerateGrid(); +}; \ No newline at end of file