351 lines
17 KiB
C#
351 lines
17 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.Tilemaps;
|
|||
|
|
using EerieVillage.Map.Data;
|
|||
|
|
using Platformer.Mechanics;
|
|||
|
|
|
|||
|
|
namespace EerieVillage.Map.Runtime
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// BT13-Dev-Map Phase 2-C — 절차 생성 결과 → Scene 적용.
|
|||
|
|
/// spec §7 정합. PD §15 OVERRIDE 6건 (2026-05-18):
|
|||
|
|
/// §15-4 OVERRIDE — GameOptimizer 위임 (단일 Level Tilemap 배치·Layer 분류는 GameOptimizer.SetupJumpThroughPlatforms 영역).
|
|||
|
|
/// §15-7 OVERRIDE — Enemy 재-spawn timer·count 영역 SpawnPoint 영역 전달.
|
|||
|
|
/// §15-8 OVERRIDE — baseEnemyPrefab + MonsterRandomizer 활용 (SpawnPointPlanner 영역 처리).
|
|||
|
|
/// §15-9 OVERRIDE — BossRoom 우측 끝 영역 보스 spawn (bossPrefab 영역 등록 시 spawn).
|
|||
|
|
/// </summary>
|
|||
|
|
public static class MapApplier
|
|||
|
|
{
|
|||
|
|
const string MAP_ROOT = "MapRoot";
|
|||
|
|
const string LEVEL_NAME = "Level";
|
|||
|
|
const string GRID_NAME = "Grid";
|
|||
|
|
const string SPAWNROOT_NAME = "SpawnPoints";
|
|||
|
|
const string PROPROOT_NAME = "BackgroundProps";
|
|||
|
|
const string BOSSROOT_NAME = "BossSpawn";
|
|||
|
|
const string BG_ROOT_NAME = "MapBackground";
|
|||
|
|
|
|||
|
|
/// <summary>절차 생성 결과 영역 Scene 적용.</summary>
|
|||
|
|
public static void Apply(MapBlueprint blueprint, MapGenerationConfig config)
|
|||
|
|
{
|
|||
|
|
if (blueprint == null || config == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogError("[MapApplier] blueprint or config null");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Clear();
|
|||
|
|
|
|||
|
|
// 1. MapRoot · Grid 영역 확보
|
|||
|
|
var grid = FindOrCreateGrid();
|
|||
|
|
|
|||
|
|
// 2. §15-4 OVERRIDE — 단일 Level Tilemap 영역 모든 Tile 배치 (지면 + 공중 발판).
|
|||
|
|
// Layer 분류 (Level Layer 0 / AutoForeground Layer 16)는 GameOptimizer.SetupJumpThroughPlatforms 위임.
|
|||
|
|
// 예측성·결정성 영역 GameOptimizer 영역 룰 영역 영역.
|
|||
|
|
// PD 2026-05-19 — terrain 영역 9-slice 자동 분기 적용 (PickGroundTile9Slice).
|
|||
|
|
var levelTm = FindOrCreateTilemap(grid, LEVEL_NAME, 0, addCompositeCollider: true);
|
|||
|
|
var catalog = config.catalog;
|
|||
|
|
foreach (var t in blueprint.terrain)
|
|||
|
|
{
|
|||
|
|
var tile = PickGroundTile9Slice(t.x, t.y, blueprint, catalog) ?? t.tile;
|
|||
|
|
levelTm.SetTile(new Vector3Int(t.x, t.y, 0), tile);
|
|||
|
|
}
|
|||
|
|
// 공중 발판 영역 동일 Level Tilemap 영역 — GameOptimizer 영역 작은 공중 발판 판정 영역 영역 AutoForeground 영역 자동 이동.
|
|||
|
|
foreach (var p in blueprint.platforms)
|
|||
|
|
{
|
|||
|
|
levelTm.SetTile(new Vector3Int(p.x, p.y, 0), p.tile);
|
|||
|
|
}
|
|||
|
|
EnsureCollidersProcessed(levelTm);
|
|||
|
|
|
|||
|
|
// 3. SpawnPoints (Enemy 거점 · §8 · §15-7 OVERRIDE timer·count 전달)
|
|||
|
|
var spawnRoot = FindOrCreateRoot(SPAWNROOT_NAME);
|
|||
|
|
foreach (var sp in blueprint.spawnPoints)
|
|||
|
|
{
|
|||
|
|
if (sp.enemyPrefab == null) continue;
|
|||
|
|
var go = new GameObject($"SpawnPoint_{sp.position.x:F1}_{sp.position.y:F1}");
|
|||
|
|
go.transform.SetParent(spawnRoot.transform, false);
|
|||
|
|
go.transform.position = new Vector3(sp.position.x, sp.position.y, 0f);
|
|||
|
|
var sc = go.AddComponent<SpawnPoint>();
|
|||
|
|
sc.enemyPrefab = sp.enemyPrefab;
|
|||
|
|
sc.spawnRadiusX = 0.5f;
|
|||
|
|
sc.initialSpawnCount = 1;
|
|||
|
|
sc.maxConcurrent = 1;
|
|||
|
|
// §15-7 OVERRIDE — config 영역 timer·count 영역 전달
|
|||
|
|
sc.respawnTimer = config.respawnTimer;
|
|||
|
|
sc.respawnMaxCount = config.respawnMaxCount;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 4. Player Start Point (catalog.playerPrefab 영역 Instantiate)
|
|||
|
|
if (config.catalog != null && config.catalog.playerPrefab != null)
|
|||
|
|
{
|
|||
|
|
var existingPlayer = GameObject.FindGameObjectWithTag("Player");
|
|||
|
|
if (existingPlayer != null)
|
|||
|
|
{
|
|||
|
|
existingPlayer.transform.position = new Vector3(blueprint.startPoint.x, blueprint.startPoint.y, 0f);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Object.Instantiate(config.catalog.playerPrefab,
|
|||
|
|
new Vector3(blueprint.startPoint.x, blueprint.startPoint.y, 0f),
|
|||
|
|
Quaternion.identity);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 5. Goal (VictoryZone) — PD 2026-05-19 catalog.goalPrefab 폐기. 후속 PD 결정 시 부활.
|
|||
|
|
|
|||
|
|
// 6. §15-9 OVERRIDE — BossRoom 우측 끝 영역 보스 spawn (bossPrefab 영역 등록 시).
|
|||
|
|
// BossRoom 영역 평탄 청크 영역 ChunkPlanner 영역 추가 (GroundPlanner 영역 영역 flat 영역 영역).
|
|||
|
|
// Boss spawn 위치 = BossRoom 청크 중앙 영역 지면 위.
|
|||
|
|
if (config.bossRoomEnabled && config.catalog != null && config.catalog.bossPrefab != null)
|
|||
|
|
{
|
|||
|
|
int bossRoomStart = config.lengthTiles - config.bossRoomLengthTiles;
|
|||
|
|
int bossX = bossRoomStart + config.bossRoomLengthTiles / 2;
|
|||
|
|
int bossGroundY = (bossX >= 0 && bossX < blueprint.groundSurfaceY.Length)
|
|||
|
|
? blueprint.groundSurfaceY[bossX]
|
|||
|
|
: 0;
|
|||
|
|
var bossRoot = FindOrCreateRoot(BOSSROOT_NAME);
|
|||
|
|
var bossGo = Object.Instantiate(config.catalog.bossPrefab,
|
|||
|
|
new Vector3(bossX + 0.5f, bossGroundY + 1.5f, 0f),
|
|||
|
|
Quaternion.identity);
|
|||
|
|
bossGo.transform.SetParent(bossRoot.transform, true);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 7. 배경 Prop
|
|||
|
|
if (blueprint.backgroundProps != null && blueprint.backgroundProps.Count > 0)
|
|||
|
|
{
|
|||
|
|
var propRoot = FindOrCreateRoot(PROPROOT_NAME);
|
|||
|
|
foreach (var prop in blueprint.backgroundProps)
|
|||
|
|
{
|
|||
|
|
if (prop.prefab == null) continue;
|
|||
|
|
var go = Object.Instantiate(prop.prefab,
|
|||
|
|
new Vector3(prop.position.x, prop.position.y, 0f),
|
|||
|
|
Quaternion.identity);
|
|||
|
|
go.transform.SetParent(propRoot.transform, true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 8. 배경 이미지 레이어 (PD 2026-05-19 — 맵 가장 하위 레이어 · 반복 배치)
|
|||
|
|
if (config.catalog != null && config.catalog.backgroundSprite != null)
|
|||
|
|
{
|
|||
|
|
ApplyBackgroundLayer(config.catalog.backgroundSprite, config.lengthTiles, blueprint);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Debug.Log($"[MapApplier] Apply 완료 — terrain={blueprint.terrain.Count} platforms={blueprint.platforms.Count} spawnPoints={blueprint.spawnPoints.Count} seed={blueprint.generationSeed} (§15-4 단일 Level + §15-9 BossRoom={config.bossRoomEnabled})");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>Scene 영역 본 시스템 영역 적용 결과 제거.</summary>
|
|||
|
|
public static void Clear()
|
|||
|
|
{
|
|||
|
|
// §15-4 OVERRIDE — 단일 Level Tilemap 영역 clear. AutoForeground 영역 GameOptimizer 영역 영역 영역 영역 영역 clear 영역 영역.
|
|||
|
|
ClearTilemap(LEVEL_NAME);
|
|||
|
|
ClearTilemap("AutoForeground"); // GameOptimizer 영역 영역 영역 영역 안전 clear
|
|||
|
|
|
|||
|
|
// SpawnPoint·BackgroundProps·BossSpawn root 영역 제거
|
|||
|
|
DestroyByName(SPAWNROOT_NAME);
|
|||
|
|
DestroyByName(PROPROOT_NAME);
|
|||
|
|
DestroyByName(BOSSROOT_NAME);
|
|||
|
|
DestroyByName(BG_ROOT_NAME);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ───── 헬퍼 ─────
|
|||
|
|
|
|||
|
|
static GameObject FindOrCreateGrid()
|
|||
|
|
{
|
|||
|
|
var grid = GameObject.Find(GRID_NAME);
|
|||
|
|
if (grid == null)
|
|||
|
|
{
|
|||
|
|
grid = new GameObject(GRID_NAME);
|
|||
|
|
grid.AddComponent<Grid>();
|
|||
|
|
}
|
|||
|
|
else if (grid.GetComponent<Grid>() == null)
|
|||
|
|
{
|
|||
|
|
grid.AddComponent<Grid>();
|
|||
|
|
}
|
|||
|
|
return grid;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static Tilemap FindOrCreateTilemap(GameObject grid, string name, int layer, bool addCompositeCollider)
|
|||
|
|
{
|
|||
|
|
var existing = GameObject.Find(name);
|
|||
|
|
if (existing != null)
|
|||
|
|
{
|
|||
|
|
var t = existing.GetComponent<Tilemap>();
|
|||
|
|
if (t == null) t = existing.AddComponent<Tilemap>();
|
|||
|
|
if (existing.GetComponent<TilemapRenderer>() == null) existing.AddComponent<TilemapRenderer>();
|
|||
|
|
var tc = existing.GetComponent<TilemapCollider2D>();
|
|||
|
|
if (tc == null) tc = existing.AddComponent<TilemapCollider2D>();
|
|||
|
|
if (addCompositeCollider)
|
|||
|
|
{
|
|||
|
|
var cc = existing.GetComponent<CompositeCollider2D>();
|
|||
|
|
if (cc == null) cc = existing.AddComponent<CompositeCollider2D>();
|
|||
|
|
var rb = existing.GetComponent<Rigidbody2D>();
|
|||
|
|
if (rb == null) rb = existing.AddComponent<Rigidbody2D>();
|
|||
|
|
rb.bodyType = RigidbodyType2D.Static;
|
|||
|
|
tc.compositeOperation = Collider2D.CompositeOperation.Merge;
|
|||
|
|
}
|
|||
|
|
existing.layer = layer;
|
|||
|
|
if (existing.transform.parent != grid.transform)
|
|||
|
|
existing.transform.SetParent(grid.transform, false);
|
|||
|
|
return t;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var go = new GameObject(name);
|
|||
|
|
go.transform.SetParent(grid.transform, false);
|
|||
|
|
go.layer = layer;
|
|||
|
|
var tm = go.AddComponent<Tilemap>();
|
|||
|
|
go.AddComponent<TilemapRenderer>();
|
|||
|
|
var tilemapCollider = go.AddComponent<TilemapCollider2D>();
|
|||
|
|
if (addCompositeCollider)
|
|||
|
|
{
|
|||
|
|
var cc = go.AddComponent<CompositeCollider2D>();
|
|||
|
|
var rb = go.GetComponent<Rigidbody2D>();
|
|||
|
|
if (rb == null) rb = go.AddComponent<Rigidbody2D>();
|
|||
|
|
rb.bodyType = RigidbodyType2D.Static;
|
|||
|
|
tilemapCollider.compositeOperation = Collider2D.CompositeOperation.Merge;
|
|||
|
|
}
|
|||
|
|
return tm;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static void EnsureCollidersProcessed(Tilemap tm)
|
|||
|
|
{
|
|||
|
|
if (tm == null) return;
|
|||
|
|
var tc = tm.GetComponent<TilemapCollider2D>();
|
|||
|
|
if (tc != null) tc.ProcessTilemapChanges();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static GameObject FindOrCreateRoot(string name)
|
|||
|
|
{
|
|||
|
|
var existing = GameObject.Find(name);
|
|||
|
|
if (existing != null) return existing;
|
|||
|
|
return new GameObject(name);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static void ClearTilemap(string name)
|
|||
|
|
{
|
|||
|
|
var go = GameObject.Find(name);
|
|||
|
|
if (go == null) return;
|
|||
|
|
var tm = go.GetComponent<Tilemap>();
|
|||
|
|
if (tm != null) tm.ClearAllTiles();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>§ PD 2026-05-19 — 배경 이미지 맵 전체 크기 반복 배치.
|
|||
|
|
/// SpriteRenderer.drawMode=Tiled · sortingOrder=-1000 · size=(lengthTiles, mapHeight).</summary>
|
|||
|
|
static void ApplyBackgroundLayer(Sprite backgroundSprite, int lengthTiles, MapBlueprint blueprint)
|
|||
|
|
{
|
|||
|
|
// BG_ROOT (단일 GameObject · SpriteRenderer)
|
|||
|
|
var bgGo = new GameObject(BG_ROOT_NAME);
|
|||
|
|
var sr = bgGo.AddComponent<SpriteRenderer>();
|
|||
|
|
sr.sprite = backgroundSprite;
|
|||
|
|
sr.drawMode = SpriteDrawMode.Tiled;
|
|||
|
|
sr.tileMode = SpriteTileMode.Continuous;
|
|||
|
|
sr.sortingOrder = -1000; // 가장 하위 레이어
|
|||
|
|
|
|||
|
|
// 맵 크기 산출 — 가로=lengthTiles, 세로=groundSurfaceY 범위 + 점프 도달 여유
|
|||
|
|
int maxGroundY = 0;
|
|||
|
|
int minGroundY = 0;
|
|||
|
|
if (blueprint.groundSurfaceY != null && blueprint.groundSurfaceY.Length > 0)
|
|||
|
|
{
|
|||
|
|
maxGroundY = blueprint.groundSurfaceY[0];
|
|||
|
|
minGroundY = blueprint.groundSurfaceY[0];
|
|||
|
|
for (int i = 1; i < blueprint.groundSurfaceY.Length; i++)
|
|||
|
|
{
|
|||
|
|
if (blueprint.groundSurfaceY[i] > maxGroundY) maxGroundY = blueprint.groundSurfaceY[i];
|
|||
|
|
if (blueprint.groundSurfaceY[i] < minGroundY) minGroundY = blueprint.groundSurfaceY[i];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 세로 = (지면 범위 + 점프 도달 H_max 11.24m × 2 여유 + 지면 두께 6)
|
|||
|
|
float widthUnits = lengthTiles;
|
|||
|
|
float heightUnits = (maxGroundY - minGroundY) + 6f + 24f; // 지면 6 + 점프 24
|
|||
|
|
sr.size = new Vector2(widthUnits, heightUnits);
|
|||
|
|
|
|||
|
|
// 위치 정합 — Sprite pivot = (0.5, 0.5) center (BgImg_1.png.meta 확인)
|
|||
|
|
// 지면 x ∈ [0, lengthTiles] · y ∈ [minGroundY-5, maxGroundY] → 배경 중심 (lengthTiles/2, (maxY+minY)/2)
|
|||
|
|
// z = 0 — sortingOrder=-1000 영역 카메라 transparency sort 우선
|
|||
|
|
float centerX = lengthTiles * 0.5f;
|
|||
|
|
float centerY = (maxGroundY + minGroundY) * 0.5f + 6f; // 점프 영역 상단 여유 반영
|
|||
|
|
bgGo.transform.position = new Vector3(centerX, centerY, 0f);
|
|||
|
|
|
|||
|
|
Debug.Log($"[MapApplier] Background 적용 완료 — sprite={backgroundSprite.name} size=({widthUnits:F1}, {heightUnits:F1}) Tiled");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// PD 2026-05-19 — terrain tile (x, y) 영역 9-slice 자동 분기.
|
|||
|
|
/// blueprint.groundSurfaceY[x] = 영역 표면 y 좌표. 지면 영역 = surfaceY 부터 surfaceY-5 까지 6 tile (GroundPlanner 정합).
|
|||
|
|
/// 인접 column (x-1·x+1) 영역 동일 y 지면 존재 여부 판정 후 9 slot + variant 자동 선택.
|
|||
|
|
/// 자연 연결: 표면 좌·우 가장자리 = TopLeft·TopRight, 표면 중간 = Top variant random.
|
|||
|
|
/// 바닥 좌·우 가장자리 = BottomLeft·BottomRight, 바닥 중간 = Bottom variant random.
|
|||
|
|
/// 중간 측면 = Left·Right, 그 외 = Center.
|
|||
|
|
/// </summary>
|
|||
|
|
static TileBase PickGroundTile9Slice(int x, int y, MapBlueprint bp, MapResourceCatalog cat)
|
|||
|
|
{
|
|||
|
|
if (bp == null || cat == null || bp.groundSurfaceY == null || bp.groundSurfaceY.Length == 0)
|
|||
|
|
return null;
|
|||
|
|
if (x < 0 || x >= bp.groundSurfaceY.Length) return null;
|
|||
|
|
|
|||
|
|
int surfaceY = bp.groundSurfaceY[x];
|
|||
|
|
const int THICKNESS = 6; // GroundPlanner: surfaceY 부터 surfaceY-5 까지 6 tile
|
|||
|
|
|
|||
|
|
bool isSurface = (y == surfaceY);
|
|||
|
|
bool isBottom = (y == surfaceY - (THICKNESS - 1));
|
|||
|
|
|
|||
|
|
// 인접 column 영역 동일 y 위치 지면 존재 여부
|
|||
|
|
bool LeftHasGroundAtY(int xx, int yy)
|
|||
|
|
{
|
|||
|
|
if (xx < 0 || xx >= bp.groundSurfaceY.Length) return false;
|
|||
|
|
int s = bp.groundSurfaceY[xx];
|
|||
|
|
return (yy <= s) && (yy >= s - (THICKNESS - 1));
|
|||
|
|
}
|
|||
|
|
bool leftHas = LeftHasGroundAtY(x - 1, y);
|
|||
|
|
bool rightHas = LeftHasGroundAtY(x + 1, y);
|
|||
|
|
bool isLeftEdge = !leftHas;
|
|||
|
|
bool isRightEdge = !rightHas;
|
|||
|
|
|
|||
|
|
// 9-slice 분기
|
|||
|
|
if (isSurface)
|
|||
|
|
{
|
|||
|
|
if (isLeftEdge && cat.groundTopLeft != null) return cat.groundTopLeft;
|
|||
|
|
if (isRightEdge && cat.groundTopRight != null) return cat.groundTopRight;
|
|||
|
|
// Top variant random — seed^x 결정성
|
|||
|
|
if (cat.groundTopVariants != null && cat.groundTopVariants.Length > 0)
|
|||
|
|
{
|
|||
|
|
int seed = bp.generationSeed ^ x;
|
|||
|
|
int idx = (seed & 0x7FFFFFFF) % cat.groundTopVariants.Length;
|
|||
|
|
var v = cat.groundTopVariants[idx];
|
|||
|
|
if (v != null) return v;
|
|||
|
|
}
|
|||
|
|
return cat.groundTop;
|
|||
|
|
}
|
|||
|
|
if (isBottom)
|
|||
|
|
{
|
|||
|
|
if (isLeftEdge && cat.groundBottomLeft != null) return cat.groundBottomLeft;
|
|||
|
|
if (isRightEdge && cat.groundBottomRight != null) return cat.groundBottomRight;
|
|||
|
|
if (cat.groundBottomVariants != null && cat.groundBottomVariants.Length > 0)
|
|||
|
|
{
|
|||
|
|
int seed = bp.generationSeed ^ (x + 999);
|
|||
|
|
int idx = (seed & 0x7FFFFFFF) % cat.groundBottomVariants.Length;
|
|||
|
|
var v = cat.groundBottomVariants[idx];
|
|||
|
|
if (v != null) return v;
|
|||
|
|
}
|
|||
|
|
return cat.groundBottom;
|
|||
|
|
}
|
|||
|
|
// 중간 layer (surfaceY-1 ~ surfaceY-4)
|
|||
|
|
if (isLeftEdge && cat.groundLeft != null) return cat.groundLeft;
|
|||
|
|
if (isRightEdge && cat.groundRight != null) return cat.groundRight;
|
|||
|
|
return cat.groundCenter;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
static void DestroyByName(string name)
|
|||
|
|
{
|
|||
|
|
var go = GameObject.Find(name);
|
|||
|
|
if (go != null)
|
|||
|
|
{
|
|||
|
|
#if UNITY_EDITOR
|
|||
|
|
if (!Application.isPlaying) Object.DestroyImmediate(go);
|
|||
|
|
else Object.Destroy(go);
|
|||
|
|
#else
|
|||
|
|
Object.Destroy(go);
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|