TigerProject/Assets/Test/Block/BlockMapGenerator.cs

48 lines
1.4 KiB
C#

using UnityEngine;
public class BlockMapGenerator : MonoBehaviour
{
[Header("생성 설정")]
[SerializeField]
private GameObject blockPrefab; // 위에서 만든 DestructibleBlock 프리팹
[SerializeField]
private int width = 20; // 맵 가로 크기
[SerializeField]
private int height = 10; // 맵 세로 크기
[SerializeField]
private float spacing = 1.0f; // 블록 간격 (스프라이트 크기에 맞춰 조절)
[Header("랜덤 확률 (0 ~ 1)")]
[Range(0, 1)]
[SerializeField]
private float fillPercent = 0.5f; // 50% 확률로 블록 생성
private void Start()
{
GenerateMap();
}
void GenerateMap()
{
// 맵의 중앙을 (0,0)에 맞추기 위한 시작 위치 계산
Vector2 startPos = new Vector2(-width / 2f * spacing, -height / 2f * spacing);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
// 플레이어 시작 위치(중앙)는 비워두기
if (x == width / 2 && y == height / 2)
{
continue;
}
if (Random.value < fillPercent)
{
Vector2 spawnPos = startPos + new Vector2(x * spacing, y * spacing);
Instantiate(blockPrefab, spawnPos, Quaternion.identity, transform);
}
}
}
}
}