using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using EerieVillage.Map.Data;
namespace EerieVillage.Map.Generation
{
///
/// BT13-Dev-Map — 지면 절차 생성 Planner (PD 2026-05-19 — 9-slice 자동 분기 위임).
/// PD 지시 (2026-05-19 추가): "타일맵이 자연스럽게 연결될 수 있도록 구성."
/// 본 Planner 영역 terrain.tile = fallback (groundCenter 우선) — 실 배치 영역 MapApplier.PickGroundTile9Slice 영역
/// blueprint.groundSurfaceY + 인접 column 검사 후 9 slot + variant 자동 선택.
/// 이전 본문 백업: 공유/개발팀_백업/EerieVillage/BT13-Dev-Map_20260519/GroundPlanner.bak_20260519_1100.cs
///
public static class GroundPlanner
{
public static void Plan(MapGenerationConfig config, MapBlueprint blueprint)
{
if (config == null || blueprint == null) return;
int length = config.lengthTiles;
blueprint.groundSurfaceY = new int[length];
// Tile pick — PD 2026-05-19 임시 작업 영역 groundCenter 일관 사용
TileBase groundTile = PickGroundTile(config.catalog);
if (groundTile == null) return;
int baseY = config.groundBaseY;
int amplitude = Mathf.Max(0, config.groundPerlinAmplitude);
float scale = Mathf.Max(0.0001f, config.groundPerlinScale);
for (int x = 0; x < length; x++)
{
// Perlin noise — 0~1 → -amplitude..+amplitude
float n = Mathf.PerlinNoise(x * scale, 0f);
int offset = Mathf.RoundToInt((n * 2f - 1f) * amplitude);
int surfaceY = baseY + offset;
blueprint.groundSurfaceY[x] = surfaceY;
// 지면 영역 — surfaceY 부터 surfaceY-5 까지 6 Tile 두께
for (int dy = 0; dy < 6; dy++)
{
blueprint.terrain.Add(new TerrainTilePlace
{
x = x,
y = surfaceY - dy,
tile = groundTile
});
}
}
int maxSurface = baseY + amplitude;
blueprint.heightTiles = Mathf.Max(blueprint.heightTiles, maxSurface + 1);
}
///
/// PD 2026-05-19 — 9방향 9 slot 중 groundCenter 우선 사용 (임시 작업).
/// fallback 순서: groundCenter → groundTop → groundBottom → 9 slot 중 첫 non-null.
/// 향후 9-slice 자동 분기 시 (x, surfaceY) 인접 영역 검사 후 TopLeft·Top·TopRight 등 선택.
///
static TileBase PickGroundTile(MapResourceCatalog catalog)
{
if (catalog == null) return null;
if (catalog.groundCenter != null) return catalog.groundCenter;
if (catalog.groundTop != null) return catalog.groundTop;
if (catalog.groundBottom != null) return catalog.groundBottom;
// 9 slot 중 첫 non-null
if (catalog.groundTopLeft != null) return catalog.groundTopLeft;
if (catalog.groundTopRight != null) return catalog.groundTopRight;
if (catalog.groundLeft != null) return catalog.groundLeft;
if (catalog.groundRight != null) return catalog.groundRight;
if (catalog.groundBottomLeft != null) return catalog.groundBottomLeft;
if (catalog.groundBottomRight != null) return catalog.groundBottomRight;
return null;
}
}
}