Projext/Assets/00001.Scripts/Simple Random Walk Generator.cs
2026-02-22 22:37:34 +09:00

52 lines
1.4 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class SimpleMapGenerator : MonoBehaviour
{
public GameObject tilePrefab; // 배치할 타일 프리팹
public int totalTiles = 500; // 생성할 총 타일 수
public float tileSize = 1.0f; // 타일 간의 간격
void Start()
{
GenerateMap();
}
void GenerateMap()
{
// 중복 위치를 방지하기 위해 생성된 위치 저장
HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();
Vector2Int currentPos = Vector2Int.zero; // 시작점 (0, 0)
floorPositions.Add(currentPos);
for (int i = 0; i < totalTiles; i++)
{
// 상하좌우 중 랜덤한 방향 선택
currentPos += GetRandomDirection();
// 새로운 위치라면 목록에 추가
if (!floorPositions.Contains(currentPos))
{
floorPositions.Add(currentPos);
}
}
// 결정된 위치에 타일 생성
foreach (var pos in floorPositions)
{
Instantiate(tilePrefab, new Vector3(pos.x * tileSize, 0, pos.y * tileSize), Quaternion.identity, transform);
}
}
Vector2Int GetRandomDirection()
{
Vector2Int[] directions = {
Vector2Int.up,
Vector2Int.down,
Vector2Int.left,
Vector2Int.right
};
return directions[Random.Range(0, directions.Length)];
}
}