TigerProject/Assets/Scripts/MapChunk.cs

85 lines
2.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class MapChunk : MonoBehaviour
{
[Header("Tilemaps")]
[SerializeField] private Tilemap wallTilemap;
[SerializeField] private Tilemap breakableTilemap;
[SerializeField] private Tilemap propsTilemap;
[Header("Procedural Settings")]
[SerializeField] private TileBase[] availablePropTiles;
[Range(0f, 1f)] [SerializeField] private float erosionRate = 0.1f;
[Range(0f, 1f)] [SerializeField] private float propSpawnRate = 0.2f;
public void InitializeChunk(bool isMirrored)
{
if (isMirrored)
{
Vector3 scale = transform.localScale;
scale.x = -1;
transform.localScale = scale;
}
ErodeTerrain();
GenerateProps();
}
public void SpawnEnemies(GameObject[] groundPrefabs, int groundCount, GameObject[] airPrefabs, int airCount)
{
// 추후 팩토리 패턴 적용 예정
}
private void ErodeTerrain()
{
if (breakableTilemap == null) return;
BoundsInt bounds = breakableTilemap.cellBounds;
for (int x = bounds.xMin; x < bounds.xMax; x++)
{
for (int y = bounds.yMin; y < bounds.yMax; y++)
{
Vector3Int pos = new Vector3Int(x, y, 0);
if (breakableTilemap.HasTile(pos))
{
if (Random.value < erosionRate)
{
breakableTilemap.SetTile(pos, null);
}
}
}
}
}
private void GenerateProps()
{
if (propsTilemap == null || availablePropTiles.Length == 0) return;
BoundsInt bounds = breakableTilemap.cellBounds;
for (int x = bounds.xMin; x < bounds.xMax; x++)
{
for (int y = bounds.yMin; y < bounds.yMax; y++)
{
Vector3Int currentPos = new Vector3Int(x, y, 0);
Vector3Int abovePos = new Vector3Int(x, y + 1, 0);
bool hasGround = breakableTilemap.HasTile(currentPos);
bool isAirAbove = !wallTilemap.HasTile(abovePos) && !breakableTilemap.HasTile(abovePos);
if (hasGround && isAirAbove)
{
if (Random.value < propSpawnRate)
{
TileBase randomProp = availablePropTiles[Random.Range(0, availablePropTiles.Length)];
propsTilemap.SetTile(abovePos, randomProp);
}
}
}
}
}
}