HuePuzzle/Assets/Scripts/LevelGenerator.cs
2025-11-21 16:12:41 +09:00

58 lines
2.2 KiB
C#

using UnityEngine;
public class LevelGenerator : MonoBehaviour
{
public GameObject tilePrefab;
public Transform gridParent; // 타일들이 깔릴 부모 오브젝트 (정리용)
// 2차원 배열로 타일 관리 (승리 체크용)
public TileController[,] tileGrid;
public float tileGap = 0.05f;
public void SpawnLevel(LevelData data)
{
// 기존 타일 청소
foreach (Transform child in gridParent) Destroy(child.gameObject);
int width = data.gridSize.x;
int height = data.gridSize.y;
tileGrid = new TileController[width, height];
float spacing = 1.0f + tileGap;
Vector2 offset = new Vector2((width - 1) * spacing * 0.5f, (height - 1) * spacing * 0.5f);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
// 1. 색상 보간 계산 (데이터 기반)
float xPct = (float)x / (width - 1);
float yPct = (float)y / (height - 1);
Color top = Color.Lerp(data.colorTL, data.colorTR, xPct);
Color bot = Color.Lerp(data.colorBL, data.colorBR, xPct);
Color finalColor = Color.Lerp(top, bot, yPct);
// 2. 타일 생성
GameObject obj = Instantiate(tilePrefab, gridParent);
// 위치 계산에 spacing 곱하기
float posX = (x * spacing) - offset.x;
float posY = offset.y - (y * spacing);
obj.transform.position = new Vector2(posX, posY);
// 타일 크기를 살짝 줄여서 갭을 더 강조할 수도 있음 (선택)
// obj.transform.localScale = Vector3.one * 0.95f;
obj.name = $"Tile_{x}_{y}";
// 3. 속성 주입
TileController tile = obj.GetComponent<TileController>();
bool isFixed = (x == 0 || x == width - 1) && (y == 0 || y == height - 1); // 모서리만 고정
tile.Initialize(new Vector2Int(x, y), finalColor, isFixed);
tileGrid[x, y] = tile;
}
Camera.main.orthographicSize = (Mathf.Max(width, height) * spacing) * 0.6f + 1;
}
}
}