83 lines
2.2 KiB
C#
83 lines
2.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Tilemaps;
|
|
|
|
public class MapGenerator : MonoBehaviour
|
|
{
|
|
[Header("Map Size")]
|
|
[SerializeField] private int width = 64;
|
|
[SerializeField] private int height = 64;
|
|
|
|
[Header("Map Shape")]
|
|
[SerializeField] private float shapePower = 2.4f;
|
|
[SerializeField] private int edgeMargin = 2;
|
|
|
|
[Header("References")]
|
|
[SerializeField] private Tilemap groundTilemap;
|
|
[SerializeField] private TileBase groundTile;
|
|
|
|
[Header("Generation")]
|
|
[SerializeField] private bool centerMap = true;
|
|
|
|
[ContextMenu("Generate Map")]
|
|
public void GenerateMap()
|
|
{
|
|
if (groundTilemap == null)
|
|
{
|
|
Debug.LogError("MapGenerator: Ground Tilemap не назначен.");
|
|
return;
|
|
}
|
|
|
|
if (groundTile == null)
|
|
{
|
|
Debug.LogError("MapGenerator: Ground Tile не назначен.");
|
|
return;
|
|
}
|
|
|
|
groundTilemap.ClearAllTiles();
|
|
|
|
int startX = centerMap ? -width / 2 : 0;
|
|
int startY = centerMap ? -height / 2 : 0;
|
|
|
|
float centerX = (width - 1) / 2f;
|
|
float centerY = (height - 1) / 2f;
|
|
|
|
float radiusX = (width / 2f) - edgeMargin;
|
|
float radiusY = (height / 2f) - edgeMargin;
|
|
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
float normalizedX = (x - centerX) / radiusX;
|
|
float normalizedY = (y - centerY) / radiusY;
|
|
|
|
float shape =
|
|
Mathf.Pow(Mathf.Abs(normalizedX), shapePower) +
|
|
Mathf.Pow(Mathf.Abs(normalizedY), shapePower);
|
|
|
|
if (shape <= 1f)
|
|
{
|
|
Vector3Int position = new Vector3Int(
|
|
startX + x,
|
|
startY + y,
|
|
0
|
|
);
|
|
|
|
groundTilemap.SetTile(position, groundTile);
|
|
}
|
|
}
|
|
}
|
|
|
|
Debug.Log($"Map generated: {width}x{height}");
|
|
}
|
|
|
|
[ContextMenu("Clear Map")]
|
|
public void ClearMap()
|
|
{
|
|
if (groundTilemap != null)
|
|
{
|
|
groundTilemap.ClearAllTiles();
|
|
Debug.Log("Map cleared.");
|
|
}
|
|
}
|
|
} |